Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Tree_Decomposition.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
31
111# ifndef TREE_DECOMPOSITION_H
112# define TREE_DECOMPOSITION_H
113
114# include <ah-graph-concepts.H>
115
116# include <algorithm>
117# include <concepts>
118# include <cstddef>
119# include <limits>
120# include <type_traits>
121# include <utility>
122
123# include <ah-errors.H>
124# include <ahFunction.H>
125# include <tpl_array.H>
126# include <tpl_dynListStack.H>
127# include <tpl_dynMapOhash.H>
128# include <tpl_dynMapTree.H>
129# include <tpl_graph.H>
130# include <tpl_segment_tree.H>
131
132namespace Aleph
133{
134 namespace tree_decomposition_detail
135 {
139 struct Id_Range
140 {
141 size_t left = 0;
142 size_t right = 0;
143 };
144
153 template <AlephGraph GT, ArcFilter<GT> SA>
155 {
156 // moved-from: tested through Heavy_Light_Decomposition, HLD_Path_Query
157 // and Centroid_Decomposition, which keep it
158 // (Tests/moved_from_graphs_test.cc)
159 public:
160 using Node = typename GT::Node;
161 using Arc = typename GT::Arc;
162
163 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
164
165 private:
166 using Pair_Key = std::pair<size_t, size_t>;
167
168 const GT *graph_ = nullptr;
169 SA sa_;
170 Node *root_ = nullptr;
171 size_t root_id_ = NONE;
172
173 size_t n_ = 0;
174
178
179 static Pair_Key normalize_pair(size_t u, size_t v) noexcept
180 {
181 if (u > v)
182 std::swap(u, v);
183 return std::make_pair(u, v);
184 }
185
186 void check_id(const size_t id, const char *where) const
187 {
189 << where << ": id=" << id << " is out of range [0, " << n_ << ")";
190 }
191
193 {
195
196 if (n_ == 0)
197 {
198 ah_domain_error_if(root_ != nullptr)
199 << "Rooted_Tree_Topology: root node provided but graph is empty";
200 return;
201 }
202
205 node_to_id_.swap(tmp);
206 }
207
208 size_t next_id = 0;
209 for (Node_Iterator<GT> it(*graph_); it.has_curr(); it.next_ne())
210 {
211 Node *p = it.get_curr();
212 id_to_node_(next_id) = p;
213 node_to_id_.insert(p, next_id);
214 ++next_id;
215 }
216
217 ah_runtime_error_unless(next_id == n_)
218 << "Rooted_Tree_Topology: failed to index all graph nodes";
219
220 if (root_ == nullptr)
221 root_ = id_to_node_(0);
222
224 << "Rooted_Tree_Topology: root node does not belong to graph";
225
227 }
228
230 {
231 if (n_ == 0)
232 return;
233
234 adjacency_.empty();
235 adjacency_.reserve(n_);
236 for (size_t i = 0; i < n_; ++i)
237 adjacency_.append(Array<size_t>());
238
239 DynMapTree<Pair_Key, char> unique_edges;
240 size_t edge_count = 0;
241
242 for (Arc_Iterator<GT, SA> it(*graph_, sa_); it.has_curr(); it.next_ne())
243 {
244 Arc *a = it.get_curr_ne();
245 Node *src = graph_->get_src_node(a);
246 Node *tgt = graph_->get_tgt_node(a);
247
248 const auto *src_item = node_to_id_.search(src);
249 const auto *tgt_item = node_to_id_.search(tgt);
250
251 ah_runtime_error_unless(src_item != nullptr and tgt_item != nullptr)
252 << "Rooted_Tree_Topology: arc endpoint is not indexed";
253
254 const size_t u = src_item->second;
255 const size_t v = tgt_item->second;
256
257 ah_domain_error_if(u == v)
258 << "Rooted_Tree_Topology: self-loop detected in filtered graph";
259
260 const Pair_Key key = normalize_pair(u, v);
261 ah_domain_error_if(unique_edges.search(key) != nullptr)
262 << "Rooted_Tree_Topology: parallel/duplicate edge detected in filtered graph";
263
264 unique_edges.insert(key, 1);
265 ++edge_count;
266 }
267
268 ah_domain_error_if(edge_count != n_ - 1)
269 << "Rooted_Tree_Topology: filtered graph is not a tree (expected "
270 << (n_ - 1) << " edges, got " << edge_count << ")";
271
272 for (typename DynMapTree<Pair_Key, char>::Iterator it(unique_edges);
273 it.has_curr(); it.next_ne())
274 {
275 const auto & [fst, snd] = it.get_curr();
276 (void) snd;
277
278 const size_t u = fst.first;
279 const size_t v = fst.second;
280 adjacency_(u).append(v);
281 adjacency_(v).append(u);
282 }
283 }
284
286 {
287 if (n_ == 0)
288 return;
289
291 for (size_t i = 0; i < n_; ++i)
292 visited(i) = 0;
293
294 struct Frame
295 {
296 size_t node;
297 size_t parent;
298 size_t next_child;
299 };
300
302 visited(root_id_) = 1;
303 size_t visited_count = 1;
304 stack.push({root_id_, NONE, 0});
305
306 while (not stack.is_empty())
307 {
308 auto & fr = stack.top();
309
310 if (fr.next_child == adjacency_(fr.node).size())
311 {
312 (void) stack.pop();
313 continue;
314 }
315
316 const size_t nxt = adjacency_(fr.node)(fr.next_child++);
317 if (nxt == fr.parent)
318 continue;
319
320 ah_domain_error_if(visited(nxt))
321 << "Rooted_Tree_Topology: filtered graph is not acyclic";
322
323 visited(nxt) = 1;
324 ++visited_count;
325 stack.push({nxt, fr.node, 0});
326 }
327
328 ah_domain_error_if(visited_count != n_)
329 << "Rooted_Tree_Topology: filtered graph is not connected";
330 }
331
332 public:
333 Rooted_Tree_Topology(const GT & g, Node *root, SA sa = SA())
334 : graph_(&g), sa_(std::move(sa)), root_(root)
335 {
336 index_nodes();
339 }
340
345
356 noexcept(std::is_nothrow_move_constructible_v<SA>)
357 : graph_(std::exchange(other.graph_, nullptr)), sa_(std::move(other.sa_)),
358 root_(std::exchange(other.root_, nullptr)),
359 root_id_(std::exchange(other.root_id_, NONE)), n_(std::exchange(other.n_, 0)),
360 id_to_node_(std::move(other.id_to_node_)), node_to_id_(std::move(other.node_to_id_)),
361 adjacency_(std::move(other.adjacency_))
362 {}
363
369
376 noexcept(noexcept(swap(other)))
377 {
378 swap(other);
379 return *this;
380 }
381
389 void swap(Rooted_Tree_Topology & other) noexcept(std::is_nothrow_swappable_v<SA>)
390 {
391 // the swap that the specification evaluates: that of SA found by
392 // its arguments, else std::swap(); first, since it may throw
393 using std::swap;
394 swap(sa_, other.sa_);
395 std::swap(graph_, other.graph_);
396 std::swap(root_, other.root_);
397 std::swap(root_id_, other.root_id_);
398 std::swap(n_, other.n_);
399 id_to_node_.swap(other.id_to_node_);
400 node_to_id_.swap(other.node_to_id_);
401 adjacency_.swap(other.adjacency_);
402 }
403
404 [[nodiscard]] size_t size() const noexcept { return n_; }
405 [[nodiscard]] bool is_empty() const noexcept { return n_ == 0; }
406 [[nodiscard]] Node * root() const noexcept { return root_; }
407 [[nodiscard]] size_t root_id() const noexcept { return root_id_; }
408
409 [[nodiscard]] const Array<Node *> &id_to_node() const noexcept
410 {
411 return id_to_node_;
412 }
413
414 [[nodiscard]] const Array<Array<size_t>> &adjacency() const noexcept
415 {
416 return adjacency_;
417 }
418
419 [[nodiscard]] size_t id_of(const Node *node) const
420 {
421 ah_invalid_argument_if(node == nullptr)
422 << "Rooted_Tree_Topology::id_of: null node";
423
424 const auto *item = node_to_id_.search(const_cast<Node *>(node));
425 ah_domain_error_if(item == nullptr)
426 << "Rooted_Tree_Topology::id_of: node does not belong to graph";
427
428 return item->second;
429 }
430
431 [[nodiscard]] Node * node_of(const size_t id) const
432 {
433 check_id(id, "Rooted_Tree_Topology::node_of");
434 return id_to_node_(id);
435 }
436
437 void validate_id(const size_t id, const char *where) const
438 {
439 check_id(id, where);
440 }
441 };
442 } // namespace tree_decomposition_detail
443
444
459 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
461 {
462 public:
463 using Node = typename GT::Node;
465
466 private:
468 static constexpr size_t NONE = Topology::NONE;
469
471
479
480 void ensure_not_empty(const char *where) const
481 {
482 ah_domain_error_if(is_empty()) << where << ": tree is empty";
483 }
484
486 {
487 const size_t n = size();
488 if (n == 0)
489 return;
490
498
499 for (size_t i = 0; i < n; ++i)
500 {
501 parent_(i) = NONE;
502 depth_(i) = 0;
503 subtree_size_(i) = 0;
504 heavy_(i) = NONE;
505 head_(i) = NONE;
506 pos_(i) = NONE;
507 inv_pos_(i) = NONE;
508 }
509
510 Array<char> visited = Array<char>::create(n);
511 for (size_t i = 0; i < n; ++i)
512 visited(i) = 0;
513
514 Array<size_t> order;
515 order.reserve(n);
516
517 struct Frame
518 {
519 size_t node;
520 size_t parent;
521 size_t next_child;
522 };
523
524 const size_t root_id = topology_.root_id();
526 depth_(root_id) = 0;
527 visited(root_id) = 1;
528
529 size_t visited_count = 1;
530 order.append(root_id);
531
533 stack.push({root_id, NONE, 0});
534
535 while (not stack.is_empty())
536 {
537 auto & fr = stack.top();
538
539 if (fr.next_child == topology_.adjacency()(fr.node).size())
540 {
541 (void) stack.pop();
542 continue;
543 }
544
545 const size_t nxt = topology_.adjacency()(fr.node)(fr.next_child++);
546 if (nxt == fr.parent)
547 continue;
548
549 ah_domain_error_if(visited(nxt))
550 << "Gen_Heavy_Light_Decomposition: filtered graph is not acyclic";
551
552 visited(nxt) = 1;
553 ++visited_count;
554
555 parent_(nxt) = fr.node;
556 depth_(nxt) = depth_(fr.node) + 1;
557 order.append(nxt);
558
559 stack.push({nxt, fr.node, 0});
560 }
561
562 ah_domain_error_if(visited_count != n)
563 << "Gen_Heavy_Light_Decomposition: filtered graph is not connected";
564
565 for (size_t i = order.size(); i-- > 0;)
566 {
567 const size_t u = order(i);
568 subtree_size_(u) = 1;
569 heavy_(u) = NONE;
570
571 for (size_t j = 0; j < topology_.adjacency()(u).size(); ++j)
572 {
573 const size_t v = topology_.adjacency()(u)(j);
574 if (parent_(v) != u)
575 continue;
576
578 if (heavy_(u) == NONE or subtree_size_(v) > subtree_size_(heavy_(u)))
579 heavy_(u) = v;
580 }
581 }
582
583 size_t next_pos = 0;
585 chain_stack.push({root_id, root_id});
586
587 while (not chain_stack.is_empty())
588 {
589 const auto [chain_head, chain_start] = chain_stack.pop();
590
591 size_t u = chain_start;
592 while (u != NONE)
593 {
594 head_(u) = chain_head;
595 pos_(u) = next_pos;
596 inv_pos_(next_pos) = u;
597 ++next_pos;
598
599 for (size_t j = 0; j < topology_.adjacency()(u).size(); ++j)
600 if (const size_t v = topology_.adjacency()(u)(j); parent_(v) == u and v != heavy_(u))
601 chain_stack.push({v, v});
602
603 u = heavy_(u);
604 }
605 }
606
607 ah_runtime_error_unless(next_pos == n)
608 << "Gen_Heavy_Light_Decomposition: invalid decomposition size";
609 }
610
611 public:
618 Gen_Heavy_Light_Decomposition(const GT & g, Node *root, SA sa = SA())
619 : topology_(g, root, std::move(sa))
620 {
621 build_decomposition();
622 }
623
629 Gen_Heavy_Light_Decomposition(const GT & g, SA sa = SA())
630 : topology_(g, nullptr, std::move(sa))
631 {
632 build_decomposition();
633 }
634
635 [[nodiscard]] size_t size() const noexcept { return topology_.size(); }
636 [[nodiscard]] bool is_empty() const noexcept { return topology_.is_empty(); }
637
638 [[nodiscard]] Node * root() const noexcept { return topology_.root(); }
639 [[nodiscard]] size_t root_id() const noexcept { return topology_.root_id(); }
640
641 [[nodiscard]] Node * node_of(const size_t id) const
642 {
643 return topology_.node_of(id);
644 }
645
646 [[nodiscard]] size_t id_of(const Node *node) const
647 {
648 return topology_.id_of(node);
649 }
650
651 [[nodiscard]] size_t parent_id(const size_t id) const
652 {
653 topology_.validate_id(id, "Gen_Heavy_Light_Decomposition::parent_id");
654 return parent_(id);
655 }
656
657 [[nodiscard]] Node * parent_of(const Node *node) const
658 {
659 const size_t pid = parent_id(id_of(node));
660 return pid == NONE ? nullptr : node_of(pid);
661 }
662
663 [[nodiscard]] size_t depth_of_id(const size_t id) const
664 {
665 topology_.validate_id(id, "Gen_Heavy_Light_Decomposition::depth_of_id");
666 return depth_(id);
667 }
668
669 [[nodiscard]] size_t depth_of(const Node *node) const
670 {
671 return depth_of_id(id_of(node));
672 }
673
674 [[nodiscard]] size_t subtree_size_of_id(const size_t id) const
675 {
676 topology_.validate_id(id, "Gen_Heavy_Light_Decomposition::subtree_size_of_id");
677 return subtree_size_(id);
678 }
679
680 [[nodiscard]] size_t subtree_size_of(const Node *node) const
681 {
682 return subtree_size_of_id(id_of(node));
683 }
684
685 [[nodiscard]] size_t heavy_child_id(const size_t id) const
686 {
687 topology_.validate_id(id, "Gen_Heavy_Light_Decomposition::heavy_child_id");
688 return heavy_(id);
689 }
690
691 [[nodiscard]] Node * heavy_child(const Node *node) const
692 {
693 const size_t cid = heavy_child_id(id_of(node));
694 return cid == NONE ? nullptr : node_of(cid);
695 }
696
697 [[nodiscard]] size_t head_id(const size_t id) const
698 {
699 topology_.validate_id(id, "Gen_Heavy_Light_Decomposition::head_id");
700 return head_(id);
701 }
702
703 [[nodiscard]] Node * head_of(const Node *node) const
704 {
705 return node_of(head_id(id_of(node)));
706 }
707
708 [[nodiscard]] size_t position_of_id(const size_t id) const
709 {
710 topology_.validate_id(id, "Gen_Heavy_Light_Decomposition::position_of_id");
711 return pos_(id);
712 }
713
714 [[nodiscard]] size_t position_of(const Node *node) const
715 {
716 return position_of_id(id_of(node));
717 }
718
720 [[nodiscard]] Range subtree_range_id(const size_t id) const
721 {
722 topology_.validate_id(id, "Gen_Heavy_Light_Decomposition::subtree_range_id");
723 const size_t l = pos_(id);
724 const size_t r = l + subtree_size_(id) - 1;
725 return Range{l, r};
726 }
727
729 [[nodiscard]] Range subtree_range(const Node *node) const
730 {
731 return subtree_range_id(id_of(node));
732 }
733
735 [[nodiscard]] bool is_ancestor_id(const size_t u, const size_t v) const
736 {
737 const Range r = subtree_range_id(u);
738 const size_t pv = position_of_id(v);
739 return r.left <= pv and pv <= r.right;
740 }
741
743 [[nodiscard]] bool is_ancestor(const Node *u, const Node *v) const
744 {
745 return is_ancestor_id(id_of(u), id_of(v));
746 }
747
749 [[nodiscard]] size_t lca_id(size_t u, size_t v) const
750 {
751 ensure_not_empty("Gen_Heavy_Light_Decomposition::lca_id");
752 topology_.validate_id(u, "Gen_Heavy_Light_Decomposition::lca_id");
753 topology_.validate_id(v, "Gen_Heavy_Light_Decomposition::lca_id");
754
755 while (head_(u) != head_(v))
756 if (depth_(head_(u)) >= depth_(head_(v)))
757 u = parent_(head_(u));
758 else
759 v = parent_(head_(v));
760
761 return (depth_(u) <= depth_(v)) ? u : v;
762 }
763
765 [[nodiscard]] Node * lca(const Node *u, const Node *v) const
766 {
767 return node_of(lca_id(id_of(u), id_of(v)));
768 }
769
771 [[nodiscard]] size_t distance_id(const size_t u, const size_t v) const
772 {
773 const size_t a = lca_id(u, v);
774 return depth_(u) + depth_(v) - 2 * depth_(a);
775 }
776
778 [[nodiscard]] size_t distance(const Node *u, const Node *v) const
779 {
780 return distance_id(id_of(u), id_of(v));
781 }
782
793 template <class F>
794 void for_each_path_segment_id(size_t u, size_t v, F && visit) const
795 {
796 ensure_not_empty("Gen_Heavy_Light_Decomposition::for_each_path_segment_id");
797 topology_.validate_id(u, "Gen_Heavy_Light_Decomposition::for_each_path_segment_id");
798 topology_.validate_id(v, "Gen_Heavy_Light_Decomposition::for_each_path_segment_id");
799
800 DynListStack<Range> down_segments;
801
802 while (head_(u) != head_(v))
803 if (depth_(head_(u)) >= depth_(head_(v)))
804 {
805 visit(pos_(head_(u)), pos_(u), true);
806 u = parent_(head_(u));
807 }
808 else
809 {
810 down_segments.push(Range{pos_(head_(v)), pos_(v)});
811 v = parent_(head_(v));
812 }
813
814 if (depth_(u) >= depth_(v))
815 visit(pos_(v), pos_(u), true);
816 else
817 down_segments.push(Range{pos_(u), pos_(v)});
818
819 while (not down_segments.is_empty())
820 {
821 const auto [left, right] = down_segments.pop();
822 visit(left, right, false);
823 }
824 }
825
827 template <class F>
828 void for_each_path_segment(const Node *u, const Node *v, F && visit) const
829 {
830 for_each_path_segment_id(id_of(u), id_of(v), std::forward<F>(visit));
831 }
832
837 template <class F>
839 const size_t v,
840 F && visit) const
841 {
842 for_each_path_segment_id(u, v,
843 [&](const size_t l, const size_t r, const bool)
844 {
845 visit(l, r);
846 });
847 }
848 };
849
850
875 template <AlephGraph GT,
876 typename T = typename GT::Node::Node_Type,
877 class Op = Aleph::plus<T>,
878 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
880 {
881 public:
882 using Node = typename GT::Node;
883
884 private:
888 Op op_;
889
890 void ensure_not_empty(const char *where) const
891 {
892 ah_domain_error_if(is_empty()) << where << ": tree is empty";
893 }
894
895 template <class Getter>
896 requires std::invocable<Getter, Node *> and
897 std::convertible_to<std::invoke_result_t<Getter, Node *>, T>
899 Getter getter)
900 {
901 const size_t n = hld.size();
902 if (n == 0)
903 return Array<T>();
904
905 auto base = Array<T>::create(n);
906 for (size_t id = 0; id < n; ++id)
907 {
908 const size_t p = hld.position_of_id(id);
909 base(p) = static_cast<T>(getter(hld.node_of(id)));
910 }
911
912 return base;
913 }
914
916 {
917 return build_base_array(hld,
918 [](Node *p) -> T
919 {
920 return static_cast<T>(p->get_info());
921 });
922 }
923
924 public:
927 Node *root,
928 const T & identity,
929 Op op = Op(),
930 SA sa = SA())
931 : hld_(g, root, std::move(sa)),
933 identity_(identity),
934 op_(op)
935 {
936 // Empty
937 }
938
941 const T & identity,
942 Op op = Op(),
943 SA sa = SA())
944 : hld_(g, std::move(sa)),
946 identity_(identity),
947 op_(op)
948 {
949 // Empty
950 }
951
953 template <class Getter>
954 requires std::invocable<Getter, Node *> and
955 std::convertible_to<std::invoke_result_t<Getter, Node *>, T>
957 Node *root,
958 Getter getter,
959 const T & identity,
960 Op op = Op(),
961 SA sa = SA())
962 : hld_(g, root, std::move(sa)),
963 segment_tree_(build_base_array(hld_, getter), identity, op),
964 identity_(identity),
965 op_(op)
966 {
967 // Empty
968 }
969
971 template <class Getter>
972 requires std::invocable<Getter, Node *> and
973 std::convertible_to<std::invoke_result_t<Getter, Node *>, T>
975 Getter getter,
976 const T & identity,
977 Op op = Op(),
978 SA sa = SA())
979 : hld_(g, std::move(sa)),
980 segment_tree_(build_base_array(hld_, getter), identity, op),
981 identity_(identity),
982 op_(op)
983 {
984 // Empty
985 }
986
990 Gen_HLD_Path_Query(const Gen_HLD_Path_Query & other) = default;
991
1001
1007
1020 noexcept(noexcept(swap(other)))
1021 {
1022 swap(other);
1023 return *this;
1024 }
1025
1038 noexcept(std::is_nothrow_swappable_v<T> and std::is_nothrow_swappable_v<Op>
1039 and std::is_nothrow_move_assignable_v<Gen_Heavy_Light_Decomposition<GT, SA>>)
1040 {
1041 // The values and the operations are swapped as the specification
1042 // evaluates them: found by their arguments, else std::swap()
1043 using std::swap;
1044 if constexpr (noexcept(std::swap(hld_, other.hld_)) and std::is_nothrow_swappable_v<T>
1045 and std::is_nothrow_swappable_v<Op>)
1046 {
1047 // None may throw: the order cannot be seen, and that of the
1048 // members keeps the code of the common types. std::swap() moves
1049 // the decomposition through a temporary
1050 std::swap(hld_, other.hld_);
1051 segment_tree_.swap(other.segment_tree_);
1052 swap(identity_, other.identity_);
1053 swap(op_, other.op_);
1054 }
1055 else
1056 swap_all_or_none(other);
1057 }
1058
1059 private:
1060 // The values, the operations, the segment trees, whose swap is all or
1061 // none, and the decompositions, which their move assignment exchanges,
1062 // all or none, as those of their topologies (the filters first) and of
1063 // their arrays do. If one throws, those before it are exchanged back
1065 {
1066 using std::swap;
1067 int done = 0;
1068 try
1069 {
1070 swap(identity_, other.identity_);
1071 ++done;
1072 swap(op_, other.op_);
1073 ++done;
1074 segment_tree_.swap(other.segment_tree_);
1075 ++done;
1076 hld_ = std::move(other.hld_);
1077 }
1078 catch (...)
1079 {
1080 if (done > 2)
1081 segment_tree_.swap(other.segment_tree_);
1082 if (done > 1)
1083 swap(op_, other.op_);
1084 if (done > 0)
1085 swap(identity_, other.identity_);
1086 throw;
1087 }
1088 }
1089
1090 public:
1091 [[nodiscard]] const Gen_Heavy_Light_Decomposition<GT, SA> &decomposition() const noexcept
1092 {
1093 return hld_;
1094 }
1095
1096 [[nodiscard]] size_t size() const noexcept { return hld_.size(); }
1097 [[nodiscard]] bool is_empty() const noexcept { return hld_.is_empty(); }
1098
1100 [[nodiscard]] T query_path_id(const size_t u, const size_t v) const
1101 {
1102 ensure_not_empty("Gen_HLD_Path_Query::query_path_id");
1103
1104 T ret = identity_;
1105 hld_.for_each_path_segment_id(u, v,
1106 [&](const size_t l,
1107 const size_t r,
1108 const bool)
1109 {
1110 ret = op_(ret, segment_tree_.query(l, r));
1111 });
1112 return ret;
1113 }
1114
1116 [[nodiscard]] T query_path(const Node *u, const Node *v) const
1117 {
1118 return query_path_id(hld_.id_of(u), hld_.id_of(v));
1119 }
1120
1122 [[nodiscard]] T query_subtree_id(const size_t id) const
1123 {
1124 ensure_not_empty("Gen_HLD_Path_Query::query_subtree_id");
1125 const auto range = hld_.subtree_range_id(id);
1126 return segment_tree_.query(range.left, range.right);
1127 }
1128
1130 [[nodiscard]] T query_subtree(const Node *node) const
1131 {
1132 return query_subtree_id(hld_.id_of(node));
1133 }
1134
1136 void update_node_id(const size_t id, const T & delta)
1137 {
1138 ensure_not_empty("Gen_HLD_Path_Query::update_node_id");
1139 segment_tree_.update(hld_.position_of_id(id), delta);
1140 }
1141
1143 void update_node(const Node *node, const T & delta)
1144 {
1145 update_node_id(hld_.id_of(node), delta);
1146 }
1147
1149 void set_node_id(const size_t id, const T & value)
1150 {
1151 ensure_not_empty("Gen_HLD_Path_Query::set_node_id");
1152 segment_tree_.set(hld_.position_of_id(id), value);
1153 }
1154
1156 void set_node(const Node *node, const T & value)
1157 {
1158 set_node_id(hld_.id_of(node), value);
1159 }
1160
1162 [[nodiscard]] T get_node_id(const size_t id) const
1163 {
1164 ensure_not_empty("Gen_HLD_Path_Query::get_node_id");
1165 return segment_tree_.get(hld_.position_of_id(id));
1166 }
1167
1169 [[nodiscard]] T get_node(const Node *node) const
1170 {
1171 return get_node_id(hld_.id_of(node));
1172 }
1173 };
1174
1175
1194 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1196 {
1197 public:
1198 using Node = typename GT::Node;
1199
1200 private:
1202 static constexpr size_t NONE = Topology::NONE;
1203
1205
1210
1213
1216
1217 void ensure_not_empty(const char *where) const
1218 {
1219 ah_domain_error_if(is_empty()) << where << ": tree is empty";
1220 }
1221
1223 {
1224 if (is_empty())
1225 return;
1226
1227 const size_t n = size();
1235
1236 for (size_t i = 0; i < n; ++i)
1237 {
1238 removed_(i) = 0;
1240 centroid_level_(i) = 0;
1241 local_parent_(i) = NONE;
1242 local_subtree_(i) = 0;
1245 }
1246
1248 }
1249
1251 {
1254
1255 local_parent_(start) = NONE;
1256 stack.push(start);
1257
1258 while (not stack.is_empty())
1259 {
1260 const size_t u = stack.pop();
1261 nodes.append(u);
1262
1263 for (size_t i = 0; i < topology_.adjacency()(u).size(); ++i)
1264 {
1265 const size_t v = topology_.adjacency()(u)(i);
1266 if (removed_(v))
1267 continue;
1268
1269 if (v == local_parent_(u))
1270 continue;
1271
1272 local_parent_(v) = u;
1273 stack.push(v);
1274 }
1275 }
1276
1277 return nodes;
1278 }
1279
1281 {
1282 const size_t total = nodes.size();
1283 ah_runtime_error_unless(total > 0)
1284 << "Gen_Centroid_Decomposition: empty component while choosing centroid";
1285
1286 for (size_t i = total; i-- > 0;)
1287 {
1288 const size_t u = nodes(i);
1289 local_subtree_(u) = 1;
1290
1291 for (size_t j = 0; j < topology_.adjacency()(u).size(); ++j)
1292 {
1293 const size_t v = topology_.adjacency()(u)(j);
1294 if (removed_(v))
1295 continue;
1296
1297 if (local_parent_(v) == u)
1299 }
1300 }
1301
1302 size_t centroid = nodes(0);
1303 size_t best_balance = total + 1;
1304
1305 for (size_t i = 0; i < total; ++i)
1306 {
1307 const size_t u = nodes(i);
1308
1309 size_t max_part = total - local_subtree_(u);
1310 for (size_t j = 0; j < topology_.adjacency()(u).size(); ++j)
1311 {
1312 const size_t v = topology_.adjacency()(u)(j);
1313 if (removed_(v))
1314 continue;
1315
1316 if (local_parent_(v) == u)
1317 max_part = std::max(max_part, local_subtree_(v));
1318 }
1319
1320 if (max_part < best_balance)
1321 {
1322 best_balance = max_part;
1323 centroid = u;
1324 }
1325 }
1326
1327 return centroid;
1328 }
1329
1330 void append_centroid_annotations(const size_t centroid)
1331 {
1332 struct Frame
1333 {
1334 size_t node;
1335 size_t parent;
1336 size_t dist;
1337 };
1338
1339 DynListStack<Frame> stack;
1340 stack.push({centroid, NONE, 0});
1341
1342 while (not stack.is_empty())
1343 {
1344 const Frame fr = stack.pop();
1345 centroid_ancestors_(fr.node).append(centroid);
1346 centroid_distances_(fr.node).append(fr.dist);
1347
1348 for (size_t i = 0; i < topology_.adjacency()(fr.node).size(); ++i)
1349 {
1350 const size_t v = topology_.adjacency()(fr.node)(i);
1351 if (removed_(v) or v == fr.parent)
1352 continue;
1353
1354 stack.push({v, fr.node, fr.dist + 1});
1355 }
1356 }
1357 }
1358
1360 {
1361 const size_t n = size();
1362 if (n == 0)
1363 return;
1364
1365 init_storage();
1366
1367 struct Task
1368 {
1369 size_t start;
1370 size_t parent_centroid;
1371 size_t level;
1372 };
1373
1374 DynListStack<Task> tasks;
1375 tasks.push({topology_.root_id(), NONE, 0});
1376
1377 size_t centroid_count = 0;
1378
1379 while (not tasks.is_empty())
1380 {
1381 const Task task = tasks.pop();
1382 if (removed_(task.start))
1383 continue;
1384
1385 const Array<size_t> nodes = collect_component_nodes(task.start);
1386 const size_t centroid = choose_centroid(nodes);
1387
1389
1390 centroid_parent_(centroid) = task.parent_centroid;
1391 centroid_level_(centroid) = task.level;
1392 if (task.parent_centroid == NONE)
1393 centroid_root_ = centroid;
1394
1395 removed_(centroid) = 1;
1396 ++centroid_count;
1397
1398 for (size_t i = 0; i < topology_.adjacency()(centroid).size(); ++i)
1399 if (const size_t v = topology_.adjacency()(centroid)(i); not removed_(v))
1400 tasks.push({v, centroid, task.level + 1});
1401 }
1402
1403 ah_runtime_error_unless(centroid_count == n)
1404 << "Gen_Centroid_Decomposition: invalid centroid tree size";
1405 }
1406
1407 public:
1409 Gen_Centroid_Decomposition(const GT & g, Node *root, SA sa = SA())
1410 : topology_(g, root, std::move(sa))
1411 {
1412 build_centroid_tree();
1413 }
1414
1416 Gen_Centroid_Decomposition(const GT & g, SA sa = SA())
1417 : topology_(g, nullptr, std::move(sa))
1418 {
1419 build_centroid_tree();
1420 }
1421
1426
1437 noexcept(std::is_nothrow_move_constructible_v<Topology>)
1438 : topology_(std::move(other.topology_)), removed_(std::move(other.removed_)),
1439 centroid_parent_(std::move(other.centroid_parent_)),
1440 centroid_level_(std::move(other.centroid_level_)),
1441 centroid_root_(std::exchange(other.centroid_root_, NONE)),
1442 local_parent_(std::move(other.local_parent_)),
1443 local_subtree_(std::move(other.local_subtree_)),
1444 centroid_ancestors_(std::move(other.centroid_ancestors_)),
1445 centroid_distances_(std::move(other.centroid_distances_))
1446 {}
1447
1452 Gen_Centroid_Decomposition & operator = (const Gen_Centroid_Decomposition & other) = default;
1453
1460 noexcept(noexcept(swap(other)))
1461 {
1462 swap(other);
1463 return *this;
1464 }
1465
1473 void swap(Gen_Centroid_Decomposition & other) noexcept(std::is_nothrow_swappable_v<SA>)
1474 {
1475 topology_.swap(other.topology_);
1476 removed_.swap(other.removed_);
1477 centroid_parent_.swap(other.centroid_parent_);
1478 centroid_level_.swap(other.centroid_level_);
1479 std::swap(centroid_root_, other.centroid_root_);
1480 local_parent_.swap(other.local_parent_);
1481 local_subtree_.swap(other.local_subtree_);
1482 centroid_ancestors_.swap(other.centroid_ancestors_);
1483 centroid_distances_.swap(other.centroid_distances_);
1484 }
1485
1486 [[nodiscard]] size_t size() const noexcept { return topology_.size(); }
1487 [[nodiscard]] bool is_empty() const noexcept { return topology_.is_empty(); }
1488
1489 [[nodiscard]] Node * root() const noexcept { return topology_.root(); }
1490 [[nodiscard]] size_t root_id() const noexcept { return topology_.root_id(); }
1491
1492 [[nodiscard]] Node * node_of(const size_t id) const
1493 {
1494 return topology_.node_of(id);
1495 }
1496
1497 [[nodiscard]] size_t id_of(const Node *node) const
1498 {
1499 return topology_.id_of(node);
1500 }
1501
1503 [[nodiscard]] size_t centroid_root_id() const noexcept
1504 {
1505 return centroid_root_;
1506 }
1507
1509 [[nodiscard]] Node * centroid_root() const noexcept
1510 {
1511 return centroid_root_ == NONE ? nullptr : node_of(centroid_root_);
1512 }
1513
1515 [[nodiscard]] size_t centroid_parent_id(const size_t id) const
1516 {
1517 topology_.validate_id(id, "Gen_Centroid_Decomposition::centroid_parent_id");
1518 return centroid_parent_(id);
1519 }
1520
1522 [[nodiscard]] Node * centroid_parent(const Node *node) const
1523 {
1524 const size_t pid = centroid_parent_id(id_of(node));
1525 return pid == NONE ? nullptr : node_of(pid);
1526 }
1527
1529 [[nodiscard]] size_t centroid_level_of_id(const size_t id) const
1530 {
1531 topology_.validate_id(id, "Gen_Centroid_Decomposition::centroid_level_of_id");
1532 return centroid_level_(id);
1533 }
1534
1536 [[nodiscard]] size_t centroid_level_of(const Node *node) const
1537 {
1538 return centroid_level_of_id(id_of(node));
1539 }
1540
1542 [[nodiscard]] size_t max_centroid_level() const noexcept
1543 {
1544 size_t ret = 0;
1545 for (size_t i = 0; i < size(); ++i)
1546 ret = std::max(ret, centroid_level_(i));
1547 return ret;
1548 }
1549
1551 [[nodiscard]] size_t centroid_path_length_of_id(const size_t id) const
1552 {
1553 topology_.validate_id(id, "Gen_Centroid_Decomposition::centroid_path_length_of_id");
1554 return centroid_ancestors_(id).size();
1555 }
1556
1558 [[nodiscard]] size_t centroid_path_length_of(const Node *node) const
1559 {
1560 return centroid_path_length_of_id(id_of(node));
1561 }
1562
1564 [[nodiscard]] const Array<size_t> &centroid_ancestors_of_id(const size_t id) const
1565 {
1566 topology_.validate_id(id, "Gen_Centroid_Decomposition::centroid_ancestors_of_id");
1567 return centroid_ancestors_(id);
1568 }
1569
1571 [[nodiscard]] const Array<size_t> &centroid_distances_of_id(const size_t id) const
1572 {
1573 topology_.validate_id(id, "Gen_Centroid_Decomposition::centroid_distances_of_id");
1574 return centroid_distances_(id);
1575 }
1576
1578 [[nodiscard]] const Array<size_t> &centroid_ancestors_of(const Node *node) const
1579 {
1580 return centroid_ancestors_of_id(id_of(node));
1581 }
1582
1584 [[nodiscard]] const Array<size_t> &centroid_distances_of(const Node *node) const
1585 {
1586 return centroid_distances_of_id(id_of(node));
1587 }
1588
1590 [[nodiscard]] bool is_centroid_ancestor_id(const size_t ancestor,
1591 const size_t node) const
1592 {
1593 topology_.validate_id(ancestor, "Gen_Centroid_Decomposition::is_centroid_ancestor_id");
1594 topology_.validate_id(node, "Gen_Centroid_Decomposition::is_centroid_ancestor_id");
1595
1596 const auto & chain = centroid_ancestors_(node);
1597 for (size_t i = 0; i < chain.size(); ++i)
1598 if (chain(i) == ancestor)
1599 return true;
1600
1601 return false;
1602 }
1603
1608 [[nodiscard]] size_t distance_to_centroid_id(const size_t node,
1609 const size_t centroid) const
1610 {
1611 topology_.validate_id(node, "Gen_Centroid_Decomposition::distance_to_centroid_id");
1612 topology_.validate_id(centroid, "Gen_Centroid_Decomposition::distance_to_centroid_id");
1613
1614 const auto & chain = centroid_ancestors_(node);
1615 const auto & dists = centroid_distances_(node);
1616
1617 for (size_t i = 0; i < chain.size(); ++i)
1618 if (chain(i) == centroid)
1619 return dists(i);
1620
1621 return NONE;
1622 }
1623
1628 template <class F>
1629 void for_each_centroid_ancestor_id(const size_t node, F && f) const
1630 {
1631 ensure_not_empty("Gen_Centroid_Decomposition::for_each_centroid_ancestor_id");
1632 topology_.validate_id(node, "Gen_Centroid_Decomposition::for_each_centroid_ancestor_id");
1633
1634 const auto & chain = centroid_ancestors_(node);
1635 const auto & dists = centroid_distances_(node);
1636
1637 ah_runtime_error_unless(chain.size() == dists.size())
1638 << "Gen_Centroid_Decomposition: invalid centroid annotation state";
1639
1640 for (size_t i = 0; i < chain.size(); ++i)
1641 f(chain(i), dists(i), i);
1642 }
1643
1648 template <class F>
1649 void for_each_centroid_ancestor(const Node *node, F && f) const
1650 {
1651 for_each_centroid_ancestor_id(id_of(node), std::forward<F>(f));
1652 }
1653 };
1654
1655
1657 template <class GT, class SA = Dft_Show_Arc<GT>>
1659
1661 template <class GT,
1662 typename T = typename GT::Node::Node_Type,
1663 class Op = Aleph::plus<T>,
1664 class SA = Dft_Show_Arc<GT>>
1666
1668 template <class GT, class SA = Dft_Show_Arc<GT>>
1670} // namespace Aleph
1671
1672# endif // TREE_DECOMPOSITION_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.
Standard functor implementations and comparison objects.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
size_t size_t int32_t value
Definition ca-c-api.h:116
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
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
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
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.
Centroid decomposition over a rooted tree represented as an Aleph graph.
Node * centroid_parent(const Node *node) const
Parent centroid of node (or nullptr if centroid root).
size_t centroid_path_length_of(const Node *node) const
Number of centroid ancestors stored for node.
Array< Array< size_t > > centroid_distances_
size_t centroid_level_of_id(const size_t id) const
Level of centroid id in centroid tree (root level = 0).
const Array< size_t > & centroid_ancestors_of(const Node *node) const
Centroid ancestors of node from centroid root down to local centroid.
Node * centroid_root() const noexcept
Root centroid node of the centroid tree (or nullptr if empty).
bool is_centroid_ancestor_id(const size_t ancestor, const size_t node) const
True iff ancestor is in the centroid-ancestor chain of node.
void ensure_not_empty(const char *where) const
size_t centroid_level_of(const Node *node) const
Level of centroid node in centroid tree (root level = 0).
size_t centroid_root_id() const noexcept
Root centroid ID of the centroid tree (or NONE if empty).
Gen_Centroid_Decomposition(const GT &g, Node *root, SA sa=SA())
Construct using an explicit root node.
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.
Node * node_of(const size_t id) const
size_t centroid_parent_id(const size_t id) const
Parent centroid ID of id (or NONE if centroid root).
size_t centroid_path_length_of_id(const size_t id) const
Number of centroid ancestors stored for node ID.
Gen_Centroid_Decomposition(Gen_Centroid_Decomposition &&other) noexcept(std::is_nothrow_move_constructible_v< Topology >)
Take the centroid tree of another decomposition, which is left empty.
const Array< size_t > & centroid_distances_of(const Node *node) const
Distances to centroid ancestors (aligned with centroid_ancestors_of).
size_t max_centroid_level() const noexcept
Maximum centroid-tree level.
void swap(Gen_Centroid_Decomposition &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this decomposition and another.
size_t distance_to_centroid_id(const size_t node, const size_t centroid) const
Distance from node ID to a centroid ancestor ID.
void append_centroid_annotations(const size_t centroid)
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
Array< size_t > collect_component_nodes(const size_t start)
Gen_Centroid_Decomposition(const Gen_Centroid_Decomposition &other)=default
Copy the centroid tree of another decomposition.
void for_each_centroid_ancestor(const Node *node, F &&f) const
Visit each centroid ancestor of node.
const Array< size_t > & centroid_distances_of_id(const size_t id) const
Distances to centroid ancestors (aligned with centroid_ancestors_of_id).
Gen_Centroid_Decomposition(const GT &g, SA sa=SA())
Construct using the first graph node as root.
size_t choose_centroid(const Array< size_t > &nodes)
Array< Array< size_t > > centroid_ancestors_
Segment-tree-powered path/subtree queries over an HLD layout.
Gen_Heavy_Light_Decomposition< GT, SA > hld_
T query_subtree(const Node *node) const
Subtree query for node in O(log n).
static Array< T > build_base_from_node_info(const Gen_Heavy_Light_Decomposition< GT, SA > &hld)
Gen_HLD_Path_Query & operator=(const Gen_HLD_Path_Query &other)=default
Copy the decomposition and the values of another engine.
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).
Gen_HLD_Path_Query(const GT &g, Node *root, const T &identity, Op op=Op(), SA sa=SA())
Construct from graph/root using node->get_info() as initial values.
Gen_HLD_Path_Query(Gen_HLD_Path_Query &&other)=default
Take the decomposition and the values of another engine, which is left empty.
void ensure_not_empty(const char *where) const
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).
Gen_HLD_Path_Query(const GT &g, const T &identity, Op op=Op(), SA sa=SA())
Construct from graph (first node as root) using node->get_info().
T get_node_id(const size_t id) const
Return current value at node ID.
void swap_all_or_none(Gen_HLD_Path_Query &other)
size_t size() const noexcept
T query_subtree_id(const size_t id) const
Subtree query for node ID in O(log n).
void update_node_id(const size_t id, const T &delta)
Point update: a[id] = Op(a[id], delta).
bool is_empty() const noexcept
void set_node_id(const size_t id, const T &value)
Point assignment: a[id] = value.
Gen_HLD_Path_Query(const Gen_HLD_Path_Query &other)=default
Copy the decomposition and the values of another engine.
Gen_Segment_Tree< T, Op > segment_tree_
and static std::convertible_to< std::invoke_result_t< Getter, Node * >, T > Array< T > build_base_array(const Gen_Heavy_Light_Decomposition< GT, SA > &hld, Getter getter)
void swap(Gen_HLD_Path_Query &other) noexcept(std::is_nothrow_swappable_v< T > and std::is_nothrow_swappable_v< Op > and std::is_nothrow_move_assignable_v< Gen_Heavy_Light_Decomposition< GT, SA > >)
Exchange in O(1) this engine and another.
and std::convertible_to< std::invoke_result_t< Getter, Node * >, T > Gen_HLD_Path_Query(const GT &g, Node *root, Getter getter, const T &identity, Op op=Op(), SA sa=SA())
Construct with custom node-to-value getter.
and std::convertible_to< std::invoke_result_t< Getter, Node * >, T > Gen_HLD_Path_Query(const GT &g, Getter getter, const T &identity, Op op=Op(), SA sa=SA())
Construct with custom getter and implicit root (first node).
T get_node(const Node *node) const
Return current value at node.
Heavy-Light Decomposition over a rooted tree represented as an Aleph graph.
Gen_Heavy_Light_Decomposition(const GT &g, SA sa=SA())
Construct using the first graph node as root.
size_t position_of(const Node *node) const
void for_each_path_segment_id(size_t u, size_t v, F &&visit) const
Enumerate path segments from u to v in path order.
size_t parent_id(const size_t id) const
size_t head_id(const size_t id) const
size_t distance_id(const size_t u, const size_t v) const
Distance (number of edges) between two node IDs.
size_t lca_id(size_t u, size_t v) const
Lowest common ancestor in O(log n).
size_t position_of_id(const size_t id) const
Node * head_of(const Node *node) const
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
size_t depth_of_id(const size_t id) const
size_t heavy_child_id(const size_t id) const
void ensure_not_empty(const char *where) const
Node * lca(const Node *u, const Node *v) const
Lowest common ancestor in O(log n).
Node * heavy_child(const Node *node) const
bool is_ancestor(const Node *u, const Node *v) const
True iff u is ancestor of v in the rooted tree.
Range subtree_range(const Node *node) const
Base-array range of a full subtree (inclusive endpoints).
size_t id_of(const Node *node) const
size_t depth_of(const Node *node) const
Gen_Heavy_Light_Decomposition(const GT &g, Node *root, SA sa=SA())
Construct using an explicit root node.
Node * node_of(const size_t id) const
size_t subtree_size_of(const Node *node) const
Range subtree_range_id(const size_t id) const
Base-array range of a full subtree (inclusive endpoints).
Node * parent_of(const Node *node) const
bool is_ancestor_id(const size_t u, const size_t v) const
True iff u is ancestor of v in the rooted tree.
void for_each_path_segment(const Node *u, const Node *v, F &&visit) const
Enumerate path segments from u to v in path order.
void for_each_path_segment_undirected_id(const size_t u, const size_t v, F &&visit) const
Enumerate path segments ignoring direction.
Segment tree over an arbitrary associative binary operation.
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:447
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
void validate_id(const size_t id, const char *where) const
void swap(Rooted_Tree_Topology &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this topology and another.
const Array< Node * > & id_to_node() const noexcept
Rooted_Tree_Topology(const GT &g, Node *root, SA sa=SA())
Rooted_Tree_Topology(const Rooted_Tree_Topology &other)=default
Copy the topology of another tree.
Rooted_Tree_Topology & operator=(const Rooted_Tree_Topology &other)=default
Copy the topology of another tree.
const Array< Array< size_t > > & adjacency() const noexcept
void check_id(const size_t id, const char *where) const
static Pair_Key normalize_pair(size_t u, size_t v) noexcept
Rooted_Tree_Topology(Rooted_Tree_Topology &&other) noexcept(std::is_nothrow_move_constructible_v< SA >)
Take the topology of another tree, which is left empty.
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
static Tree_Node< int > * chain(size_t n)
__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
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
STL namespace.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
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).
Task with priority for job scheduling.
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.
Segment trees for dynamic range queries with lazy propagation.
DynList< int > l