|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Generic rerooting dynamic programming (all-roots DP). More...
#include <Tree_DP.H>
Public Types | |
| using | Node = typename GT::Node |
| Node type. | |
| using | Init_Fn = std::function< T(Node *)> |
| Initialization function signature. | |
| using | Merge_Fn = std::function< T(const T &, const T &)> |
| Merge function signature. | |
| using | Apply_Edge_Fn = std::function< T(Node *, Node *, const T &)> |
| Edge transformation signature. | |
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. | |
Generic rerooting dynamic programming (all-roots DP).
Efficiently computes a DP answer for every node in the tree as if each node were the root.
Standard bottom-up DP only gives the answer for a fixed root. Rerooting DP achieves the all-roots result in O(n) by performing:
The user provides:
identity: Neutral element for the merge operation.init: Base value for a single node.merge: Associative operation to combine results from children.apply_edge: Function to transform a subtree's result when passing through an edge to the parent.| GT | Graph type. |
| T | Value type. |
| SA | Arc filter. |
|
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 518 of file Tree_DP.H.
References Aleph::Gen_Reroot_DP< GT, T, SA >::answer_, Aleph::blossom_maximum_cardinality_matching(), Aleph::Array< T >::create(), 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 641 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 632 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 623 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 611 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 617 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().
|
private |
Final answer for each node as root.
Definition at line 505 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().
|
private |
Bottom-up DP results.
Definition at line 503 of file Tree_DP.H.
Referenced by Aleph::Gen_Reroot_DP< GT, T, SA >::Gen_Reroot_DP().
|
private |
Top-down contribution from parent side.
Definition at line 504 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 501 of file Tree_DP.H.
Referenced by Aleph::Gen_Reroot_DP< GT, T, SA >::Gen_Reroot_DP().
|
private |
Cached per-node base values.
Definition at line 502 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 500 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().