-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs-traversal.cpp
49 lines (47 loc) · 1.01 KB
/
dfs-traversal.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
44
45
46
47
48
49
//dfs traversal of undirected graph using recursive approach
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>>&adj, vector<int>&v,int x)
{
v[x]=1;
cout<<x<<" ";//print the vertex visited
for(int i=0;i<adj[x].size();i++)
{
if(v[adj[x][i]]==0)
{
dfs(adj,v,adj[x][i]);//call the dfs function if a node is not visited
}
}
}
int main()
{
int n,m;
cin>>n>>m;//enter the number of vertices and number of edges
vector<vector<int>>adj(n+1,vector<int>());
//create an adjacency list according to the edges given in input
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cout<<"The dfs traversal is \n";
vector<int>v(n+1,0);
dfs(adj,v,1);
return 0;
}
//INPUT
//6 5
//1 2
//2 6
//1 3
//3 4
//3 5
//OUTPUT
//The dfs traversal is
//1 2 6 3 4 5
//TIME COMPLEXITY OF THE PROGRAM
//O(V+E)
//where V is number of vertices
//and E is number of edges