Design, develop and implement a program in C for the following operations on a graph in C for the following operations on a GRAPH G of cities

Design, develop and implement a program in C for the following operations on a graph in C for the following operations on a GRAPH G of cities

Design, develop and implement a program in C for the following operations on a graph in C for the following operations on a GRAPH G of cities

(i)  Create a graph of n cities using adjacency matrix
(ii) Print all the nodes reachable from a given starting node in digraph using BFS/DFS method

Program:- 

#include<stdio.h>
int a[10][10],visit[10],n;
void DFS(int s)
{
int i;
printf("%d ",s);
visit[s]=1;
for(i=1;i<=n;i++)
{
if((a[s][i]==1)&&(visit[i]==0))
DFS(i);
}
}
int main()
{
int i,s,j;
printf("No of vertices\n");
scanf("%d",&n);
printf("\n Enter adjacency matrix");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
printf("\n Enter the source node");
scanf("%d",&s);
printf("\n Nodes reachable from %d are \n",s);
DFS(s);
for(i=1;i<=n;i++)
if(visit[i]==0)
printf("\nThe vertex %d is not rechable " ,i);
return 0;
}

Click to rate this post!
[Total: 0 Average: 0]

We will be happy to hear your thoughts

Leave a reply

EducationTrick
Logo
Enable registration in settings - general