Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_oracles.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
45#ifndef ALEPH_TESTS_GRAPH_ORACLES_H
46#define ALEPH_TESTS_GRAPH_ORACLES_H
47
48#include <algorithm>
49#include <limits>
50#include <numeric>
51#include <set>
52#include <vector>
53
54#include "graph_harness.H"
55
57{
59
62 {
63 std::vector<int> parent;
64
65 explicit Union_Find(int n) : parent(size_t(n))
66 {
67 std::iota(parent.begin(), parent.end(), 0);
68 }
69
70 int find(int x)
71 {
72 while (parent[size_t(x)] != x)
73 x = parent[size_t(x)] = parent[size_t(parent[size_t(x)])];
74 return x;
75 }
76
78 bool join(int a, int b)
79 {
80 a = find(a);
81 b = find(b);
82 if (a == b)
83 return false;
84 parent[size_t(a)] = b;
85 return true;
86 }
87 };
88
90 inline int degree(const Spec & s, int x)
91 {
92 int d = 0;
93 for (const auto & a : s.arcs)
94 d += (a[0] == x) + (a[1] == x);
95 return d;
96 }
97
100 inline std::vector<int> component_of(const Spec & s, int skip_node = -1,
101 int skip_arc = -1)
102 {
103 Union_Find uf(s.n);
104 for (size_t i = 0; i < s.arcs.size(); ++i)
105 {
106 const int u = s.arcs[i][0], v = s.arcs[i][1];
107 if (int(i) == skip_arc or u == skip_node or v == skip_node)
108 continue;
109 uf.join(u, v);
110 }
111 std::vector<int> comp(size_t(s.n));
112 for (int x = 0; x < s.n; ++x)
113 comp[size_t(x)] = uf.find(x);
114 return comp;
115 }
116
119 inline int components(const Spec & s, int skip_node = -1, int skip_arc = -1)
120 {
121 const auto comp = component_of(s, skip_node, skip_arc);
122 std::set<int> roots;
123 for (int x = 0; x < s.n; ++x)
124 if (x != skip_node)
125 roots.insert(comp[size_t(x)]);
126 return int(roots.size());
127 }
128
130 inline std::set<int> cut_nodes(const Spec & s)
131 {
132 const int c = components(s);
133 std::set<int> r;
134 for (int x = 0; x < s.n; ++x)
135 if (components(s, x) > c)
136 r.insert(x);
137 return r;
138 }
139
141 inline std::set<int> cut_nodes(const Spec & s, int start)
142 {
143 const auto comp = component_of(s);
144 std::set<int> r;
145 for (int x : cut_nodes(s))
146 if (comp[size_t(x)] == comp[size_t(start)])
147 r.insert(x);
148 return r;
149 }
150
152 inline std::set<int> bridges(const Spec & s)
153 {
154 const int c = components(s);
155 std::set<int> r;
156 for (size_t i = 0; i < s.arcs.size(); ++i)
157 if (components(s, -1, int(i)) > c)
158 r.insert(int(i));
159 return r;
160 }
161
163 inline bool has_cycle(const Spec & s)
164 {
165 Union_Find uf(s.n);
166 for (const auto & a : s.arcs)
167 if (not uf.join(a[0], a[1]))
168 return true;
169 return false;
170 }
171
173 inline bool on_cycle(const Spec & s, int x)
174 {
175 for (size_t i = 0; i < s.arcs.size(); ++i)
176 {
177 const int u = s.arcs[i][0], v = s.arcs[i][1];
178 if (u != x and v != x)
179 continue;
180 if (u == v)
181 return true;
182 const auto comp = component_of(s, -1, int(i));
183 if (comp[size_t(u)] == comp[size_t(v)])
184 return true;
185 }
186 return false;
187 }
188
191 inline bool is_bipartite(const Spec & s)
192 {
193 std::vector<int> color(size_t(s.n), -1);
194 for (int root = 0; root < s.n; ++root)
195 {
196 if (color[size_t(root)] != -1)
197 continue;
198 color[size_t(root)] = 0;
199 std::vector<int> stack = {root};
200 while (not stack.empty())
201 {
202 const int x = stack.back();
203 stack.pop_back();
204 for (const auto & a : s.arcs)
205 for (int k = 0; k < 2; ++k)
206 if (a[size_t(k)] == x)
207 {
208 const int y = a[size_t(1 - k)];
209 if (color[size_t(y)] == -1)
210 {
211 color[size_t(y)] = 1 - color[size_t(x)];
212 stack.push_back(y);
213 }
214 else if (color[size_t(y)] == color[size_t(x)])
215 return false;
216 }
217 }
218 }
219 return true;
220 }
221
224 inline int max_matching(const Spec & s)
225 {
226 const size_t m = s.arcs.size();
227 int best = 0;
228 for (unsigned long sub = 0; sub < (1UL << m); ++sub)
229 {
230 std::vector<bool> used(size_t(s.n), false);
231 int size = 0;
232 bool ok = true;
233 for (size_t i = 0; i < m and ok; ++i)
234 if (sub & (1UL << i))
235 {
236 const int u = s.arcs[i][0], v = s.arcs[i][1];
237 if (u == v or used[size_t(u)] or used[size_t(v)])
238 ok = false;
239 else
240 {
241 used[size_t(u)] = used[size_t(v)] = true;
242 ++size;
243 }
244 }
245 if (ok)
246 best = std::max(best, size);
247 }
248 return best;
249 }
250
252 inline bool is_matching(const Spec & s, const std::vector<int> & arcs)
253 {
254 std::vector<bool> used(size_t(s.n), false);
255 for (int i : arcs)
256 {
257 const int u = s.arcs[size_t(i)][0], v = s.arcs[size_t(i)][1];
258 if (u == v or used[size_t(u)] or used[size_t(v)])
259 return false;
260 used[size_t(u)] = used[size_t(v)] = true;
261 }
262 return true;
263 }
264
266 inline long long min_spanning_forest_weight(const Spec & s)
267 {
268 std::vector<size_t> order(s.arcs.size());
269 std::iota(order.begin(), order.end(), size_t(0));
270 std::stable_sort(order.begin(), order.end(),
271 [&] (size_t a, size_t b) { return s.w[a] < s.w[b]; });
272 Union_Find uf(s.n);
273 long long total = 0;
274 for (size_t i : order)
275 if (uf.join(s.arcs[i][0], s.arcs[i][1]))
276 total += s.w[i];
277 return total;
278 }
279
282 inline std::vector<std::vector<bool>> closure(const Spec & s, bool directed)
283 {
284 std::vector<std::vector<bool>> r(size_t(s.n), std::vector<bool>(size_t(s.n), false));
285 for (const auto & a : s.arcs)
286 {
287 r[size_t(a[0])][size_t(a[1])] = true;
288 if (not directed)
289 r[size_t(a[1])][size_t(a[0])] = true;
290 }
291 for (int k = 0; k < s.n; ++k)
292 for (int i = 0; i < s.n; ++i)
293 if (r[size_t(i)][size_t(k)])
294 for (int j = 0; j < s.n; ++j)
295 if (r[size_t(k)][size_t(j)])
296 r[size_t(i)][size_t(j)] = true;
297 return r;
298 }
299
301 inline bool reaches(const std::vector<std::vector<bool>> & r, int u, int v)
302 {
303 return u == v or r[size_t(u)][size_t(v)];
304 }
305
308 inline std::vector<int> scc_partition(const Spec & s)
309 {
310 const auto r = closure(s, true);
311 std::vector<int> part(size_t(s.n));
312 for (int u = 0; u < s.n; ++u)
313 {
314 part[size_t(u)] = u;
315 for (int v = 0; v < u; ++v)
316 if (r[size_t(u)][size_t(v)] and r[size_t(v)][size_t(u)])
317 {
318 part[size_t(u)] = part[size_t(v)];
319 break;
320 }
321 }
322 return part;
323 }
324
326 inline bool is_dag(const Spec & s)
327 {
328 const auto r = closure(s, true);
329 for (int u = 0; u < s.n; ++u)
330 if (r[size_t(u)][size_t(u)])
331 return false;
332 return true;
333 }
334
336 inline bool is_topological_order(const Spec & s, const std::vector<int> & order)
337 {
338 if (int(order.size()) != s.n)
339 return false;
340 std::vector<int> pos(size_t(s.n), -1);
341 for (size_t i = 0; i < order.size(); ++i)
342 {
343 if (order[i] < 0 or order[i] >= s.n or pos[size_t(order[i])] != -1)
344 return false;
345 pos[size_t(order[i])] = int(i);
346 }
347 for (const auto & a : s.arcs)
348 if (pos[size_t(a[0])] >= pos[size_t(a[1])])
349 return false;
350 return true;
351 }
352
355 inline std::vector<std::vector<int>> hops(const Spec & s, bool directed)
356 {
357 std::vector<std::vector<int>> h(size_t(s.n), std::vector<int>(size_t(s.n), -1));
358 for (int u = 0; u < s.n; ++u)
359 {
360 auto & d = h[size_t(u)];
361 d[size_t(u)] = 0;
362 std::vector<int> queue = {u};
363 for (size_t k = 0; k < queue.size(); ++k)
364 for (const auto & a : s.arcs)
365 for (size_t e = 0; e < (directed ? 1U : 2U); ++e)
366 if (a[e] == queue[k] and d[size_t(a[1 - e])] == -1)
367 {
368 d[size_t(a[1 - e])] = d[size_t(queue[k])] + 1;
369 queue.push_back(a[1 - e]);
370 }
371 }
372 return h;
373 }
374
376 inline Spec reversed(const Spec & s)
377 {
378 Spec r = s;
379 for (auto & a : r.arcs)
380 std::swap(a[0], a[1]);
381 return r;
382 }
383
386 inline Spec without(const Spec & s, unsigned long hidden, std::vector<int> & orig)
387 {
388 Spec r;
389 r.n = s.n;
390 orig.clear();
391 for (size_t i = 0; i < s.arcs.size(); ++i)
392 if (((hidden >> i) & 1UL) == 0)
393 {
394 r.arcs.push_back(s.arcs[i]);
395 if (i < s.w.size())
396 r.w.push_back(s.w[i]);
397 orig.push_back(int(i));
398 }
399 return r;
400 }
401
405 inline std::vector<int> kahn_ranks(const Spec & s)
406 {
407 const auto r = closure(s, true);
408 std::vector<int> rank(size_t(s.n), 0);
409 for (int u = 0; u < s.n; ++u)
410 if (r[size_t(u)][size_t(u)])
411 for (int v = 0; v < s.n; ++v)
412 if (reaches(r, u, v))
413 rank[size_t(v)] = -1;
414 // The predecessors of a node that comes out also come out, so that n
415 // rounds give the longest paths
416 for (int round = 0; round < s.n; ++round)
417 for (const auto & a : s.arcs)
418 if (rank[size_t(a[0])] != -1 and rank[size_t(a[1])] != -1)
419 rank[size_t(a[1])] = std::max(rank[size_t(a[1])], rank[size_t(a[0])] + 1);
420 return rank;
421 }
422
426 inline std::vector<std::vector<bool>> dominators(const Spec & s, int root)
427 {
428 // The nodes reached from root without going through skip
429 auto reached = [&] (int skip)
430 {
431 std::vector<bool> seen(size_t(s.n), false);
432 if (root == skip)
433 return seen;
434 seen[size_t(root)] = true;
435 for (bool grew = true; grew; )
436 {
437 grew = false;
438 for (const auto & a : s.arcs)
439 if (seen[size_t(a[0])] and a[1] != skip and not seen[size_t(a[1])])
440 {
441 seen[size_t(a[1])] = true;
442 grew = true;
443 }
444 }
445 return seen;
446 };
447
448 const auto all = reached(-1);
449 std::vector<std::vector<bool>> dom(size_t(s.n), std::vector<bool>(size_t(s.n), false));
450 for (int d = 0; d < s.n; ++d)
451 {
452 if (not all[size_t(d)])
453 continue;
454 const auto rest = reached(d);
455 for (int v = 0; v < s.n; ++v)
456 dom[size_t(d)][size_t(v)] = all[size_t(v)] and (d == v or not rest[size_t(v)]);
457 }
458 return dom;
459 }
460
463 inline std::vector<int> immediate_dominators(const Spec & s, int root)
464 {
465 const auto dom = dominators(s, root);
466 std::vector<int> idom(size_t(s.n), -1);
467 for (int v = 0; v < s.n; ++v)
468 for (int d = 0; d < s.n; ++d)
469 {
470 if (d == v or not dom[size_t(d)][size_t(v)])
471 continue;
472 bool deepest = true;
473 for (int e = 0; e < s.n and deepest; ++e)
474 if (e != v and dom[size_t(e)][size_t(v)])
475 deepest = dom[size_t(e)][size_t(d)];
476 if (deepest)
477 idom[size_t(v)] = d;
478 }
479 return idom;
480 }
481
484 inline std::vector<std::set<int>> dominance_frontiers(const Spec & s, int root)
485 {
486 const auto dom = dominators(s, root);
487 std::vector<std::set<int>> df(size_t(s.n));
488 for (int x = 0; x < s.n; ++x)
489 for (const auto & a : s.arcs)
490 if (dom[size_t(x)][size_t(a[0])]
491 and (x == a[1] or not dom[size_t(x)][size_t(a[1])]))
492 df[size_t(x)].insert(a[1]);
493 return df;
494 }
495
499 inline bool is_euler_walk(const Spec & s, const std::vector<int> & arcs,
500 bool directed, bool closed)
501 {
502 if (arcs.size() != s.arcs.size())
503 return false;
504 std::vector<bool> used(s.arcs.size(), false);
505 for (int i : arcs)
506 {
507 if (i < 0 or size_t(i) >= s.arcs.size() or used[size_t(i)])
508 return false;
509 used[size_t(i)] = true;
510 }
511 if (arcs.empty())
512 return true;
513 for (int start : {s.arcs[size_t(arcs[0])][0], s.arcs[size_t(arcs[0])][1]})
514 {
515 int at = start;
516 bool ok = true;
517 for (int i : arcs)
518 {
519 const int u = s.arcs[size_t(i)][0], v = s.arcs[size_t(i)][1];
520 if (u == at)
521 at = v;
522 else if (not directed and v == at)
523 at = u;
524 else
525 {
526 ok = false;
527 break;
528 }
529 }
530 if (ok and (not closed or at == start))
531 return true;
532 if (directed)
533 break;
534 }
535 return false;
536 }
537
539 enum class Euler { None, Path, Cycle };
540
544 inline Euler euler_class(const Spec & s, bool directed)
545 {
546 if (s.arcs.empty())
547 return Euler::Cycle;
548 Union_Find uf(s.n);
549 for (const auto & a : s.arcs)
550 uf.join(a[0], a[1]);
551 std::set<int> roots;
552 for (const auto & a : s.arcs)
553 roots.insert(uf.find(a[0]));
554 if (roots.size() > 1)
555 return Euler::None;
556
557 if (not directed)
558 {
559 int odd = 0;
560 for (int x = 0; x < s.n; ++x)
561 odd += degree(s, x) % 2;
562 return odd == 0 ? Euler::Cycle : odd == 2 ? Euler::Path : Euler::None;
563 }
564
565 int plus = 0, minus = 0;
566 for (int x = 0; x < s.n; ++x)
567 {
568 int out = 0, in = 0;
569 for (const auto & a : s.arcs)
570 {
571 out += a[0] == x;
572 in += a[1] == x;
573 }
574 if (out == in)
575 continue;
576 if (out == in + 1)
577 ++plus;
578 else if (in == out + 1)
579 ++minus;
580 else
581 return Euler::None;
582 }
583 if (plus == 0 and minus == 0)
584 return Euler::Cycle;
585 return plus == 1 and minus == 1 ? Euler::Path : Euler::None;
586 }
587
589 constexpr long long Inf = std::numeric_limits<long long>::max();
590
592 inline long long saturated_add(long long a, long long b)
593 {
594 if (b > 0 and a > std::numeric_limits<long long>::max() - b)
595 return std::numeric_limits<long long>::max();
596 if (b < 0 and a < std::numeric_limits<long long>::lowest() - b)
597 return std::numeric_limits<long long>::lowest();
598 return a + b;
599 }
600
606 inline std::vector<std::vector<long long>>
607 shortest_distances(const Spec & s, bool directed, bool & neg)
608 {
609 std::vector<std::vector<long long>> d(size_t(s.n), std::vector<long long>(size_t(s.n), Inf));
610 for (int i = 0; i < s.n; ++i)
611 d[size_t(i)][size_t(i)] = 0;
612 for (size_t i = 0; i < s.arcs.size(); ++i)
613 {
614 const size_t u = size_t(s.arcs[i][0]), v = size_t(s.arcs[i][1]);
615 d[u][v] = std::min(d[u][v], s.w[i]);
616 if (not directed)
617 d[v][u] = std::min(d[v][u], s.w[i]);
618 }
619 for (size_t k = 0; k < size_t(s.n); ++k)
620 for (size_t i = 0; i < size_t(s.n); ++i)
621 for (size_t j = 0; j < size_t(s.n); ++j)
622 if (d[i][k] != Inf and d[k][j] != Inf)
623 d[i][j] = std::min(d[i][j], saturated_add(d[i][k], d[k][j]));
624 neg = false;
625 for (size_t i = 0; i < size_t(s.n); ++i)
626 neg = neg or d[i][i] < 0;
627 return d;
628 }
629
633 inline bool is_negative_cycle(const Spec & s, const std::vector<int> & arcs,
634 bool directed)
635 {
636 if (arcs.empty())
637 return false;
638 const int first = s.arcs[size_t(arcs[0])][0];
639 long long total = 0;
640 // Try each direction of the first arc in a graph that is not directed
641 for (int start : {first, s.arcs[size_t(arcs[0])][1]})
642 {
643 int at = start;
644 total = 0;
645 bool ok = true;
646 for (int i : arcs)
647 {
648 const int u = s.arcs[size_t(i)][0], v = s.arcs[size_t(i)][1];
649 if (u == at)
650 at = v;
651 else if (not directed and v == at)
652 at = u;
653 else
654 {
655 ok = false;
656 break;
657 }
658 total += s.w[size_t(i)];
659 }
660 if (ok and at == start and total < 0)
661 return true;
662 if (directed)
663 break;
664 }
665 return false;
666 }
667
670 inline long long max_flow(const Spec & s, int src, int snk)
671 {
672 std::vector<std::vector<long long>> c(size_t(s.n), std::vector<long long>(size_t(s.n), 0));
673 for (size_t i = 0; i < s.arcs.size(); ++i)
674 if (s.arcs[i][0] != s.arcs[i][1])
675 c[size_t(s.arcs[i][0])][size_t(s.arcs[i][1])] += s.w[i];
676 long long flow = 0;
677 while (true)
678 {
679 std::vector<int> parent(size_t(s.n), -1);
680 parent[size_t(src)] = src;
681 std::vector<int> queue = {src};
682 for (size_t h = 0; h < queue.size(); ++h)
683 for (int v = 0; v < s.n; ++v)
684 if (parent[size_t(v)] == -1 and c[size_t(queue[h])][size_t(v)] > 0)
685 {
686 parent[size_t(v)] = queue[h];
687 queue.push_back(v);
688 }
689 if (parent[size_t(snk)] == -1)
690 return flow;
691 long long b = Inf;
692 for (int v = snk; v != src; v = parent[size_t(v)])
693 b = std::min(b, c[size_t(parent[size_t(v)])][size_t(v)]);
694 for (int v = snk; v != src; v = parent[size_t(v)])
695 {
696 c[size_t(parent[size_t(v)])][size_t(v)] -= b;
697 c[size_t(v)][size_t(parent[size_t(v)])] += b;
698 }
699 flow += b;
700 }
701 }
702} // namespace graph_oracles
703
704#endif // ALEPH_TESTS_GRAPH_ORACLES_H
long double h
Definition btreepic.C:154
size_t size_t int32_t * out
Definition ca-c-api.h:120
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
Every small multigraph, on every kind of graph, for the tests.
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
static mpfr_t y
Definition mpfr_mul_d.c:3
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
and
Check uniqueness with explicit hash + equality functors.
static long & df(typename GT::Node *p)
Internal helper: DFS discovery time stored in NODE_COUNTER(p).
static long & color(typename GT::Node *p)
bool is_dag(const Spec &s)
Whether the digraph has no directed cycle (a loop is one)
constexpr long long Inf
No distance, in the matrices of the oracles.
bool is_matching(const Spec &s, const std::vector< int > &arcs)
Whether the arcs, by number, form a matching of s.
bool has_cycle(const Spec &s)
Whether the multigraph, not directed, has a cycle.
std::vector< std::vector< int > > hops(const Spec &s, bool directed)
hops[u][v]: the least number of arcs of a path from u to v, 0 from u to itself and -1 if there is no ...
int max_matching(const Spec &s)
The size of a maximum matching, by brute force over the subsets of the arcs; loops never match.
std::vector< std::vector< long long > > shortest_distances(const Spec &s, bool directed, bool &neg)
Floyd on a matrix of long long: the shortest distances, Inf where there is no path,...
Euler euler_class(const Spec &s, bool directed)
The Eulerian class of s: the arcs must be in one component (weakly, in a digraph),...
std::vector< std::vector< bool > > dominators(const Spec &s, int root)
dom[d][v]: d dominates v from root in a digraph, that is, v is reached from root and every path from ...
std::vector< std::vector< bool > > closure(const Spec &s, bool directed)
reach[u][v]: v is reached from u by a directed path of one arc or more (in a graph that is not direct...
std::vector< std::set< int > > dominance_frontiers(const Spec &s, int root)
The dominance frontier of each node from root: y is in that of x if x dominates a predecessor of y an...
long long min_spanning_forest_weight(const Spec &s)
The weight of a minimum spanning forest: Kruskal on the list of arcs.
std::vector< int > component_of(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of the component of each node, as that of its root, of the graph without the node skip_nod...
long long max_flow(const Spec &s, int src, int snk)
The maximum flow from src to snk, by Edmonds-Karp on the matrix of the capacities w (loops carry noth...
bool reaches(const std::vector< std::vector< bool > > &r, int u, int v)
Whether there is a path, of zero arcs or more, from u to v.
std::vector< int > kahn_ranks(const Spec &s)
The rank that Kahn's algorithm gives each node of a digraph: 0 for a node without arcs in,...
std::set< int > cut_nodes(const Spec &s)
The nodes whose removal splits their component.
std::set< int > bridges(const Spec &s)
The arcs, by number, whose removal splits their component.
int components(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of components, without the node skip_node and the arc skip_arc.
bool on_cycle(const Spec &s, int x)
Whether the node x lies on a cycle of the multigraph, not directed.
bool is_topological_order(const Spec &s, const std::vector< int > &order)
Whether order, a sequence of all the nodes, is a topological order.
bool is_bipartite(const Spec &s)
Whether the multigraph, not directed, has two colors with no arc inside one (a loop is an odd cycle)
long long saturated_add(long long a, long long b)
a + b, kept within the range of long long
Spec without(const Spec &s, unsigned long hidden, std::vector< int > &orig)
The spec without the arcs whose numbers are set in hidden, with orig[i] the number in s of its i-th a...
int degree(const Spec &s, int x)
The degree of x: a loop counts 2.
Euler
What kind of Eulerian walk the multigraph has.
bool is_euler_walk(const Spec &s, const std::vector< int > &arcs, bool directed, bool closed)
Whether arcs, by number, walk every arc of s once, each from the node where the one before ended (fro...
Spec reversed(const Spec &s)
The spec with each arc reversed; the arcs keep their numbers.
std::vector< int > immediate_dominators(const Spec &s, int root)
The immediate dominator of each node from root: the strict dominator that all the others dominate; -1...
bool is_negative_cycle(const Spec &s, const std::vector< int > &arcs, bool directed)
Whether the arcs, by number, form a closed walk of s whose weights add up to less than zero; in a gra...
std::vector< int > scc_partition(const Spec &s)
The strongly connected components, as the smallest node of the component of each node.
A small multigraph: its nodes are 0 .
std::vector< long long > w
std::vector< std::array< int, 2 > > arcs
Union-find over the nodes 0 .. n - 1.
bool join(int a, int b)
Joins the sets of a and b; false if they were the same set.
std::vector< int > parent
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k
Gsl_Compatible_Rng r