0% found this document useful (0 votes)
4 views2 pages

11 PGM

Uploaded by

r8978428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

11 PGM

Uploaded by

r8978428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

11 Design, Develop and Implement a Program in C Language for

the following operations on Graph (G) of Cities

a. Create a Graph of N cities using Adjacency Matrix.

b. Print all the nodes reachable from a given starting node

in a digraph using DFS/BFS method

#include<stdio.h>

int a[10][10],n,v[10],source;

void input();

void dfs(int);

void output();

int main()

input();

dfs(source);

output();

return 0;

void input()

int i,j;

printf("enter the no of nodes:");

scanf("%d",&n);

printf("enter the adjacency matrix:");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

scanf("%d",&a[i][j]);

printf("enter the source vertex:");

scanf("%d",&source);

for(i=1;i<=n;i++)

v[i]=0;
}

void dfs(int s)

int k;

v[s]=1;

for(k=1;k<=n;k++)

if(a[s][k]==1 && v[k]==0)

printf("%d->%d",s,k);

dfs(k);

void output()

int i;

for(i=1;i<=n;i++)

if(v[i]==0)

printf("%d not reacable\n",i);

else

printf("%d reacable\n",i);

You might also like