/* 5(b). W.A.P. to print all nodes that are reachable from a given starting
node in a digraph using Breadth First Search (BFS). */
#include<stdio.h>
#include<conio.h>
int visited[10],q[10],a[10][10],n,node;
void Bfs(int node)
{
int i,f = 0,r = -1,flag = 1; /* Initialize front & rear end of a queue */
q[++r] = node;
visited[node] = 1; /* Starting from the specified node */
printf("\nNodes reachable from ");
while(1)
{
for (i = 0; i < n; i++) // Check for already visited & exploring i
for (j = 0; j < n; j++)
scanf("%d",&a[i][j]);
visited[i] = 0; /* Initially all nodes are unvisited */
}
printf("\nEnter the start vertex : ");
scanf("%d",&start);
Bfs(start-1);
getch();
}
/* OUTPUT
Enter no. of nodes : 4
Enter the adjacency matrix :
0 1 0 0
0 0 0 0
1 0 0 1
0 0 0 0
Enter the start vertex : 3
Nodes reachable from Vertex : 3
Vertex : 1
Vertex : 4
Vertex : 2
Enter no. of nodes : 2
Enter the adjacency matrix :
1 0
0 0
Enter the start vertex : 2
Nodes reachable from Vertex : 2
None.
*/