|
CPP Program To Implement Graph Traverses Using Depth First Search: |
|
#include
#include
int a [10][10],visited[10].n;
void main()
{
int i,j;
void search from(int);
clrscr();
cout<<"enter the no. of nodes\n";
cin>>n;
cout<<"enter the adjacency matrix\n";
for(i=1;<=n;i++)
for(j=1;<=n;j++)
cin>>a[i][j];
for(i=1;i<=n;i++)
visited[i]=0;
cout<<"Depth First Path:";
for(i=1;i<=n;i++)
if(visited[i]==0)
searchfrom(i);
}
void search from(int k)
{
int i;
printf("%d\t",k);
visited[k]=1;
for(i=1;i<=n;i++)
if(visited[i]==0)
searchfrom(i);
return;
}
SAMPLE INPUT AND OUTPUT:
Enter the no.of nodes
4
Enter the adjacency matrix
0 1 0 1
0 0 1 1
0 0 0 1
0 0 0 0
Depth First Path
1 2 3 4
|
|
⇓ Student Projects ⇓
⇑ Student Projects ⇑ |