-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.c
53 lines (43 loc) · 1.01 KB
/
bfs.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
/*
* Write a program to print all the nodes reachable from a given
* starting node in a digraph using BFS method and determine
* the time required.
*/
#include <stdio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
void bfs(int v)
{
for (i = 1; i <= n; i++)
if (a[v][i] && !visited[i])
q[++r] = i;
if (f <= r)
{
visited[q[f]] = 1;
bfs(q[f++]);
}
}
int main()
{
int v;
printf("Enter the number of vertices: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
q[i] = 0;
visited[i] = 0;
}
printf("Enter graph data in matrix form:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
printf("\nEnter the starting vertex: ");
scanf("%d", &v);
bfs(v);
printf("\nThe nodes which are reachable are:\n");
for (i = 1; i <= n; i++)
if (visited[i])
printf("%d\t", i);
else
printf("\nBfs is not possible");
return 0;
}