Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
lca_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
46#include <gtest/gtest.h>
47
48#include <algorithm>
49#include <chrono>
50#include <cstddef>
51#include <cstdlib>
52#include <functional>
53#include <limits>
54#include <random>
55#include <stdexcept>
56#include <utility>
57
58#include <LCA.H>
59#include <tpl_agraph.H>
60#include <tpl_array.H>
61#include <tpl_graph.H>
62#include <tpl_sgraph.H>
63#include <htlist.H>
64
65#include "test_graph_helpers.h"
66
67using namespace Aleph;
71
72namespace
73{
74 class Naive_Tree_Oracle
75 {
76 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
77
78 size_t n_ = 0;
79 size_t root_ = 0;
81 Array<size_t> parent_;
82 Array<size_t> depth_;
83 Array<size_t> tin_;
84 Array<size_t> tout_;
85 size_t timer_ = 0;
86
87 void dfs(const size_t u, const size_t p, const size_t d)
88 {
89 struct Frame
90 {
91 size_t u = 0;
92 size_t p = NONE;
93 size_t d = 0;
94 bool entering = true;
95 };
96
97 Array<Frame> stack;
98 stack.reserve(n_ * 2);
99 stack.append(Frame{u, p, d, true});
100
101 while (not stack.is_empty())
102 {
103 const Frame f = stack.remove_last();
104 if (f.entering)
105 {
106 parent_[f.u] = f.p;
107 depth_[f.u] = f.d;
108 tin_[f.u] = timer_++;
109
110 stack.append(Frame{f.u, f.p, f.d, false});
111
112 Array<size_t> children;
113 for (auto it = adj_[f.u].get_it(); it.has_curr(); it.next_ne())
114 {
115 const size_t v = it.get_curr();
116 if (v != f.p)
117 children.append(v);
118 }
119
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});
122 }
123 else
124 {
125 tout_[f.u] = timer_ - 1;
126 }
127 }
128 }
129
130 public:
131 Naive_Tree_Oracle(const size_t n,
132 const Array<std::pair<size_t, size_t>> & edges,
133 const size_t root)
134 : n_(n),
135 root_(root),
136 adj_(Array<DynList<size_t>>::create(n)),
137 parent_(n, NONE),
138 depth_(n, 0),
139 tin_(n, 0),
140 tout_(n, 0)
141 {
142 ah_runtime_error_if(n_ == 0) << "Naive_Tree_Oracle requires n > 0";
143 ah_runtime_error_if(root_ >= n_) << "Naive_Tree_Oracle: invalid root";
144 ah_runtime_error_if(edges.size() != n_ - 1)
145 << "Naive_Tree_Oracle: edges.size() must be n-1";
146
147 for (auto it = edges.get_it(); it.has_curr(); it.next_ne())
148 {
149 const auto & e = it.get_curr();
150 const size_t u = e.first;
151 const size_t v = e.second;
152
153 ah_runtime_error_if(u >= n_ or v >= n_ or u == v)
154 << "Naive_Tree_Oracle: invalid edge";
155 adj_[u].append(v);
156 adj_[v].append(u);
157 }
158
159 dfs(root_, NONE, 0);
160
161 for (size_t i = 0; i < n_; ++i)
162 ah_runtime_error_if(parent_[i] == NONE and i != root_)
163 << "Naive_Tree_Oracle: disconnected input";
164 }
165
166 [[nodiscard]] size_t depth(const size_t u) const
167 {
168 ah_runtime_error_if(u >= n_) << "Naive_Tree_Oracle::depth: bad index";
169 return depth_[u];
170 }
171
172 [[nodiscard]] bool is_ancestor(const size_t u, const size_t v) const
173 {
174 ah_runtime_error_if(u >= n_ or v >= n_) << "Naive_Tree_Oracle::is_ancestor: bad index";
175 return tin_[u] <= tin_[v] and tout_[v] <= tout_[u];
176 }
177
178 [[nodiscard]] size_t lca(size_t u, size_t v) const
179 {
180 ah_runtime_error_if(u >= n_ or v >= n_) << "Naive_Tree_Oracle::lca: bad index";
181
182 if (depth_[u] < depth_[v])
183 std::swap(u, v);
184
185 while (depth_[u] > depth_[v])
186 u = parent_[u];
187
188 while (u != v)
189 {
190 u = parent_[u];
191 v = parent_[v];
192 }
193
194 return u;
195 }
196
197 [[nodiscard]] size_t distance(const size_t u, const size_t v) const
198 {
199 const size_t a = lca(u, v);
200 return depth_[u] + depth_[v] - 2 * depth_[a];
201 }
202
203 [[nodiscard]] size_t kth_ancestor(const size_t u, const size_t k) const
204 {
205 ah_runtime_error_if(u >= n_) << "Naive_Tree_Oracle::kth_ancestor: bad index";
206 if (k > depth_[u])
207 return NONE;
208
209 size_t cur = u;
210 for (size_t i = 0; i < k; ++i)
211 cur = parent_[cur];
212 return cur;
213 }
214
215 [[nodiscard]] static size_t none() noexcept { return NONE; }
216 };
217
218 template <class GT>
219 class LcaTypedTest : public ::testing::Test
220 {
221 };
222
223 using GraphBackends = ::testing::Types<
227
228 TYPED_TEST_SUITE(LcaTypedTest, GraphBackends);
229}
230
231
232TYPED_TEST(LcaTypedTest, EmptyTree)
233{
234 using Graph = TypeParam;
235
236 Graph g;
237
240
241 EXPECT_TRUE(bl.is_empty());
242 EXPECT_TRUE(er.is_empty());
243 EXPECT_EQ(bl.size(), 0u);
244 EXPECT_EQ(er.size(), 0u);
245
246 EXPECT_THROW(bl.lca_id(0, 0), std::domain_error);
247 EXPECT_THROW(er.lca_id(0, 0), std::domain_error);
248}
249
250TYPED_TEST(LcaTypedTest, SingleNode)
251{
252 using Graph = TypeParam;
253 using Node = typename Graph::Node;
254
255 Graph g;
256 Node * n0 = g.insert_node(7);
257
259 Euler_RMQ_LCA<Graph> er(g, n0);
260
261 EXPECT_EQ(bl.size(), 1u);
262 EXPECT_EQ(er.size(), 1u);
263 EXPECT_EQ(bl.root(), n0);
264 EXPECT_EQ(er.root(), n0);
265
266 EXPECT_EQ(bl.lca(n0, n0), n0);
267 EXPECT_EQ(er.lca(n0, n0), n0);
268
269 EXPECT_EQ(bl.depth_of(n0), 0u);
270 EXPECT_EQ(er.depth_of(n0), 0u);
271 EXPECT_EQ(bl.distance(n0, n0), 0u);
272 EXPECT_EQ(er.distance(n0, n0), 0u);
273
274 EXPECT_EQ(bl.parent_of(n0), nullptr);
275 EXPECT_EQ(er.parent_of(n0), nullptr);
276 EXPECT_EQ(bl.kth_ancestor(n0, 1), nullptr);
277}
278
279TYPED_TEST(LcaTypedTest, ManualTreeQueries)
280{
281 using Graph = TypeParam;
282
283 // Rooted at 0:
284 // 0
285 // / (right)
286 // (left) 2
287 // 1 children of 2
288 // / \ 5 6
289 // 3 4 children of 5
290 // 7 and 8
292 edges.reserve(8);
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));
301
302 Graph g;
303 auto nodes = build_graph_with_unit_arcs(g, 9, edges);
304
306 Euler_RMQ_LCA<Graph> er(g, nodes(0));
307
308 auto check = [&](const size_t u, const size_t v,
309 const size_t expected_lca, const size_t expected_dist)
310 {
311 auto * bl_lca = bl.lca(nodes(u), nodes(v));
312 auto * er_lca = er.lca(nodes(u), nodes(v));
313
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;
319
320 EXPECT_EQ(bl.distance(nodes(u), nodes(v)), expected_dist);
321 EXPECT_EQ(er.distance(nodes(u), nodes(v)), expected_dist);
322 };
323
324 check(3, 4, 1, 2);
325 check(3, 6, 0, 4);
326 check(7, 8, 5, 2);
327 check(7, 6, 2, 3);
328 check(0, 8, 0, 3);
329
330 EXPECT_EQ(bl.kth_ancestor(nodes(8), 1), nodes(5));
331 EXPECT_EQ(bl.kth_ancestor(nodes(8), 2), nodes(2));
332 EXPECT_EQ(bl.kth_ancestor(nodes(8), 3), nodes(0));
333 EXPECT_EQ(bl.kth_ancestor(nodes(8), 4), nullptr);
334
335 EXPECT_TRUE(bl.is_ancestor(nodes(2), nodes(7)));
336 EXPECT_FALSE(bl.is_ancestor(nodes(1), nodes(7)));
337 EXPECT_TRUE(er.is_ancestor(nodes(2), nodes(7)));
338 EXPECT_FALSE(er.is_ancestor(nodes(1), nodes(7)));
339}
340
341TYPED_TEST(LcaTypedTest, DefaultRootIsFirstNode)
342{
343 using Graph = TypeParam;
344 using Node = typename Graph::Node;
345
347 edges.reserve(4);
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));
352
353 Graph g;
354 auto nodes = build_graph_with_unit_arcs(g, 5, edges);
355
356 // Find the first node returned by Node_Iterator for this backend.
357 // List_Graph / Array_Graph iterate in insertion order, so the first
358 // node is nodes(0). List_SGraph iterates by pointer address (BST
359 // in-order), so the first node may differ — this is expected behaviour.
360 Node * first_node = nullptr;
361 for (Node_Iterator<Graph> it(g); it.has_curr(); it.next_ne())
362 {
363 first_node = it.get_curr();
364 break;
365 }
366 ASSERT_NE(first_node, nullptr);
367
370
371 // Both engines must choose the same default root = first iterated node.
372 EXPECT_EQ(bl.root(), first_node);
373 EXPECT_EQ(er.root(), first_node);
374
375 // Use the oracle with the actual default root to derive the expected LCA.
376 const size_t root_info = static_cast<size_t>(first_node->get_info());
377 Naive_Tree_Oracle oracle(5, edges, root_info);
378
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);
382}
383
384TYPED_TEST(LcaTypedTest, RejectsCycle)
385{
386 using Graph = TypeParam;
387
389 edges.reserve(3);
390 edges.append(std::make_pair(0, 1));
391 edges.append(std::make_pair(1, 2));
392 edges.append(std::make_pair(2, 0));
393
394 Graph g;
395 auto nodes = build_graph_with_unit_arcs(g, 3, edges);
396
397 EXPECT_THROW((Binary_Lifting_LCA<Graph>(g, nodes(0))), std::domain_error);
398 EXPECT_THROW((Euler_RMQ_LCA<Graph>(g, nodes(0))), std::domain_error);
399}
400
401TYPED_TEST(LcaTypedTest, RejectsDisconnectedGraph)
402{
403 using Graph = TypeParam;
404
406 edges.reserve(2);
407 edges.append(std::make_pair(0, 1));
408 edges.append(std::make_pair(2, 3));
409
410 Graph g;
411 auto nodes = build_graph_with_unit_arcs(g, 4, edges);
412
413 EXPECT_THROW((Binary_Lifting_LCA<Graph>(g, nodes(0))), std::domain_error);
414 EXPECT_THROW((Euler_RMQ_LCA<Graph>(g, nodes(0))), std::domain_error);
415}
416
417TYPED_TEST(LcaTypedTest, RejectsLoopAndParallelEdges)
418{
419 using Graph = TypeParam;
420
421 {
422 Graph g;
424 edges.reserve(3);
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);
429 EXPECT_THROW((Binary_Lifting_LCA<Graph>(g, nodes(0))), std::domain_error);
430 EXPECT_THROW((Euler_RMQ_LCA<Graph>(g, nodes(0))), std::domain_error);
431 }
432
433 {
434 Graph g;
436 edges.reserve(2);
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);
440 EXPECT_THROW((Binary_Lifting_LCA<Graph>(g, nodes(0))), std::domain_error);
441 EXPECT_THROW((Euler_RMQ_LCA<Graph>(g, nodes(0))), std::domain_error);
442 }
443}
444
445TYPED_TEST(LcaTypedTest, ArcFilterSelectsTree)
446{
447 using Graph = TypeParam;
448
449 Graph g;
451 for (size_t i = 0; i < 6; ++i)
452 nodes(i) = g.insert_node(static_cast<int>(i));
453
454 // Tree edges (kept by filter).
455 g.insert_arc(nodes(0), nodes(1), 1);
456 g.insert_arc(nodes(0), nodes(2), 1);
457 g.insert_arc(nodes(1), nodes(3), 1);
458 g.insert_arc(nodes(1), nodes(4), 1);
459 g.insert_arc(nodes(2), nodes(5), 1);
460
461 // Extra non-tree edges (ignored by filter).
462 g.insert_arc(nodes(3), nodes(5), -1);
463 g.insert_arc(nodes(4), nodes(2), -1);
464
465 EXPECT_THROW((Binary_Lifting_LCA<Graph>(g, nodes(0))), std::domain_error);
466 EXPECT_THROW((Euler_RMQ_LCA<Graph>(g, nodes(0))), std::domain_error);
467
470
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);
475}
476
477TYPED_TEST(LcaTypedTest, RejectsNodeFromAnotherGraphAndBadIds)
478{
479 using Graph = TypeParam;
480
481 Graph g1;
483 edges1.reserve(2);
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);
487
488 Graph g2;
490 edges2.reserve(1);
491 edges2.append(std::make_pair(0, 1));
492 auto n2 = build_graph_with_unit_arcs(g2, 2, edges2);
493
494 Binary_Lifting_LCA<Graph> bl(g1, n1(0));
495 Euler_RMQ_LCA<Graph> er(g1, n1(0));
496
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);
501
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);
506}
507
508TYPED_TEST(LcaTypedTest, RandomTreesAgainstNaiveOracle)
509{
510 using Graph = TypeParam;
511
512 std::mt19937 rng(0x1ca2026u);
513
514 for (size_t sample = 0; sample < 24; ++sample)
515 {
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);
519
520 Graph g;
521 auto nodes = build_graph_with_unit_arcs(g, n, edges);
522
523 std::uniform_int_distribution<size_t> root_pick(0, n - 1);
524 const size_t root = root_pick(rng);
525
528 Naive_Tree_Oracle oracle(n, edges, root);
529
530 for (size_t u = 0; u < n; ++u)
531 {
532 EXPECT_EQ(bl.depth_of(nodes(u)), oracle.depth(u));
533 EXPECT_EQ(er.depth_of(nodes(u)), oracle.depth(u));
534 EXPECT_EQ(bl.node_of(bl.id_of(nodes(u))), nodes(u));
535 EXPECT_EQ(er.node_of(er.id_of(nodes(u))), nodes(u));
536
537 std::uniform_int_distribution<size_t> k_pick(0, oracle.depth(u) + 2);
538 const size_t k = k_pick(rng);
539 const size_t expected = oracle.kth_ancestor(u, k);
540 auto * got = bl.kth_ancestor(nodes(u), k);
541 if (expected == Naive_Tree_Oracle::none())
542 EXPECT_EQ(got, nullptr);
543 else
544 EXPECT_EQ(static_cast<size_t>(got->get_info()), expected);
545 }
546
547 std::uniform_int_distribution<size_t> node_pick(0, n - 1);
548 for (size_t q = 0; q < 450; ++q)
549 {
550 const size_t u = node_pick(rng);
551 const size_t v = node_pick(rng);
552
553 const size_t expected_lca = oracle.lca(u, v);
554 const size_t expected_dist = oracle.distance(u, v);
555
556 auto * bl_lca = bl.lca(nodes(u), nodes(v));
557 auto * er_lca = er.lca(nodes(u), nodes(v));
558
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;
564
565 EXPECT_EQ(bl.distance(nodes(u), nodes(v)), expected_dist);
566 EXPECT_EQ(er.distance(nodes(u), nodes(v)), expected_dist);
567
568 EXPECT_EQ(bl.is_ancestor(nodes(expected_lca), nodes(u)),
569 oracle.is_ancestor(expected_lca, u));
570 EXPECT_EQ(er.is_ancestor(nodes(expected_lca), nodes(v)),
571 oracle.is_ancestor(expected_lca, v));
572 }
573 }
574}
575
576TYPED_TEST(LcaTypedTest, PerfRegression)
577{
578 using Graph = TypeParam;
579 using Clock = std::chrono::steady_clock;
580
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";
586# endif
587
588 constexpr size_t n = 100000;
589 constexpr size_t query_count = 300000;
590# ifdef NDEBUG
591 constexpr long long kBudgetMs = 12000;
592# else
593 constexpr long long kBudgetMs = 25000;
594# endif
595
596 std::mt19937 rng(0x1ca2026u);
597 const auto edges = make_random_tree_edges<Array<std::pair<size_t, size_t>>>(n, rng);
598
599 Graph g;
600 const auto nodes = build_graph_with_unit_arcs(g, n, edges);
601
602 std::uniform_int_distribution<size_t> root_pick(0, n - 1);
603 const size_t root = root_pick(rng);
604
605 const auto t_start = Clock::now();
608 const auto t_after_build = Clock::now();
609
610 // Keep an independent sanity check without impacting timed query loop.
611 Naive_Tree_Oracle oracle(n, edges, root);
612 std::uniform_int_distribution<size_t> sanity_pick(0, n - 1);
613 for (size_t i = 0; i < 64; ++i)
614 {
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);
619
620 ASSERT_EQ(static_cast<size_t>(bl.lca(nodes(u), nodes(v))->get_info()),
621 expected_lca);
622 ASSERT_EQ(static_cast<size_t>(er.lca(nodes(u), nodes(v))->get_info()),
623 expected_lca);
624 ASSERT_EQ(bl.distance(nodes(u), nodes(v)), expected_dist);
625 ASSERT_EQ(er.distance(nodes(u), nodes(v)), expected_dist);
626 }
627
628 // Reuse the same node-picking pattern as RandomTreesAgainstNaiveOracle.
629 std::uniform_int_distribution<size_t> node_pick(0, n - 1);
630 size_t checksum = 0;
631 for (size_t q = 0; q < query_count; ++q)
632 {
633 const size_t u = node_pick(rng);
634 const size_t v = node_pick(rng);
635
636 auto * bl_lca = bl.lca(nodes(u), nodes(v));
637 auto * er_lca = er.lca(nodes(u), nodes(v));
638 ASSERT_EQ(bl_lca, er_lca);
639
640 const size_t d1 = bl.distance(nodes(u), nodes(v));
641 const size_t d2 = er.distance(nodes(u), nodes(v));
642 ASSERT_EQ(d1, d2);
643
644 checksum ^= (static_cast<size_t>(bl_lca->get_info()) + 0x9e3779b9u);
645 checksum ^= (d1 + (q << 1));
646 }
647
648 const auto t_end = Clock::now();
649
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();
656
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;
665}
Lowest Common Ancestor (LCA) on rooted trees represented as Aleph graphs.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:415
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
WeightedDigraph::Node Node
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:444
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
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.
Definition LCA.H:539
size_t depth_of(const Node *node) const
Distance from root to node.
Definition LCA.H:729
bool is_empty() const noexcept
Returns true if the tree has no nodes.
Definition LCA.H:685
Node * node_of(const size_t id) const
Map an internal ID [0, n-1] back to a Node pointer.
Definition LCA.H:710
bool is_ancestor(const Node *u, const Node *v) const
Returns true if node u is an ancestor of v (or u == v).
Definition LCA.H:755
Node * kth_ancestor(const Node *node, const size_t k) const
k-th ancestor by node pointer in O(log n).
Definition LCA.H:778
size_t id_of(const Node *node) const
Map a Node pointer to its internal ID [0, n-1].
Definition LCA.H:716
Node * root() const noexcept
Returns the root node of the tree.
Definition LCA.H:691
size_t distance(const Node *u, const Node *v) const
Number of edges on the path between two nodes in O(log n).
Definition LCA.H:829
size_t size() const noexcept
Total number of nodes in the tree.
Definition LCA.H:679
size_t lca_id(size_t u, size_t v) const
LCA query by node ids in O(log n).
Definition LCA.H:785
Node * parent_of(const Node *node) const
Parent of node, or nullptr if root.
Definition LCA.H:742
Node * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(log n).
Definition LCA.H:816
LCA via Euler tour + RMQ on depth in a rooted tree.
Definition LCA.H:857
Node * parent_of(const Node *node) const
Parent of node, or nullptr if root.
Definition LCA.H:1021
Node * node_of(const size_t id) const
Map an internal ID [0, n-1] back to a Node pointer.
Definition LCA.H:989
bool is_empty() const noexcept
Returns true if the tree has no nodes.
Definition LCA.H:971
bool is_ancestor(const Node *u, const Node *v) const
Returns true if node u is an ancestor of v (or u == v).
Definition LCA.H:1034
size_t id_of(const Node *node) const
Map a Node pointer to its internal ID [0, n-1].
Definition LCA.H:995
size_t lca_id(const size_t u, const size_t v) const
LCA query by node ids in O(1).
Definition LCA.H:1040
size_t size() const noexcept
Total number of nodes in the tree.
Definition LCA.H:965
Node * root() const noexcept
Returns the root node of the tree.
Definition LCA.H:977
size_t depth_of(const Node *node) const
Distance from root to node.
Definition LCA.H:1008
Node * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(1).
Definition LCA.H:1055
size_t distance(const Node *u, const Node *v) const
Number of edges on the path between two nodes in O(1).
Definition LCA.H:1068
Arc for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:197
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Node Node
The graph type.
Definition tpl_graph.H:443
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Graph class implemented with singly-linked adjacency lists.
Definition tpl_sgraph.H:274
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
Minimal std::expected-style result type for C++20.
static mt19937 rng
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)
Definition gmpfrxx.h:4138
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Singly linked list implementations with head-tail access.
TYPED_TEST(LcaTypedTest, EmptyTree)
Definition lca_test.cc:232
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.
Definition tpl_graph.H:231
bool check(GT &g)
static int * k
Array-based graph implementation.
Dynamic array container with automatic resizing.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.