Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Tree_DP.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
53#ifndef TREE_DP_H
54#define TREE_DP_H
55
56# include <ah-graph-concepts.H>
57
58#include <algorithm>
59#include <cstddef>
60#include <functional>
61#include <limits>
62#include <type_traits>
63#include <utility>
64
65#include <ah-errors.H>
66#include <tpl_array.H>
67#include <tpl_dynListStack.H>
68#include <tpl_dynMapOhash.H>
69#include <tpl_dynMapTree.H>
70#include <tpl_graph.H>
71
72namespace Aleph {
73namespace tree_dp_detail {
82template <AlephGraph GT, ArcFilter<GT> SA>
84{
85 // moved-from: tested through Tree_DP and Reroot_DP,
86 // which keep it (Tests/moved_from_graphs_test.cc)
87public:
88 using Node = typename GT::Node;
89 using Arc = typename GT::Arc;
92 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
93
94private:
95 const GT *graph_ = nullptr;
96 SA sa_;
97 Node *root_ = nullptr;
98 size_t n_ = 0;
108 {
110 if (n_ == 0)
111 {
112 ah_domain_error_if(root_ != nullptr)
113 << "Tree_Topology: non-null root provided for empty graph";
114 return;
115 }
116
118 {
120 node_to_id_.swap(tmp);
121 }
122
123 size_t next_id = 0;
124 for (Node_Iterator<GT> it(*graph_); it.has_curr(); it.next_ne())
125 {
126 Node *p = it.get_curr();
127 id_to_node_(next_id) = p;
128 node_to_id_.insert(p, next_id);
129 ++next_id;
130 }
131
132 if (root_ == nullptr)
133 root_ = id_to_node_(0);
134 else
135 {
137 << "Tree_Topology: root node is not part of the graph";
138 }
139 }
140
143 {
144 if (n_ == 0)
145 return;
146
147 children_.reserve(n_);
148 for (size_t i = 0; i < n_; ++i)
149 children_.append(Array<size_t>());
150
151 using Pair_Key = std::pair<size_t, size_t>;
152 DynMapTree<Pair_Key, char> unique_edges;
153 size_t edge_count = 0;
154
155 for (Arc_Iterator<GT, SA> it(*graph_, sa_); it.has_curr(); it.next_ne())
156 {
157 Arc *a = it.get_curr_ne();
158 Node *src = graph_->get_src_node(a);
159 Node *tgt = graph_->get_tgt_node(a);
160
161 const auto *si = node_to_id_.search(src);
162 const auto *ti = node_to_id_.search(tgt);
163 ah_runtime_error_unless(si != nullptr and ti != nullptr)
164 << "Tree_Topology: arc endpoint not indexed";
165
166 const size_t u = si->second;
167 const size_t v = ti->second;
168 ah_domain_error_if(u == v) << "Tree_Topology: self-loop detected";
169
170 Pair_Key key = u < v ? std::make_pair(u, v) : std::make_pair(v, u);
171 if (unique_edges.search(key) != nullptr)
172 continue; // skip duplicate
173 unique_edges.insert(key, 1);
174 ++edge_count;
175 }
176
177 ah_domain_error_if(n_ > 1 and edge_count != n_ - 1)
178 << "Tree_Topology: not a tree (expected " << (n_ - 1) << " edges, got " << edge_count << ")";
179
180 for (typename DynMapTree<Pair_Key, char>::Iterator it(unique_edges); it.has_curr(); it.next_ne())
181 {
182 const auto &[fst, snd] = it.get_curr();
183 const size_t u = fst.first;
184 const size_t v = fst.second;
185 children_(u).append(v);
186 children_(v).append(u);
187 }
188 }
189
192 {
193 if (n_ == 0)
194 return;
195
197 for (size_t i = 0; i < n_; ++i)
198 parent_(i) = NONE;
199
201
203 for (size_t i = 0; i < n_; ++i)
204 visited(i) = 0;
205
206 const size_t root_id = node_to_id_.find(root_);
207
208 struct Frame
209 {
210 size_t node;
211 size_t next_child;
212 };
214
215 visited(root_id) = 1;
216 stack.push({root_id, 0});
217
218 while (not stack.is_empty())
219 {
220 auto &fr = stack.top();
221 if (fr.next_child == children_(fr.node).size())
222 {
223 order_.append(fr.node);
224 (void) stack.pop();
225 continue;
226 }
227
228 const size_t nxt = children_(fr.node)(fr.next_child++);
229 if (visited(nxt))
230 continue;
231
232 visited(nxt) = 1;
233 parent_(nxt) = fr.node;
234 stack.push({nxt, 0});
235 }
236
237 ah_domain_error_if(order_.size() != n_) << "Tree_Topology: graph is not connected";
238
239 // Filter children_ to only contain children after rooting
240 for (size_t i = 0; i < n_; ++i)
241 {
243 const size_t p = parent_(i);
244 for (size_t j = 0; j < children_(i).size(); ++j)
245 if (children_(i)[j] != p)
247 children_(i).swap(children);
248 }
249 }
250
251public:
258 Tree_Topology(const GT &g, Node *root, SA sa = SA()) : graph_(&g), sa_(std::move(sa)), root_(root)
259 {
260 index_nodes();
262 build_order();
263 }
264
268 Tree_Topology(const Tree_Topology & other) = default;
269
278 Tree_Topology(Tree_Topology && other) noexcept(std::is_nothrow_move_constructible_v<SA>)
279 : graph_(std::exchange(other.graph_, nullptr)), sa_(std::move(other.sa_)),
280 root_(std::exchange(other.root_, nullptr)), n_(std::exchange(other.n_, 0)),
281 id_to_node_(std::move(other.id_to_node_)), node_to_id_(std::move(other.node_to_id_)),
282 children_(std::move(other.children_)), parent_(std::move(other.parent_)),
283 order_(std::move(other.order_))
284 {}
285
290 Tree_Topology & operator=(const Tree_Topology & other) = default;
291
297 Tree_Topology & operator=(Tree_Topology && other) noexcept(noexcept(swap(other)))
298 {
299 swap(other);
300 return *this;
301 }
302
309 void swap(Tree_Topology & other) noexcept(std::is_nothrow_swappable_v<SA>)
310 {
311 // the swap that the specification evaluates: that of SA found by its
312 // arguments, else std::swap(); first, since it may throw
313 using std::swap;
314 swap(sa_, other.sa_);
315 std::swap(graph_, other.graph_);
316 std::swap(root_, other.root_);
317 std::swap(n_, other.n_);
318 id_to_node_.swap(other.id_to_node_);
319 node_to_id_.swap(other.node_to_id_);
320 children_.swap(other.children_);
321 parent_.swap(other.parent_);
322 order_.swap(other.order_);
323 }
324
326 [[nodiscard]] size_t size() const noexcept
327 {
328 return n_;
329 }
330
332 [[nodiscard]] Node *root() const noexcept
333 {
334 return root_;
335 }
336
338 [[nodiscard]] size_t id_of(Node *node) const
339 {
340 const auto *item = node_to_id_.search(node);
341 ah_domain_error_if(item == nullptr) << "Tree_Topology::id_of: node not in graph";
342 return item->second;
343 }
344
346 [[nodiscard]] Node *node_of(size_t id) const
347 {
348 ah_out_of_range_error_if(id >= n_) << "Tree_Topology::node_of: id out of range";
349 return id_to_node_(id);
350 }
351
353 [[nodiscard]] const Array<size_t> &children(size_t id) const
354 {
355 return children_(id);
356 }
357
359 [[nodiscard]] size_t parent(size_t id) const noexcept
360 {
361 return parent_(id);
362 }
363
365 [[nodiscard]] const Array<size_t> &post_order() const noexcept
366 {
367 return order_;
368 }
369
371 [[nodiscard]] const Array<Node *> &nodes() const noexcept
372 {
373 return id_to_node_;
374 }
375};
376} // namespace tree_dp_detail
377
404template <AlephGraph GT, typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
406{
407public:
408 using Node = typename GT::Node;
413 using Init_Fn = std::function<T(Node *)>;
414
419 using Combine_Fn = std::function<T(Node *, const T &, Node *, const T &)>;
420
421private:
425public:
434 Gen_Tree_DP(const GT &g, Node *root, Init_Fn init, Combine_Fn combine, SA sa = SA())
435 : topo_(g, root, std::move(sa))
436 {
437 const size_t n = topo_.size();
438 if (n == 0)
439 return;
440
442
443 // Initialize all nodes
444 for (size_t i = 0; i < n; ++i)
445 dp_(i) = init(topo_.node_of(i));
446
447 // Process in post-order (leaves first)
448 const auto &order = topo_.post_order();
449 for (size_t k = 0; k < n; ++k)
450 {
451 const size_t v = order[k];
452 const size_t par = topo_.parent(v);
454 continue; // root, no parent to update
455 dp_(par) = combine(topo_.node_of(par), dp_(par), topo_.node_of(v), dp_(v));
456 }
457 }
458
463 [[nodiscard]] const T &value(Node *node) const
464 {
465 return dp_(topo_.id_of(node));
466 }
467
469 [[nodiscard]] const Array<T> &values() const noexcept
470 {
471 return dp_;
472 }
473
475 [[nodiscard]] size_t size() const noexcept
476 {
477 return topo_.size();
478 }
479
484 [[nodiscard]] Node *node_of(size_t id) const
485 {
486 return topo_.node_of(id);
487 }
488
493 [[nodiscard]] size_t id_of(Node *node) const
494 {
495 return topo_.id_of(node);
496 }
497};
498
504template <class GT, typename T, class SA = Dft_Show_Arc<GT>>
506
542template <AlephGraph GT, typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
544{
545public:
546 using Node = typename GT::Node;
551 using Init_Fn = std::function<T(Node *)>;
552
556 using Merge_Fn = std::function<T(const T &, const T &)>;
557
562 using Apply_Edge_Fn = std::function<T(Node *, Node *, const T &)>;
563
564private:
572public:
584 Node *root,
585 const T &identity,
588 Apply_Edge_Fn apply_edge,
589 SA sa = SA())
590 : topo_(g, root, std::move(sa)), identity_(identity)
591 {
592 const size_t n = topo_.size();
593 if (n == 0)
594 return;
595
600
601 for (size_t i = 0; i < n; ++i)
602 {
603 init_vals_(i) = init(topo_.node_of(i));
604 dp_down_(i) = init_vals_[i];
605 dp_up_(i) = identity_;
606 }
607
608 // Phase 1: bottom-up
609 const auto &order = topo_.post_order();
610 for (size_t k = 0; k < n; ++k)
611 {
612 const size_t v = order[k];
613 const size_t par = topo_.parent(v);
615 continue;
616 const T contrib = apply_edge(topo_.node_of(par), topo_.node_of(v), dp_down_(v));
617 dp_down_(par) = merge(dp_down_(par), contrib);
618 }
619
620 // Phase 2: top-down with prefix/suffix
621 // Process nodes in reverse post-order (root first)
622 for (size_t k = n; k-- > 0;)
623 {
624 const size_t v = order[k];
625 Node *vn = topo_.node_of(v);
626
627 // Collect children of v
628 const auto &children = topo_.children(v);
629 const size_t nc = children.size();
630 if (nc == 0)
631 continue;
632
633 Array<T> contribs = Array<T>::create(nc);
634 for (size_t j = 0; j < nc; ++j)
635 {
636 const size_t c = children[j];
637 contribs(j) = apply_edge(vn, topo_.node_of(c), dp_down_(c));
638 }
639
640 // Build prefix and suffix arrays of merged contributions
643 prefix(0) = identity_;
644 suffix(nc) = identity_;
645
646 for (size_t j = 0; j < nc; ++j)
647 prefix(j + 1) = merge(prefix(j), contribs(j));
648
649 for (size_t j = nc; j-- > 0;)
650 suffix(j) = merge(suffix(j + 1), contribs(j));
651
652 const T base = merge(init_vals_[v], dp_up_(v));
653
654 // For each child, compute dp_up
655 for (size_t j = 0; j < nc; ++j)
656 {
657 const size_t c = children[j];
658 Node *cn = topo_.node_of(c);
659 // Value of v without child c's subtree:
660 // merge(init(v), dp_up(v), prefix(j), suffix(j+1))
661 T without_c = merge(base, prefix(j));
662 without_c = merge(without_c, suffix(j + 1));
663 dp_up_(c) = apply_edge(cn, vn, without_c);
664 }
665 }
666
667 // Phase 3: compute answers
668 for (size_t i = 0; i < n; ++i)
669 answer_(i) = merge(dp_down_(i), dp_up_(i));
670 }
671
676 [[nodiscard]] const T &value(Node *node) const
677 {
678 return answer_(topo_.id_of(node));
679 }
680
682 [[nodiscard]] const Array<T> &values() const noexcept
683 {
684 return answer_;
685 }
686
688 [[nodiscard]] size_t size() const noexcept
689 {
690 return topo_.size();
691 }
692
697 [[nodiscard]] Node *node_of(size_t id) const
698 {
699 return topo_.node_of(id);
700 }
701
706 [[nodiscard]] size_t id_of(Node *node) const
707 {
708 return topo_.id_of(node);
709 }
710};
711
717template <class GT, typename T, class SA = Dft_Show_Arc<GT>>
719
720// ── Convenience functions ─────────────────────────────────────────
721
731template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
732[[nodiscard]] Array<size_t> tree_subtree_sizes(const GT &g, typename GT::Node *root, SA sa = SA())
733{
735 root,
736 [](auto *) -> size_t
737 {
738 return 1;
739 },
740 [](auto *, const size_t &acc, auto *, const size_t &child) -> size_t
741 {
742 return acc + child;
743 },
744 std::move(sa));
745
746 // Copy values out
747 const auto &vals = dp.values();
749 for (size_t i = 0; i < vals.size(); ++i)
750 result(i) = vals[i];
751 return result;
752}
753
766template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
767[[nodiscard]] Array<size_t> tree_max_distance(const GT &g, typename GT::Node *root, SA sa = SA())
768{
770 root,
771 static_cast<size_t>(0),
772 [](auto *) -> size_t
773 {
774 return 0;
775 },
776 [](const size_t &a, const size_t &b) -> size_t
777 {
778 return std::max(a, b);
779 },
780 [](auto *, auto *, const size_t &v) -> size_t
781 {
782 return v + 1;
783 },
784 std::move(sa));
785
786 const auto &vals = dp.values();
788 for (size_t i = 0; i < vals.size(); ++i)
789 result(i) = vals[i];
790 return result;
791}
792
804template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
805[[nodiscard]] Array<size_t> tree_sum_of_distances(const GT &g, typename GT::Node *root, SA sa = SA())
806{
807 using P = std::pair<size_t, size_t>; // (count, sum_dist)
808
810 root,
811 P{0, 0},
812 [](auto *) -> P
813 {
814 return {1, 0};
815 },
816 [](const P &a, const P &b) -> P
817 {
818 return {a.first + b.first, a.second + b.second};
819 },
820 [](auto *, auto *, const P &v) -> P
821 {
822 return {v.first, v.second + v.first};
823 },
824 std::move(sa));
825
826 const auto &vals = dp.values();
828 for (size_t i = 0; i < vals.size(); ++i)
829 result(i) = vals[i].second;
830 return result;
831}
832} // namespace Aleph
833
834#endif // TREE_DP_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
C++20 concepts for the protocol shared by graph algorithms.
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.
Generic rerooting dynamic programming (all-roots DP).
Definition Tree_DP.H:544
Array< T > dp_up_
Top-down contribution from parent side.
Definition Tree_DP.H:569
std::function< T(Node *)> Init_Fn
Initialization function signature.
Definition Tree_DP.H:551
std::function< T(const T &, const T &)> Merge_Fn
Merge function signature.
Definition Tree_DP.H:556
Gen_Reroot_DP(const GT &g, Node *root, const T &identity, Init_Fn init, Merge_Fn merge, Apply_Edge_Fn apply_edge, SA sa=SA())
Construct and compute rerooting DP.
Definition Tree_DP.H:583
typename GT::Node Node
Node type.
Definition Tree_DP.H:546
Node * node_of(size_t id) const
Returns the node pointer for a given internal ID.
Definition Tree_DP.H:697
size_t id_of(Node *node) const
Returns the internal ID for a given node pointer.
Definition Tree_DP.H:706
Array< T > dp_down_
Bottom-up DP results.
Definition Tree_DP.H:568
Array< T > init_vals_
Cached per-node base values.
Definition Tree_DP.H:567
tree_dp_detail::Tree_Topology< GT, SA > topo_
Tree topology and order.
Definition Tree_DP.H:565
std::function< T(Node *, Node *, const T &)> Apply_Edge_Fn
Edge transformation signature.
Definition Tree_DP.H:562
Array< T > answer_
Final answer for each node as root.
Definition Tree_DP.H:570
const Array< T > & values() const noexcept
Returns all computed answers (indexed by internal ID).
Definition Tree_DP.H:682
size_t size() const noexcept
Returns the number of nodes in the tree.
Definition Tree_DP.H:688
T identity_
Identity for merge operation.
Definition Tree_DP.H:566
const T & value(Node *node) const
Returns the answer for a given node as root.
Definition Tree_DP.H:676
Generic bottom-up tree dynamic programming.
Definition Tree_DP.H:406
const Array< T > & values() const noexcept
Returns all DP values (indexed by internal node ID).
Definition Tree_DP.H:469
typename GT::Node Node
Node type.
Definition Tree_DP.H:408
Node * node_of(size_t id) const
Returns the node pointer for a given internal ID.
Definition Tree_DP.H:484
size_t id_of(Node *node) const
Returns the internal ID for a given node pointer.
Definition Tree_DP.H:493
Gen_Tree_DP(const GT &g, Node *root, Init_Fn init, Combine_Fn combine, SA sa=SA())
Construct and compute bottom-up DP.
Definition Tree_DP.H:434
size_t size() const noexcept
Returns the number of nodes in the tree.
Definition Tree_DP.H:475
Array< T > dp_
Computed DP values.
Definition Tree_DP.H:423
tree_dp_detail::Tree_Topology< GT, SA > topo_
Tree topology and order.
Definition Tree_DP.H:422
std::function< T(Node *, const T &, Node *, const T &)> Combine_Fn
Combine function signature.
Definition Tree_DP.H:419
std::function< T(Node *)> Init_Fn
Initialization function signature.
Definition Tree_DP.H:413
const T & value(Node *node) const
Returns the DP value for a given node.
Definition Tree_DP.H:463
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
Pre-processes and extracts tree topology from a graph.
Definition Tree_DP.H:84
const Array< size_t > & children(size_t id) const
Returns children IDs of a node.
Definition Tree_DP.H:353
static constexpr size_t NONE
Sentinel for null/none parent or id.
Definition Tree_DP.H:92
void swap(Tree_Topology &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this topology and another.
Definition Tree_DP.H:309
size_t n_
Number of nodes.
Definition Tree_DP.H:98
const Array< size_t > & post_order() const noexcept
Returns post-order traversal (leaves first, root last).
Definition Tree_DP.H:365
Array< Array< size_t > > children_
Children list in the rooted tree.
Definition Tree_DP.H:102
Tree_Topology & operator=(Tree_Topology &&other) noexcept(noexcept(swap(other)))
Exchange this topology with another, which is left with the previous topology of this one.
Definition Tree_DP.H:297
Node * node_of(size_t id) const
Returns node pointer for a given ID.
Definition Tree_DP.H:346
const GT * graph_
Source graph.
Definition Tree_DP.H:95
size_t id_of(Node *node) const
Returns internal ID of a node.
Definition Tree_DP.H:338
typename GT::Arc Arc
Arc type.
Definition Tree_DP.H:89
void index_nodes()
Assign unique IDs to nodes and validate the root.
Definition Tree_DP.H:107
MapOLhash< Node *, size_t > node_to_id_
Mapping from node pointer to id.
Definition Tree_DP.H:101
Array< size_t > parent_
Parent id for each node.
Definition Tree_DP.H:103
Array< Node * > id_to_node_
Mapping from id to node pointer.
Definition Tree_DP.H:100
Node * root() const noexcept
Returns root node pointer.
Definition Tree_DP.H:332
Tree_Topology & operator=(const Tree_Topology &other)=default
Copy the topology of another tree.
void build_order()
BFS/DFS traversal to establish parent-child relations and order.
Definition Tree_DP.H:191
size_t parent(size_t id) const noexcept
Returns parent ID of a node.
Definition Tree_DP.H:359
size_t size() const noexcept
Returns number of nodes.
Definition Tree_DP.H:326
void build_adjacency()
Build undirected adjacency list and verify tree properties.
Definition Tree_DP.H:142
Array< size_t > order_
Post-order traversal (leaves first).
Definition Tree_DP.H:104
const Array< Node * > & nodes() const noexcept
Returns all node pointers indexed by ID.
Definition Tree_DP.H:371
Tree_Topology(Tree_Topology &&other) noexcept(std::is_nothrow_move_constructible_v< SA >)
Take the topology of another tree, which is left empty.
Definition Tree_DP.H:278
Tree_Topology(const GT &g, Node *root, SA sa=SA())
Preprocess tree topology.
Definition Tree_DP.H:258
Tree_Topology(const Tree_Topology &other)=default
Copy the topology of another tree.
typename GT::Node Node
Node type.
Definition Tree_DP.H:88
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
pair< size_t, string > P
__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
Main namespace for Aleph-w library functions.
Array< size_t > tree_subtree_sizes(const GT &g, typename GT::Node *root, SA sa=SA())
Compute subtree sizes for every node.
Definition Tree_DP.H:732
static void suffix(Node *root, DynList< Node * > &acc)
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
static void prefix(Node *root, DynList< Node * > &acc)
and
Check uniqueness with explicit hash + equality functors.
Itor3 merge(Itor1 source1Beg, Itor1 source1End, Itor2 source2Beg, Itor2 source2End, Itor3 destBeg)
Merge two sorted ranges.
Definition ahAlgo.H:1410
Array< size_t > tree_max_distance(const GT &g, typename GT::Node *root, SA sa=SA())
Compute the maximum distance from each node to any leaf.
Definition Tree_DP.H:767
Array< size_t > tree_sum_of_distances(const GT &g, typename GT::Node *root, SA sa=SA())
Compute sum of distances from each node to all others.
Definition Tree_DP.H:805
static std::atomic< bool > init
Definition hash-fct.C:54
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).
static int * k
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.