|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Rerooting DP: O(n) computation of DP answer for all roots. More...
#include <Tree_DP.H>
Public Types | |
| using | Node = typename GT::Node |
| Node type. | |
| using | Init_Fn = std::function< T(Node *)> |
| Initialization function: base contribution of a node. | |
| using | Merge_Fn = std::function< T(const T &, const T &)> |
| Merge function: associative binary operation to combine results. | |
| using | Apply_Edge_Fn = std::function< T(Node *, Node *, const T &)> |
| Edge transformation function: applies edge effects to a subtree result. | |
Public Member Functions | |
| 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. | |
| const T & | value (Node *node) const |
| Returns the answer for a given 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. | |
| 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. | |
Private Attributes | |
| tree_dp_detail::Tree_Topology< GT, SA > | topo_ |
| Tree topology and order. | |
| T | identity_ |
| Identity for merge operation. | |
| Array< T > | init_vals_ |
| Cached per-node base values. | |
| Array< T > | dp_down_ |
| Bottom-up DP results. | |
| Array< T > | dp_up_ |
| Top-down contribution from parent side. | |
| Array< T > | answer_ |
| Final answer for each node as root. | |
Rerooting DP: O(n) computation of DP answer for all roots.
Uses prefix/suffix merges to efficiently compute the DP value as if each node were the root.
The user provides:
| GT | Graph type. |
| T | Value type. |
| SA | Arc filter. |
| using Aleph::Gen_Reroot_DP< GT, T, SA >::Apply_Edge_Fn = std::function<T(Node *, Node *, const T &)> |
Edge transformation function: applies edge effects to a subtree result.
Parameters: (parent_node, child_node, subtree_value) Returns: contribution to parent's DP.
|
inline |
Construct and compute rerooting DP.
| [in] | g | The graph (tree under SA). |
| [in] | root | Initial root for bottom-up pass. |
| [in] | identity | Neutral element for merge. |
| [in] | init | Base value for each node. |
| [in] | merge | Associative merge operation. |
| [in] | apply_edge | Transform child value across an edge. |
| [in] | sa | Arc filter. |
Definition at line 498 of file Tree_DP.H.
References Aleph::Gen_Reroot_DP< GT, T, SA >::answer_, Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Reroot_DP< GT, T, SA >::dp_down_, Aleph::Gen_Reroot_DP< GT, T, SA >::dp_up_, Aleph::Gen_Reroot_DP< GT, T, SA >::identity_, Aleph::init, Aleph::Gen_Reroot_DP< GT, T, SA >::init_vals_, k, Aleph::merge(), Aleph::prefix(), Aleph::suffix(), and Aleph::Gen_Reroot_DP< GT, T, SA >::topo_.
|
inline |
Returns the internal ID for a given node pointer.
| [in] | node | Node pointer. |
Definition at line 619 of file Tree_DP.H.
References Aleph::Gen_Reroot_DP< GT, T, SA >::topo_.
|
inline |
Returns the node pointer for a given internal ID.
| [in] | id | Internal node ID. |
Definition at line 610 of file Tree_DP.H.
References Aleph::Gen_Reroot_DP< GT, T, SA >::topo_.
|
inlinenoexcept |
Returns the number of nodes in the tree.
Definition at line 601 of file Tree_DP.H.
References Aleph::Gen_Reroot_DP< GT, T, SA >::topo_.
Returns the answer for a given node as root.
| [in] | node | Node pointer. |
Definition at line 589 of file Tree_DP.H.
References Aleph::Gen_Reroot_DP< GT, T, SA >::answer_, and Aleph::Gen_Reroot_DP< GT, T, SA >::topo_.
Returns all computed answers (indexed by internal ID).
Definition at line 595 of file Tree_DP.H.
References Aleph::Gen_Reroot_DP< GT, T, SA >::answer_.
Referenced by Aleph::tree_max_distance(), and Aleph::tree_sum_of_distances().
Final answer for each node as root.
Definition at line 485 of file Tree_DP.H.
Referenced by Aleph::Gen_Reroot_DP< GT, T, SA >::Gen_Reroot_DP(), Aleph::Gen_Reroot_DP< GT, T, SA >::value(), and Aleph::Gen_Reroot_DP< GT, T, SA >::values().
Bottom-up DP results.
Definition at line 483 of file Tree_DP.H.
Referenced by Aleph::Gen_Reroot_DP< GT, T, SA >::Gen_Reroot_DP().
Top-down contribution from parent side.
Definition at line 484 of file Tree_DP.H.
Referenced by Aleph::Gen_Reroot_DP< GT, T, SA >::Gen_Reroot_DP().
|
private |
Identity for merge operation.
Definition at line 481 of file Tree_DP.H.
Referenced by Aleph::Gen_Reroot_DP< GT, T, SA >::Gen_Reroot_DP().
Cached per-node base values.
Definition at line 482 of file Tree_DP.H.
Referenced by Aleph::Gen_Reroot_DP< GT, T, SA >::Gen_Reroot_DP().
|
private |
Tree topology and order.
Definition at line 480 of file Tree_DP.H.
Referenced by Aleph::Gen_Reroot_DP< GT, T, SA >::Gen_Reroot_DP(), Aleph::Gen_Reroot_DP< GT, T, SA >::id_of(), Aleph::Gen_Reroot_DP< GT, T, SA >::node_of(), Aleph::Gen_Reroot_DP< GT, T, SA >::size(), and Aleph::Gen_Reroot_DP< GT, T, SA >::value().