-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdfs.c
52 lines (40 loc) · 858 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#define ROW 100
#define COL 100
#define GREY 100
#define WHITE -1
int matrix[ROW][COL];
int arr[ROW];
int num;
DFS(int node)
{
int count = 0;
printf("Node [%d]", node);
arr[node] = GREY;
for (; count <num; count++) {
if (matrix[node][count] == 0)
continue;
if (arr[count] == WHITE)
DFS(count);
}
}
int main()
{
int r, c, count;
printf("Enter Number of nodes in your graph..");
scanf("%d", &num);
for (r=0; r < num; r++) {
for (c = 0; c< num; c++) {
scanf("%d", &matrix[r][c]);
}
}
for (r=0; r < num; r++) {
for (c = 0; c< num; c++) {
printf("[%d]", matrix[r][c]);
}
}
for (count =0; count < num; count++)
arr[count] = WHITE;
DFS(0);
}