-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.c
63 lines (52 loc) · 975 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
53
54
55
56
57
58
59
60
61
62
63
// Consider a Graph G Perform Depth First Traversal on the given graph. Vertices of the graph goes like 1, 2, 3 ...... n.
// Input :
// n - Integer - number of vertices
// nxn - adjacency matrix for the graph G
// m - Integer - starting vertex
// Output:
// List of n vertices - DFS Order.
// For example:
// Input Result
// 4
// 0 1 0 1
// 1 0 1 0
// 0 1 0 0
// 1 0 0 0
// 2
// 2
// 1
// 4
// 3
#include <stdio.h>
#define max 50
int n, start;
int arr[max][max];
int visited[max];
void dfs(int start)
{
printf("%d\n", start + 1);
for (int i = 0; i < n; i++)
{
if (arr[start][i] == 1 && visited[i] != 1)
{
visited[i] = 1;
dfs(i);
}
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &arr[i][j]);
}
}
scanf("%d", &start);
--start;
visited[start] = 1;
dfs(start);
return 0;
}