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

Rerooting DP: O(n) computation of DP answer for all roots. 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: 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 Tvalue (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.
 
Nodenode_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, SAtopo_
 Tree topology and order.
 
T identity_
 Identity for merge operation.
 
Array< Tinit_vals_
 Cached per-node base values.
 
Array< Tdp_down_
 Bottom-up DP results.
 
Array< Tdp_up_
 Top-down contribution from parent side.
 
Array< Tanswer_
 Final answer for each node as root.
 

Detailed Description

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

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:

  • identity: neutral element for merge
  • init: base value for a node (leaf contribution)
  • merge: associative binary operation to combine child contributions
  • apply_edge: transforms a child's contribution when passing through an edge (parent, child, child_merged_value) -> contribution
Template Parameters
GTGraph type.
TValue type.
SAArc filter.
Example: Sum of distances from every node
// Each node's answer = sum of distances to all other nodes.
// Here T is a pair (count, sum_dist).
Note
Complexity: O(n) time and space.

Definition at line 461 of file Tree_DP.H.

Member Typedef Documentation

◆ Apply_Edge_Fn

template<class GT , typename T , class 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 function: applies edge effects to a subtree result.

Parameters: (parent_node, child_node, subtree_value) Returns: contribution to parent's DP.

Definition at line 477 of file Tree_DP.H.

◆ Init_Fn

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

Initialization function: base contribution of a node.

Definition at line 467 of file Tree_DP.H.

◆ Merge_Fn

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

Merge function: associative binary operation to combine results.

Definition at line 470 of file Tree_DP.H.

◆ Node

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

Node type.

Definition at line 464 of file Tree_DP.H.

Constructor & Destructor Documentation

◆ Gen_Reroot_DP()

template<class GT , typename T , class 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 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_.

Member Function Documentation

◆ id_of()

template<class GT , typename T , class 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 619 of file Tree_DP.H.

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

◆ node_of()

template<class GT , typename T , class 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 610 of file Tree_DP.H.

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

◆ size()

template<class GT , typename T , class 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 601 of file Tree_DP.H.

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

◆ value()

template<class GT , typename T , class 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 589 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<class GT , typename T , class 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 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().

Member Data Documentation

◆ answer_

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

◆ dp_down_

template<class GT , typename T , class SA = Dft_Show_Arc<GT>>
Array<T> Aleph::Gen_Reroot_DP< GT, T, SA >::dp_down_
private

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

◆ dp_up_

template<class GT , typename T , class 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 484 of file Tree_DP.H.

Referenced by Aleph::Gen_Reroot_DP< GT, T, SA >::Gen_Reroot_DP().

◆ identity_

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

◆ init_vals_

template<class GT , typename T , class 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 482 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: