-
Notifications
You must be signed in to change notification settings - Fork 0
/
ordered_graph_condensation.cpp
75 lines (66 loc) · 2.01 KB
/
ordered_graph_condensation.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
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
using Graph = vector<vector<int>>;
class Condensator {
private:
Graph graph_, reversed_;
vector<char> visited_;
vector<int> color_;
int current_color_ = 0;
stack<uint32_t> vertex_order_;
// Kosaraju Algorithm
void FillVertexOrder(uint32_t cur) {
visited_[cur] = 1;
for (uint32_t next : graph_[cur]) {
if (!visited_[next]) {
FillVertexOrder(next);
}
}
vertex_order_.push(cur);
}
void PaintVertexes(uint32_t cur) {
color_[cur] = current_color_;
for (uint32_t next : reversed_[cur]) {
if (color_[next] == -1) {
PaintVertexes(next);
}
}
}
public:
Condensator(const Graph& g)
: graph_(g)
, reversed_(Graph(g.size()))
, visited_(vector<char>(g.size(), 0))
, color_(vector<int>(g.size(), -1))
{
for (size_t i = 0; i < g.size(); ++i) {
for (uint32_t j : g[i]) {
reversed_[j].push_back(i);
}
}
}
pair<Graph, vector<vector<int>>> Condensate() {
for (size_t v = 0; v < graph_.size(); ++v) {
if (!visited_[v]) {
FillVertexOrder(v);
}
}
for (; !vertex_order_.empty(); vertex_order_.pop()) {
if (color_[vertex_order_.top()] == -1) {
PaintVertexes(vertex_order_.top());
++current_color_;
}
}
Graph condensation(static_cast<size_t>(current_color_));
vector<vector<int>> mapping_to_colors(current_color_);
for (size_t v = 0; v < graph_.size(); ++v) {
size_t a = static_cast<size_t>(color_[v]);
mapping_to_colors[a].push_back(v);
for (uint32_t u : graph_[v]) {
size_t b = static_cast<size_t>(color_[u]);
if (a != b) {
condensation[a].push_back(b);
}
}
}
return { condensation, mapping_to_colors };
}
};