-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFS.cpp
More file actions
64 lines (50 loc) · 1.51 KB
/
DFS.cpp
File metadata and controls
64 lines (50 loc) · 1.51 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
60
61
62
63
64
#include "DFS.h"
#include <map>
#include <queue>
using std::map;
using std::queue;
DFS::DFS(vector<vector<double>> graph_, vector<string> vertices_, vector<vector<string>> dset) : graph(graph_), vertices(vertices_), dv(dset) {
//cout << __LINE__ << endl;
}
string DFS::getAirportIdFromName(std::string name) {
for (size_t i = 0; i < dv.size(); i++) {
if (dv[i][1].substr(1, dv[i][1].length() - 2) == name) {
return dv[i][0];
}
}
return "";
}
string DFS::getAirportIdFromIndex(int index) {
return dv[index][0];
}
bool DFS::detect_cycle(vector<string> airportIds) {
makeGraph mkg;
mkg.populateGraph();
queue<string> dfs;
dfs.push(airportIds[0]);
map<string, bool> visited;
for (size_t i = 0; i < vertices.size(); i++) visited[getAirportIdFromName(vertices[i])] = false;
visited[airportIds[0]] = true;
while (!dfs.empty()) {
string airportId = dfs.front();
dfs.pop();
int id = std::stoi(airportId);
vector<int> neighborsIdx = mkg.getNeighbors(id);
if (neighborsIdx.size() == 0) continue;
vector<string> neighbors;
for (int idx : neighborsIdx) {
neighbors.push_back(getAirportIdFromIndex(idx));
}
for (string str : neighbors) {
if (!visited[str]) {
visited[str] = true;
} else {
return true;
}
}
for (string str : neighbors) {
dfs.push(str);
}
}
return false;
}