-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDFS-usingVector.cpp
More file actions
41 lines (37 loc) · 791 Bytes
/
Copy pathDFS-usingVector.cpp
File metadata and controls
41 lines (37 loc) · 791 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
#include<iostream>
#include<vector>
using namespace std;
void addEdge(vector<int> adj[], int u, int v){
adj[u].push_back(v);
adj[v].push_back(u);
}
void DFSUtil(int u, vector<int> adj[],
vector<bool> &visited)
{
visited[u] = true;
cout << u << " ";
for (int i=0; i<adj[u].size(); i++)
if (visited[adj[u][i]] == false)
DFSUtil(adj[u][i], adj, visited);
}
void DFS(vector<int> adj[] , int v){
vector<bool> visited(v,false);
for(int u=0;u<v;u++){
if(visited[u]== false){
DFSUtil(u, adj ,visited);
}
}
}
int main(){
int v=5;
vector<int> adj[v];
addEdge(adj ,0,1);
addEdge(adj ,0,4);
addEdge(adj ,1,2);
addEdge(adj ,1,3);
addEdge(adj ,1,4);
addEdge(adj ,2,3);
addEdge(adj ,3,4);
DFS(adj ,v);
return 0;
}