Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_contract_matrix_trees_test.cc
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
54#include <algorithm>
55#include <cstdlib>
56#include <set>
57#include <stdexcept>
58#include <type_traits>
59#include <utility>
60#include <vector>
61
62#include <gtest/gtest.h>
63
64#include <tpl_graph.H>
65#include <LCA.H>
66#include <HLD.H>
67#include <Tree_Decomposition.H>
68#include <Tree_DP.H>
69
70#include "graph_harness.H"
73
74using namespace Aleph;
75using namespace graph_harness;
76using namespace graph_contract_matrix;
77
78namespace
79{
81 template <class List>
82 std::set<int> arc_numbers(const List & l)
83 {
84 std::set<int> r;
85 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
86 r.insert(it.get_curr()->get_info());
87 return r;
88 }
89} // namespace
90
91namespace
92{
95 Spec cycle_spec()
96 {
97 Spec s;
98 s.n = 4;
99 s.arcs = {{0, 1}, {1, 2}, {0, 2}, {2, 3}};
100 return s;
101 }
102
104 Spec tree_spec()
105 {
106 Spec s;
107 s.n = 3;
108 s.arcs = {{0, 1}, {1, 2}};
109 return s;
110 }
111
113 template <class GT, class SA>
114 size_t counted(const SA & sa)
115 {
116 if constexpr (std::is_same_v<SA, graph_filters::Counting<GT>>)
117 return sa.calls == nullptr ? 0 : *sa.calls;
118 else
119 return 0;
120 }
121
130 template <class GT, class Make, class Path4, class Path3>
131 void on_trees(Make && make, Path4 && path4, Path3 && path3)
132 {
133 Built<GT> cycle(cycle_spec());
134 Built<GT> tree(tree_spec());
135
136 for_each_filter<GT>([&] (auto sa, const char * name)
137 {
138 using SA = decltype(sa);
139 SCOPED_TRACE(name);
140 for_each_constness(cycle.g, [&] (auto & g)
141 {
142 if constexpr (hides<GT, SA>)
143 {
144 const size_t before = counted<GT>(sa);
145 path4(make(g, cycle.nodes, sa), cycle.nodes);
146 if constexpr (std::is_same_v<SA, graph_filters::Counting<GT>>)
147 {
148 EXPECT_GT(counted<GT>(sa), before) << "the filter given is not the one used";
149 }
150 }
151 else
152 {
153 EXPECT_THROW_OR_FILTER(make(g, cycle.nodes, sa), std::domain_error);
154 }
155 });
156 for_each_constness(tree.g, [&] (auto & g)
157 {
158 path3(make(g, tree.nodes, sa), tree.nodes);
159 });
160 });
161 }
162
164 template <class A>
165 std::vector<size_t> sorted(const A & a)
166 {
167 std::vector<size_t> r;
168 for (size_t i = 0; i < a.size(); ++i)
169 r.push_back(a(i));
170 std::sort(r.begin(), r.end());
171 return r;
172 }
173
175 template <class GT>
176 int node_value(typename GT::Node * p)
177 {
178 return p->get_info();
179 }
180
183 template <class T, class Nodes>
184 void expect_lca_on_path4(const T & t, const Nodes & n)
185 {
186 EXPECT_EQ(t.size(), 4U);
187 EXPECT_FALSE(t.is_empty());
188 EXPECT_EQ(t.root(), n[0]);
189 EXPECT_EQ(t.node_of(t.root_id()), n[0]);
190 EXPECT_EQ(t.depth_of(n[3]), 3U);
191 EXPECT_EQ(t.depth_of_id(t.id_of(n[2])), 2U);
192 EXPECT_EQ(t.parent_of(n[2]), n[1]);
193 EXPECT_EQ(t.parent_id(t.id_of(n[1])), t.id_of(n[0]));
194 EXPECT_TRUE(t.is_ancestor(n[1], n[3]));
195 EXPECT_FALSE(t.is_ancestor_id(t.id_of(n[3]), t.id_of(n[1])));
196 EXPECT_EQ(t.lca(n[1], n[3]), n[1]);
197 EXPECT_EQ(t.lca_id(t.id_of(n[2]), t.id_of(n[3])), t.id_of(n[2]));
198 EXPECT_EQ(t.distance(n[0], n[3]), 3U);
199 EXPECT_EQ(t.distance_id(t.id_of(n[1]), t.id_of(n[3])), 2U);
200 }
201} // namespace
202
203// ---------------------------------------------------------------------
204// LCA.H
205// ---------------------------------------------------------------------
206
207template <class GT>
208class ContractTreesLca : public ::testing::Test {};
209
211
213{
214 using GT = TypeParam;
215 on_trees<GT>([] (auto & g, const auto & n, auto sa)
216 {
217 using SA = decltype(sa);
218 // The root is the first node, which follows the addresses in
219 // List_SGraph: only its size is known
220 EXPECT_EQ((Binary_Lifting_LCA<GT, SA>(g, sa).size()), g.get_num_nodes());
221 return Gen_Binary_Lifting_LCA<GT, SA>(g, n[0], sa);
222 },
223 [] (const auto & t, const auto & n)
224 {
225 expect_lca_on_path4(t, n);
226 EXPECT_GE(t.num_levels(), 2U);
227 EXPECT_EQ(t.kth_ancestor(n[3], 2), n[1]);
228 EXPECT_EQ(t.kth_ancestor(n[3], 4), nullptr);
229 EXPECT_EQ(t.kth_ancestor_id(t.id_of(n[2]), 1), t.id_of(n[1]));
230 },
231 [] (const auto & t, const auto & n)
232 {
233 EXPECT_EQ(t.distance(n[0], n[2]), 2U);
234 EXPECT_EQ(t.lca(n[1], n[2]), n[1]);
235 });
236}
237
239{
240 using GT = TypeParam;
241 on_trees<GT>([] (auto & g, const auto & n, auto sa)
242 {
243 using SA = decltype(sa);
244 EXPECT_EQ((Euler_RMQ_LCA<GT, SA>(g, sa).size()), g.get_num_nodes());
245 return Gen_Euler_RMQ_LCA<GT, SA>(g, n[0], sa);
246 },
247 [] (const auto & t, const auto & n)
248 {
249 expect_lca_on_path4(t, n);
250 EXPECT_EQ(t.euler_tour_size(), 7U);
251 EXPECT_EQ(t.euler_tour().size(), 7U);
252 },
253 [] (const auto & t, const auto & n)
254 {
255 EXPECT_EQ(t.distance(n[0], n[2]), 2U);
256 EXPECT_EQ(t.lca(n[1], n[2]), n[1]);
257 });
258}
259
260// ---------------------------------------------------------------------
261// HLD.H: the values of the nodes are their numbers
262// ---------------------------------------------------------------------
263
264template <class GT>
265class ContractTreesHld : public ::testing::Test {};
266
268
270{
271 using GT = TypeParam;
272 using Sum = Aleph::plus<int>;
273 on_trees<GT>([] (auto & g, const auto & n, auto sa)
274 {
275 using SA = decltype(sa);
276 EXPECT_EQ((Gen_HLD<GT, int, Sum, SA>(g, node_value<GT>, 0, Sum(), sa).size()),
277 g.get_num_nodes());
278 return Gen_HLD<GT, int, Sum, SA>(g, n[0], node_value<GT>, 0, Sum(), sa);
279 },
280 [] (auto && t, const auto & n)
281 {
282 EXPECT_EQ(t.size(), 4U);
283 EXPECT_FALSE(t.is_empty());
284 EXPECT_EQ(t.root(), n[0]);
285 EXPECT_EQ(t.node_of(t.root_id()), n[0]);
286 EXPECT_EQ(t.position(t.id_of(n[0])), 0U);
287 EXPECT_EQ(t.chain_head_id(t.id_of(n[3])), t.id_of(n[0]));
288 EXPECT_EQ(t.num_chains(), 1U);
289 EXPECT_EQ(t.depth_of(n[3]), 3U);
290 EXPECT_EQ(t.depth_of_id(t.id_of(n[1])), 1U);
291 EXPECT_EQ(t.subtree_size_of(n[1]), 3U);
292 EXPECT_EQ(t.subtree_size_of_id(t.id_of(n[3])), 1U);
293 EXPECT_EQ(t.parent_of(n[3]), n[2]);
294 EXPECT_EQ(t.parent_id(t.id_of(n[1])), t.id_of(n[0]));
295 EXPECT_EQ(t.path_query(n[0], n[3]), 6);
296 EXPECT_EQ(t.path_query_id(t.id_of(n[1]), t.id_of(n[2])), 3);
297 EXPECT_EQ(t.path_query_edges(n[1], n[3]), 5);
298 EXPECT_EQ(t.path_query_edges_id(t.id_of(n[0]), t.id_of(n[3])), 6);
299 EXPECT_EQ(t.subtree_query(n[2]), 5);
300 EXPECT_EQ(t.subtree_query_id(t.id_of(n[0])), 6);
301 EXPECT_EQ(t.lca(n[1], n[3]), n[1]);
302 EXPECT_EQ(t.lca_id(t.id_of(n[2]), t.id_of(n[3])), t.id_of(n[2]));
303 EXPECT_EQ(t.distance(n[0], n[3]), 3U);
304 EXPECT_EQ(t.distance_id(t.id_of(n[1]), t.id_of(n[3])), 2U);
305 EXPECT_EQ(t.position_array().size(), 4U);
306 EXPECT_EQ(t.chain_head_array().size(), 4U);
307 EXPECT_EQ(t.depth_array().size(), 4U);
308 EXPECT_EQ(t.subtree_size_array().size(), 4U);
309 EXPECT_EQ(t.parent_array().size(), 4U);
310 EXPECT_EQ(t.get_value(n[2]), 2);
311 EXPECT_EQ(t.get_value_at_id(t.id_of(n[3])), 3);
312 t.point_update(n[3], 10);
313 EXPECT_EQ(t.path_query(n[0], n[3]), 13);
314 t.point_update_id(t.id_of(n[0]), 5);
315 EXPECT_EQ(t.path_query(n[0], n[3]), 18);
316 },
317 [] (auto && t, const auto & n)
318 {
319 EXPECT_EQ(t.path_query(n[0], n[2]), 3);
320 EXPECT_EQ(t.distance(n[0], n[2]), 2U);
321 });
322}
323
325{
326 using GT = TypeParam;
327 on_trees<GT>([] (auto & g, const auto & n, auto sa)
328 {
329 using SA = decltype(sa);
330 EXPECT_EQ((HLD_Sum<GT, int, SA>(g, node_value<GT>, sa).size()), g.get_num_nodes());
331 return HLD_Sum<GT, int, SA>(g, n[0], node_value<GT>, sa);
332 },
333 [] (const auto & t, const auto & n)
334 {
335 EXPECT_EQ(t.path_query(n[0], n[3]), 6);
336 EXPECT_EQ(t.subtree_query(n[2]), 5);
337 },
338 [] (const auto & t, const auto & n)
339 {
340 EXPECT_EQ(t.path_query(n[0], n[2]), 3);
341 });
342
343 on_trees<GT>([] (auto & g, const auto & n, auto sa)
344 {
345 using SA = decltype(sa);
346 EXPECT_EQ((HLD_Max<GT, int, SA>(g, node_value<GT>, sa).size()), g.get_num_nodes());
347 return HLD_Max<GT, int, SA>(g, n[0], node_value<GT>, sa);
348 },
349 [] (const auto & t, const auto & n)
350 {
351 EXPECT_EQ(t.path_query(n[0], n[3]), 3);
352 EXPECT_EQ(t.subtree_query(n[2]), 3);
353 },
354 [] (const auto & t, const auto & n)
355 {
356 EXPECT_EQ(t.path_query(n[0], n[2]), 2);
357 });
358
359 on_trees<GT>([] (auto & g, const auto & n, auto sa)
360 {
361 using SA = decltype(sa);
362 EXPECT_EQ((HLD_Min<GT, int, SA>(g, node_value<GT>, sa).size()), g.get_num_nodes());
363 return HLD_Min<GT, int, SA>(g, n[0], node_value<GT>, sa);
364 },
365 [] (const auto & t, const auto & n)
366 {
367 EXPECT_EQ(t.path_query(n[1], n[3]), 1);
368 EXPECT_EQ(t.subtree_query(n[2]), 2);
369 },
370 [] (const auto & t, const auto & n)
371 {
372 EXPECT_EQ(t.path_query(n[1], n[2]), 1);
373 });
374}
375
376// ---------------------------------------------------------------------
377// Tree_Decomposition.H
378// ---------------------------------------------------------------------
379
380template <class GT>
381class ContractTreesDecomposition : public ::testing::Test {};
382
384
386{
387 using GT = TypeParam;
388 on_trees<GT>([] (auto & g, const auto & n, auto sa)
389 {
390 using SA = decltype(sa);
391 EXPECT_EQ((Heavy_Light_Decomposition<GT, SA>(g, sa).size()), g.get_num_nodes());
392 return Gen_Heavy_Light_Decomposition<GT, SA>(g, n[0], sa);
393 },
394 [] (const auto & t, const auto & n)
395 {
396 EXPECT_EQ(t.size(), 4U);
397 EXPECT_FALSE(t.is_empty());
398 EXPECT_EQ(t.root(), n[0]);
399 EXPECT_EQ(t.node_of(t.root_id()), n[0]);
400 EXPECT_EQ(t.parent_of(n[3]), n[2]);
401 EXPECT_EQ(t.parent_id(t.id_of(n[1])), t.id_of(n[0]));
402 EXPECT_EQ(t.depth_of(n[3]), 3U);
403 EXPECT_EQ(t.depth_of_id(t.id_of(n[1])), 1U);
404 EXPECT_EQ(t.subtree_size_of(n[1]), 3U);
405 EXPECT_EQ(t.subtree_size_of_id(t.id_of(n[3])), 1U);
406 EXPECT_EQ(t.heavy_child(n[0]), n[1]);
407 EXPECT_EQ(t.heavy_child(n[3]), nullptr);
408 EXPECT_EQ(t.heavy_child_id(t.id_of(n[1])), t.id_of(n[2]));
409 EXPECT_EQ(t.head_of(n[3]), n[0]);
410 EXPECT_EQ(t.head_id(t.id_of(n[2])), t.id_of(n[0]));
411 EXPECT_EQ(t.position_of(n[0]), 0U);
412 EXPECT_EQ(t.position_of_id(t.id_of(n[3])), 3U);
413 const auto r = t.subtree_range(n[2]);
414 EXPECT_EQ(r.left, 2U);
415 EXPECT_EQ(r.right, 3U);
416 EXPECT_EQ(t.subtree_range_id(t.id_of(n[0])).right, 3U);
417 EXPECT_TRUE(t.is_ancestor(n[1], n[3]));
418 EXPECT_FALSE(t.is_ancestor_id(t.id_of(n[3]), t.id_of(n[1])));
419 EXPECT_EQ(t.lca(n[1], n[3]), n[1]);
420 EXPECT_EQ(t.lca_id(t.id_of(n[2]), t.id_of(n[3])), t.id_of(n[2]));
421 EXPECT_EQ(t.distance(n[0], n[3]), 3U);
422 EXPECT_EQ(t.distance_id(t.id_of(n[1]), t.id_of(n[3])), 2U);
423 // The path is one heavy chain, so that each query has one segment
424 size_t segments = 0;
425 t.for_each_path_segment(n[0], n[3], [&] (size_t, size_t, bool) { ++segments; });
426 t.for_each_path_segment_id(t.id_of(n[3]), t.id_of(n[1]),
427 [&] (size_t, size_t, bool) { ++segments; });
428 t.for_each_path_segment_undirected_id(t.id_of(n[1]), t.id_of(n[2]),
429 [&] (size_t, size_t) { ++segments; });
430 EXPECT_EQ(segments, 3U);
431 },
432 [] (const auto & t, const auto & n)
433 {
434 EXPECT_EQ(t.distance(n[0], n[2]), 2U);
435 EXPECT_EQ(t.lca(n[1], n[2]), n[1]);
436 });
437}
438
440{
441 using GT = TypeParam;
442 using Sum = Aleph::plus<int>;
443 on_trees<GT>([] (auto & g, const auto & n, auto sa)
444 {
445 using SA = decltype(sa);
447 EXPECT_EQ(Q(g, 0, Sum(), sa).size(), g.get_num_nodes());
448 EXPECT_EQ(Q(g, node_value<GT>, 0, Sum(), sa).size(), g.get_num_nodes());
449 EXPECT_EQ((HLD_Path_Query<GT, int, Sum, SA>(g, n[0], node_value<GT>, 0, Sum(), sa)
450 .query_subtree(n[0])),
451 int(g.get_num_nodes() * (g.get_num_nodes() - 1) / 2));
452 return Q(g, n[0], 0, Sum(), sa);
453 },
454 [] (auto && t, const auto & n)
455 {
456 EXPECT_EQ(t.size(), 4U);
457 EXPECT_FALSE(t.is_empty());
458 EXPECT_EQ(t.decomposition().size(), 4U);
459 EXPECT_EQ(t.query_path(n[0], n[3]), 6);
460 EXPECT_EQ(t.query_path_id(t.decomposition().id_of(n[1]),
461 t.decomposition().id_of(n[2])), 3);
462 EXPECT_EQ(t.query_subtree(n[2]), 5);
463 EXPECT_EQ(t.query_subtree_id(t.decomposition().id_of(n[0])), 6);
464 EXPECT_EQ(t.get_node(n[3]), 3);
465 t.update_node(n[3], 1);
466 EXPECT_EQ(t.get_node(n[3]), 4);
467 t.update_node_id(t.decomposition().id_of(n[0]), 1);
468 EXPECT_EQ(t.get_node_id(t.decomposition().id_of(n[0])), 1);
469 t.set_node(n[3], 0);
470 t.set_node_id(t.decomposition().id_of(n[0]), 0);
471 EXPECT_EQ(t.query_path(n[0], n[3]), 3);
472 },
473 [] (auto && t, const auto & n)
474 {
475 EXPECT_EQ(t.query_path(n[0], n[2]), 3);
476 });
477}
478
480{
481 using GT = TypeParam;
482 on_trees<GT>([] (auto & g, const auto & n, auto sa)
483 {
484 using SA = decltype(sa);
485 EXPECT_EQ((Centroid_Decomposition<GT, SA>(g, sa).size()), g.get_num_nodes());
486 return Gen_Centroid_Decomposition<GT, SA>(g, n[0], sa);
487 },
488 [] (const auto & t, const auto & n)
489 {
490 EXPECT_EQ(t.size(), 4U);
491 EXPECT_FALSE(t.is_empty());
492 EXPECT_EQ(t.root(), n[0]);
493 EXPECT_EQ(t.node_of(t.root_id()), n[0]);
494 // The centroids of the path 0 - 1 - 2 - 3 are 1 and 2
495 auto * c = t.centroid_root();
496 ASSERT_TRUE(c == n[1] or c == n[2]);
497 const size_t cid = t.centroid_root_id();
498 EXPECT_EQ(t.id_of(c), cid);
499 EXPECT_EQ(t.centroid_parent(c), nullptr);
500 EXPECT_EQ(t.centroid_level_of(c), 0U);
501 EXPECT_EQ(t.centroid_level_of_id(cid), 0U);
502 EXPECT_GE(t.max_centroid_level(), 1U);
503 for (size_t i = 0; i < 4; ++i)
504 {
505 auto * u = n[i];
506 const size_t uid = t.id_of(u);
507 const auto & chain = t.centroid_ancestors_of(u);
508 const auto & dists = t.centroid_distances_of(u);
509 ASSERT_GE(chain.size(), 1U);
510 EXPECT_EQ(chain(0), cid);
511 EXPECT_EQ(chain(chain.size() - 1), uid);
512 EXPECT_EQ(dists(dists.size() - 1), 0U);
513 EXPECT_EQ(t.centroid_path_length_of(u), chain.size());
514 EXPECT_EQ(t.centroid_path_length_of_id(uid), chain.size());
515 EXPECT_EQ(t.centroid_ancestors_of_id(uid).size(), chain.size());
516 EXPECT_EQ(t.centroid_distances_of_id(uid).size(), chain.size());
517 EXPECT_TRUE(t.is_centroid_ancestor_id(cid, uid));
518 const int d = std::abs(int(i) - c->get_info());
519 EXPECT_EQ(t.distance_to_centroid_id(uid, cid), size_t(d));
520 size_t visits = 0;
521 t.for_each_centroid_ancestor(u, [&] (size_t, size_t, size_t) { ++visits; });
522 t.for_each_centroid_ancestor_id(uid, [&] (size_t, size_t, size_t) { ++visits; });
523 EXPECT_EQ(visits, 2 * chain.size());
524 if (u != c)
525 {
526 EXPECT_NE(t.centroid_parent(u), nullptr);
527 EXPECT_NE(t.centroid_parent_id(uid), uid);
528 }
529 }
530 },
531 [] (const auto & t, const auto & n)
532 {
533 EXPECT_EQ(t.centroid_root(), n[1]);
534 });
535}
536
537// ---------------------------------------------------------------------
538// Tree_DP.H
539// ---------------------------------------------------------------------
540
541template <class GT>
542class ContractTreesDp : public ::testing::Test {};
543
545
546namespace
547{
549 template <class GT>
550 size_t one(typename GT::Node *) { return 1; }
551
553 template <class GT>
554 size_t add_child(typename GT::Node *, const size_t & acc, typename GT::Node *,
555 const size_t & child)
556 {
557 return acc + child;
558 }
559
562 template <class GT>
563 size_t zero(typename GT::Node *) { return 0; }
564
565 size_t larger(const size_t & a, const size_t & b) { return std::max(a, b); }
566
567 template <class GT>
568 size_t one_more(typename GT::Node *, typename GT::Node *, const size_t & v) { return v + 1; }
569} // namespace
570
572{
573 using GT = TypeParam;
574 on_trees<GT>([] (auto & g, const auto & n, auto sa)
575 {
576 using SA = decltype(sa);
577 EXPECT_EQ((Tree_DP<GT, size_t, SA>(g, n[0], one<GT>, add_child<GT>, sa).size()),
578 g.get_num_nodes());
579 return Gen_Tree_DP<GT, size_t, SA>(g, n[0], one<GT>, add_child<GT>, sa);
580 },
581 [] (const auto & t, const auto & n)
582 {
583 EXPECT_EQ(t.size(), 4U);
584 EXPECT_EQ(t.value(n[0]), 4U);
585 EXPECT_EQ(t.value(n[3]), 1U);
586 EXPECT_EQ(t.values().size(), 4U);
587 EXPECT_EQ(t.node_of(t.id_of(n[2])), n[2]);
588 },
589 [] (const auto & t, const auto & n)
590 {
591 EXPECT_EQ(t.value(n[0]), 3U);
592 });
593}
594
596{
597 using GT = TypeParam;
598 on_trees<GT>([] (auto & g, const auto & n, auto sa)
599 {
600 using SA = decltype(sa);
601 EXPECT_EQ((Reroot_DP<GT, size_t, SA>(g, n[0], size_t(0), zero<GT>, larger,
602 one_more<GT>, sa).size()),
603 g.get_num_nodes());
604 return Gen_Reroot_DP<GT, size_t, SA>(g, n[0], size_t(0), zero<GT>, larger,
605 one_more<GT>, sa);
606 },
607 [] (const auto & t, const auto & n)
608 {
609 EXPECT_EQ(t.size(), 4U);
610 EXPECT_EQ(t.value(n[0]), 3U);
611 EXPECT_EQ(t.value(n[1]), 2U);
612 EXPECT_EQ(t.value(n[2]), 2U);
613 EXPECT_EQ(t.value(n[3]), 3U);
614 EXPECT_EQ(t.values().size(), 4U);
615 EXPECT_EQ(t.node_of(t.id_of(n[2])), n[2]);
616 },
617 [] (const auto & t, const auto & n)
618 {
619 EXPECT_EQ(t.value(n[1]), 1U);
620 });
621}
622
624{
625 using GT = TypeParam;
626 using V = std::vector<size_t>;
627
628 // An object that holds the three answers, so that on_trees() checks them
629 struct Answers
630 {
631 Array<size_t> sizes, eccentricities, sums;
632 };
633
634 on_trees<GT>([] (auto & g, const auto & n, auto sa)
635 {
636 return Answers{tree_subtree_sizes(g, n[0], sa), tree_max_distance(g, n[0], sa),
637 tree_sum_of_distances(g, n[0], sa)};
638 },
639 [] (const Answers & a, const auto &)
640 {
641 EXPECT_EQ(sorted(a.sizes), (V{1, 2, 3, 4}));
642 EXPECT_EQ(sorted(a.eccentricities), (V{2, 2, 3, 3}));
643 EXPECT_EQ(sorted(a.sums), (V{4, 4, 6, 6}));
644 },
645 [] (const Answers & a, const auto &)
646 {
647 EXPECT_EQ(sorted(a.sizes), (V{1, 2, 3}));
648 EXPECT_EQ(sorted(a.eccentricities), (V{1, 2, 2}));
649 EXPECT_EQ(sorted(a.sums), (V{2, 3, 3}));
650 });
651}
Heavy-Light Decomposition for tree path queries on Aleph graphs.
Lowest Common Ancestor (LCA) on rooted trees represented as Aleph graphs.
Generic tree dynamic programming and rerooting algorithms.
Heavy-Light and Centroid decomposition on rooted trees represented as Aleph graphs.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
LCA via binary lifting on a rooted tree.
Definition LCA.H:539
Centroid decomposition over a rooted tree represented as an Aleph graph.
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.
Heavy-Light Decomposition with segment tree for path/subtree queries.
Definition HLD.H:624
Heavy-Light Decomposition over a rooted tree represented as an Aleph graph.
Generic rerooting dynamic programming (all-roots DP).
Definition Tree_DP.H:544
Generic bottom-up tree dynamic programming.
Definition Tree_DP.H:406
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
static Tree_Node< int > * chain(size_t n)
What the files of the contract matrix of the graph templates, graph_contract_matrix_*_test....
TYPED_TEST(ContractTreesLca, BinaryLifting)
TYPED_TEST_SUITE(ContractTreesLca, Undirected_Kinds)
The kinds of arc filter and of distance that the graph algorithms must accept.
Every small multigraph, on every kind of graph, for the tests.
Main namespace for Aleph-w library functions.
Array< size_t > tree_subtree_sizes(const GT &g, typename GT::Node *root, SA sa=SA())
Compute subtree sizes for every node.
Definition Tree_DP.H:732
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
Array< size_t > tree_max_distance(const GT &g, typename GT::Node *root, SA sa=SA())
Compute the maximum distance from each node to any leaf.
Definition Tree_DP.H:767
Array< size_t > tree_sum_of_distances(const GT &g, typename GT::Node *root, SA sa=SA())
Compute sum of distances from each node to all others.
Definition Tree_DP.H:805
void for_each_constness(GT &g, F &&f)
Calls f(g) with g as it is and as a const graph.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
HLD with path/subtree max queries.
Definition HLD.H:1172
HLD with path/subtree min queries.
Definition HLD.H:1199
HLD with path/subtree sum queries.
Definition HLD.H:1145
A graph made from a spec, with its nodes and arcs by number.
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
size_t V
Gsl_Compatible_Rng r
Generic graph and digraph implementations.
DynList< int > l