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

C Program for Graph Operations

This C program allows the user to: 1. Create an adjacency matrix representation of a graph with N cities. 2. Perform breadth-first search (BFS) on the graph to find all cities reachable from a starting city. 3. Check if the graph is connected by performing depth-first search (DFS) from a starting city and checking if all cities are visited. The program uses arrays to represent the adjacency matrix and queues/stacks for the BFS and DFS algorithms. It prompts the user for options to create the graph, run BFS/DFS searches, and exit.

Uploaded by

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

C Program for Graph Operations

This C program allows the user to: 1. Create an adjacency matrix representation of a graph with N cities. 2. Perform breadth-first search (BFS) on the graph to find all cities reachable from a starting city. 3. Check if the graph is connected by performing depth-first search (DFS) from a starting city and checking if all cities are visited. The program uses arrays to represent the adjacency matrix and queues/stacks for the BFS and DFS algorithms. It prompts the user for options to create the graph, run BFS/DFS searches, and exit.

Uploaded by

mumthaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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
#include<stdio.h>
#include<stdlib.h>
int a[10][10], n, m, i, j, source, s[10], b[10];
int visited[10];
void create()
{
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]);
}
void bfs()
{
int q[10], u, front=0, rear=-1;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
q[++rear] = source;
visited[source] = 1;
printf("\nThe reachable vertices are: ");
while(front<=rear)
{
u = q[front++];
for(i=1; i<=n; i++)
{
if(a[u][i] == 1 && visited[i] == 0)
{
q[++rear] = i;
visited[i] = 1;
printf("\n%d", i);
}
}
}
}
void dfs(int source)
{
int v, top = -1;
s[++top] = 1;
b[source] = 1;
for(v=1; v<=n; v++)
{
if(a[source][v] == 1 && b[v] == 0)
{
printf("\n%d -> %d", source, v);
dfs(v);
}
}
}
void main()
{
int ch;
while(1)
{
printf("\n1.Create Graph\n2.BFS\n3.Check graph connected or not(DFS)\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create(); break;
case 2:
bfs();
for(i=1;i<=n;i++)
if(visited[i]==0)
printf("\nThe vertex that is not rechable %d" ,i);
break;
case 3:
printf("\nEnter the source vertex to find the connectivity: ");
scanf("%d",&source);
m=1;
dfs(source);
for(i=1;i<=n;i++)
{
if(b[i]==0)
m=0;
}
if(m==1)
printf("\nGraph is Connected");
else
printf("\nGraph is not Connected");
break;
default: exit(0);
}
}
}

You might also like