-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS2.c
58 lines (56 loc) · 969 Bytes
/
BFS2.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<stdio.h>
#include<stdlib.h>
int g[10][10]={},vis[10]={},n,m,c;
void main()
{
int i,a,b;
printf("enter no.of vert");
scanf("%d",&n);
printf("no of edges");
scanf("%d",&m);
printf("ente edges:");
for(i=0 ; i<m ; i++)
{
scanf("%d%d",&a,&b);
g[a][b] = 1;
g[b][a] = 1;
}
graph();
if(c==1)
printf("cconnected");
else
printf("disconnected,no of comps : %d",c);
}
void graph()
{
int i;
for(i=0 ; i<n ; i++)
{
if(vis[i]==0)
{
bfs(i);
c++;
}
}
}
void bfs(int v)
{
int q[10],f,r,i,j;
f=r=-1;
q[++r] = v;
vis[v] = 1;
printf("%d\t",v);
while(f != r)
{
v = q[++f];
for(i=0 ; i<n ; i++)
{
if(g[v][i] == 1 && vis[i] == 0)
{
vis[i] = 1;
printf("%d\t",i);
q[++r] = i;
}
}
}
}