Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Gen_Reroot_DP< GT, T, SA > Class Template Reference

Generic rerooting dynamic programming (all-roots DP). More...

#include <Tree_DP.H>

Collaboration diagram for Aleph::Gen_Reroot_DP< GT, T, SA >:
[legend]

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.
 

Detailed Description

template<AlephGraph GT, typename T, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
class Aleph::Gen_Reroot_DP< GT, T, SA >

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:

  1. A bottom-up pass: Computes "downward" values for a fixed root.
  2. A top-down pass: Combines downward results with prefix/suffix merges to compute "upward" (parent-side) contributions.

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.
Template Parameters
GTGraph type.
TValue type.
SAArc filter.
Complexity: Time O(n), Space O(n).
Example: Tree Diameter / Eccentricities
0, // identity
[](auto *n) { return 0; }, // init: leaf distance is 0
[](size_t a, size_t b) { return std::max(a, b); }, // merge max
[](auto *p, auto *c, size_t val) { return val + 1; } // apply edge: dist+1
);
size_t max_dist_from_v = dp.value(v); // Max distance from v to any leaf
Generic rerooting dynamic programming (all-roots DP).
Definition Tree_DP.H:479
const T & value(Node *node) const
Returns the answer for a given node as root.
Definition Tree_DP.H:611
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466

Definition at line 478 of file Tree_DP.H.

Member Typedef Documentation

◆ Apply_Edge_Fn

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_Reroot_DP< GT, T, SA >::Apply_Edge_Fn = std::function<T(Node *, Node *, const T &)>

Edge transformation signature.

Applies the effect of an edge between parent and child to a subtree result. Signature: (parent, child, subtree_merged_value) -> contribution_to_parent

Definition at line 497 of file Tree_DP.H.

◆ Init_Fn

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_Reroot_DP< GT, T, SA >::Init_Fn = std::function<T(Node *)>

Initialization function signature.

Returns the base contribution of a node.

Definition at line 486 of file Tree_DP.H.

◆ Merge_Fn

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_Reroot_DP< GT, T, SA >::Merge_Fn = std::function<T(const T &, const T &)>

Merge function signature.

An associative binary operation to combine results from multiple branches.

Definition at line 491 of file Tree_DP.H.

◆ Node

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_Reroot_DP< GT, T, SA >::Node = typename GT::Node

Node type.

Definition at line 481 of file Tree_DP.H.

Constructor & Destructor Documentation

◆ Gen_Reroot_DP()

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Gen_Reroot_DP< GT, T, SA >::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() 
)
inline

Construct and compute rerooting DP.

Parameters
[in]gThe graph (tree under SA).
[in]rootInitial root for bottom-up pass.
[in]identityNeutral element for merge.
[in]initBase value for each node.
[in]mergeAssociative merge operation.
[in]apply_edgeTransform child value across an edge.
[in]saArc 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_.

Member Function Documentation

◆ id_of()

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Reroot_DP< GT, T, SA >::id_of ( Node *  node) const
inline

Returns the internal ID for a given node pointer.

Parameters
[in]nodeNode pointer.
Returns
Internal node ID.

Definition at line 641 of file Tree_DP.H.

References Aleph::Gen_Reroot_DP< GT, T, SA >::topo_.

◆ node_of()

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_Reroot_DP< GT, T, SA >::node_of ( size_t  id) const
inline

Returns the node pointer for a given internal ID.

Parameters
[in]idInternal node ID.
Returns
Node pointer.

Definition at line 632 of file Tree_DP.H.

References Aleph::Gen_Reroot_DP< GT, T, SA >::topo_.

◆ size()

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Reroot_DP< GT, T, SA >::size ( ) const
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_.

◆ value()

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const T & Aleph::Gen_Reroot_DP< GT, T, SA >::value ( Node *  node) const
inline

Returns the answer for a given node as root.

Parameters
[in]nodeNode pointer.
Returns
Constant reference to the computed DP answer.

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_.

◆ values()

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const Array< T > & Aleph::Gen_Reroot_DP< GT, T, SA >::values ( ) const
inlinenoexcept

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().

Member Data Documentation

◆ answer_

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Array<T> Aleph::Gen_Reroot_DP< GT, T, SA >::answer_
private

◆ dp_down_

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Array<T> Aleph::Gen_Reroot_DP< GT, T, SA >::dp_down_
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().

◆ dp_up_

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Array<T> Aleph::Gen_Reroot_DP< GT, T, SA >::dp_up_
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().

◆ identity_

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_Reroot_DP< GT, T, SA >::identity_
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().

◆ init_vals_

template<AlephGraph GT, typename T , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Array<T> Aleph::Gen_Reroot_DP< GT, T, SA >::init_vals_
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().

◆ topo_


The documentation for this class was generated from the following file: