DFS tree in C++

#include<iostream.h>
#include<conio.h>
int cost[10][10],i,j,k,n;
int stack[10],top,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 Source & Distination\n";
for(k=1;k<=m;k++)
{
cout<<"\nEnter Source: ";
cin >>i;
cout<<"\nEnter Distination: ";
cin>>j;
cost[i][j]=1;
cout<<"\n-----------------------\n";
}
cout <<"\nEnter initial vertex: ";
cin >>v;
cout <<"\nORDER OF VISITED VERTICES:\n";
cout <<v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stack[top]=j;
top++;
}
v=stack[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
}

No comments

Powered by Blogger.