62#include <gtest/gtest.h>
82 std::set<int> arc_numbers(
const List &
l)
85 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
86 r.insert(it.get_curr()->get_info());
99 s.
arcs = {{0, 1}, {1, 2}, {0, 2}, {2, 3}};
108 s.
arcs = {{0, 1}, {1, 2}};
113 template <
class GT,
class SA>
114 size_t counted(
const SA & sa)
116 if constexpr (std::is_same_v<SA, graph_filters::Counting<GT>>)
117 return sa.calls ==
nullptr ? 0 : *sa.calls;
130 template <
class GT,
class Make,
class Path4,
class Path3>
131 void on_trees(Make && make, Path4 && path4, Path3 && path3)
136 for_each_filter<GT>([&] (
auto sa,
const char * name)
138 using SA =
decltype(sa);
142 if constexpr (hides<GT, SA>)
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>>)
148 EXPECT_GT(counted<GT>(sa), before) <<
"the filter given is not the one used";
153 EXPECT_THROW_OR_FILTER(make(g, cycle.nodes, sa), std::domain_error);
158 path3(make(g, tree.nodes, sa), tree.nodes);
165 std::vector<size_t> sorted(
const A & a)
167 std::vector<size_t>
r;
168 for (
size_t i = 0; i < a.size(); ++i)
170 std::sort(
r.begin(),
r.end());
176 int node_value(
typename GT::Node * p)
183 template <
class T,
class Nodes>
184 void expect_lca_on_path4(
const T & t,
const Nodes & n)
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);
214 using GT = TypeParam;
215 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
217 using SA =
decltype(sa);
223 [] (
const auto & t,
const auto & n)
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]));
231 [] (
const auto & t,
const auto & n)
233 EXPECT_EQ(t.distance(n[0], n[2]), 2U);
234 EXPECT_EQ(t.lca(n[1], n[2]), n[1]);
240 using GT = TypeParam;
241 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
243 using SA =
decltype(sa);
247 [] (
const auto & t,
const auto & n)
249 expect_lca_on_path4(t, n);
250 EXPECT_EQ(t.euler_tour_size(), 7U);
251 EXPECT_EQ(t.euler_tour().size(), 7U);
253 [] (
const auto & t,
const auto & n)
255 EXPECT_EQ(t.distance(n[0], n[2]), 2U);
256 EXPECT_EQ(t.lca(n[1], n[2]), n[1]);
271 using GT = TypeParam;
273 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
275 using SA =
decltype(sa);
280 [] (
auto && t,
const auto & n)
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);
317 [] (
auto && t,
const auto & n)
319 EXPECT_EQ(t.path_query(n[0], n[2]), 3);
320 EXPECT_EQ(t.distance(n[0], n[2]), 2U);
326 using GT = TypeParam;
327 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
329 using SA =
decltype(sa);
333 [] (
const auto & t,
const auto & n)
335 EXPECT_EQ(t.path_query(n[0], n[3]), 6);
336 EXPECT_EQ(t.subtree_query(n[2]), 5);
338 [] (
const auto & t,
const auto & n)
340 EXPECT_EQ(t.path_query(n[0], n[2]), 3);
343 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
345 using SA =
decltype(sa);
349 [] (
const auto & t,
const auto & n)
351 EXPECT_EQ(t.path_query(n[0], n[3]), 3);
352 EXPECT_EQ(t.subtree_query(n[2]), 3);
354 [] (
const auto & t,
const auto & n)
356 EXPECT_EQ(t.path_query(n[0], n[2]), 2);
359 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
361 using SA =
decltype(sa);
365 [] (
const auto & t,
const auto & n)
367 EXPECT_EQ(t.path_query(n[1], n[3]), 1);
368 EXPECT_EQ(t.subtree_query(n[2]), 2);
370 [] (
const auto & t,
const auto & n)
372 EXPECT_EQ(t.path_query(n[1], n[2]), 1);
387 using GT = TypeParam;
388 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
390 using SA =
decltype(sa);
394 [] (
const auto & t,
const auto & n)
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);
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);
432 [] (
const auto & t,
const auto & n)
434 EXPECT_EQ(t.distance(n[0], n[2]), 2U);
435 EXPECT_EQ(t.lca(n[1], n[2]), n[1]);
441 using GT = TypeParam;
443 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
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());
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);
454 [] (
auto && t,
const auto & n)
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);
470 t.set_node_id(t.decomposition().id_of(n[0]), 0);
471 EXPECT_EQ(t.query_path(n[0], n[3]), 3);
473 [] (
auto && t,
const auto & n)
475 EXPECT_EQ(t.query_path(n[0], n[2]), 3);
481 using GT = TypeParam;
482 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
484 using SA =
decltype(sa);
488 [] (
const auto & t,
const auto & n)
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]);
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)
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);
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));
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());
526 EXPECT_NE(t.centroid_parent(u),
nullptr);
527 EXPECT_NE(t.centroid_parent_id(uid), uid);
531 [] (
const auto & t,
const auto & n)
533 EXPECT_EQ(t.centroid_root(), n[1]);
550 size_t one(
typename GT::Node *) {
return 1; }
554 size_t add_child(
typename GT::Node *,
const size_t & acc,
typename GT::Node *,
555 const size_t & child)
563 size_t zero(
typename GT::Node *) {
return 0; }
565 size_t larger(
const size_t & a,
const size_t & b) {
return std::max(a, b); }
568 size_t one_more(
typename GT::Node *,
typename GT::Node *,
const size_t & v) {
return v + 1; }
573 using GT = TypeParam;
574 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
576 using SA =
decltype(sa);
581 [] (
const auto & t,
const auto & n)
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]);
589 [] (
const auto & t,
const auto & n)
591 EXPECT_EQ(t.value(n[0]), 3U);
597 using GT = TypeParam;
598 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
600 using SA =
decltype(sa);
602 one_more<GT>, sa).
size()),
607 [] (
const auto & t,
const auto & n)
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]);
617 [] (
const auto & t,
const auto & n)
619 EXPECT_EQ(t.value(n[1]), 1U);
625 using GT = TypeParam;
626 using V = std::vector<size_t>;
634 on_trees<GT>([] (
auto & g,
const auto & n,
auto sa)
639 [] (
const Answers & a,
const auto &)
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}));
645 [] (
const Answers & a,
const auto &)
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}));
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.
Node belonging to a double circular linked list with header node.
LCA via binary lifting on a rooted tree.
Centroid decomposition over a rooted tree represented as an Aleph graph.
LCA via Euler tour + RMQ on depth in a rooted tree.
Segment-tree-powered path/subtree queries over an HLD layout.
Heavy-Light Decomposition with segment tree for path/subtree queries.
Heavy-Light Decomposition over a rooted tree represented as an Aleph graph.
Generic rerooting dynamic programming (all-roots DP).
Generic bottom-up tree dynamic programming.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
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.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::decay_t< typename HeadC::Item_Type > T
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.
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.
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.
HLD with path/subtree min queries.
HLD with path/subtree sum queries.
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
Generic graph and digraph implementations.