49#include <gtest/gtest.h>
77 class Naive_Path_Oracle
79 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
83 std::vector<std::vector<size_t>> adj_;
84 std::vector<size_t> parent_;
85 std::vector<size_t> depth_;
86 std::vector<int> values_;
88 void dfs(
const size_t u,
const size_t p,
const size_t d)
93 for (
const auto v : adj_[u])
99 Naive_Path_Oracle(
const size_t n,
100 const std::vector<std::pair<size_t, size_t>> & edges,
102 const std::vector<int> & vals)
103 : n_(n), root_(
root), adj_(n), parent_(n, NONE),
104 depth_(n, 0), values_(vals)
106 for (
const auto & [u, v] : edges)
108 adj_[u].push_back(v);
109 adj_[v].push_back(u);
114 [[nodiscard]]
size_t lca(
size_t u,
size_t v)
const
116 if (depth_[u] < depth_[v])
119 while (depth_[u] > depth_[v])
130 [[nodiscard]]
int path_sum(
size_t u,
size_t v)
const
132 const size_t a = lca(u, v);
137 result += values_[x];
143 result += values_[x];
146 result += values_[a];
150 [[nodiscard]]
int path_max(
size_t u,
size_t v)
const
152 const size_t a = lca(u, v);
153 int result = std::numeric_limits<int>::lowest();
157 result = std::max(result, values_[x]);
163 result = std::max(result, values_[x]);
166 result = std::max(result, values_[a]);
170 [[nodiscard]]
int path_min(
size_t u,
size_t v)
const
172 const size_t a = lca(u, v);
173 int result = std::numeric_limits<int>::max();
177 result = std::min(result, values_[x]);
183 result = std::min(result, values_[x]);
186 result = std::min(result, values_[a]);
190 [[nodiscard]]
int subtree_sum(
size_t v)
const
192 int result = values_[v];
193 std::vector<size_t> stack = {v};
194 while (not stack.empty())
196 const size_t u = stack.back();
198 for (
const auto c : adj_[u])
201 result += values_[c];
208 [[nodiscard]]
size_t distance(
size_t u,
size_t v)
const
210 const size_t a = lca(u, v);
211 return depth_[u] + depth_[v] - 2 * depth_[a];
214 void set_value(
size_t u,
int val) { values_[u] = val; }
215 [[nodiscard]]
int value(
size_t u)
const {
return values_[u]; }
216 [[nodiscard]]
size_t depth(
size_t u)
const {
return depth_[u]; }
220 class HldTypedTest :
public ::testing::Test
224 using GraphBackends = ::testing::Types<
235 using Graph = TypeParam;
239 auto nv = [](
typename Graph::Node * p) {
return p->get_info(); };
243 EXPECT_EQ(hld.
size(), 0u);
246 EXPECT_THROW(hld.
lca_id(0, 0), std::domain_error);
251 using Graph = TypeParam;
257 auto nv = [](
Node * p) {
return p->get_info(); };
260 EXPECT_EQ(hld.
size(), 1u);
261 EXPECT_EQ(hld.
root(), n0);
265 EXPECT_EQ(hld.
lca(n0, n0), n0);
266 EXPECT_EQ(hld.
distance(n0, n0), 0u);
274 using Graph = TypeParam;
286 const std::vector<std::pair<size_t, size_t>> edges = {
293 auto nodes = build_graph_with_unit_arcs(g, 9, edges);
295 auto nv = [](
Node * p) {
return p->get_info() * 10; };
322 using Graph = TypeParam;
325 const std::vector<std::pair<size_t, size_t>> edges = {
332 auto nodes = build_graph_with_unit_arcs(g, 9, edges);
335 const int vals[] = {5, 3, 8, 1, 9, 2, 7, 4, 6};
336 auto nv = [&vals](
Node * p) {
return vals[p->get_info()]; };
351 using Graph = TypeParam;
354 const std::vector<std::pair<size_t, size_t>> edges = {
361 auto nodes = build_graph_with_unit_arcs(g, 9, edges);
363 auto nv = [](
Node * p) {
return p->get_info() + 1; };
384 using Graph = TypeParam;
387 const std::vector<std::pair<size_t, size_t>> edges = {
388 {0, 1}, {0, 2}, {1, 3}, {1, 4}};
391 auto nodes = build_graph_with_unit_arcs(g, 5, edges);
394 auto nv = [](
Node *) {
return 10; };
416 using Graph = TypeParam;
425 const std::vector<std::pair<size_t, size_t>> edges = {
426 {0, 1}, {0, 2}, {1, 3}};
429 auto nodes = build_graph_with_unit_arcs(g, 4, edges);
432 const int edge_vals[] = {0, 5, 3, 7};
433 auto nv = [&edge_vals](
Node * p) {
return edge_vals[p->get_info()]; };
452 using Graph = TypeParam;
455 const std::vector<std::pair<size_t, size_t>> edges = {
462 auto nodes = build_graph_with_unit_arcs(g, 9, edges);
464 auto nv = [](
Node * p) {
return p->get_info(); };
469 for (
size_t u = 0; u < 9; ++u)
470 for (
size_t v = u; v < 9; ++v)
474 EXPECT_EQ(hld_lca, bl_lca)
475 <<
"u=" << u <<
" v=" << v;
484 using Graph = TypeParam;
487 const std::vector<std::pair<size_t, size_t>> edges = {
494 auto nodes = build_graph_with_unit_arcs(g, 9, edges);
496 auto nv = [](
Node * p) {
return p->get_info(); };
499 EXPECT_EQ(hld.
size(), 9u);
504 std::vector<bool> seen(9,
false);
505 for (
size_t i = 0; i < 9; ++i)
509 EXPECT_FALSE(seen[pos]) <<
"Duplicate position at node " << i;
523 for (
size_t i = 0; i < 9; ++i)
532 using Graph = TypeParam;
535 auto nv = [](
Node * p) {
return p->get_info(); };
540 auto nodes = build_graph_with_unit_arcs(g, 3, {{0, 1}, {1, 2}, {2, 0}});
547 auto nodes = build_graph_with_unit_arcs(g, 4, {{0, 1}, {2, 3}});
554 auto nodes = build_graph_with_unit_arcs(g, 3, {{0, 1}, {1, 2}, {2, 2}});
561 auto nodes = build_graph_with_unit_arcs(g, 2, {{0, 1}, {0, 1}});
568 using Graph = TypeParam;
573 for (
size_t i = 0; i < 6; ++i)
588 auto nv = [](
Node * p) {
return p->get_info(); };
595 EXPECT_EQ(hld.
size(), 6u);
598 EXPECT_EQ(
static_cast<size_t>(
l->get_info()), 0u);
601 EXPECT_EQ(
static_cast<size_t>(
l->get_info()), 1u);
606 using Graph = TypeParam;
609 std::mt19937
rng(0x41d2026u);
611 for (
size_t sample = 0; sample < 20; ++sample)
613 std::uniform_int_distribution<size_t> n_pick(2, 80);
614 const size_t n = n_pick(
rng);
615 const auto edges = make_random_tree_edges<std::vector<std::pair<size_t, size_t>>>(n,
rng);
617 std::uniform_int_distribution<int> val_pick(-100, 100);
618 std::vector<int> vals(n);
619 for (
size_t i = 0; i < n; ++i)
620 vals[i] = val_pick(
rng);
622 std::uniform_int_distribution<size_t> root_pick(0, n - 1);
623 const size_t root = root_pick(
rng);
626 auto nodes = build_graph_with_unit_arcs(g, n, edges);
628 auto nv_sum = [&vals](
Node * p) {
return vals[p->get_info()]; };
629 auto nv_max = [&vals](
Node * p) {
return vals[p->get_info()]; };
630 auto nv_min = [&vals](
Node * p) {
return vals[p->get_info()]; };
636 Naive_Path_Oracle
oracle(n, edges,
root, vals);
638 std::uniform_int_distribution<size_t> node_pick(0, n - 1);
641 for (
size_t q = 0; q < 200; ++q)
643 const size_t u = node_pick(
rng);
644 const size_t v = node_pick(
rng);
648 <<
"sample=" << sample <<
" u=" << u <<
" v=" << v;
652 <<
"sample=" << sample <<
" u=" << u <<
" v=" << v;
656 <<
"sample=" << sample <<
" u=" << u <<
" v=" << v;
659 EXPECT_EQ(
static_cast<size_t>(hld_sum.
lca(
nodes(u),
nodes(v))->get_info()),
661 <<
"sample=" << sample <<
" u=" << u <<
" v=" << v;
666 <<
"sample=" << sample <<
" u=" << u <<
" v=" << v;
670 for (
size_t q = 0; q < 50; ++q)
672 const size_t v = node_pick(
rng);
675 <<
"sample=" << sample <<
" v=" << v;
679 for (
size_t q = 0; q < 20; ++q)
681 const size_t v = node_pick(
rng);
682 const int new_val = val_pick(
rng);
685 oracle.set_value(v, new_val);
688 const size_t u1 = node_pick(
rng);
689 const size_t v1 = node_pick(
rng);
692 <<
"sample=" << sample <<
" after update";
Heavy-Light Decomposition for tree path queries on Aleph graphs.
Lowest Common Ancestor (LCA) on rooted trees represented as Aleph graphs.
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
WeightedDigraph::Node Node
size_t size_t int32_t value
static Array create(const size_t n)
Create an array with n logical elements.
LCA via binary lifting on a rooted tree.
size_t distance(const Node *u, const Node *v) const
Number of edges on the path between two nodes in O(log n).
Node * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(log n).
size_t distance(const Node *u, const Node *v) const
Number of edges between two nodes in O(log n).
T path_query(const Node *u, const Node *v) const
Path query by node pointers in O(log^2 n).
T path_query_id(size_t u, size_t v) const
Path query by node IDs in O(log^2 n).
size_t chain_head_id(const size_t id) const
Head of the chain containing node id.
size_t depth_of_id(const size_t id) const
Depth of node id from root.
size_t subtree_size_of_id(const size_t id) const
Subtree size of node id.
size_t num_chains() const noexcept
Number of heavy chains in the decomposition.
T get_value(const Node *v) const
Current node value at node pointer v.
size_t size() const noexcept
Total number of nodes in the tree.
Node * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(log n).
void point_update(const Node *v, const T &new_value)
Point update by node pointer: set value to new_value.
size_t position(const size_t id) const
HLD flat-array position of node id.
size_t lca_id(size_t u, size_t v) const
LCA query by node IDs in O(log n).
size_t root_id() const noexcept
Returns the internal ID of the root node.
T subtree_query(const Node *v) const
Subtree query by node pointer in O(log n).
bool is_empty() const noexcept
Returns true if the tree has no nodes.
size_t depth_of(const Node *node) const
Depth of node from root.
T subtree_query_id(const size_t v) const
Subtree query by node ID in O(log n).
Node * root() const noexcept
Returns the root node.
Node * parent_of(const Node *node) const
Parent of node, or nullptr if root.
T path_query_edges(const Node *u, const Node *v) const
Edge-weighted path query by node pointers.
Arc for graphs implemented with simple adjacency lists.
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
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
TYPED_TEST(HldTypedTest, EmptyTree)
DFSResult< typename Domain::Distance > dfs(Domain &domain, typename Domain::State &state, SearchPath< typename Domain::Move > &path, const typename Domain::Distance g, const typename Domain::Distance threshold, const size_t depth, IDAStarResult< Solution, typename Domain::Distance > &result, OnSolution &on_solution)
Core recursive DFS used by IDA* for a single threshold pass.
Aleph::Array< typename GT::Node * > build_graph_with_unit_arcs(GT &g, const size_t n, const std::vector< std::pair< size_t, size_t > > &edges)
EdgeContainer make_random_tree_edges(const size_t n, std::mt19937 &rng)
Main namespace for Aleph-w library functions.
Arc of graph implemented with double-linked adjacency lists.
HLD with path/subtree max queries.
HLD with path/subtree min queries.
HLD with path/subtree sum queries.
Array-based graph implementation.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.