-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfg algorithm.c
42 lines (39 loc) · 894 Bytes
/
dfg algorithm.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
#include <stdio.h>
#include <stdlib.h>
int g[20][20],n,visited[20];
void fnDepthFirstSearch(int);
int main(void)
{
int i,j,k;
int v;
printf("Enter the number of vertices : ");
scanf("%d",&n);
for (i=1; i<=n; i++)
visited[i] = 0;
printf("Enter the adjacency matrix :\n");
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
scanf("%d",&g[i][j]);
printf("Enter the source vertex : ");
scanf("%d",&v);
fnDepthFirstSearch(v);
for (k=1; k<=n; k++)
{
if(visited[k])
{
printf("\nVertex %d reachable",k);
}
else
{
printf("\nVertex %d not reachable",k);
}
}
}
void fnDepthFirstSearch(int currentVertex)
{
int i;
visited[currentVertex] = 1;
for (i=1; i<=n; i++)
if (g[currentVertex][i] && !visited[i])
fnDepthFirstSearch(i);
}