-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathDFT.cpp
More file actions
74 lines (54 loc) · 1.36 KB
/
Copy pathDFT.cpp
File metadata and controls
74 lines (54 loc) · 1.36 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Link->https://practice.geeksforgeeks.org/problems/depth-first-traversal-for-a-graph/1
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
/* Function to do DFS of graph
g : adjacency list of graph
N : number of vertices
return a list containing the DFS traversal of the given graph
*/
void dfsTraverseRecur(bool visited[],vector<int> adj[],int v,vector<int> &vect) {
visited[v]=true;
vect.push_back(v);
for(vector<int>::const_iterator it=adj[v].begin(); it!=adj[v].end(); it++) {
if(visited[*it]!=true) {
dfsTraverseRecur(visited,adj,*it,vect);
}
}
}
vector <int> dfs(vector<int> g[], int N)
{
// Your code here
bool visited[N];
vector<int> vect;
for(int i=0; i<N; i++)
visited[i]=false;
dfsTraverseRecur(visited,g,0,vect);
return vect;
}
// { Driver Code Starts.
int main()
{
int T;
cin>>T;
while(T--)
{
int N, E;
cin>>N>>E;
vector<int> g[N];
bool vis[N];
memset(vis, false, sizeof(vis));
for(int i=0;i<E;i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
vector <int> res = dfs(g, N);
for (int i = 0; i < res.size (); i++)
cout << res[i] << " ";
cout<<endl;
}
} // } Driver Code Ends