implimentation of Breadth First Search

#include
#include
class node
{
public:
int queue[50];
int a[50][51];
void enqueue(int);
int dequeue();
void bfs(int);
}
node1;
int n,e,front=0;
int rear=-1;
void node::enqueue(int x)
{
queue[(++rear)%50]=x;
}
int node::dequeue()
{
int x=queue[front];
front=(front+1)%50;
return(x);
}
void node::bfs(int x)
{
int b;
a[x][50]=1;
enqueue(x);
while(((rear+1)%50)!=front)
{
b=dequeue();
cout<<" "< for(int i=0;i {
if(a[b][i]==1)
if(a[i][50]==0)
{
a[i][50]=1;
enqueue(i);
}
}
}
}
void main()
{
int i,x,y;
clrscr();
for(i=0;i {
node1.a[i][50]=0;
for(int j=0;j node1.a[i][j]=0;
}
cout<<"\n Enter the number of vertices: ";
cin>>n;
cout<<"\n Enter the number of edges: ";
cin>>e;
cout<<"\n Enter the Source & Destination Vertex: \n";
for(i=0;i {
cout<<"\n Enter the "< cin>>x;
cout<<"to ";
cin>>y;
x--;
y--;
node1.a[x][y]=1;
node1.a[y][x]=1;
}
cout<<"\n The Breadth First Search Result is: ";
if(n>0)
for(i=0;i{
if(node1.a[i][50]==0)
{
node1.bfs(i);
cout<<"\t";
}
}
else
cout<<"Empty";
getch();
}

Comments

  1. it gives you the complete program to impliment BFS using c++

    ReplyDelete

Post a Comment