0% found this document useful (0 votes)
3 views

DSA Program 11

The document describes a C program for creating a graph of cities using an adjacency matrix and implementing depth-first search (DFS) to determine the reachability of nodes from a given source city. It includes sample outputs demonstrating various scenarios of city connectivity based on user input for the adjacency matrix. The program initializes the graph, performs the DFS traversal, and prints which nodes are reachable from the source city.

Uploaded by

sagarsriram535
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DSA Program 11

The document describes a C program for creating a graph of cities using an adjacency matrix and implementing depth-first search (DFS) to determine the reachability of nodes from a given source city. It includes sample outputs demonstrating various scenarios of city connectivity based on user input for the adjacency matrix. The program initializes the graph, performs the DFS traversal, and prints which nodes are reachable from the source city.

Uploaded by

sagarsriram535
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Laboratory Program – 11

Design, Develop and Implement a Program in C 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.

C Program:

#include<stdio.h>

int visited[10],a[10][10],n;

void dfs(int source)


{
int i;
visited[source]=1;
for(i=1;i<=n;i++)
if(a[source][i]==1 && visited[i]==0)
dfs(i);
}

void main()
{
int i,j,source;
clrscr();
printf("Enter the number of cities\n");
scanf("%d",&n);
printf("Enter the adjacency matrix\n");
printf("0 if there is no path, 1 if there is a path\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("Enter the source city\n");
scanf("%d",&source);
for(i=1;i<=n;i++)
visited[i]=0;
dfs(source);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("node %d is not reachable\n",i);
else
printf("node %d is reachable\n",i);
}
}
OUTPUT 1:
Enter the number of cities
4
Enter the adjacency matrix
0 if there is no path, 1 if there is a path
0 1 0 0
0 0 1 1
1 0 0 0
0 0 0 0
Enter the source city
3
node 1 is reachable
node 2 is reachable
node 3 is reachable
node 4 is reachable

OUTPUT 2:
Enter the number of cities
4
Enter the adjacency matrix
0 if there is no path, 1 if there is a path
0 1 0 0
0 0 1 1
1 0 0 0
0 0 0 0
Enter the source city
3
node 1 is not reachable
node 2 is not reachable
node 3 is not reachable
node 4 is reachable
OUTPUT 3:
Enter the number of cities
4
Enter the adjacency matrix
0 if there is no path, 1 if there is a path
0 1 0 0
0 0 1 1
1 0 0 0
0 0 0 0
Enter the source city
2
node 1 is reachable
node 2 is reachable
node 3 is reachable
node 4 is reachable

OUTPUT 4:
Enter the number of cities
4
Enter the adjacency matrix
0 if there is no path, 1 if there is a path
0 1 1 0
0 0 0 0
0 0 0 0
0 1 1 0
Enter the source city
2
node 1 is not reachable
node 2 is reachable
node 3 is not reachable
node 4 is not reachable
OUTPUT 5:
Enter the number of cities
4
Enter the adjacency matrix
0 if there is no path, 1 if there is a path
0 1 1 0
0 0 0 0
0 0 0 0
0 1 1 0
Enter the source city
1
node 1 is reachable
node 2 is reachable
node 3 is reachable
node 4 is not reachable

OUTPUT 6:
Enter the number of cities
5
Enter the adjacency matrix
0 if there is no path, 1 if there is a path
0 0 1 1 1
1 0 1 0 0
0 0 0 0 0
0 0 0 0 1
0 0 1 0 0
Enter the source city
1
node 1 is reachable
node 2 is not reachable
node 3 is reachable
node 4 is reachable
node 5 is reachable

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy