-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreachableCount.c
More file actions
53 lines (41 loc) · 1.12 KB
/
reachableCount.c
File metadata and controls
53 lines (41 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdbool.h>
#include "Graph.h"
#define MAX_NODES 1000
int visited[MAX_NODES]; // array to store visiting order
// indexed by vertex 0..nV-1
int reachableCount(Graph g, int nV, Vertex v) {
Vertex w;
int total = 0;
visited[v] = 1;
for (w = 0; i < nV; i++) {
if (adjacent(g, v , w) && visited[w] == -1) {
visited[w] = 1;
int countR = reachableCount(g, nV, w);
total = total + countR;
}
}
total = total + 1; // current node
return total;
}
int main(void) {
int V = 6;
Graph g = newGraph(V);
Edge e;
e.v = 0; e.w = 1; insertEdge(g, e);
e.v = 0; e.w = 4; insertEdge(g, e);
e.v = 0; e.w = 5; insertEdge(g, e);
e.v = 5; e.w = 4; insertEdge(g, e);
e.v = 4; e.w = 2; insertEdge(g, e);
e.v = 4; e.w = 3; insertEdge(g, e);
e.v = 5; e.w = 3; insertEdge(g, e);
e.v = 1; e.w = 2; insertEdge(g, e);
e.v = 3; e.w = 2; insertEdge(g, e);
for (int i = 0; i < V; i++) {
visited[i] = -1;
}
int src = 1;
int countReachable = reachableCount(g, V, src);
printf(" src: %d, reachableCount: %d \n", src, countReachable);
return 0;
}