Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
moved_from_graphs_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
42#include "moved_from.H"
43
44#include <Floyd_Warshall.H>
45#include <HLD.H>
46#include <Hungarian.H>
47#include <LCA.H>
48#include <Simplex.H>
49#include <Tree_DP.H>
50#include <Tree_Decomposition.H>
51#include <tpl_array.H>
52#include <tpl_dynMat.H>
53#include <tpl_graph.H>
54#include <tpl_mo_on_trees.H>
55#include <tpl_tree_node.H>
56
57#include <algorithm>
58#include <cstddef>
59#include <limits>
60#include <stdexcept>
61#include <utility>
62
63using namespace Aleph;
64
65namespace
66{
68 using Gnode = Graph::Node;
70 using Dnode = Dgraph::Node;
71 using Tnode = Tree_Node<int>;
72
73 constexpr size_t No_Id = std::numeric_limits<size_t>::max();
74
75 // The tree of n nodes in which the node i is a child of the node
76 // (i - 1) / 2 and has the info i % 7
77 struct Test_Tree
78 {
79 Graph g;
81
82 explicit Test_Tree(const size_t n)
83 {
84 for (size_t i = 0; i < n; ++i)
85 nodes.append(g.insert_node(static_cast<int>(i % 7)));
86 for (size_t i = 1; i < n; ++i)
87 g.insert_arc(nodes((i - 1) / 2), nodes(i), 1);
88 }
89 };
90
91 // The tree of n nodes, built once: it outlives the objects built on it
92 // and those that receive their state
93 const Test_Tree & tree_of(const size_t n)
94 {
95 static DynList<Test_Tree> trees;
96 for (auto it = trees.get_it(); it.has_curr(); it.next_ne())
97 if (it.get_curr().nodes.size() == n)
98 return it.get_curr();
99 return trees.append(Test_Tree(n));
100 }
101
102 // The same tree of Tree_Node, which frees its nodes
103 struct Test_Node_Tree
104 {
106
107 explicit Test_Node_Tree(const size_t n)
108 {
109 for (size_t i = 0; i < n; ++i)
110 nodes.append(new Tnode(static_cast<int>(i % 7)));
111 for (size_t i = 1; i < n; ++i)
112 nodes((i - 1) / 2)->insert_rightmost_child(nodes(i));
113 }
114
115 // the source of a move is left without nodes, and frees none
116 Test_Node_Tree(Test_Node_Tree &&) = default;
117
118 ~Test_Node_Tree()
119 {
120 if (not nodes.is_empty())
122 }
123 };
124
125 const Test_Node_Tree & node_tree_of(const size_t n)
126 {
127 static DynList<Test_Node_Tree> trees;
128 for (auto it = trees.get_it(); it.has_curr(); it.next_ne())
129 if (it.get_curr().nodes.size() == n)
130 return it.get_curr();
131 return trees.append(Test_Node_Tree(n));
132 }
133
134 // The cycle of n nodes and n arcs, i -> (i + 1) % n of weight 1 + i % 3
135 struct Test_Digraph
136 {
137 Dgraph g;
139
140 explicit Test_Digraph(const size_t n)
141 {
142 for (size_t i = 0; i < n; ++i)
143 nodes.append(g.insert_node(static_cast<int>(i)));
144 for (size_t i = 0; i < n; ++i)
145 g.insert_arc(nodes(i), nodes((i + 1) % n), static_cast<int>(1 + i % 3));
146 }
147 };
148
149 const Test_Digraph & digraph_of(const size_t n)
150 {
151 static DynList<Test_Digraph> digraphs;
152 for (auto it = digraphs.get_it(); it.has_curr(); it.next_ne())
153 if (it.get_curr().nodes.size() == n)
154 return it.get_curr();
155 return digraphs.append(Test_Digraph(n));
156 }
157
158 // A node of a digraph that no index has
159 Dnode * stranger()
160 {
161 static Dgraph g;
162 static Dnode * const p = g.insert_node(0);
163 return p;
164 }
165} // namespace
166
168{
170
171 static L make(const size_t n) { return L(tree_of(n).g); }
172
173 static bool is_empty(const L & l)
174 {
175 return l.is_empty() and l.size() == 0 and l.num_levels() == 0 and l.root() == nullptr;
176 }
177
178 static void use(L & l)
179 {
180 for (size_t id = 0; id < l.size(); ++id)
181 {
182 EXPECT_EQ(l.id_of(l.node_of(id)), id);
183 EXPECT_EQ(l.lca_id(l.root_id(), id), l.root_id());
184 EXPECT_EQ(l.distance_id(l.root_id(), id), l.depth_of_id(id));
185 EXPECT_EQ(l.kth_ancestor_id(id, l.depth_of_id(id)), l.root_id());
186 EXPECT_TRUE(id == l.root_id() or l.is_ancestor_id(l.parent_id(id), id));
187 }
188 EXPECT_THROW((void) l.node_of(l.size()), std::out_of_range);
189 }
190};
191
193{
195
196 static L make(const size_t n) { return L(tree_of(n).g); }
197
198 static bool is_empty(const L & l)
199 {
200 return l.is_empty() and l.size() == 0 and l.euler_tour_size() == 0 and l.root() == nullptr;
201 }
202
203 static void use(L & l)
204 {
205 for (size_t id = 0; id < l.size(); ++id)
206 {
207 EXPECT_EQ(l.id_of(l.node_of(id)), id);
208 EXPECT_EQ(l.lca_id(l.root_id(), id), l.root_id());
209 EXPECT_EQ(l.distance_id(l.root_id(), id), l.depth_of_id(id));
210 EXPECT_TRUE(id == l.root_id() or l.lca_id(l.parent_id(id), id) == l.parent_id(id));
211 }
212 EXPECT_EQ(l.euler_tour_size(), l.is_empty() ? 0 : 2 * l.size() - 1);
213 EXPECT_EQ(l.euler_tour().size(), l.euler_tour_size());
214 EXPECT_THROW((void) l.node_of(l.size()), std::out_of_range);
215 }
216};
217
218template <> struct Moved_From_Case<HLD_Sum<Graph, int>>
219{
221
222 static H make(const size_t n)
223 {
224 return H(tree_of(n).g, [](Gnode * p) { return p->get_info(); });
225 }
226
227 static bool is_empty(const H & h)
228 {
229 return h.is_empty() and h.size() == 0 and h.num_chains() == 0 and h.root() == nullptr;
230 }
231
232 static void use(H & h)
233 {
234 int total = 0;
235 for (size_t id = 0; id < h.size(); ++id)
236 {
237 EXPECT_EQ(h.id_of(h.node_of(id)), id);
238 EXPECT_EQ(h.lca_id(h.root_id(), id), h.root_id());
239 EXPECT_EQ(h.path_query_id(id, id), h.get_value_at_id(id));
240 h.point_update_id(id, h.get_value_at_id(id) + 1);
241 total += h.get_value_at_id(id);
242 (void) h.path_query_id(h.root_id(), id);
243 }
244 EXPECT_TRUE(h.is_empty() or h.subtree_query_id(h.root_id()) == total);
245 EXPECT_THROW((void) h.node_of(h.size()), std::out_of_range);
246 }
247};
248
250{
252
253 static H make(const size_t n) { return H(tree_of(n).g); }
254
255 static bool is_empty(const H & h)
256 {
257 return h.is_empty() and h.size() == 0 and h.root() == nullptr;
258 }
259
260 static void use(H & h)
261 {
262 for (size_t id = 0; id < h.size(); ++id)
263 {
264 EXPECT_EQ(h.id_of(h.node_of(id)), id);
265 EXPECT_LT(h.position_of_id(id), h.size());
266 const auto range = h.subtree_range_id(id);
267 EXPECT_EQ(range.right - range.left + 1, h.subtree_size_of_id(id));
268 EXPECT_EQ(h.lca_id(h.root_id(), id), h.root_id());
269 EXPECT_EQ(h.distance_id(h.root_id(), id), h.depth_of_id(id));
270 EXPECT_LT(h.head_id(id), h.size());
271 size_t segments = 0;
272 h.for_each_path_segment_id(h.root_id(), id,
273 [&segments](size_t, size_t, bool) { ++segments; });
274 EXPECT_GT(segments, 0u);
275 }
276 EXPECT_THROW((void) h.node_of(h.size()), std::out_of_range);
277 }
278};
279
280template <> struct Moved_From_Case<HLD_Path_Query<Graph, int, Aleph::plus<int>>>
281{
283
284 static Q make(const size_t n) { return Q(tree_of(n).g, 0); }
285
286 static bool is_empty(const Q & q)
287 {
288 return q.is_empty() and q.size() == 0 and q.decomposition().size() == 0;
289 }
290
291 static void use(Q & q)
292 {
293 int total = 0;
294 for (size_t id = 0; id < q.size(); ++id)
295 {
296 q.update_node_id(id, 1);
297 EXPECT_EQ(q.query_path_id(id, id), q.get_node_id(id));
298 total += q.get_node_id(id);
299 }
300 EXPECT_TRUE(q.is_empty() or q.query_subtree_id(q.decomposition().root_id()) == total);
301 }
302};
303
305{
307
308 static C make(const size_t n) { return C(tree_of(n).g); }
309
310 static bool is_empty(const C & c)
311 {
312 return c.is_empty() and c.size() == 0 and c.centroid_root_id() == No_Id
313 and c.centroid_root() == nullptr;
314 }
315
316 static void use(C & c)
317 {
318 EXPECT_EQ(c.centroid_root() == nullptr, c.is_empty());
319 EXPECT_LE(c.max_centroid_level(), c.size());
320 for (size_t id = 0; id < c.size(); ++id)
321 {
323 EXPECT_TRUE(c.is_centroid_ancestor_id(c.centroid_root_id(), id));
324 EXPECT_NE(c.distance_to_centroid_id(id, c.centroid_root_id()), No_Id);
325 size_t k = 0;
326 c.for_each_centroid_ancestor_id(id, [&k](size_t, size_t, size_t) { ++k; });
327 EXPECT_EQ(k, c.centroid_path_length_of_id(id));
328 }
329 }
330};
331
332// The size of the subtree of each node
333template <> struct Moved_From_Case<Tree_DP<Graph, long>>
334{
336
337 static D make(const size_t n)
338 {
339 const Test_Tree & t = tree_of(n);
340 return D(t.g, n == 0 ? nullptr : t.nodes(0), [](Gnode *) { return 1L; },
341 [](Gnode *, const long & acc, Gnode *, const long & child) { return acc + child; });
342 }
343
344 static bool is_empty(const D & d) { return d.size() == 0 and d.values().size() == 0; }
345
346 static void use(D & d)
347 {
348 long largest = 0;
349 for (size_t id = 0; id < d.size(); ++id)
350 {
351 Gnode * p = d.node_of(id);
352 EXPECT_EQ(d.id_of(p), id);
353 EXPECT_EQ(d.value(p), d.values()[id]);
354 largest = std::max(largest, d.value(p));
355 }
356 EXPECT_EQ(largest, static_cast<long>(d.size())); // the subtree of the root
357 EXPECT_EQ(d.values().size(), d.size());
358 }
359};
360
361// The eccentricity of each node: its largest distance to another one
362template <> struct Moved_From_Case<Reroot_DP<Graph, long>>
363{
365
366 static R make(const size_t n)
367 {
368 const Test_Tree & t = tree_of(n);
369 return R(t.g, n == 0 ? nullptr : t.nodes(0), 0L, [](Gnode *) { return 0L; },
370 [](const long & a, const long & b) { return std::max(a, b); },
371 [](Gnode *, Gnode *, const long & v) { return v + 1; });
372 }
373
374 static bool is_empty(const R & r) { return r.size() == 0 and r.values().size() == 0; }
375
376 static void use(R & r)
377 {
378 for (size_t id = 0; id < r.size(); ++id)
379 {
380 Gnode * p = r.node_of(id);
381 EXPECT_EQ(r.id_of(p), id);
382 EXPECT_EQ(r.value(p), r.values()[id]);
383 EXPECT_LT(r.value(p), static_cast<long>(r.size()));
384 }
385 EXPECT_EQ(r.values().size(), r.size());
386 }
387};
388
389// The engine gives no access to its nodes: they are those of the tree of
390// its size, on which make() built it
392{
394
395 static M make(const size_t n)
396 {
397 const Test_Tree & t = tree_of(n);
398 return M(t.g, n == 0 ? nullptr : t.nodes(0));
399 }
400
401 static bool is_empty(const M & m) { return m.is_empty() and m.size() == 0; }
402
403 static void use(M & m)
404 {
405 const Test_Tree & t = tree_of(m.size());
406 Array<Gnode *> roots;
408 for (size_t i = 0; i < m.size(); i += 1 + m.size() / 8)
409 {
410 roots.append(t.nodes(i));
411 pairs.append(std::make_pair(t.nodes(0), t.nodes(i)));
412 }
413 EXPECT_EQ(m.subtree_solve(roots).size(), roots.size());
414 EXPECT_EQ(m.path_solve(pairs).size(), pairs.size());
415 // the values are i % 7
416 EXPECT_TRUE(m.is_empty() or m.subtree_solve({t.nodes(0)})(0) == std::min<size_t>(m.size(), 7));
417 }
418};
419
420// As the previous one, on a tree of Tree_Node, of one node at least: the
421// constructor demands a root
423{
425
426 static M make(const size_t n) { return M(node_tree_of(std::max<size_t>(n, 1)).nodes(0)); }
427
428 static bool is_empty(const M & m) { return m.is_empty() and m.size() == 0; }
429
430 static void use(M & m)
431 {
432 const Test_Node_Tree & t = node_tree_of(m.size());
433 Array<Tnode *> roots;
435 for (size_t i = 0; i < m.size(); i += 1 + m.size() / 8)
436 {
437 roots.append(t.nodes(i));
438 pairs.append(std::make_pair(t.nodes(i), t.nodes(m.size() - 1)));
439 }
440 EXPECT_EQ(m.subtree_solve(roots).size(), roots.size());
441 EXPECT_EQ(m.path_solve(pairs).size(), pairs.size());
442 EXPECT_TRUE(m.is_empty() or m.subtree_solve({t.nodes(0)})(0) == std::min<size_t>(m.size(), 7));
443 }
444};
445
446// n^2 distances: a cycle of at most 30 nodes
447template <> struct Moved_From_Case<Floyd_All_Shortest_Paths<Dgraph>>
448{
450
451 static F make(const size_t n) { return F(digraph_of(std::clamp<size_t>(n, 1, 30)).g); }
452
453 static bool is_empty(const F & f)
454 {
455 return f.get_nodes_ref().size() == 0 and f.get_dist_mat().rows() == 0
456 and f.get_path_mat().rows() == 0 and not f.has_negative_cycle();
457 }
458
459 static void use(F & f)
460 {
461 const long n = static_cast<long>(f.get_nodes_ref().size());
462 for (long i = 0; i < n; ++i)
463 for (long j = 0; j < n; ++j)
464 {
465 const Path<Dgraph> path = f.get_min_path(i, j);
466 EXPECT_EQ(path.get_first_node(), f.select_node(i));
467 EXPECT_EQ(path.size() == 1, i == j); // every node reaches the others
468 }
469 // a matrix index past the nodes is out of range, whatever their number
470 EXPECT_THROW((void) f.get_min_path(n, 0), std::out_of_range);
471 }
472};
473
474// The cycle of n nodes has n arcs: the index of n arcs is that of the cycle
475// of n nodes
476template <> struct Moved_From_Case<In_Arcs_Index<Dgraph>>
477{
479
480 static I make(const size_t n) { return I(digraph_of(n).g); }
481
482 static bool is_empty(const I & x) { return x.size() == 0; }
483
484 static void use(I & x)
485 {
486 // whatever the index, it does not find a node of another digraph
487 EXPECT_THROW((void) x.in_degree(stranger()), std::domain_error);
488
489 const Test_Digraph & d = digraph_of(x.size());
490 size_t total = 0;
491 for (size_t i = 0; i < d.nodes.size(); ++i)
492 {
493 size_t k = 0;
494 for (auto it = x.get_it(d.nodes(i)); it.has_curr(); it.next_ne(), ++k)
495 EXPECT_EQ(d.g.get_tgt_node(it.get_curr_ne()), d.nodes(i));
496 EXPECT_EQ(k, x.in_degree(d.nodes(i)));
497 total += k;
498 }
499 EXPECT_EQ(total, x.size());
500 }
501};
502
503// A square problem of at most 40 rows
504template <> struct Moved_From_Case<Hungarian_Assignment<long>>
505{
507
508 static long cost(const size_t i, const size_t j) { return static_cast<long>((i * 31 + j * 17) % 97); }
509
510 static H make(const size_t n)
511 {
512 const size_t rows = std::clamp<size_t>(n, 1, 40);
514 for (size_t i = 0; i < rows; ++i)
515 for (size_t j = 0; j < rows; ++j)
516 c.write(i, j, cost(i, j));
517 return H(c);
518 }
519
520 static bool is_empty(const H & h)
521 {
522 return h.rows() == 0 and h.cols() == 0 and h.dimension() == 0 and h.get_total_cost() == 0
523 and h.get_row_assignments().size() == 0 and h.get_col_assignments().size() == 0;
524 }
525
526 static void use(H & h)
527 {
528 long total = 0;
529 for (size_t i = 0; i < h.rows(); ++i)
530 {
531 const long j = h.get_assignment(i);
532 ASSERT_GE(j, 0);
533 EXPECT_EQ(h.get_col_assignments()[static_cast<size_t>(j)], static_cast<long>(i));
534 total += cost(i, static_cast<size_t>(j));
535 }
536 EXPECT_EQ(total, h.get_total_cost());
537 EXPECT_EQ(h.get_assignments().size(), h.rows());
538 EXPECT_THROW((void) h.get_assignment(h.rows()), std::out_of_range);
539 }
540};
541
542// At most 50 variables, under a restriction on their sum; use() adds
543// another one and solves the program, while it is not solved
544template <> struct Moved_From_Case<Simplex<double>>
545{
547
548 static void add_sum_restriction(S & s)
549 {
550 auto row = Array<double>::create(s.get_num_vars() + 1);
551 for (size_t j = 0; j < s.get_num_vars(); ++j)
552 row(j) = 1.0 + j % 2;
553 row(s.get_num_vars()) = 10.0;
554 (void) s.put_restriction(&row.base());
555 }
556
557 static S make(const size_t n)
558 {
559 S s(std::clamp<size_t>(n, 1, 50));
560 for (size_t j = 0; j < s.get_num_vars(); ++j)
561 s.put_objetive_function_coef(j, 1.0 + j % 3);
562 add_sum_restriction(s);
563 return s;
564 }
565
566 static bool is_empty(const S & s)
567 {
568 return s.get_num_vars() == 0 and s.get_num_restrictions() == 0
569 and s.get_state() == S::Not_Solved;
570 }
571
572 static void use(S & s)
573 {
574 const size_t n = s.get_num_vars();
575 for (size_t j = 0; j < n; ++j)
576 {
577 s.put_objetive_function_coef(j, 1.0 + j % 3);
578 EXPECT_EQ(s.get_objetive_function_coef(j), 1.0 + j % 3);
579 }
580 for (size_t r = 0; r < s.get_num_restrictions(); ++r)
581 for (size_t j = 0; j < n; ++j)
582 EXPECT_GT(s.get_restriction_coef(r, j), 0.0);
583 EXPECT_THROW(s.put_objetive_function_coef(n, 1.0), std::out_of_range);
584 if (s.get_state() == S::Not_Solved)
585 {
586 add_sum_restriction(s);
588 EXPECT_EQ(s.solve(), S::Solved);
589 }
590 s.load_solution();
591 EXPECT_TRUE(s.verify_solution());
592 }
593};
594
595// At most 50 variables and two constraints
596template <> struct Moved_From_Case<RevisedSimplex<double>>
597{
599
600 static S make(const size_t n) { return S(std::clamp<size_t>(n, 1, 50), 2); }
601
602 static bool is_empty(const S & s)
603 {
604 return s.get_num_vars() == 0 and s.get_num_constraints() == 0
605 and s.get_state() == S::Not_Solved;
606 }
607
608 static void use(S & s)
609 {
610 const size_t n = s.get_num_vars();
611 for (size_t j = 0; j < n; ++j)
612 s.set_objective(j, 1.0 + j % 3);
613 for (size_t i = 0; i < s.get_num_constraints(); ++i)
614 {
615 for (size_t j = 0; j < n; ++j)
616 s.set_constraint(i, j, 1.0 + (i + j) % 2);
617 s.set_rhs(i, 10.0);
618 }
619 EXPECT_THROW(s.set_objective(n, 1.0), std::out_of_range);
620 // solve() runs once: a second use finds the program solved
621 EXPECT_EQ(s.get_state() == S::Not_Solved ? s.solve() : s.get_state(), S::Solved);
622 for (size_t j = 0; j < n; ++j)
623 EXPECT_GE(s.get_solution(j), 0.0);
624 EXPECT_TRUE(s.verify_solution());
625 }
626};
627
628using Graphs = ::testing::Types<Binary_Lifting_LCA<Graph>, Euler_RMQ_LCA<Graph>,
Floyd-Warshall algorithm for all-pairs shortest paths.
Heavy-Light Decomposition for tree path queries on Aleph graphs.
Hungarian (Kuhn-Munkres) algorithm for the optimal assignment problem.
Lowest Common Ancestor (LCA) on rooted trees represented as Aleph graphs.
Simplex algorithm for linear programming.
Generic tree dynamic programming and rerooting algorithms.
Heavy-Light and Centroid decomposition on rooted trees represented as Aleph graphs.
long double h
Definition btreepic.C:154
size_t row
Definition ca-c-api.h:115
size_t * rows
Definition ca-c-api.h:112
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
size_t size() const noexcept
Return the current dimension of array.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Dynamic matrix with sparse storage.
Definition tpl_dynMat.H:122
T & write(const size_t i, const size_t j, const T &data)
Write a value to position (i, j).
Definition tpl_dynMat.H:514
constexpr size_t rows() const noexcept
Get the number of rows.
Definition tpl_dynMat.H:441
Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g ...
Path< GT > get_min_path(const long src_idx, const long tgt_idx) const
Reconstruct the shortest path between two nodes by matrix indices.
const DynMatrix< Distance_Type > & get_dist_mat() const noexcept
Get the distance matrix.
Node * select_node(long i) const noexcept
Get the graph node at a given matrix index.
constexpr bool has_negative_cycle() const noexcept
Check if the graph contains a negative cycle.
const DynArray< Node * > & get_nodes_ref() const noexcept
Get the array of graph nodes as a constant reference.
const DynMatrix< long > & get_path_mat() const noexcept
Get the path reconstruction matrix.
LCA via binary lifting on a rooted tree.
Definition LCA.H:539
Centroid decomposition over a rooted tree represented as an Aleph graph.
Node * centroid_root() const noexcept
Root centroid node of the centroid tree (or nullptr if empty).
bool is_centroid_ancestor_id(const size_t ancestor, const size_t node) const
True iff ancestor is in the centroid-ancestor chain of node.
size_t centroid_root_id() const noexcept
Root centroid ID of the centroid tree (or NONE if empty).
const Array< size_t > & centroid_ancestors_of_id(const size_t id) const
Centroid ancestors of node ID from centroid root down to local centroid.
size_t centroid_path_length_of_id(const size_t id) const
Number of centroid ancestors stored for node ID.
size_t max_centroid_level() const noexcept
Maximum centroid-tree level.
size_t distance_to_centroid_id(const size_t node, const size_t centroid) const
Distance from node ID to a centroid ancestor ID.
void for_each_centroid_ancestor_id(const size_t node, F &&f) const
Visit each centroid ancestor of node ID.
LCA via Euler tour + RMQ on depth in a rooted tree.
Definition LCA.H:857
Segment-tree-powered path/subtree queries over an HLD layout.
const Gen_Heavy_Light_Decomposition< GT, SA > & decomposition() const noexcept
T query_path_id(const size_t u, const size_t v) const
Path query between two node IDs in O(log^2 n).
T get_node_id(const size_t id) const
Return current value at node ID.
size_t size() const noexcept
T query_subtree_id(const size_t id) const
Subtree query for node ID in O(log n).
void update_node_id(const size_t id, const T &delta)
Point update: a[id] = Op(a[id], delta).
bool is_empty() const noexcept
Heavy-Light Decomposition over a rooted tree represented as an Aleph graph.
Offline subtree and path queries on N-ary trees (Tree_Node).
Offline subtree and path queries on trees via Mo's algorithm.
Generic rerooting dynamic programming (all-roots DP).
Definition Tree_DP.H:544
Generic bottom-up tree dynamic programming.
Definition Tree_DP.H:406
const Array< T > & values() const noexcept
Returns all DP values (indexed by internal node ID).
Definition Tree_DP.H:469
Node * node_of(size_t id) const
Returns the node pointer for a given internal ID.
Definition Tree_DP.H:484
size_t id_of(Node *node) const
Returns the internal ID for a given node pointer.
Definition Tree_DP.H:493
size_t size() const noexcept
Returns the number of nodes in the tree.
Definition Tree_DP.H:475
const T & value(Node *node) const
Returns the DP value for a given node.
Definition Tree_DP.H:463
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Implementation of the Hungarian (Munkres) algorithm.
Definition Hungarian.H:143
Index of the arcs that go into each node of a graph.
Definition tpl_graph.H:2224
size_t in_degree(const Node *p) const
Return the number of arcs into p that the filter showed.
Definition tpl_graph.H:2495
Iterator get_it(const Node *p) const
Return an iterator on the arcs into p that the filter showed.
Definition tpl_graph.H:2506
size_t size() const noexcept
Return the number of arcs of the index: those that the filter showed.
Definition tpl_graph.H:2488
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Node Node
The graph type.
Definition tpl_graph.H:443
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Path on a graph.
Definition tpl_graph.H:3832
size_t size() const noexcept
Return the path length in nodes.
Definition tpl_graph.H:3987
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
Definition tpl_graph.H:4316
Revised Simplex algorithm for linear programming.
Definition Simplex.H:2176
void set_objective(size_t j, T coef)
Sets objective coefficient.
Definition Simplex.H:2732
size_t get_num_vars() const noexcept
Gets number of variables.
Definition Simplex.H:2935
size_t get_num_constraints() const noexcept
Gets number of constraints.
Definition Simplex.H:2938
State get_state() const noexcept
Gets current state.
Definition Simplex.H:2929
T get_solution(size_t j) const
Gets solution value for variable j.
Definition Simplex.H:2910
void set_constraint(size_t i, size_t j, T coef)
Sets constraint coefficient.
Definition Simplex.H:2756
State solve()
Solves the linear program.
Definition Simplex.H:2813
void set_rhs(size_t i, T rhs)
Sets RHS value for a constraint.
Definition Simplex.H:2770
bool verify_solution() const
Verifies solution satisfies all constraints.
Definition Simplex.H:2948
Linear program solver using the Simplex method.
Definition Simplex.H:279
T & get_restriction_coef(const size_t rest_num, size_t idx)
Gets a specific coefficient from a constraint.
Definition Simplex.H:1256
bool verify_solution() const
Verifies that the solution satisfies all constraints.
Definition Simplex.H:1393
T * put_restriction(const T *coefs=nullptr, ConstraintType type=ConstraintType::LE)
Adds a constraint to the linear program.
Definition Simplex.H:1132
void prepare_linear_program()
Prepares the linear program for solving.
Definition Simplex.H:1287
size_t get_num_restrictions() const noexcept
Gets the number of constraints.
Definition Simplex.H:1216
void put_objetive_function_coef(size_t i, const T &coef)
Sets a coefficient in the objective function.
Definition Simplex.H:1072
void load_solution() noexcept
Loads the solution values into the solution array.
Definition Simplex.H:1337
size_t get_num_vars() const noexcept
Gets the number of decision variables.
Definition Simplex.H:1225
State get_state() const noexcept
Gets the current state of the solver.
Definition Simplex.H:1324
State solve()
Solves the linear program.
Definition Simplex.H:1310
const T & get_objetive_function_coef(size_t i) const
Gets a coefficient from the objective function.
Definition Simplex.H:1085
Forward declaration used by CRTP helpers before the full node definition.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
The typed suite of the moved-from tests.
Definition moved_from.H:115
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
constexpr bool is_empty() const noexcept
Checks if the table is empty.
Definition hashDry.H:959
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
The pattern test of the moved-from objects.
::testing::Types< Binary_Lifting_LCA< Graph >, Euler_RMQ_LCA< Graph >, HLD_Sum< Graph, int >, Heavy_Light_Decomposition< Graph >, HLD_Path_Query< Graph, int, Aleph::plus< int > >, Centroid_Decomposition< Graph >, Tree_DP< Graph, long >, Reroot_DP< Graph, long >, Distinct_Count_Mo_On_Trees< Graph >, Distinct_Count_Mo_On_Tree_Node< int >, Floyd_All_Shortest_Paths< Dgraph >, In_Arcs_Index< Dgraph >, Hungarian_Assignment< long >, Simplex< double >, RevisedSimplex< double > > Graphs
INSTANTIATE_TYPED_TEST_SUITE_P(Graphs, MovedFrom, Graphs)
FFT< double > F
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
HLD with path/subtree sum queries.
Definition HLD.H:1145
static long cost(const size_t i, const size_t j)
How the moved-from tests build, check and use objects of a class.
Definition moved_from.H:92
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
Dynamic matrix with lazy allocation.
Generic graph and digraph implementations.
Mo's algorithm on trees: offline subtree and path queries.
General tree (n-ary tree) node.
DynList< int > l