Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "Graph.h"
- #include "Queue.h"
- /*BFS, but uses a dist array to keep track of the distances of vertices
- when they are found. You could use the visited array as the dist array, like
- what I did in furthestReachable. */
- int numWithin(Graph g, int src, int dist) {
- int n = GraphNumVertices(g);
- int visited[n];
- memset(visited, -1, sizeof(int) * n);
- visited[src] = src;
- int distA[n];
- distA[src] = 0;
- int ret = 0;
- Queue q = QueueNew();
- QueueEnqueue(q, src);
- while (!QueueIsEmpty(q)) {
- int v = QueueDequeue(q);
- if (distA[v] <= dist) {
- ret++;
- }
- for (int i = 0; i < n; i++) {
- if (visited[i] == -1 && GraphIsAdjacent(g, i, v)) {
- QueueEnqueue(q, i);
- visited[i] = v;
- distA[i] = distA[v] + 1;
- }
- }
- }
- QueueFree(q);
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement