39# include <gtest/gtest.h>
54using namespace testing;
63 compute_parents(
const GT & g,
typename GT::Node * tree_root)
67 parent.
insert(tree_root,
nullptr);
69 stk.
push({tree_root,
nullptr});
72 auto [cur, par] = stk.
pop();
73 for (
auto it =
typename GT::Node_Arc_Iterator(cur);
74 it.has_curr(); it.next_ne())
76 auto * a = it.get_curr();
99 stk.
push(subtree_root);
102 auto * cur = stk.
pop();
103 seen.
insert(cur->get_info());
104 for (
auto it =
typename GT::Node_Arc_Iterator(cur);
105 it.has_curr(); it.next_ne())
107 auto * a = it.get_curr();
109 if (nb != parent.
find(cur))
128 for (
auto * p = u; p; p = parent.
find(p))
132 while (!anc_u.
has(lca_node))
133 lca_node = parent.
find(lca_node);
136 for (
auto * p = u; p != lca_node; p = parent.
find(p))
137 vals.
insert(p->get_info());
138 for (
auto * p = v; p != lca_node; p = parent.
find(p))
139 vals.
insert(p->get_info());
140 vals.
insert(lca_node->get_info());
157 TreeInfo<GT> build_random_tree(
size_t n,
int max_val, std::mt19937 &
rng)
167 for (
size_t i = 0; i < n; ++i)
168 info.nodes[i] = info.g.insert_node(
static_cast<int>(
rng() % max_val));
172 for (
size_t i = 1; i < n; ++i)
174 size_t parent_idx =
rng() % i;
175 info.g.insert_arc(info.nodes[parent_idx], info.nodes[i]);
178 info.root = info.nodes[0];
187 TreeInfo<GT> build_chain(
const Array<int> & vals)
191 for (
size_t i = 0; i < vals.
size(); ++i)
192 info.nodes[i] = info.g.insert_node(vals[i]);
194 for (
size_t i = 1; i < vals.
size(); ++i)
195 info.g.insert_arc(info.nodes[i-1], info.nodes[i]);
196 info.root = info.nodes.is_empty() ? nullptr : info.nodes[0];
209 EXPECT_EQ(g.
vsize(), 0u);
210 EXPECT_EQ(g.
esize(), 0u);
211 GTEST_SKIP() <<
"Gen_Mo_On_Trees requires a non-null root node; "
212 <<
"empty-graph constructor semantics are undefined.";
222 EXPECT_EQ(mot.
size(), 1u);
226 EXPECT_EQ(sub(0), 1u);
229 EXPECT_EQ(path(0), 1u);
243 EXPECT_EQ(sub(0), 1u);
244 EXPECT_EQ(sub(1), 1u);
247 EXPECT_EQ(path(0), 1u);
250TEST(MoOnTrees, TwoNodesDifferentValues)
261 EXPECT_EQ(sub(0), 2u);
262 EXPECT_EQ(sub(1), 1u);
264 auto path = mot.
path_solve({{a, b}, {a, a}, {b, b}});
265 EXPECT_EQ(path(0), 2u);
266 EXPECT_EQ(path(1), 1u);
267 EXPECT_EQ(path(2), 1u);
270TEST(MoOnTrees, InvalidNodeThrows)
285 EXPECT_THROW(mot.
path_solve({{a, alien}}), std::domain_error);
292TEST(MoOnTreesSubtree, SmallTreeListGraph)
312 auto parent = compute_parents(g,
r);
324 EXPECT_EQ(ans(0), 3u);
325 EXPECT_EQ(ans(1), 2u);
326 EXPECT_EQ(ans(2), 1u);
327 EXPECT_EQ(ans(3), 1u);
328 EXPECT_EQ(ans(4), 1u);
335TEST(MoOnTreesPath, ChainListSGraph)
339 auto info = build_chain<G>({1, 2, 3, 4, 5});
341 auto & n = info.nodes;
342 auto parent = compute_parents(g, n[0]);
353 EXPECT_EQ(ans(0), 5u);
354 EXPECT_EQ(ans(1), 3u);
355 EXPECT_EQ(ans(2), 1u);
356 EXPECT_EQ(ans(3), 3u);
358 for (
size_t i = 0; i < 4; ++i)
360 auto [u, v] = std::pair{
361 i == 0 ? n[0] : i == 1 ? n[1] : i == 2 ? n[0] : n[2],
362 i == 0 ? n[4] : i == 1 ? n[3] : i == 2 ? n[0] : n[4]};
368TEST(MoOnTreesPath, StarListSGraph)
385 auto parent = compute_parents(g, center);
405TEST(MoOnTreesSubtree, SmallTreeArrayGraph)
425 auto parent = compute_parents(g,
r);
433 EXPECT_EQ(sub(0), 3u);
434 EXPECT_EQ(sub(1), 2u);
435 EXPECT_EQ(sub(2), 1u);
460 std::mt19937
rng(123);
461 auto info = build_random_tree<G>(50, 10,
rng);
463 auto parent = compute_parents(g, info.root);
469 for (
size_t i = 0; i < info.nodes.size(); ++i)
470 queries(i) = info.nodes[i];
474 for (
size_t i = 0; i < info.nodes.size(); ++i)
482 std::mt19937
rng(456);
483 auto info = build_random_tree<G>(50, 10,
rng);
485 auto parent = compute_parents(g, info.root);
486 const size_t n = info.nodes.size();
491 const size_t Q = 200;
493 typename G::Node*>>::create(Q);
494 for (
size_t i = 0; i < Q; ++i)
495 queries(i) = {info.nodes[
rng() % n], info.nodes[
rng() % n]};
499 for (
size_t i = 0; i < Q; ++i)
509 std::mt19937
rng(789);
510 auto info = build_random_tree<G>(500, 20,
rng);
512 auto parent = compute_parents(g, info.root);
513 const size_t n = info.nodes.size();
517 const size_t Q = 500;
519 for (
size_t i = 0; i < Q; ++i)
520 queries(i) = info.nodes[
rng() % n];
524 for (
size_t i = 0; i < Q; ++i)
532 std::mt19937
rng(101112);
533 auto info = build_random_tree<G>(500, 20,
rng);
535 auto parent = compute_parents(g, info.root);
536 const size_t n = info.nodes.size();
540 const size_t Q = 1000;
542 typename G::Node*>>::create(Q);
543 for (
size_t i = 0; i < Q; ++i)
544 queries(i) = {info.nodes[
rng() % n], info.nodes[
rng() % n]};
548 for (
size_t i = 0; i < Q; ++i)
559TEST(MoOnTreesPowerful, ChainPath)
565 auto info = build_chain<G>({1, 2, 1, 3});
566 auto & n = info.nodes;
571 EXPECT_EQ(ans(0), 9LL);
575 EXPECT_EQ(sub(0), 9LL);
585 const size_t N = 5000;
587 std::vector<typename G::Node*>
nodes;
588 for (
size_t i = 0; i <
N; ++i)
590 for (
size_t i = 1; i <
N; ++i)
597 EXPECT_EQ(sub(0), 7u);
601 EXPECT_EQ(path(0), 7u);
608TEST(MoOnTrees, EmptyQueryArrays)
616 EXPECT_EQ(sub.size(), 0u);
619 Array<std::pair<typename G::Node*, typename G::Node*>>());
620 EXPECT_EQ(path.size(), 0u);
630 auto *
r =
new TN(42);
632 EXPECT_EQ(mot.
size(), 1u);
635 EXPECT_EQ(sub(0), 1u);
638 EXPECT_EQ(path(0), 1u);
643TEST(MoOnTreeNode, SmallTreeSubtree)
653 auto *
r =
new TN(1);
654 auto * a =
new TN(2);
655 auto * b =
new TN(1);
656 auto * c =
new TN(3);
657 auto * d =
new TN(4);
658 auto * e =
new TN(2);
659 auto * f =
new TN(1);
660 auto * g =
new TN(5);
662 r->insert_rightmost_child(a);
663 r->insert_rightmost_child(b);
664 r->insert_rightmost_child(c);
665 a->insert_rightmost_child(d);
666 a->insert_rightmost_child(e);
667 c->insert_rightmost_child(f);
668 d->insert_rightmost_child(g);
671 EXPECT_EQ(mot.
size(), 8u);
674 EXPECT_EQ(sub(0), 5u);
675 EXPECT_EQ(sub(1), 3u);
676 EXPECT_EQ(sub(2), 1u);
677 EXPECT_EQ(sub(3), 2u);
678 EXPECT_EQ(sub(4), 2u);
679 EXPECT_EQ(sub(5), 1u);
680 EXPECT_EQ(sub(6), 1u);
681 EXPECT_EQ(sub(7), 1u);
686TEST(MoOnTreeNode, SmallTreePath)
690 auto *
r =
new TN(1);
691 auto * a =
new TN(2);
692 auto * b =
new TN(1);
693 auto * c =
new TN(3);
694 auto * d =
new TN(4);
695 auto * e =
new TN(2);
696 auto * f =
new TN(1);
697 auto * g =
new TN(5);
699 r->insert_rightmost_child(a);
700 r->insert_rightmost_child(b);
701 r->insert_rightmost_child(c);
702 a->insert_rightmost_child(d);
703 a->insert_rightmost_child(e);
704 c->insert_rightmost_child(f);
705 d->insert_rightmost_child(g);
709 auto path = mot.
path_solve({{g, f}, {e, b}, {d, c}, {
r,
r}, {a, g}});
711 EXPECT_EQ(path(0), 5u);
713 EXPECT_EQ(path(1), 2u);
715 EXPECT_EQ(path(2), 4u);
717 EXPECT_EQ(path(3), 1u);
719 EXPECT_EQ(path(4), 3u);
724TEST(MoOnTreeNode, StressRandomSubtree)
727 const size_t N = 200;
728 const int max_val = 10;
730 std::mt19937
rng(54321);
731 std::uniform_int_distribution<int> val_dist(0, max_val - 1);
732 std::uniform_int_distribution<size_t> parent_dist;
735 std::vector<TN*>
nodes(
N);
737 for (
size_t i = 1; i <
N; ++i)
740 size_t par = parent_dist(
rng) % i;
745 EXPECT_EQ(mot.
size(),
N);
748 auto brute_subtree = [&](TN * sub_root) ->
size_t
751 std::vector<TN*> stk;
752 stk.push_back(sub_root);
755 auto * cur = stk.back();
757 seen.
insert(cur->get_key());
758 for (
auto * ch = cur->get_left_child(); ch !=
nullptr;
759 ch = ch->get_right_sibling())
767 for (
size_t i = 0; i <
N; ++i)
768 qroots(i) =
nodes[i];
771 for (
size_t i = 0; i <
N; ++i)
772 EXPECT_EQ(ans(i), brute_subtree(
nodes[i]))
773 <<
"Mismatch at subtree node " << i;
778TEST(MoOnTreeNode, NullRootThrows)
785TEST(MoOnTreeNode, EmptyQueryArrays)
788 auto *
r =
new TN(1);
792 EXPECT_EQ(sub.size(), 0u);
795 EXPECT_EQ(path.size(), 0u);
WeightedDigraph::Node Node
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
static Array create(const size_t n)
Create an array with n logical elements.
Self-adjusting dynamic hash table.
Key * insert(const Key &key)
Inserts key into the hash set.
bool has(const Key &key) const noexcept
Checks if a key exists in the hash table.
Dynamic stack of elements of generic type T based on a singly linked list.
bool is_empty() const noexcept
Check if the stack is empty.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
Pair * insert(const Key &key, const Data &data)
Inserts into the hash map the pair (key, record) indexed by key.
Data & find(const Key &key)
Finds and returns a reference to the value associated with key.
Offline subtree and path queries on N-ary trees (Tree_Node).
size_t size() const noexcept
Number of nodes in the tree.
Array< answer_type > path_solve(const Array< std::pair< Node *, Node * > > &query_pairs) const
Answer path queries on the N-ary tree.
Array< answer_type > subtree_solve(const Array< Node * > &query_roots) const
Answer subtree queries on the N-ary tree.
Offline subtree and path queries on trees via Mo's algorithm.
Array< answer_type > subtree_solve(const Array< Node * > &query_roots) const
Answer subtree queries using Mo's algorithm.
bool is_empty() const noexcept
True if the tree is empty.
size_t size() const noexcept
Number of nodes in the tree.
Array< answer_type > path_solve(const Array< std::pair< Node *, Node * > > &query_pairs) const
Answer path queries between node pairs.
Arc for graphs implemented with simple adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Graph_Node< Node_Info > Node
The graph type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
Number Theoretic Transform over Z / MOD Z.
Forward declaration used by CRTP helpers before the full node definition.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
constexpr size_t vsize() const noexcept
See get_num_nodes().
size_t esize() const noexcept
Return the total of arcs of graph.
__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)
DynArray< Graph::Node * > nodes
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
static size_t brute_subtree_distinct(const GT &g, typename GT::Node *tree_root, typename GT::Node *subtree_root)
static size_t brute_path_distinct(const GT &g, typename GT::Node *root, typename GT::Node *u, typename GT::Node *v)
Main namespace for Aleph-w library functions.
Arc of graph implemented with double-linked adjacency lists.
Array-based graph implementation.
::testing::Types< ListGraph, ListDigraph, SparseGraph, SparseDigraph, ArrayGraph, ArrayDigraph > GraphTypes
Dynamic set implementations based on hash tables.
Generic graph and digraph implementations.
Mo's algorithm on trees: offline subtree and path queries.
TYPED_TEST_SUITE(MoOnTreesStress, GraphTypes)
TYPED_TEST(MoOnTreesStress, SubtreeRandomSmall)
Simple graph implementation with adjacency lists.