-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopologicalX.cc
99 lines (90 loc) · 2.19 KB
/
TopologicalX.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <cstdlib>
#include <ctime>
#include <deque>
#include <print>
// 零入度拓扑序算法
struct AdjMatrixDigraph {
int V;
std::deque<std::deque<bool>> adj;
AdjMatrixDigraph(int v) : V{v}, adj(v, std::deque<bool>(v, false)) {}
};
struct TopologicalX {
std::deque<int> topology;
std::deque<int> rank;
TopologicalX(const AdjMatrixDigraph &G) : rank(G.V, -1) {
std::deque<int> indegree(G.V);
int V = G.V;
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (G.adj[i][j])
++indegree[j];
}
}
int count{0};
std::deque<int> queue;
for (int v = 0; v < V; ++v)
if (indegree[v] == 0)
queue.push_back(v);
while (!queue.empty()) {
int v = queue.front();
queue.pop_front();
topology.push_back(v);
rank[v] = count++;
for (int w = 0; w < V; ++w)
if (G.adj[v][w]) {
--indegree[w];
if (indegree[w] == 0)
queue.push_back(w);
}
}
// clear if there is a cycle
if (count != V) {
topology.clear();
rank = std::deque<int>(V, -1);
}
}
bool hasOrder() const { return !topology.empty(); }
};
void printAdjMatrixDigraph(const AdjMatrixDigraph &G) {
std::print("V\t");
for (int i = 0; i < G.V; ++i)
std::print("v{}\t", i);
std::print("\n");
for (int i = 0; i < G.V; ++i) {
std::print("v{}\t", i);
for (int j = 0; j < G.V; ++j)
std::print("{}\t", G.adj[i][j]);
std::print("\n");
}
}
int main() {
std::srand(std::time(nullptr));
const int e{8}, v{8};
AdjMatrixDigraph G(v);
std::deque<int> a(e), b(e);
int i{0};
while (i < e) {
a[i] = std::rand() % v, b[i] = std::rand() % v;
if (a[i] == b[i])
continue;
int j{0};
for (; j < i; ++j) {
if (a[i] == a[j] && b[i] == b[j])
break;
if (a[i] == b[j] && b[i] == a[j])
break;
}
if (i != j)
continue;
G.adj[a[i]][b[i]] = true;
++i;
}
std::print("AdjMatrixDigraph\n");
printAdjMatrixDigraph(G);
std::print("\n");
std::print("TopologicalX\t");
std::deque<int> topology{TopologicalX(G).topology};
for (const auto &e : topology)
std::print("{}\t", e);
std::print("\n");
}