37# include <gtest/gtest.h>
59 build_graph_with_values(
GT & g,
60 const std::vector<int> & values,
61 const std::vector<std::pair<size_t, size_t>> & edges)
66 for (
size_t i = 0; i < values.size(); ++i)
69 for (
const auto & [u, v] : edges)
75 std::vector<std::pair<size_t, size_t>>
76 random_tree_edges(
const size_t n, std::mt19937 &
rng)
78 std::vector<std::pair<size_t, size_t>> edges;
79 edges.reserve(n > 0 ? n - 1 : 0);
81 for (
size_t v = 1; v < n; ++v)
83 std::uniform_int_distribution<size_t> pick(0, v - 1);
84 edges.emplace_back(v, pick(
rng));
92 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
97 std::vector<std::vector<size_t>> adj_;
98 std::vector<std::vector<size_t>> children_;
99 std::vector<size_t> parent_;
100 std::vector<size_t> depth_;
103 Rooted_Oracle(
const size_t n,
104 const std::vector<std::pair<size_t, size_t>> & edges,
113 for (
const auto & [u, v] : edges)
115 adj_[u].push_back(v);
116 adj_[v].push_back(u);
119 std::vector<char> visited(n_, 0);
127 std::vector<Frame> stack;
128 stack.push_back({root_, NONE, 0});
130 parent_[root_] = NONE;
133 while (not stack.empty())
135 auto & fr = stack.back();
136 if (fr.next == adj_[fr.u].size())
142 const size_t v = adj_[fr.u][fr.next++];
151 depth_[v] = depth_[fr.u] + 1;
152 children_[fr.u].push_back(v);
153 stack.push_back({v, fr.u, 0});
157 [[nodiscard]]
size_t lca(
size_t u,
size_t v)
const
159 while (depth_[u] > depth_[v])
162 while (depth_[v] > depth_[u])
174 [[nodiscard]]
size_t distance(
const size_t u,
const size_t v)
const
176 const size_t a = lca(u, v);
177 return depth_[u] + depth_[v] - 2 * depth_[a];
180 [[nodiscard]]
int path_sum(
const std::vector<int> & values,
186 while (depth_[u] > depth_[v])
192 while (depth_[v] > depth_[u])
210 [[nodiscard]]
int subtree_sum(
const std::vector<int> & values,
211 const size_t u)
const
214 std::vector<size_t> stack;
217 while (not stack.empty())
219 const size_t x = stack.back();
223 for (
const auto ch : children_[x])
232 class TreeDecompositionTypedTest :
public ::testing::Test
236 using GraphBackends = ::testing::Types<
248 using Graph = TypeParam;
254 EXPECT_EQ(hld.
size(), 0u);
262 EXPECT_EQ(cd.
size(), 0u);
273 using Graph = TypeParam;
324 using Graph = TypeParam;
341 EXPECT_EQ(hld.
size(), 4u);
342 EXPECT_EQ(hld.
distance(n2, n3), 2u);
350 EXPECT_EQ(cd.
size(), 4u);
354TYPED_TEST(TreeDecompositionTypedTest, DeterministicHeavyLightQueries)
356 using Graph = TypeParam;
358 const std::vector<int> values = {5, 3, 4, 7, 2, 6, 1};
359 const std::vector<std::pair<size_t, size_t>> edges = {
360 {0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {5, 6}};
363 auto nodes = build_graph_with_values(g, values, edges);
365 Rooted_Oracle
oracle(values.size(), edges, 0);
370 for (
size_t u = 0; u < values.size(); ++u)
371 for (
size_t v = 0; v < values.size(); ++v)
378 for (
size_t u = 0; u < values.size(); ++u)
386 std::vector<int> mutable_values = values;
388 mutable_values[3] += 5;
390 mutable_values[4] = 10;
392 for (
size_t u = 0; u < values.size(); ++u)
393 for (
size_t v = 0; v < values.size(); ++v)
395 oracle.path_sum(mutable_values, u, v));
397 for (
size_t u = 0; u < values.size(); ++u)
401TYPED_TEST(TreeDecompositionTypedTest, RandomHeavyLightAgainstOracle)
403 using Graph = TypeParam;
405 std::mt19937
rng(0xA13F2026u);
406 std::uniform_int_distribution<int> pick_value(-20, 20);
408 for (
size_t trial = 0; trial < 20; ++trial)
410 std::uniform_int_distribution<size_t> pick_n(5, 60);
411 const size_t n = pick_n(
rng);
413 auto edges = random_tree_edges(n,
rng);
415 std::vector<int> values(n);
416 for (
size_t i = 0; i < n; ++i)
417 values[i] = pick_value(
rng);
420 auto nodes = build_graph_with_values(g, values, edges);
422 Rooted_Oracle
oracle(n, edges, 0);
426 std::uniform_int_distribution<size_t> pick_id(0, n - 1);
428 for (
size_t it = 0; it < 250; ++it)
430 const int action =
static_cast<int>(
rng() % 100);
434 const size_t u = pick_id(
rng);
435 if ((
rng() & 1U) == 0U)
437 const int delta = pick_value(
rng);
443 const int val = pick_value(
rng);
450 const size_t u = pick_id(
rng);
451 const size_t v = pick_id(
rng);
452 const size_t w = pick_id(
rng);
463TYPED_TEST(TreeDecompositionTypedTest, CentroidAnnotationsAndDynamicNearest)
465 using Graph = TypeParam;
467 const std::vector<int> values = {0, 1, 2, 3, 4, 5, 6, 7, 8};
468 const std::vector<std::pair<size_t, size_t>> edges = {
469 {0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}, {6, 7}, {6, 8}};
472 auto nodes = build_graph_with_values(g, values, edges);
474 Rooted_Oracle
oracle(values.size(), edges, 0);
477 EXPECT_EQ(cd.
size(), values.size());
480 std::vector<size_t> algo_of_input(values.size());
481 std::vector<size_t> input_of_algo(values.size());
482 for (
size_t i = 0; i < values.size(); ++i)
485 algo_of_input[i] = aid;
486 input_of_algo[aid] = i;
489 for (
size_t u = 0; u < values.size(); ++u)
491 const size_t au = algo_of_input[u];
495 ASSERT_EQ(anc.size(), dist.size());
496 ASSERT_GT(anc.size(), 0u);
499 EXPECT_EQ(anc(anc.size() - 1), au);
500 EXPECT_EQ(dist(anc.size() - 1), 0u);
502 for (
size_t k = 0;
k < anc.size(); ++
k)
503 EXPECT_EQ(dist(
k),
oracle.distance(u, input_of_algo[anc(
k)]));
505 for (
size_t k = 1;
k < anc.size(); ++
k)
513 const size_t INF = std::numeric_limits<size_t>::max() / 4;
514 std::vector<size_t> best(cd.
size(), INF);
515 std::vector<char> active(values.size(), 0);
517 auto activate = [&](
const size_t u)
522 [&](
const size_t c,
const size_t d,
const size_t)
529 auto query = [&](
const size_t u)
534 [&](
const size_t c,
const size_t d,
const size_t)
537 ans = std::min(ans, best[c] + d);
542 auto brute_query = [&](
const size_t u)
545 for (
size_t v = 0; v < active.size(); ++v)
547 ans = std::min(ans,
oracle.distance(u, v));
551 std::mt19937
rng(0xC3E7D01Du);
552 std::uniform_int_distribution<size_t> pick_id(0, values.size() - 1);
556 for (
size_t it = 0; it < 300; ++it)
558 const size_t u = pick_id(
rng);
559 if ((
rng() % 100) < 40)
562 EXPECT_EQ(query(u), brute_query(u));
Heavy-Light and Centroid decomposition on rooted trees represented as Aleph graphs.
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
WeightedDigraph::Node Node
Simple dynamic array with automatic resizing and functional operations.
static Array create(const size_t n)
Create an array with n logical elements.
Centroid decomposition over a rooted tree represented as an Aleph graph.
bool is_empty() const noexcept
size_t centroid_level_of_id(const size_t id) const
Level of centroid id in centroid tree (root level = 0).
Node * centroid_root() const noexcept
Root centroid node of the centroid tree (or nullptr if empty).
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_parent_id(const size_t id) const
Parent centroid ID of id (or NONE if centroid root).
void for_each_centroid_ancestor_id(const size_t node, F &&f) const
Visit each centroid ancestor of node ID.
size_t id_of(const Node *node) const
const Array< size_t > & centroid_distances_of_id(const size_t id) const
Distances to centroid ancestors (aligned with centroid_ancestors_of_id).
size_t size() const noexcept
Segment-tree-powered path/subtree queries over an HLD layout.
T query_subtree(const Node *node) const
Subtree query for node in O(log n).
void set_node(const Node *node, const T &value)
Point assignment: a[node] = value.
T query_path(const Node *u, const Node *v) const
Path query between two nodes in O(log^2 n).
void update_node(const Node *node, const T &delta)
Point update: a[node] = Op(a[node], delta).
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).
bool is_empty() const noexcept
Heavy-Light Decomposition over a rooted tree represented as an Aleph graph.
size_t distance(const Node *u, const Node *v) const
Distance (number of edges) between two nodes.
size_t subtree_size_of_id(const size_t id) const
Node * lca(const Node *u, const Node *v) const
Lowest common ancestor in O(log n).
size_t id_of(const Node *node) const
Range subtree_range_id(const size_t id) const
Base-array range of a full subtree (inclusive endpoints).
size_t size() const noexcept
bool is_empty() const noexcept
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.
__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
Main namespace for Aleph-w library functions.
void next()
Advance all underlying iterators (bounds-checked).
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Arc of graph implemented with double-linked adjacency lists.
Array-based graph implementation.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.
TYPED_TEST(TreeDecompositionTypedTest, EmptyGraph)