-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.c
85 lines (65 loc) · 1.6 KB
/
dfs.c
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
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <string.h>
#define max 10000;
int n;
int time = 0, size = 0;
int prev[100], d[100], f[100], val[100];
char color[100][10];
//always jeta niye kaj korte bolbe oitake 0 defined dhore kaj korte hobe
void dfs(int graph[][n], int k){
strcpy(color[k], "Grey");
time = time + 1;
d[k] = time;
//storing values in a array
val[size] = k;
size++;
for(int j=0; j<n; j++){
if((graph[k][j] == 1) && (!strcmp(color[j], "White"))){
prev[j] = k;
dfs(graph, j);
}
}
strcpy(color[k], "Black");
time = time + 1;
f[k] = time;
}
int main(void){
printf("Enter number of node - ");
scanf("%d", &n);
for(int i=0; i<n; i++){
strcpy(color[i], "White");
prev[i] = max;
f[i] = max;
d[i] = max;
}
int arr[n][n];
//creating graph
printf("Enter Graph [in matrix form] - \n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
scanf("%d", &arr[i][j]);
}
}
//printing graph
printf("Graph is [in matrix form] - \n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
dfs(arr, 0);
// printing final time
printf("Finish time - ");
for(int i=0; i<n; i++){
printf("%d ", f[i]);
}
printf("\n");
// printing dfs array
printf("dfs-ed - ");
for(int i=0; i<n; i++){
printf("%d ", val[i]);
}
printf("\n");
return 0;
}