Skip to content

Instantly share code, notes, and snippets.

@anshumanatri
Created March 12, 2009 10:13
Show Gist options
  • Save anshumanatri/77996 to your computer and use it in GitHub Desktop.
Save anshumanatri/77996 to your computer and use it in GitHub Desktop.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int q[10],e[10][10],n,order[10],count=1,front=0,rear=-1;
void bfs(int node)
{
int i;
q[++rear]=node;
order[node]=count;
while(count!=n)
{
i=q[front++];
order[i]=++count;
for(int j=0;j<n;j++)
if(e[i][j] && j!=i && !order[j])
q[++rear]=j;
}
}
void main()
{
clrscr();
int node,i;
cout<<"\n Enter the value of n i.e., no of vertices:\t";cin>>n;
cout<<"\n Enter the adjacency matrix:\n";
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>e[i][j];
cout<<"\n Enter the starting node:\t";cin>>node;
bfs(node);
cout<<"\n order of visit in the digraph :\t";
for(i=0;i<n;i++)
cout<<"\n Vertex: [ "<<(i+1)<<" ]\t order"<<order[i];
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment