-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckCycleDFS.c
More file actions
76 lines (54 loc) · 1.52 KB
/
checkCycleDFS.c
File metadata and controls
76 lines (54 loc) · 1.52 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "adjMatrix.h"
#define white 0
#define gray 1
#define black 2
#define NIL -1
void dfs(int *adjMat, int v, type){
int *parent, *color;
int i;
parent = (int*)malloc(v*sizeof(int));
color = (int*)malloc(v*sizeof(int));
for(i=0; i<v; i++){
parent[i] = NIL;
color[i] = white;
}
dfsVisit(adjMat, v, 0, parent, color, type);
for(i=1; i<v; i++){
if(color[i] == white)
dfsVisit(adjMat, v, i, parent, color, type);
}
printf("\n\nParent Child\n");
for(i=0; i<v; i++){
printf(" %d\t %d\n", parent[i], i);
}
}
void dfsVisit(int *adjMat, int v, int u, int *parent, int *color, type){
color[u] = gray;
printf("%d, ", u);
int i;
for(i=v-1; i>=0; i--){ // adjMat[u][i] > 0 i.e. vertex i is adjacent to vertex u
if( *(adjMat+(u*v)+i) > 0 && color[i]==white){ // if adjacent vertex color is white then
parent[i] = u;
dfsVisit(adjMat, v, i, parent, color, type);
}
else if(color[i]==gray && type>2){ // directed
printf("\nBack edge found at %d, %d", u, v);
printf("\nGraph contains a cycle");
exit(1); // terminates the program
}
else if(parent[u] != i && type<3){ // un-directed
printf("\nBack edge found at %d, %d", u, v);
printf("\nGraph contains a cycle");
exit(1); // terminates the program
}
}
color[u] = black;
}
int main(){
int *adjMat, v, type;
adjMat = initializeGraph(&v);
type = inputGraph(adjMat, v);
displayGraph(adjMat, v);
dfs(adjMat, v, type);
return 0;
}