code to sort
code to sort
# include <stdio.h>
# include<conio.h>
# define MAX 5
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