46#include <gtest/gtest.h>
74 class Naive_Tree_Oracle
76 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
87 void dfs(
const size_t u,
const size_t p,
const size_t d)
99 stack.
append(Frame{u, p, d,
true});
108 tin_[f.u] = timer_++;
110 stack.
append(Frame{f.u, f.p, f.d,
false});
113 for (
auto it = adj_[f.u].get_it(); it.has_curr(); it.next_ne())
115 const size_t v = it.get_curr();
120 for (
long i =
static_cast<long>(children.
size()) - 1; i >= 0; --i)
121 stack.
append(Frame{children[
static_cast<size_t>(i)], f.u, f.d + 1,
true});
125 tout_[f.u] = timer_ - 1;
131 Naive_Tree_Oracle(
const size_t n,
132 const Array<std::pair<size_t, size_t>> & edges,
145 <<
"Naive_Tree_Oracle: edges.size() must be n-1";
147 for (
auto it = edges.get_it(); it.has_curr(); it.next_ne())
149 const auto & e = it.get_curr();
150 const size_t u = e.first;
151 const size_t v = e.second;
154 <<
"Naive_Tree_Oracle: invalid edge";
161 for (
size_t i = 0; i < n_; ++i)
163 <<
"Naive_Tree_Oracle: disconnected input";
166 [[nodiscard]]
size_t depth(
const size_t u)
const
172 [[nodiscard]]
bool is_ancestor(
const size_t u,
const size_t v)
const
175 return tin_[u] <= tin_[v]
and tout_[v] <= tout_[u];
178 [[nodiscard]]
size_t lca(
size_t u,
size_t v)
const
182 if (depth_[u] < depth_[v])
185 while (depth_[u] > depth_[v])
197 [[nodiscard]]
size_t distance(
const size_t u,
const size_t v)
const
199 const size_t a = lca(u, v);
200 return depth_[u] + depth_[v] - 2 * depth_[a];
203 [[nodiscard]]
size_t kth_ancestor(
const size_t u,
const size_t k)
const
210 for (
size_t i = 0; i <
k; ++i)
215 [[nodiscard]]
static size_t none() noexcept {
return NONE; }
219 class LcaTypedTest :
public ::testing::Test
223 using GraphBackends = ::testing::Types<
234 using Graph = TypeParam;
243 EXPECT_EQ(bl.
size(), 0u);
244 EXPECT_EQ(er.
size(), 0u);
246 EXPECT_THROW(bl.
lca_id(0, 0), std::domain_error);
247 EXPECT_THROW(er.
lca_id(0, 0), std::domain_error);
252 using Graph = TypeParam;
261 EXPECT_EQ(bl.
size(), 1u);
262 EXPECT_EQ(er.
size(), 1u);
263 EXPECT_EQ(bl.
root(), n0);
264 EXPECT_EQ(er.
root(), n0);
266 EXPECT_EQ(bl.
lca(n0, n0), n0);
267 EXPECT_EQ(er.
lca(n0, n0), n0);
281 using Graph = TypeParam;
293 edges.
append(std::make_pair(0, 1));
294 edges.
append(std::make_pair(0, 2));
295 edges.
append(std::make_pair(1, 3));
296 edges.
append(std::make_pair(1, 4));
297 edges.
append(std::make_pair(2, 5));
298 edges.
append(std::make_pair(2, 6));
299 edges.
append(std::make_pair(5, 7));
300 edges.
append(std::make_pair(5, 8));
303 auto nodes = build_graph_with_unit_arcs(g, 9, edges);
308 auto check = [&](
const size_t u,
const size_t v,
309 const size_t expected_lca,
const size_t expected_dist)
314 EXPECT_EQ(
static_cast<size_t>(bl_lca->get_info()), expected_lca)
315 <<
"u=" << u <<
" v=" << v;
316 EXPECT_EQ(
static_cast<size_t>(er_lca->get_info()), expected_lca)
317 <<
"u=" << u <<
" v=" << v;
318 EXPECT_EQ(bl_lca, er_lca) <<
"u=" << u <<
" v=" << v;
343 using Graph = TypeParam;
348 edges.
append(std::make_pair(0, 1));
349 edges.
append(std::make_pair(1, 2));
350 edges.
append(std::make_pair(2, 3));
351 edges.
append(std::make_pair(3, 4));
354 auto nodes = build_graph_with_unit_arcs(g, 5, edges);
360 Node * first_node =
nullptr;
363 first_node = it.get_curr();
366 ASSERT_NE(first_node,
nullptr);
372 EXPECT_EQ(bl.
root(), first_node);
373 EXPECT_EQ(er.
root(), first_node);
376 const size_t root_info =
static_cast<size_t>(first_node->get_info());
377 Naive_Tree_Oracle
oracle(5, edges, root_info);
379 const size_t expected_lca =
oracle.lca(4, 2);
380 EXPECT_EQ(
static_cast<size_t>(bl.
lca(
nodes(4),
nodes(2))->get_info()), expected_lca);
381 EXPECT_EQ(
static_cast<size_t>(er.
lca(
nodes(4),
nodes(2))->get_info()), expected_lca);
386 using Graph = TypeParam;
390 edges.
append(std::make_pair(0, 1));
391 edges.
append(std::make_pair(1, 2));
392 edges.
append(std::make_pair(2, 0));
395 auto nodes = build_graph_with_unit_arcs(g, 3, edges);
403 using Graph = TypeParam;
407 edges.
append(std::make_pair(0, 1));
408 edges.
append(std::make_pair(2, 3));
411 auto nodes = build_graph_with_unit_arcs(g, 4, edges);
419 using Graph = TypeParam;
425 edges.
append(std::make_pair(0, 1));
426 edges.
append(std::make_pair(1, 2));
427 edges.
append(std::make_pair(2, 2));
428 auto nodes = build_graph_with_unit_arcs(g, 3, edges);
437 edges.
append(std::make_pair(0, 1));
438 edges.
append(std::make_pair(0, 1));
439 auto nodes = build_graph_with_unit_arcs(g, 2, edges);
447 using Graph = TypeParam;
451 for (
size_t i = 0; i < 6; ++i)
471 EXPECT_EQ(
static_cast<size_t>(bl.
lca(
nodes(3),
nodes(4))->get_info()), 1u);
472 EXPECT_EQ(
static_cast<size_t>(er.
lca(
nodes(3),
nodes(4))->get_info()), 1u);
473 EXPECT_EQ(
static_cast<size_t>(bl.
lca(
nodes(3),
nodes(5))->get_info()), 0u);
474 EXPECT_EQ(
static_cast<size_t>(er.
lca(
nodes(3),
nodes(5))->get_info()), 0u);
477TYPED_TEST(LcaTypedTest, RejectsNodeFromAnotherGraphAndBadIds)
479 using Graph = TypeParam;
484 edges1.
append(std::make_pair(0, 1));
485 edges1.
append(std::make_pair(1, 2));
486 auto n1 = build_graph_with_unit_arcs(g1, 3, edges1);
491 edges2.
append(std::make_pair(0, 1));
492 auto n2 = build_graph_with_unit_arcs(g2, 2, edges2);
497 EXPECT_THROW(bl.
lca(n1(0), n2(0)), std::domain_error);
498 EXPECT_THROW(er.
lca(n1(0), n2(0)), std::domain_error);
499 EXPECT_THROW(bl.
depth_of(n2(0)), std::domain_error);
500 EXPECT_THROW(er.
depth_of(n2(0)), std::domain_error);
502 EXPECT_THROW(bl.
lca_id(0, 99), std::out_of_range);
503 EXPECT_THROW(er.
lca_id(0, 99), std::out_of_range);
504 EXPECT_THROW(bl.
node_of(99), std::out_of_range);
505 EXPECT_THROW(er.
node_of(99), std::out_of_range);
510 using Graph = TypeParam;
512 std::mt19937
rng(0x1ca2026u);
514 for (
size_t sample = 0; sample < 24; ++sample)
516 std::uniform_int_distribution<size_t> n_pick(2, 90);
517 const size_t n = n_pick(
rng);
518 const auto edges = make_random_tree_edges<Array<std::pair<size_t, size_t>>>(n,
rng);
521 auto nodes = build_graph_with_unit_arcs(g, n, edges);
523 std::uniform_int_distribution<size_t> root_pick(0, n - 1);
524 const size_t root = root_pick(
rng);
530 for (
size_t u = 0; u < n; ++u)
537 std::uniform_int_distribution<size_t> k_pick(0,
oracle.depth(u) + 2);
538 const size_t k = k_pick(
rng);
541 if (
expected == Naive_Tree_Oracle::none())
542 EXPECT_EQ(got,
nullptr);
544 EXPECT_EQ(
static_cast<size_t>(got->get_info()),
expected);
547 std::uniform_int_distribution<size_t> node_pick(0, n - 1);
548 for (
size_t q = 0; q < 450; ++q)
550 const size_t u = node_pick(
rng);
551 const size_t v = node_pick(
rng);
553 const size_t expected_lca =
oracle.lca(u, v);
554 const size_t expected_dist =
oracle.distance(u, v);
559 EXPECT_EQ(
static_cast<size_t>(bl_lca->get_info()), expected_lca)
560 <<
"sample=" << sample;
561 EXPECT_EQ(
static_cast<size_t>(er_lca->get_info()), expected_lca)
562 <<
"sample=" << sample;
563 EXPECT_EQ(bl_lca, er_lca) <<
"sample=" << sample;
569 oracle.is_ancestor(expected_lca, u));
571 oracle.is_ancestor(expected_lca, v));
578 using Graph = TypeParam;
579 using Clock = std::chrono::steady_clock;
581# ifndef LCA_PERF_TEST
582 const char * run_perf = std::getenv(
"ALEPH_RUN_LCA_PERF");
583 if (run_perf ==
nullptr or run_perf[0] !=
'1' or run_perf[1] !=
'\0')
584 GTEST_SKIP() <<
"LCA perf test disabled by default. "
585 <<
"Enable with -DLCA_PERF_TEST or ALEPH_RUN_LCA_PERF=1";
588 constexpr size_t n = 100000;
589 constexpr size_t query_count = 300000;
591 constexpr long long kBudgetMs = 12000;
593 constexpr long long kBudgetMs = 25000;
596 std::mt19937
rng(0x1ca2026u);
597 const auto edges = make_random_tree_edges<Array<std::pair<size_t, size_t>>>(n,
rng);
600 const auto nodes = build_graph_with_unit_arcs(g, n, edges);
602 std::uniform_int_distribution<size_t> root_pick(0, n - 1);
603 const size_t root = root_pick(
rng);
605 const auto t_start = Clock::now();
608 const auto t_after_build = Clock::now();
612 std::uniform_int_distribution<size_t> sanity_pick(0, n - 1);
613 for (
size_t i = 0; i < 64; ++i)
615 const size_t u = sanity_pick(
rng);
616 const size_t v = sanity_pick(
rng);
617 const size_t expected_lca =
oracle.lca(u, v);
618 const size_t expected_dist =
oracle.distance(u, v);
620 ASSERT_EQ(
static_cast<size_t>(bl.
lca(
nodes(u),
nodes(v))->get_info()),
622 ASSERT_EQ(
static_cast<size_t>(er.
lca(
nodes(u),
nodes(v))->get_info()),
629 std::uniform_int_distribution<size_t> node_pick(0, n - 1);
631 for (
size_t q = 0; q < query_count; ++q)
633 const size_t u = node_pick(
rng);
634 const size_t v = node_pick(
rng);
638 ASSERT_EQ(bl_lca, er_lca);
644 checksum ^= (
static_cast<size_t>(bl_lca->get_info()) + 0x9e3779b9u);
645 checksum ^= (d1 + (q << 1));
648 const auto t_end = Clock::now();
650 const auto build_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
651 t_after_build - t_start).count();
652 const auto query_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
653 t_end - t_after_build).count();
654 const auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
655 t_end - t_start).count();
657 ASSERT_NE(checksum, 0u);
658 ASSERT_LT(elapsed_ms, kBudgetMs)
659 <<
"Perf regression: n=" << n
660 <<
", queries=" << query_count
661 <<
", build_ms=" << build_ms
662 <<
", query_ms=" << query_ms
663 <<
", elapsed_ms=" << elapsed_ms
664 <<
", budget_ms=" << kBudgetMs;
Lowest Common Ancestor (LCA) on rooted trees represented as Aleph graphs.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
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.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Doubly-linked list (defined in tpl_dynList.H).
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
LCA via binary lifting on a rooted tree.
size_t depth_of(const Node *node) const
Distance from root to node.
bool is_empty() const noexcept
Returns true if the tree has no nodes.
Node * node_of(const size_t id) const
Map an internal ID [0, n-1] back to a Node pointer.
bool is_ancestor(const Node *u, const Node *v) const
Returns true if node u is an ancestor of v (or u == v).
Node * kth_ancestor(const Node *node, const size_t k) const
k-th ancestor by node pointer in O(log n).
size_t id_of(const Node *node) const
Map a Node pointer to its internal ID [0, n-1].
Node * root() const noexcept
Returns the root node of the tree.
size_t distance(const Node *u, const Node *v) const
Number of edges on the path between two nodes in O(log n).
size_t size() const noexcept
Total number of nodes in the tree.
size_t lca_id(size_t u, size_t v) const
LCA query by node ids in O(log n).
Node * parent_of(const Node *node) const
Parent of node, or nullptr if root.
Node * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(log n).
LCA via Euler tour + RMQ on depth in a rooted tree.
Node * parent_of(const Node *node) const
Parent of node, or nullptr if root.
Node * node_of(const size_t id) const
Map an internal ID [0, n-1] back to a Node pointer.
bool is_empty() const noexcept
Returns true if the tree has no nodes.
bool is_ancestor(const Node *u, const Node *v) const
Returns true if node u is an ancestor of v (or u == v).
size_t id_of(const Node *node) const
Map a Node pointer to its internal ID [0, n-1].
size_t lca_id(const size_t u, const size_t v) const
LCA query by node ids in O(1).
size_t size() const noexcept
Total number of nodes in the tree.
Node * root() const noexcept
Returns the root node of the tree.
size_t depth_of(const Node *node) const
Distance from root to node.
Node * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(1).
size_t distance(const Node *u, const Node *v) const
Number of edges on the path between two nodes in O(1).
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.
Filtered iterator on the nodes of a graph.
Minimal std::expected-style result type for C++20.
std::chrono::steady_clock Clock
__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
Singly linked list implementations with head-tail access.
TYPED_TEST(LcaTypedTest, 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.
and
Check uniqueness with explicit hash + equality functors.
bool none(const Container &container, Operation &operation)
Return true if no element satisfies operation.
Arc of graph implemented with double-linked adjacency lists.
Array-based graph implementation.
Dynamic array container with automatic resizing.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.