-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepthfirstsearch.cpp
More file actions
59 lines (59 loc) · 1.39 KB
/
Copy pathdepthfirstsearch.cpp
File metadata and controls
59 lines (59 loc) · 1.39 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
#include<bits/stdc++.h>
#define vi vector<int>
#define pb push_back
#define endl "\n"
using namespace std;
// a class for graph having some standard functions
class graph{
void DFSUtil(int v);
public:
// maintaining the hash map for visited Nodes and map in nesting with adjacency list
map<int,bool>visited;
map<int,list<int>>adjacency_list;
void addEdge(int u,int v);
void DFS();
};
// add the edge between vertex u and v
void graph::addEdge(int u,int v){
adjacency_list[u].pb(v);
}
// DFS function being called recursively for all the Nodes in the List as well as for their adjcanet Nodes
void graph::DFSUtil(int v){
visited[v]=true;
cout<<v<<" ";
for(auto i=adjacency_list[v].begin();i!=adjacency_list[v].end();++i){
if(!visited[*i]){
DFSUtil(*i);
}
}
}
// function for calling the DFSUtil in the main function
void graph::DFS(){
for(auto i:adjacency_list){
if(visited[i.first]==false){
DFSUtil(i.first);
}
}
}
int main(){
graph g;
g.addEdge(0, 1);
g.addEdge(0, 9);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(9, 3);
// for taking input from user
/* run a loop
while(ch==="y") {
int u,v;
cin>>u>>v;
g.addEdge(u,v);
cout<<"Do you wanna continue ?";
cin>>ch;
}
*/
cout<<"DFS of the Graph is as follows"<<endl;
g.DFS();
return 0;
}