-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectivityBFS.c
More file actions
47 lines (42 loc) · 945 Bytes
/
ConnectivityBFS.c
File metadata and controls
47 lines (42 loc) · 945 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
#include <stdio.h>
#include <stdlib.h>
void bfs(int a[][10], int n, int v[10], int source)
{
int i, q[10], f = 0, r = -1, node;
v[source] = 1;
q[++r] = source;
while (f <= r)
{
node = q[f++];
for (i = 1; i <= n; i++)
{
if (!v[i] && (a[node][i] == 1))
{
q[++r] = i;
v[i] = 1;
}
}
}
}
int main()
{
int n, a[10][10], i, v[10] = {}, j, count = 0;
printf("\nEnter the number of nodes : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
for (i = 1; i <= n; i++)
{
if (!v[i])
{
bfs(a, n, v, i);
count++;
}
}
if (count == 1)
printf("\nThe graph is connected\n");
else
printf("\nThe graph is not connected and has %d componenets\n", count);
return 0;
}