KEMBAR78
BFS Code | PDF
0% found this document useful (0 votes)
17 views2 pages

BFS Code

Bfs code in dsa
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)
17 views2 pages

BFS Code

Bfs code in dsa
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/ 2

#include<stdio.

h>
#include<conio.h>
int graph[10][10];
int visited[10],ready[10];
char name[10];
enum boolean {false,true};
int n;
int q[10],f=-1,r=-1;
void create()
{
int i,j;
printf("Enter no of nodes");
scanf("%d",&n);
printf("Enter adjacency matrix");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&graph[i][j]);
for(i=0;i<n;i++)
{
visited[i]=false;
ready[i]=false;
}
printf("Enter node name");
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%c",&name[i]);
}
}
void display()
{
int i,j;
printf("Adjacency Matricis :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%2d",graph[i][j]);
printf("\n");
}
}

void insert(int e)
{
if(f==-1)
{ f=0;r=0;}
else
r++;
q[r]=e;
}
int del()
{
int e;
e=q[f];
if(f==r)
{ f=-1;r=-1;}
else
f++;
return e;
}
void bfs()
{
int j,node;
printf("Enter starting node position\n");
scanf("%d",&node);
insert(node);
ready[node]=true;
printf("----BFS----\n");
do
{
node=del();
printf(" %c ",name[node]);
visited[node]=true;
for(j=0;j<n;j++)
{
if(graph[node][j]==1 && ready[j]==false &&
visited[j]==false)
{
ready[j]=true;
insert(j);
}
}
}while(f!=-1);

}
void main()
{
int n;
clrscr();
do
{
printf("\n Enter 1.create 2.display 3.bfs 4.exit\n");
scanf("%d",&n);
switch(n)
{
case 1: create(); break;
case 2: display(); break;
case 3: bfs(); break;
}
}while(n!=4);
getch();
}

You might also like