-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.cpp
55 lines (45 loc) · 877 Bytes
/
DFS.cpp
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
//dfs
#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
int count = 0;
int main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnter adjecency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
return 0;
}
void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1;
count++;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
count--;
visited[i]= 0;
}
//
//0 1 1 1 1 0 0 0
//1 0 0 0 0 1 0 0
//1 0 0 0 0 1 0 0
//1 0 0 0 0 0 1 0
//1 0 0 0 0 0 1 0
//0 1 1 0 0 0 0 1
//0 0 0 1 1 0 0 1
//0 0 0 0 0 1 1 0
//0 0 1 1 1
//0 0 0 0 1
//0 0 0 1 0
//0 0 0 0 1
//1 0 1 0 0