BFS tree Code in C++

#include<iostream.h>
#include<conio.h>
int cost[10][10],i,j,k,n;
int que[10],front,rare,v,visit[10],visited[10];
void main()
{
clrscr();
int m;
cout <<"Enter no of vertices\ Nodes: ";
cin >> n;
cout <<"\nEnter no of Edges: ";
cin >> m;
cout <<"\n\tEnter All EDGES by source & destination\n";
for(k=1;k<=m;k++)
{
cout<<"\nEnter Source: ";
cin >>i;
cout<<"\nEnter Destination: ";
cin>>j;
cost[i][j]=1;
cout<<"\n-----------------------\n";
}
cout <<"\nEnter initial vertex: ";
cin >>v;
cout <<"Visitied vertices:\n";
cout << v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
que[rare++]=j;
}
v=que[front++];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
}

0 comments:

Post a Comment

My Instagram