DFS and BFS On Graph
DFS and BFS On Graph
DFS and BFS On Graph
#include<stdio.h>
#include<stdlib.h>
int a[10][10], n, i, j, source, queue[10];
int visit_bfs[10], visit_dfs[10];
void create_graph()
{
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d",&a[i][j]);
}
dfs(v);
}
}
}
void main()
{
int ch;
while(1)
{
printf("\n------------- GRAPH TRAVERSALS ---------------");
printf("\n 1.Create Graph\n 2.BFS & DFS\n 3.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
create_graph();
break;
case 2:
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
printf("Nodes Reachable from Source Vertex using BFS\n");
bfs(source);
for(i=1; i<=n; i++)
if(visit_bfs[i]==0)
printf("\nThe vertex that is not reachable %d",i);
break;
default:
exit(0);
}
}
}
Output:
------------- GRAPH TRAVERSALS ---------------
1.Create Graph
2.BFS & DFS
3.Exit
Enter your choice: 1