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

code to sort

The document contains a C program that implements breadth-first search (BFS) and depth-first search (DFS) algorithms for graph traversal. It includes a menu-driven interface allowing users to input an adjacency matrix and choose between BFS and DFS traversals. The program utilizes a queue for BFS and a stack for DFS, displaying the traversal order of the graph nodes.

Uploaded by

u22ee078
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)
2 views

code to sort

The document contains a C program that implements breadth-first search (BFS) and depth-first search (DFS) algorithms for graph traversal. It includes a menu-driven interface allowing users to input an adjacency matrix and choose between BFS and DFS traversals. The program utilizes a queue for BFS and a stack for DFS, displaying the traversal order of the graph nodes.

Uploaded by

u22ee078
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
You are on page 1/ 3

Assignment 7

Name -himani gupta


Admission no- u22ee078
Roll no b-13

# include <stdio.h>
# include<conio.h>
# define MAX 5

void breadth_first_search(int adj[][MAX],int visited[],int start)


{
int queue[MAX],rear=-1,front=-1,i,k;
for(k=0;k<MAX;k++)
visited[k]=0;
queue[++rear]=start;
++front;
visited[start]=1;

while (rear>=front)
{
start=queue[front++];
printf("%c-",start+ 65);

for(i=0;i<MAX;i++)
{
if(adj[start][i] && visited[i]==0)
{
queue[++rear]=i;
visited[i]=1;
}
}
}
}
void depth_first_search(int adj[][MAX],int visited[],int start)
{
int stack[MAX];
int top=-1,i,k;
for(k=0;k<MAX;k++)
visited[k]=0;
stack[++top]=start;
visited[start]=1;

while(top!=-1)
{
start=stack[top--];
printf("%c-",start+ 65);
for(i=0;i<MAX;i++)
{
if(adj[start][i] && visited[i]==0)
{
stack[++top]=i;
visited[i]=1;
break;
}
}
}

}
int main()
{
int visited[MAX]={0};
int adj[MAX][MAX],i,j;
int option,size;
do
{
printf("\n***MAIN MENU***\n");
printf("\n 1.entre the value in graph");
printf("\n 2. BFS TRAVERSAL");
printf("\n 2. DFS TRAVERSAL");
printf("4 .exit");
printf("\n enter your option :");
scanf("%d",&option);
switch(option)
{
case 1:printf("\n enter the adjacency matrix:");
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
scanf("%d",&adj[i][j]);
break;
case 2: printf("\n BFS TRAVERSAL :");
breadth_first_search( adj, visited, 0);
break;
case 3: printf("\n DFS TRAVERSAL :");
depth_first_search(adj,visited,0);
break;

}
}
while(option!=4);
getch();
return 0;}

output

You might also like