-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.cpp
61 lines (44 loc) · 824 Bytes
/
DFS.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
50
51
52
53
54
55
56
57
58
59
60
61
/*
code by harsha_76
*/
#include<bits/stdc++.h>
using namespace std;
class Graph
{
map<int, bool> visited;
map<int, list<int>> adj;
public:
void addEdge(int u, int v);
void dfs(int v);
void bfs(int v);
};
void Graph::addEdge(int u, int v)
{
adj[u].push_back(v);
}
void Graph::dfs(int v)
{
visited[v]=true;
cout<<v<<' ';
list<int>::iterator it;
for(it= adj[v].begin(); it!=adj[v].end();it++)
{
if(!visited[*it])
{
dfs(*it);
}
}
}
void Graph::bfs(int v)
{
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
Graph g;
g.addEdge(0, 1); g.addEdge(1, 2); g.addEdge(2, 3); g.addEdge(0, 4); g.addEdge(4, 5); g.addEdge(5, 6);
g.dfs(0);
return 0;
}