Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
49 views

12) Create A Graph and Represent It Using Adjacency Matrix .Implement Using BFS and DFS Traversals

This document contains code to represent a graph using an adjacency matrix and implement breadth-first search (BFS) and depth-first search (DFS) traversals on the graph. It defines functions for BFS and DFS that take in a starting vertex. The main function prompts the user to enter the number of vertices and edges to create the graph, and provides a menu to call the BFS, DFS functions or display the adjacency matrix.

Uploaded by

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

12) Create A Graph and Represent It Using Adjacency Matrix .Implement Using BFS and DFS Traversals

This document contains code to represent a graph using an adjacency matrix and implement breadth-first search (BFS) and depth-first search (DFS) traversals on the graph. It defines functions for BFS and DFS that take in a starting vertex. The main function prompts the user to enter the number of vertices and edges to create the graph, and provides a menu to call the BFS, DFS functions or display the adjacency matrix.

Uploaded by

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

UEC2021249

12) Create a graph and represent it using adjacency matrix .Implement using BFS and DFS
traversals:

#include<stdio.h>
#define MAX 10

typedef struct Q
{
int R,F;
int data[MAX];
}Q;
int empty (Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int dequeue(Q *P);
void BFS(int);
void DFS(int);
int G[MAX][MAX];
int n=0;
int visited[MAX];

void main()
{
int i,j,v,op,nedges;

printf("\n Enter no of vertices : ");


scanf("%d",&n);
printf("\nEnter the no of edges : ");
scanf("%d",&nedges);

for(i=0;i<n;i++)
for(j=0;j<n;j++)
G[i][j] =0;
printf("\nEnter the Graph as list of edges( Starting vertex
No is 0");
for(v=0;v<nedges;v++)
{
printf("\nEnter the next edge(Start vertex, end
vertex);");
scanf("%d%d", &i,&j);
G[i][j] = G[j][i]= 1;
}
do{
printf("\n\n1. DFS\n2. BFS\n3. Display the graph\n 4.
Exit");
printf("\nEnter your choice");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter the starting vertex for DFS");
scanf("%d",&v);
for(i=0;i<n;i++)
visited[i] = 0;
DFS(v);
break;
case 2: printf("Enter the starting vertex for BFS");
scanf("%d",&v);
BFS(v);
break;
case 3: printf("Adjacency Matrix : \n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",G[i][j]);
}
break;
}
}while(op!= 4);
}

void BFS(int v)
{
int visited[MAX],i;
Q q;
q.R=q.F=-1;
for(i=0;i<n;i++)
visited[i] = 0;
enqueue(&q,v);
printf("\n visit :\n%d",v);
visited[v]=1;
while(!empty(&q))
{
v= dequeue(&q);
for(i=0;i<n;i++)
if (visited[i] ==0 && G[v][i]!= 0)
{ enqueue(&q,i);
visited[i] = 1;
printf("\n%d",i);
}
}
}

int empty(Q *P)


{
if(P->R== -1)
return(1);
return(0);
}

int full(Q*P)
{
if(P-> R == MAX-1)
return(1);
return(0);
}

void enqueue(Q *P, int x)


{
if (P->R == -1)
{ P->R=P->F=0;
P->data[P->R]= x;
}
else
{ P->R=P->R+1;
P->data[P->R]= x;
}
}

int dequeue(Q *P)


{
int x;
x=P->data[P->F];
if(P->R == P->F)
{
P->R= -1;
P->F= -1;
}
else
P->F=P->F+1;
return(x);
}

void DFS(int i)
{ int j;
printf("\n%d", i);
visited[i] =1;
for(j=0;j<n;j++)
if(!visited[j] && G[i][j] ==1)
DFS(j);
}
--------------------------------------------------------------------------------------------------------------------------------------

You might also like