-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.cpp
executable file
·45 lines (33 loc) · 838 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
#include "DFS.h"
using namespace std;
vector<string> DFS::dfs(GraphStructure graph, Vertex source,
Vertex destination) {
vector<string> output;
map<Vertex, bool> visited;
stack<Vertex> s;
stack<Vertex> s2;
s.push(source);
visited.insert(make_pair(source, true));
cout << "DFS => ";
while (!s.empty()) {
s2.push(s.top());
s.pop();
Vertex temp = s2.top();
s2.pop();
// processing
cout << temp.IATA << ", ";
output.push_back(temp.IATA);
vector<Vertex> adjacents = graph.getAdjacentVertices(temp);
for (unsigned i = 0; i < adjacents.size(); i++) {
if (!visited[adjacents[i]]) {
s.push(adjacents[i]);
visited[adjacents[i]] = true;
}
}
if (temp == destination) {
break;
}
}
cout << endl;
return output;
}