-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.cpp
More file actions
72 lines (58 loc) · 1.73 KB
/
Copy pathsimple.cpp
File metadata and controls
72 lines (58 loc) · 1.73 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
65
66
67
68
69
70
71
72
#include "simple.h"
SimpleAlgorithm::SimpleAlgorithm(size_t _n, size_t _m)
: L(_n, 0), adj(_n, vs()) {
n = _n;
m = _m;
}
bool SimpleAlgorithm::update_label(size_t u, size_t original_vertex) {
if (u == original_vertex) {
// we have found a cycle by returning to the original vertex
return false;
}
for (size_t i = 0; i < adj[u].size(); i++) {
size_t next_vertex = adj[u][i];
if (L[u] >= L[next_vertex]) {
L[next_vertex] = L[u] + 1;
if (!update_label(next_vertex, original_vertex)) {
// cycle found
return false;
}
}
}
// successfully completed the updates
return true;
}
bool SimpleAlgorithm::insert_edge(size_t u, size_t v) {
/* inserts directed edge u to v
* in case of failure (cycle or otherwise) returns false */
if (u < 0 || u >= n || v < 0 || v >= n) {
std::cout << "Nodes are in range [0, n)\n";
return false;
}
adj[u].push_back(v);
if (L[u] >= L[v]) { // we have to update v's label
L[v] = L[u] + 1;
if (!update_label(v, u)) {
// cycle found
return false;
}
}
return true;
}
bool SimpleAlgorithm::precedes(size_t u, size_t v) {
/* returns if node u precedes node v in topological ordering */
return L[u] < L[v];
}
std::vector < size_t > SimpleAlgorithm::topsort() {
std::vector < size_t > result;
std::vector < vs > labels(n, vs());
for (size_t i = 0; i < n; i++) {
labels[L[i]].push_back(i);
}
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < labels[i].size(); j++) {
result.push_back(labels[i][j]);
}
}
return result;
}