forked from the-tourist/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycles.cpp
74 lines (73 loc) · 2.07 KB
/
cycles.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
template <typename T>
vector<vector<int>> find_cycles(const graph<T> &g, int bound_cnt = 1 << 30, int bound_size = 1 << 30) {
vector<int> was(g.n, -1);
vector<int> st;
vector<vector<int>> cycles;
int total_size = 0;
function<void(int, int)> dfs = [&](int v, int pe) {
if ((int) cycles.size() >= bound_cnt || total_size >= bound_size) {
return;
}
was[v] = (int) st.size();
for (int id : g.g[v]) {
if (id == pe) {
continue;
}
auto &e = g.edges[id];
int to = e.from ^ e.to ^ v;
if (was[to] >= 0) {
vector<int> cycle(1, id);
for (int j = was[to]; j < (int) st.size(); j++) {
cycle.push_back(st[j]);
}
cycles.push_back(cycle);
total_size += (int) cycle.size();
if ((int) cycles.size() >= bound_cnt || total_size >= bound_size) {
was[v] = -2;
return;
}
continue;
}
if (was[to] == -1) {
st.push_back(id);
dfs(to, id);
st.pop_back();
}
}
was[v] = -2;
};
for (int i = 0; i < g.n; i++) {
if (was[i] == -1) {
dfs(i, -1);
}
}
return cycles;
// cycles are given by edge ids, all cycles are simple
// breaks after getting bound_cnt cycles or total_size >= bound_size
// digraph: finds at least one cycle in every connected component (if not broken)
// undigraph: finds cycle basis
}
template <typename T>
vector<int> edges_to_vertices(const graph<T> &g, const vector<int> &edge_cycle) {
int sz = (int) edge_cycle.size();
vector<int> vertex_cycle;
if (sz <= 2) {
vertex_cycle.push_back(g.edges[edge_cycle[0]].from);
if (sz == 2) {
vertex_cycle.push_back(g.edges[edge_cycle[0]].to);
}
} else {
for (int i = 0; i < sz; i++) {
int j = (i + 1) % sz;
auto &e = g.edges[edge_cycle[i]];
auto &other = g.edges[edge_cycle[j]];
if (other.from == e.from || other.to == e.from) {
vertex_cycle.push_back(e.to);
} else {
vertex_cycle.push_back(e.from);
}
}
}
return vertex_cycle;
// only for simple cycles!
}