-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.cc
70 lines (57 loc) · 1023 Bytes
/
dfs.cc
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
#include <iostream>
#include <list>
using namespace std;
class Graph{
int V;
list<int> *A;
public:
Graph(int v);
int vertex();
void addEdge(int s, int d);
friend void dfs(Graph G, int s);
};
Graph::Graph(int v){
this->V = v;
A = new list<int>[v];
}
int Graph::vertex(){
return V;
}
void Graph::addEdge(int s, int d){
A[s].push_back(d);
A[d].push_back(s);
}
void dfs(Graph G, int s){
bool* visited = new bool[G.vertex()];
for(int i=0;i<G.vertex();i++){
visited[i] = false;
}
list<int> stack;
visited[s] = true;
stack.push_front(s);
int temp;
while(!stack.empty()){
temp = stack.front();
cout << temp << " ";
stack.pop_front();
for(list<int>::iterator i = G.A[temp].begin(); i != G.A[temp].end();i++){
if(visited[*i]==false){
visited[*i] = true;
stack.push_front(*i);
}
}
}
cout << endl;
}
int main(){
Graph G(6);
G.addEdge(0,1);
G.addEdge(0,2);
G.addEdge(1,2);
G.addEdge(1,3);
G.addEdge(2,4);
G.addEdge(3,4);
G.addEdge(3,5);
G.addEdge(4,5);
dfs(G, 3);
}