PGM 11
PGM 11
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method
#include<stdio.h>
#include<stdlib.h>
void bfs(int v)
int i, cur;
visited[v] = 1;
q[++rear] = v;
cur = q[++front];
q[++rear] = i;
visited[i] = 1;
}
void dfs(int v)
int i;
visited[v] = 1;
s[++top] = v;
dfs(i);
visited[i] = 0;
scanf("%d", & start);printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
printf("\n==>2. DFS: Print all nodes reachable from a given starting node");
printf("\n==>3:Exit");
case 1:
bfs(start);
if (visited[i] == 0)
break;case 2:
dfs(start);
break;
case 3:
exit(0);
default:
OUTPUT :
*************************case-1*************************
0101
0010
0001
0000
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
*************************case-2*************************
0101
0010
0001
0000
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
*************************case-3*************************
0101
0010
0001
0000
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
*************************case-4*************************
0101
0010
0001
0000
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit