-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS.cpp
43 lines (42 loc) · 901 Bytes
/
DFS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<iomanip>
using namespace std;
int cost[10][10],v,visited[10],visit[10],stk[10],top;
int main()
{
int n,m,i,j,k;
cout<<"Enter Total No of Vertices :";
cin>>n;
cout<<"Enter Total No of Edges:";
cin>>m;
cout<<"Enter Each Edge :";
for(k=0;k<m;k++)
{
cin>>i>>j;
cost[i][j]=1;
}
cout<<"Enter initial vertex To begin Traverse from ";
cin>>v;
cout<<"DFS \n";
cout<<v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>0;j--)
{
if(cost[v][j]!=0&&visited[j]!=1&&visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
}
v=stk[--top];
cout<<v<<" ";
k++;
visit[v]=0;
visited[v]=1;
}
return 0;
}