-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.cpp
37 lines (37 loc) · 851 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
#include <bits/stdc++.h>
using namespace std;
vector<int> dfsord;
void dfs(int v,int visit[],vector<int> adj[])
{
int w;
dfsord.push_back(v);
visit[v]=1;
for(auto ii=adj[v].begin();ii!=adj[v].end();ii++)
if(visit[*ii]==0)
dfs(*ii,visit,adj);
}
int main()
{
int n,st,ed,e,i;
cout<<"Enter the no of vertices"<<endl;
cin>>n;
vector<int> adj[n];
cout<<"Enter the no of edges"<<endl;
cin>>e;
for(i=0;i<e;i++)
{
cout<<"Enter the start"<<i+1<<endl;
cin>>st;
cout<<"Enter the end"<<i+1<<endl;
cin>>ed;
adj[st].push_back(ed);
}
int visit[n];
for(i=0;i<n;i++)
visit[i]=0;
//for(i=0;i<n;i++)
// if(visit[i]==0)
dfs(2,visit,adj);
for(auto ii=dfsord.begin();ii!=dfsord.end();ii++)
cout<<*ii<<" ";
}