72namespace tree_dp_detail {
81template <AlephGraph GT, ArcFilter<GT> SA>
89 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
110 <<
"Tree_Topology: non-null root provided for empty graph";
123 Node *p = it.get_curr();
129 if (
root_ ==
nullptr)
133 <<
"Tree_Topology: root node is not part of the graph";
143 for (
size_t i = 0; i <
n_; ++i)
146 using Pair_Key = std::pair<size_t, size_t>;
152 Arc *a = it.get_curr_ne();
159 <<
"Tree_Topology: arc endpoint not indexed";
161 const size_t u =
si->second;
162 const size_t v =
ti->second;
165 Pair_Key key = u < v ? std::make_pair(u, v) : std::make_pair(v, u);
173 <<
"Tree_Topology: not a tree (expected " << (
n_ - 1) <<
" edges, got " <<
edge_count <<
")";
177 const auto &[
fst,
snd] = it.get_curr();
178 const size_t u =
fst.first;
179 const size_t v =
fst.second;
192 for (
size_t i = 0; i <
n_; ++i)
198 for (
size_t i = 0; i <
n_; ++i)
210 visited(root_id) = 1;
211 stack.
push({root_id, 0});
215 auto &
fr = stack.
top();
235 for (
size_t i = 0; i <
n_; ++i)
239 for (
size_t j = 0; j <
children_(i).size(); ++j)
339template <AlephGraph GT,
typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
372 const size_t n =
topo_.size();
379 for (
size_t i = 0; i < n; ++i)
383 const auto &order =
topo_.post_order();
384 for (
size_t k = 0;
k < n; ++
k)
386 const size_t v = order[
k];
387 const size_t par =
topo_.parent(v);
421 return topo_.node_of(
id);
430 return topo_.id_of(node);
439template <
class GT,
typename T,
class SA = Dft_Show_Arc<GT>>
477template <AlephGraph GT,
typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
527 const size_t n =
topo_.size();
536 for (
size_t i = 0; i < n; ++i)
544 const auto &order =
topo_.post_order();
545 for (
size_t k = 0;
k < n; ++
k)
547 const size_t v = order[
k];
548 const size_t par =
topo_.parent(v);
557 for (
size_t k = n;
k-- > 0;)
559 const size_t v = order[
k];
563 const auto &children =
topo_.children(v);
564 const size_t nc = children.size();
569 for (
size_t j = 0; j <
nc; ++j)
571 const size_t c = children[j];
581 for (
size_t j = 0; j <
nc; ++j)
584 for (
size_t j =
nc; j-- > 0;)
590 for (
size_t j = 0; j <
nc; ++j)
592 const size_t c = children[j];
603 for (
size_t i = 0; i < n; ++i)
634 return topo_.node_of(
id);
643 return topo_.id_of(node);
652template <
class GT,
typename T,
class SA = Dft_Show_Arc<GT>>
666template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
675 [](
auto *,
const size_t &
acc,
auto *,
const size_t &child) ->
size_t
684 for (
size_t i = 0; i <
vals.size(); ++i)
701template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
706 static_cast<size_t>(0),
711 [](
const size_t &a,
const size_t &b) ->
size_t
713 return std::max(a, b);
715 [](
auto *,
auto *,
const size_t &v) ->
size_t
723 for (
size_t i = 0; i <
vals.size(); ++i)
739template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
742 using P = std::pair<size_t, size_t>;
751 [](
const P &a,
const P &b) ->
P
753 return {a.first + b.first, a.second + b.second};
755 [](
auto *,
auto *,
const P &v) ->
P
757 return {v.first, v.second + v.first};
763 for (
size_t i = 0; i <
vals.size(); ++i)
764 result(i) =
vals[i].second;
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
Simple dynamic array with automatic resizing and functional operations.
static Array create(size_t n)
Create an array with n logical elements.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
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
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Generic rerooting dynamic programming (all-roots DP).
Array< T > dp_up_
Top-down contribution from parent side.
std::function< T(Node *)> Init_Fn
Initialization function signature.
std::function< T(const T &, const T &)> Merge_Fn
Merge function signature.
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.
typename GT::Node Node
Node type.
Node * node_of(size_t id) const
Returns the node pointer for a given internal ID.
size_t id_of(Node *node) const
Returns the internal ID for a given node pointer.
Array< T > dp_down_
Bottom-up DP results.
Array< T > init_vals_
Cached per-node base values.
tree_dp_detail::Tree_Topology< GT, SA > topo_
Tree topology and order.
std::function< T(Node *, Node *, const T &)> Apply_Edge_Fn
Edge transformation signature.
Array< T > answer_
Final answer for each node as root.
const Array< T > & values() const noexcept
Returns all computed answers (indexed by internal ID).
size_t size() const noexcept
Returns the number of nodes in the tree.
T identity_
Identity for merge operation.
const T & value(Node *node) const
Returns the answer for a given node as root.
Generic bottom-up tree dynamic programming.
const Array< T > & values() const noexcept
Returns all DP values (indexed by internal node ID).
typename GT::Node Node
Node type.
Node * node_of(size_t id) const
Returns the node pointer for a given internal ID.
size_t id_of(Node *node) const
Returns the internal ID for a given node pointer.
Gen_Tree_DP(const GT &g, Node *root, Init_Fn init, Combine_Fn combine, SA sa=SA())
Construct and compute bottom-up DP.
size_t size() const noexcept
Returns the number of nodes in the tree.
Array< T > dp_
Computed DP values.
tree_dp_detail::Tree_Topology< GT, SA > topo_
Tree topology and order.
std::function< T(Node *, const T &, Node *, const T &)> Combine_Fn
Combine function signature.
std::function< T(Node *)> Init_Fn
Initialization function signature.
const T & value(Node *node) const
Returns the DP value for a given node.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Filtered iterator on the nodes of a graph.
Pre-processes and extracts tree topology from a graph.
const Array< size_t > & children(size_t id) const
Returns children IDs of a node.
static constexpr size_t NONE
Sentinel for null/none parent or id.
size_t n_
Number of nodes.
const Array< size_t > & post_order() const noexcept
Returns post-order traversal (leaves first, root last).
Array< Array< size_t > > children_
Children list in the rooted tree.
Node * node_of(size_t id) const
Returns node pointer for a given ID.
const GT * graph_
Source graph.
size_t id_of(Node *node) const
Returns internal ID of a node.
typename GT::Arc Arc
Arc type.
void index_nodes()
Assign unique IDs to nodes and validate the root.
MapOLhash< Node *, size_t > node_to_id_
Mapping from node pointer to id.
Array< size_t > parent_
Parent id for each node.
Array< Node * > id_to_node_
Mapping from id to node pointer.
Node * root() const noexcept
Returns root node pointer.
void build_order()
BFS/DFS traversal to establish parent-child relations and order.
size_t parent(size_t id) const noexcept
Returns parent ID of a node.
size_t size() const noexcept
Returns number of nodes.
void build_adjacency()
Build undirected adjacency list and verify tree properties.
Array< size_t > order_
Post-order traversal (leaves first).
const Array< Node * > & nodes() const noexcept
Returns all node pointers indexed by ID.
Tree_Topology(const GT &g, Node *root, SA sa=SA())
Preprocess tree topology.
typename GT::Node Node
Node type.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
__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)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
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.
static void suffix(Node *root, DynList< Node * > &acc)
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
static void prefix(Node *root, DynList< Node * > &acc)
Itor3 merge(Itor1 source1Beg, Itor1 source1End, Itor2 source2Beg, Itor2 source2End, Itor3 destBeg)
Merge two sorted ranges.
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.
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.
static std::atomic< bool > init
Filtered iterator on all the arcs of a graph.
Open addressing hash map using linear probing.
Data & find(const Key &key)
Find and return the value for a key.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair (copy semantics).
Pair * search(const Key &key) const noexcept
Search for a key in the map.
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.