implementation of depth first search

#include
#include
class node
{
public:
int a[50][51];
void dfs(int);
}
node1;
int n,e;
void node::dfs(int x)
{
cout<<" "<a[x][50]=1;
for(int i=0;i{
if(a[x][i]==1)
if(a[i][50]==0)
dfs(i);
}
}
void main()
{
int i,x,y;
clrscr();
for(i=0;i{
node1.a[i][50]=0;
for(int j=0;jnode1.a[i][j]=0;
}
cout<cin>>n;
cout<<"Enter the number of edges";
cin>>e;
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 your depth first search is:";
if(n>0)
for(i=0;i{
if(node1.a[i][50]==0)
{
node1.dfs(i);
cout<<"\t\t";
}
}
else
cout<<"\n Empty";
getch();
}



Comments