-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.C
More file actions
62 lines (58 loc) · 896 Bytes
/
DFS.C
File metadata and controls
62 lines (58 loc) · 896 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
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
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct graph
{
int n;
int a[10][10];
};
struct graph g;
void readMatrix()
{
int i,j;
printf("Enter the number of nodes\n");
scanf("%d",&g.n);
printf("Enter the cost adjacency matrix\n");
for(i=0;i<g.n;i++)
for(j=0;j<g.n;j++)
scanf("%d",&g.a[i][j]);
}
void dfs(int u,int s[])
{
int v;
s[u]=1;
for(v=0;v<g.n;v++)
if(g.a[u][v]==1 && s[v]==0)
dfs(v,s);
}
int connectivity()
{
int i,j,flag=0,s[10];
for(j=0;j<g.n;j++)
{
for(i=0;i<g.n;i++)
s[i]=0;
dfs(j,s);
for(i=0;i<g.n;i++)
{
if(s[i]==0)
flag=1;
}
if(flag==0)
return 1;
}
return 0;
}
int main()
{
int flag;
clrscr();
readMatrix();
flag=connectivity();
if(flag==1)
printf("The graph is connected\n");
else
printf("The graph is not connected\n");
getch();
return 0;
}