- Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdfs.cpp
More file actions
Latest commit
32 lines (28 loc) · 610 Bytes
/
Copy pathdfs.cpp
File metadata and controls
32 lines (28 loc) · 610 Bytes
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
#include<stdio.h>
voidDFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
intmain()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
//read the adjecency matrix
printf("\nEnter adjecency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
//visited is initialized to zero
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
return(0);
}
voidDFS(int i)
{
int j;
printf("\n%d",(i+1));
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}