Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
LCA.H
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
93#ifndef LCA_H
94#define LCA_H
95
96# include <ah-graph-concepts.H>
97
98#include <algorithm>
99#include <bit>
100#include <cstddef>
101#include <limits>
102#include <type_traits>
103#include <utility>
104
105#include <ah-errors.H>
106#include <tpl_array.H>
107#include <tpl_dynListStack.H>
108#include <tpl_dynMapOhash.H>
109#include <tpl_dynMapTree.H>
110#include <tpl_graph.H>
111#include <tpl_sparse_table.H>
112
113namespace Aleph {
114namespace lca_detail {
126template <AlephGraph GT, ArcFilter<GT> SA>
128{
129 // moved-from: tested through Binary_Lifting_LCA and Euler_RMQ_LCA,
130 // which keep it (Tests/moved_from_graphs_test.cc)
131public:
132 using Node = typename GT::Node;
133 using Arc = typename GT::Arc;
134
135 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
136
137private:
138 using Pair_Key = std::pair<size_t, size_t>;
139
140 const GT *graph_ = nullptr;
141 SA sa_;
142 Node *root_ = nullptr;
143 size_t root_id_ = NONE;
144
145 size_t n_ = 0;
146
149
157 size_t euler_size_ = 0;
158
159 static Pair_Key normalize_pair(size_t u, size_t v) noexcept
160 {
161 if (u > v)
162 std::swap(u, v);
163 return std::make_pair(u, v);
164 }
165
167 {
169
170 if (n_ == 0)
171 {
172 ah_domain_error_if(root_ != nullptr)
173 << "Rooted_Tree_Data: root node provided but graph is empty";
174 return;
175 }
176
178 {
180 node_to_id_.swap(tmp);
181 }
182
183 size_t next_id = 0;
184 for (Node_Iterator<GT> it(*graph_); it.has_curr(); it.next_ne())
185 {
186 Node *p = it.get_curr();
187 id_to_node_(next_id) = p;
188 node_to_id_.insert(p, next_id);
189 ++next_id;
190 }
191
192 ah_runtime_error_unless(next_id == n_) << "Rooted_Tree_Data: failed to index all graph nodes";
193
194 if (root_ == nullptr)
195 root_ = id_to_node_(0);
196
198 << "Rooted_Tree_Data: root node does not belong to graph";
199
201 }
202
204 {
205 if (n_ == 0)
206 return;
207
208 adjacency_.empty();
209 adjacency_.reserve(n_);
210 for (size_t i = 0; i < n_; ++i)
211 adjacency_.append(Array<size_t>());
212
213 DynMapTree<Pair_Key, char> unique_edges;
214 size_t edge_count = 0;
215
216 for (Arc_Iterator<GT, SA> it(*graph_, sa_); it.has_curr(); it.next_ne())
217 {
218 Arc *a = it.get_curr_ne();
219 Node *src = graph_->get_src_node(a);
220 Node *tgt = graph_->get_tgt_node(a);
221
222 const auto *src_item = node_to_id_.search(src);
223 const auto *tgt_item = node_to_id_.search(tgt);
224
225 ah_runtime_error_unless(src_item != nullptr and tgt_item != nullptr)
226 << "Rooted_Tree_Data: arc endpoint is not indexed";
227
228 const size_t u = src_item->second;
229 const size_t v = tgt_item->second;
230
231 ah_domain_error_if(u == v) << "Rooted_Tree_Data: self-loop detected in filtered graph";
232
233 const Pair_Key key = normalize_pair(u, v);
234 ah_domain_error_if(unique_edges.search(key) != nullptr)
235 << "Rooted_Tree_Data: parallel/duplicate edge detected in filtered graph";
236
237 unique_edges.insert(key, 1);
238 ++edge_count;
239 }
240
241 ah_domain_error_if(edge_count != n_ - 1)
242 << "Rooted_Tree_Data: filtered graph is not a tree (expected " << (n_ - 1) << " edges, got "
243 << edge_count << ")";
244
245 for (typename DynMapTree<Pair_Key, char>::Iterator it(unique_edges); it.has_curr(); it.next_ne())
246 {
247 const auto &[fst, snd] = it.get_curr();
248 const size_t u = fst.first;
249 const size_t v = fst.second;
250 adjacency_(u).append(v);
251 adjacency_(v).append(u);
252 }
253 }
254
256 {
257 if (n_ == 0)
258 return;
259
266
267 for (size_t i = 0; i < n_; ++i)
268 parent_(i) = NONE;
269
271 for (size_t i = 0; i < n_; ++i)
272 visited(i) = 0;
273
274 struct Frame
275 {
276 size_t node;
277 size_t parent;
278 size_t next_child;
279 };
280
282
283 size_t timer = 0;
284 size_t visited_count = 1;
285
286 visited(root_id_) = 1;
287 depth_(root_id_) = 0;
289 tin_(root_id_) = timer++;
290
291 euler_size_ = 0;
294
295 stack.push({root_id_, NONE, 0});
296
297 while (not stack.is_empty())
298 {
299 auto &fr = stack.top();
300
301 if (fr.next_child == adjacency_(fr.node).size())
302 {
303 tout_(fr.node) = timer - 1;
304 (void) stack.pop();
305 if (not stack.is_empty())
306 euler_(euler_size_++) = stack.top().node;
307 continue;
308 }
309
310 const size_t nxt = adjacency_(fr.node)(fr.next_child++);
311 if (nxt == fr.parent)
312 continue;
313
314 ah_domain_error_if(visited(nxt)) << "Rooted_Tree_Data: filtered graph is not acyclic";
315
316 visited(nxt) = 1;
317 ++visited_count;
318
319 parent_(nxt) = fr.node;
320 depth_(nxt) = depth_(fr.node) + 1;
321 tin_(nxt) = timer++;
322
323 first_(nxt) = euler_size_;
324 euler_(euler_size_++) = nxt;
325
326 stack.push({nxt, fr.node, 0});
327 }
328
329 ah_domain_error_if(visited_count != n_) << "Rooted_Tree_Data: filtered graph is not connected";
330
332 << "Rooted_Tree_Data: unexpected Euler tour size";
333 }
334
335 void check_id(const size_t id, const char *where) const
336 {
338 << where << ": id=" << id << " is out of range [0, " << n_ << ")";
339 }
340
341public:
342 Rooted_Tree_Data(const GT &g, Node *root, SA sa = SA())
343 : graph_(&g), sa_(std::move(sa)), root_(root)
344 {
345 index_nodes();
348 }
349
353 Rooted_Tree_Data(const Rooted_Tree_Data & other) = default;
354
364 Rooted_Tree_Data(Rooted_Tree_Data && other) noexcept(std::is_nothrow_move_constructible_v<SA>)
365 : graph_(std::exchange(other.graph_, nullptr)), sa_(std::move(other.sa_)),
366 root_(std::exchange(other.root_, nullptr)), root_id_(std::exchange(other.root_id_, NONE)),
367 n_(std::exchange(other.n_, 0)), id_to_node_(std::move(other.id_to_node_)),
368 node_to_id_(std::move(other.node_to_id_)), adjacency_(std::move(other.adjacency_)),
369 parent_(std::move(other.parent_)), depth_(std::move(other.depth_)),
370 tin_(std::move(other.tin_)), tout_(std::move(other.tout_)), first_(std::move(other.first_)),
371 euler_(std::move(other.euler_)), euler_size_(std::exchange(other.euler_size_, 0))
372 {}
373
378 Rooted_Tree_Data & operator=(const Rooted_Tree_Data & other) = default;
379
385 Rooted_Tree_Data & operator=(Rooted_Tree_Data && other) noexcept(noexcept(swap(other)))
386 {
387 swap(other);
388 return *this;
389 }
390
397 void swap(Rooted_Tree_Data & other) noexcept(std::is_nothrow_swappable_v<SA>)
398 {
399 // the swap that the specification evaluates: that of SA found by its
400 // arguments, else std::swap(); first, since it may throw
401 using std::swap;
402 swap(sa_, other.sa_);
403 std::swap(graph_, other.graph_);
404 std::swap(root_, other.root_);
405 std::swap(root_id_, other.root_id_);
406 std::swap(n_, other.n_);
407 id_to_node_.swap(other.id_to_node_);
408 node_to_id_.swap(other.node_to_id_);
409 adjacency_.swap(other.adjacency_);
410 parent_.swap(other.parent_);
411 depth_.swap(other.depth_);
412 tin_.swap(other.tin_);
413 tout_.swap(other.tout_);
414 first_.swap(other.first_);
415 euler_.swap(other.euler_);
416 std::swap(euler_size_, other.euler_size_);
417 }
418
419 [[nodiscard]] size_t size() const noexcept
420 {
421 return n_;
422 }
423 [[nodiscard]] bool is_empty() const noexcept
424 {
425 return n_ == 0;
426 }
427 [[nodiscard]] Node *root() const noexcept
428 {
429 return root_;
430 }
431 [[nodiscard]] size_t root_id() const noexcept
432 {
433 return root_id_;
434 }
435
436 [[nodiscard]] const Array<Node *> &id_to_node() const noexcept
437 {
438 return id_to_node_;
439 }
440 [[nodiscard]] const Array<size_t> &parent() const noexcept
441 {
442 return parent_;
443 }
444 [[nodiscard]] const Array<size_t> &depth() const noexcept
445 {
446 return depth_;
447 }
448 [[nodiscard]] const Array<size_t> &tin() const noexcept
449 {
450 return tin_;
451 }
452 [[nodiscard]] const Array<size_t> &tout() const noexcept
453 {
454 return tout_;
455 }
456 [[nodiscard]] const Array<size_t> &first() const noexcept
457 {
458 return first_;
459 }
460 [[nodiscard]] const Array<size_t> &euler() const noexcept
461 {
462 return euler_;
463 }
464 [[nodiscard]] size_t euler_size() const noexcept
465 {
466 return euler_size_;
467 }
468
469 [[nodiscard]] size_t id_of(const Node *node) const
470 {
471 ah_invalid_argument_if(node == nullptr) << "Rooted_Tree_Data::id_of: null node";
472
473 const auto *item = node_to_id_.search(const_cast<Node *>(node));
474 ah_domain_error_if(item == nullptr) << "Rooted_Tree_Data::id_of: node does not belong to graph";
475
476 return item->second;
477 }
478
479 [[nodiscard]] Node *node_of(const size_t id) const
480 {
481 check_id(id, "Rooted_Tree_Data::node_of");
482 return id_to_node_(id);
483 }
484
485 void validate_id(const size_t id, const char *where) const
486 {
487 check_id(id, where);
488 }
489
490 [[nodiscard]] bool is_ancestor(const size_t u, const size_t v) const
491 {
492 check_id(u, "Rooted_Tree_Data::is_ancestor");
493 check_id(v, "Rooted_Tree_Data::is_ancestor");
494 return tin_(u) <= tin_(v) and tout_(v) <= tout_(u);
495 }
496};
497
499{
500 size_t depth = 0;
501 size_t node = 0;
502};
503
505{
506 Depth_Node operator()(const Depth_Node &a, const Depth_Node &b) const noexcept
507 {
508 if (a.depth < b.depth)
509 return a;
510 if (b.depth < a.depth)
511 return b;
512 return (a.node <= b.node) ? a : b;
513 }
514};
515} // namespace lca_detail
516
537template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
539{
540public:
541 using Node = typename GT::Node;
542
543private:
545 static constexpr size_t NONE = Topology::NONE;
546
548 size_t levels_ = 0;
549 Array<size_t> up_; // flattened: up_[k * n + v]
550
551 [[nodiscard]] size_t n() const noexcept
552 {
553 return topology_.size();
554 }
555
556 size_t &up_at(const size_t k, const size_t v) noexcept
557 {
558 return up_(k * n() + v);
559 }
560
561 [[nodiscard]] size_t up_at(const size_t k, const size_t v) const noexcept
562 {
563 return up_(k * n() + v);
564 }
565
566 void ensure_not_empty(const char *where) const
567 {
568 ah_domain_error_if(is_empty()) << where << ": tree is empty";
569 }
570
573 {
574 if (is_empty())
575 return;
576
577 levels_ = static_cast<size_t>(std::bit_width(n()));
578
580 for (size_t i = 0; i < levels_ * n(); ++i)
581 up_(i) = NONE;
582
583 for (size_t v = 0; v < n(); ++v)
584 up_at(0, v) = topology_.parent()(v);
585
586 for (size_t k = 1; k < levels_; ++k)
587 for (size_t v = 0; v < n(); ++v)
588 {
589 const size_t mid = up_at(k - 1, v);
590 up_at(k, v) = (mid == NONE) ? NONE : up_at(k - 1, mid);
591 }
592 }
593
595 [[nodiscard]] size_t lift(size_t v, size_t delta) const
596 {
597 for (size_t k = 0; k < levels_ and delta > 0 and v != NONE; ++k)
598 if ((delta >> k) & 1U)
599 v = up_at(k, v);
600 return v;
601 }
602
603public:
613 Gen_Binary_Lifting_LCA(const GT &g, Node *root, SA sa = SA()) : topology_(g, root, std::move(sa))
614 {
616 }
617
623 Gen_Binary_Lifting_LCA(const GT &g, SA sa = SA()) : topology_(g, nullptr, std::move(sa))
624 {
626 }
627
632
642 noexcept(std::is_nothrow_move_constructible_v<Topology>)
643 : topology_(std::move(other.topology_)), levels_(std::exchange(other.levels_, 0)),
644 up_(std::move(other.up_))
645 {}
646
652
659 noexcept(noexcept(swap(other)))
660 {
661 swap(other);
662 return *this;
663 }
664
671 void swap(Gen_Binary_Lifting_LCA & other) noexcept(std::is_nothrow_swappable_v<SA>)
672 {
673 topology_.swap(other.topology_);
674 std::swap(levels_, other.levels_);
675 up_.swap(other.up_);
676 }
677
679 [[nodiscard]] size_t size() const noexcept
680 {
681 return topology_.size();
682 }
683
685 [[nodiscard]] bool is_empty() const noexcept
686 {
687 return topology_.is_empty();
688 }
689
691 [[nodiscard]] Node *root() const noexcept
692 {
693 return topology_.root();
694 }
695
697 [[nodiscard]] size_t root_id() const noexcept
698 {
699 return topology_.root_id();
700 }
701
704 [[nodiscard]] size_t num_levels() const noexcept
705 {
706 return levels_;
707 }
708
710 [[nodiscard]] Node *node_of(const size_t id) const
711 {
712 return topology_.node_of(id);
713 }
714
716 [[nodiscard]] size_t id_of(const Node *node) const
717 {
718 return topology_.id_of(node);
719 }
720
722 [[nodiscard]] size_t depth_of_id(const size_t id) const
723 {
724 topology_.validate_id(id, "Gen_Binary_Lifting_LCA::depth_of_id");
725 return topology_.depth()(id);
726 }
727
729 [[nodiscard]] size_t depth_of(const Node *node) const
730 {
731 return depth_of_id(id_of(node));
732 }
733
735 [[nodiscard]] size_t parent_id(const size_t id) const
736 {
737 topology_.validate_id(id, "Gen_Binary_Lifting_LCA::parent_id");
738 return topology_.parent()(id);
739 }
740
742 [[nodiscard]] Node *parent_of(const Node *node) const
743 {
744 const size_t pid = parent_id(id_of(node));
745 return pid == NONE ? nullptr : node_of(pid);
746 }
747
749 [[nodiscard]] bool is_ancestor_id(const size_t u, const size_t v) const
750 {
751 return topology_.is_ancestor(u, v);
752 }
753
755 [[nodiscard]] bool is_ancestor(const Node *u, const Node *v) const
756 {
757 return is_ancestor_id(id_of(u), id_of(v));
758 }
759
766 [[nodiscard]] size_t kth_ancestor_id(const size_t id, const size_t k) const
767 {
768 topology_.validate_id(id, "Gen_Binary_Lifting_LCA::kth_ancestor_id");
769 if (k > topology_.depth()(id))
770 return NONE;
771 return lift(id, k);
772 }
773
778 [[nodiscard]] Node *kth_ancestor(const Node *node, const size_t k) const
779 {
780 const size_t a = kth_ancestor_id(id_of(node), k);
781 return a == NONE ? nullptr : node_of(a);
782 }
783
785 [[nodiscard]] size_t lca_id(size_t u, size_t v) const
786 {
787 ensure_not_empty("Gen_Binary_Lifting_LCA::lca_id");
788 topology_.validate_id(u, "Gen_Binary_Lifting_LCA::lca_id");
789 topology_.validate_id(v, "Gen_Binary_Lifting_LCA::lca_id");
790
791 if (topology_.depth()(u) < topology_.depth()(v))
792 std::swap(u, v);
793
794 u = lift(u, topology_.depth()(u) - topology_.depth()(v));
795 if (u == v)
796 return u;
797
798 for (size_t k = levels_; k-- > 0;)
799 {
800 const size_t uu = up_at(k, u);
801 const size_t vv = up_at(k, v);
802 if (uu != vv)
803 {
804 u = uu;
805 v = vv;
806 }
807 }
808
809 const size_t ret = topology_.parent()(u);
811 << "Gen_Binary_Lifting_LCA::lca_id: internal invalid parent";
812 return ret;
813 }
814
816 [[nodiscard]] Node *lca(const Node *u, const Node *v) const
817 {
818 return node_of(lca_id(id_of(u), id_of(v)));
819 }
820
822 [[nodiscard]] size_t distance_id(const size_t u, const size_t v) const
823 {
824 const size_t a = lca_id(u, v);
825 return topology_.depth()(u) + topology_.depth()(v) - 2 * topology_.depth()(a);
826 }
827
829 [[nodiscard]] size_t distance(const Node *u, const Node *v) const
830 {
831 return distance_id(id_of(u), id_of(v));
832 }
833};
834
855template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
857{
858public:
859 using Node = typename GT::Node;
860
861private:
863 static constexpr size_t NONE = Topology::NONE;
866
870
871 void ensure_not_empty(const char *where) const
872 {
873 ah_domain_error_if(is_empty()) << where << ": tree is empty";
874 }
875
878 {
879 if (is_empty())
880 return;
881
883 for (size_t i = 0; i < topology_.euler_size(); ++i)
884 {
885 const size_t node = topology_.euler()(i);
886 euler_depth_(i) = Depth_Node{topology_.depth()(node), node};
887 }
888
890 }
891
892public:
899 Gen_Euler_RMQ_LCA(const GT &g, Node *root, SA sa = SA())
900 : topology_(g, root, std::move(sa)), rmq_(1, Depth_Node(), Depth_Node_Min_Op())
901 {
902 build_rmq();
903 }
904
906 Gen_Euler_RMQ_LCA(const GT &g, SA sa = SA())
907 : topology_(g, nullptr, std::move(sa)), rmq_(1, Depth_Node(), Depth_Node_Min_Op())
908 {
909 build_rmq();
910 }
911
915 Gen_Euler_RMQ_LCA(const Gen_Euler_RMQ_LCA & other) = default;
916
926
932
945 noexcept(noexcept(swap(other)))
946 {
947 swap(other);
948 return *this;
949 }
950
957 void swap(Gen_Euler_RMQ_LCA & other) noexcept(std::is_nothrow_swappable_v<SA>)
958 {
959 topology_.swap(other.topology_);
960 euler_depth_.swap(other.euler_depth_);
961 rmq_.swap(other.rmq_);
962 }
963
965 [[nodiscard]] size_t size() const noexcept
966 {
967 return topology_.size();
968 }
969
971 [[nodiscard]] bool is_empty() const noexcept
972 {
973 return topology_.is_empty();
974 }
975
977 [[nodiscard]] Node *root() const noexcept
978 {
979 return topology_.root();
980 }
981
983 [[nodiscard]] size_t root_id() const noexcept
984 {
985 return topology_.root_id();
986 }
987
989 [[nodiscard]] Node *node_of(const size_t id) const
990 {
991 return topology_.node_of(id);
992 }
993
995 [[nodiscard]] size_t id_of(const Node *node) const
996 {
997 return topology_.id_of(node);
998 }
999
1001 [[nodiscard]] size_t depth_of_id(const size_t id) const
1002 {
1003 topology_.validate_id(id, "Gen_Euler_RMQ_LCA::depth_of_id");
1004 return topology_.depth()(id);
1005 }
1006
1008 [[nodiscard]] size_t depth_of(const Node *node) const
1009 {
1010 return depth_of_id(id_of(node));
1011 }
1012
1014 [[nodiscard]] size_t parent_id(const size_t id) const
1015 {
1016 topology_.validate_id(id, "Gen_Euler_RMQ_LCA::parent_id");
1017 return topology_.parent()(id);
1018 }
1019
1021 [[nodiscard]] Node *parent_of(const Node *node) const
1022 {
1023 const size_t pid = parent_id(id_of(node));
1024 return pid == NONE ? nullptr : node_of(pid);
1025 }
1026
1028 [[nodiscard]] bool is_ancestor_id(const size_t u, const size_t v) const
1029 {
1030 return topology_.is_ancestor(u, v);
1031 }
1032
1034 [[nodiscard]] bool is_ancestor(const Node *u, const Node *v) const
1035 {
1036 return is_ancestor_id(id_of(u), id_of(v));
1037 }
1038
1040 [[nodiscard]] size_t lca_id(const size_t u, const size_t v) const
1041 {
1042 ensure_not_empty("Gen_Euler_RMQ_LCA::lca_id");
1043 topology_.validate_id(u, "Gen_Euler_RMQ_LCA::lca_id");
1044 topology_.validate_id(v, "Gen_Euler_RMQ_LCA::lca_id");
1045
1046 size_t l = topology_.first()(u);
1047 size_t r = topology_.first()(v);
1048 if (l > r)
1049 std::swap(l, r);
1050
1051 return rmq_.query(l, r).node;
1052 }
1053
1055 [[nodiscard]] Node *lca(const Node *u, const Node *v) const
1056 {
1057 return node_of(lca_id(id_of(u), id_of(v)));
1058 }
1059
1061 [[nodiscard]] size_t distance_id(const size_t u, const size_t v) const
1062 {
1063 const size_t a = lca_id(u, v);
1064 return topology_.depth()(u) + topology_.depth()(v) - 2 * topology_.depth()(a);
1065 }
1066
1068 [[nodiscard]] size_t distance(const Node *u, const Node *v) const
1069 {
1070 return distance_id(id_of(u), id_of(v));
1071 }
1072
1074 [[nodiscard]] const Array<size_t> &euler_tour() const noexcept
1075 {
1076 return topology_.euler();
1077 }
1078
1080 [[nodiscard]] size_t euler_tour_size() const noexcept
1081 {
1082 return topology_.euler_size();
1083 }
1084};
1085
1087template <class GT, class SA = Dft_Show_Arc<GT>>
1089
1091template <class GT, class SA = Dft_Show_Arc<GT>>
1093} // namespace Aleph
1094
1095#endif // LCA_H
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Definition ah-errors.H:399
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
C++20 concepts for the protocol shared by graph algorithms.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
Dynamic stack of elements of generic type T based on a singly linked list.
T & top()
Return a modifiable reference to the top item of the stack.
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.
Generic key-value map implemented on top of a binary search tree.
typename Base::Iterator Iterator
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
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
size_t depth_of_id(const size_t id) const
Distance from root to node by ID.
Definition LCA.H:722
typename GT::Node Node
Definition LCA.H:541
Gen_Binary_Lifting_LCA(const GT &g, Node *root, SA sa=SA())
Construct from graph + explicit root.
Definition LCA.H:613
Gen_Binary_Lifting_LCA(const Gen_Binary_Lifting_LCA &other)=default
Copy the tree and the jump table of another engine.
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
size_t parent_id(const size_t id) const
Parent ID of node id, or NONE if root.
Definition LCA.H:735
size_t n() const noexcept
Definition LCA.H:551
size_t num_levels() const noexcept
Returns the number of levels in the jump table: , or 0 if the tree is empty.
Definition LCA.H:704
Gen_Binary_Lifting_LCA(Gen_Binary_Lifting_LCA &&other) noexcept(std::is_nothrow_move_constructible_v< Topology >)
Take the tree and the jump table of another engine, which is left empty.
Definition LCA.H:641
size_t distance_id(const size_t u, const size_t v) const
Number of edges on the path between two ids in O(log n).
Definition LCA.H:822
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
void swap(Gen_Binary_Lifting_LCA &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this engine and another.
Definition LCA.H:671
Gen_Binary_Lifting_LCA(const GT &g, SA sa=SA())
Construct using the first graph node as root.
Definition LCA.H:623
size_t kth_ancestor_id(const size_t id, const size_t k) const
k-th ancestor by id in O(log n).
Definition LCA.H:766
size_t & up_at(const size_t k, const size_t v) noexcept
Definition LCA.H:556
size_t up_at(const size_t k, const size_t v) const noexcept
Definition LCA.H:561
Array< size_t > up_
Definition LCA.H:549
size_t id_of(const Node *node) const
Map a Node pointer to its internal ID [0, n-1].
Definition LCA.H:716
Gen_Binary_Lifting_LCA & operator=(const Gen_Binary_Lifting_LCA &other)=default
Copy the tree and the jump table of another engine.
Gen_Binary_Lifting_LCA & operator=(Gen_Binary_Lifting_LCA &&other) noexcept(noexcept(swap(other)))
Exchange this engine with another, which is left with the previous tree of this one.
Definition LCA.H:658
Node * root() const noexcept
Returns the root node of the tree.
Definition LCA.H:691
size_t lift(size_t v, size_t delta) const
Definition LCA.H:595
bool is_ancestor_id(const size_t u, const size_t v) const
Returns true if node u is an ancestor of v (or u == v).
Definition LCA.H:749
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
static constexpr size_t NONE
Definition LCA.H:545
size_t root_id() const noexcept
Returns the internal ID of the root node.
Definition LCA.H:697
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
void ensure_not_empty(const char *where) const
Definition LCA.H:566
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
static constexpr size_t NONE
Definition LCA.H:863
Gen_Euler_RMQ_LCA(Gen_Euler_RMQ_LCA &&other)=default
Take the tree and the tables of another engine, which is left empty.
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
lca_detail::Depth_Node_Min_Op Depth_Node_Min_Op
Definition LCA.H:865
Gen_Euler_RMQ_LCA(const GT &g, SA sa=SA())
Construct using the first graph node as root.
Definition LCA.H:906
typename GT::Node Node
Definition LCA.H:859
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 depth_of_id(const size_t id) const
Distance from root to node by ID.
Definition LCA.H:1001
void swap(Gen_Euler_RMQ_LCA &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this engine and another.
Definition LCA.H:957
Gen_Euler_RMQ_LCA(const Gen_Euler_RMQ_LCA &other)=default
Copy the tree and the tables of another engine.
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
void ensure_not_empty(const char *where) const
Definition LCA.H:871
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 distance_id(const size_t u, const size_t v) const
Number of edges on the path between two ids in O(1).
Definition LCA.H:1061
Gen_Euler_RMQ_LCA(const GT &g, Node *root, SA sa=SA())
Construct from graph + explicit root.
Definition LCA.H:899
size_t root_id() const noexcept
Returns the internal ID of the root node.
Definition LCA.H:983
size_t depth_of(const Node *node) const
Distance from root to node.
Definition LCA.H:1008
size_t parent_id(const size_t id) const
Parent ID of node id, or NONE if root.
Definition LCA.H:1014
Node * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(1).
Definition LCA.H:1055
Gen_Euler_RMQ_LCA & operator=(const Gen_Euler_RMQ_LCA &other)=default
Copy the tree and the tables of another engine.
bool is_ancestor_id(const size_t u, const size_t v) const
Returns true if node u is an ancestor of v (or u == v).
Definition LCA.H:1028
size_t euler_tour_size() const noexcept
Euler tour size ( for empty tree, else ).
Definition LCA.H:1080
const Array< size_t > & euler_tour() const noexcept
Euler tour sequence of node ids ( entries).
Definition LCA.H:1074
Gen_Euler_RMQ_LCA & operator=(Gen_Euler_RMQ_LCA &&other) noexcept(noexcept(swap(other)))
Exchange this engine with another, which is left with the previous tree of this one.
Definition LCA.H:944
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
Gen_Sparse_Table< Depth_Node, Depth_Node_Min_Op > rmq_
Definition LCA.H:869
Array< Depth_Node > euler_depth_
Definition LCA.H:868
Sparse Table over an arbitrary associative and idempotent binary operation.
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
Rooted_Tree_Data(const GT &g, Node *root, SA sa=SA())
Definition LCA.H:342
size_t root_id() const noexcept
Definition LCA.H:431
const Array< size_t > & depth() const noexcept
Definition LCA.H:444
Array< Array< size_t > > adjacency_
Definition LCA.H:150
MapOLhash< Node *, size_t > node_to_id_
Definition LCA.H:148
size_t size() const noexcept
Definition LCA.H:419
const Array< Node * > & id_to_node() const noexcept
Definition LCA.H:436
size_t euler_size() const noexcept
Definition LCA.H:464
Array< Node * > id_to_node_
Definition LCA.H:147
Rooted_Tree_Data & operator=(Rooted_Tree_Data &&other) noexcept(noexcept(swap(other)))
Exchange this topology with another, which is left with the previous topology of this one.
Definition LCA.H:385
const Array< size_t > & euler() const noexcept
Definition LCA.H:460
static constexpr size_t NONE
Definition LCA.H:135
static Pair_Key normalize_pair(size_t u, size_t v) noexcept
Definition LCA.H:159
void check_id(const size_t id, const char *where) const
Definition LCA.H:335
void swap(Rooted_Tree_Data &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this topology and another.
Definition LCA.H:397
Node * root() const noexcept
Definition LCA.H:427
Rooted_Tree_Data & operator=(const Rooted_Tree_Data &other)=default
Copy the topology of another tree.
bool is_ancestor(const size_t u, const size_t v) const
Definition LCA.H:490
Rooted_Tree_Data(const Rooted_Tree_Data &other)=default
Copy the topology of another tree.
const Array< size_t > & first() const noexcept
Definition LCA.H:456
const Array< size_t > & tout() const noexcept
Definition LCA.H:452
Rooted_Tree_Data(Rooted_Tree_Data &&other) noexcept(std::is_nothrow_move_constructible_v< SA >)
Take the topology of another tree, which is left empty.
Definition LCA.H:364
const Array< size_t > & tin() const noexcept
Definition LCA.H:448
Node * node_of(const size_t id) const
Definition LCA.H:479
const Array< size_t > & parent() const noexcept
Definition LCA.H:440
void validate_id(const size_t id, const char *where) const
Definition LCA.H:485
size_t id_of(const Node *node) const
Definition LCA.H:469
std::pair< size_t, size_t > Pair_Key
Definition LCA.H:138
bool is_empty() const noexcept
Definition LCA.H:423
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Open addressing hash map using linear probing.
Data & find(const Key &key)
Find and return the value for a key.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Search for a key in the map.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair (copy semantics).
Depth_Node operator()(const Depth_Node &a, const Depth_Node &b) const noexcept
Definition LCA.H:506
static int * k
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
Dynamic stack implementation based on linked lists.
Dynamic map with open hashing.
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.
Sparse Table for static range queries in O(1).
DynList< int > l