forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFStraversal.c
34 lines (34 loc) · 877 Bytes
/
DFStraversal.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
//a program for DFS traversal
#include <stdio.h>
#define max 10
void buildadjm(int adj[][max], int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("enter 1 if there is an edge from %d to %d, otherwise enter 0 \n", i,j);
scanf("%d",&adj[i][j]);
}
}
void dfs(int x,int visited[],int adj[][max],int n){
int j;
visited[x] = 1;
printf("The node visited id %d\n",x);
for(j=0;j<n;j++)
if(adj[x][j] ==1 && visited[j] ==0)
dfs(j,visited,adj,n);
}
void main()
{
int adj[max][max],node,n;
int i, visited[max];
printf("enter the number of nodes in graph maximum = %d\n",max);
scanf("%d",&n);
buildadjm(adj,n);
for(i=0; i<n; i++)
visited[i] =0;
for(i=0; i<n; i++)
if(visited[i] ==0)
dfs(i,visited,adj,n);
}