-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFS.cpp
More file actions
43 lines (35 loc) · 750 Bytes
/
DFS.cpp
File metadata and controls
43 lines (35 loc) · 750 Bytes
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<bits/stdc++.h>
using namespace std;
void DFSRec(vector<int> adj[], int s, bool visited[])
{
visited[s]=true;
cout<< s <<" ";
for(int u:adj[s]){
if(visited[u]==false)
DFSRec(adj,u,visited);
}
}
void DFS(vector<int> adj[], int V, int s){
bool visited[V];
for(int i = 0;i<V; i++)
visited[i] = false;
DFSRec(adj,s,visited);
}
void addEdge(vector<int> adj[], int u, int v){
adj[u].push_back(v);
adj[v].push_back(u);
}
int main()
{
int V=5;
vector<int> adj[V];
addEdge(adj,0,1);
addEdge(adj,0,2);
addEdge(adj,2,3);
addEdge(adj,1,3);
addEdge(adj,1,4);
addEdge(adj,3,4);
cout << "Following is Depth First Traversal: "<< endl;
DFS(adj,V,0);
return 0;
}