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

Heavy-Light Decomposition with segment tree for path/subtree queries. More...

#include <HLD.H>

Inheritance diagram for Aleph::Gen_HLD< GT, T, Op, SA >:
[legend]
Collaboration diagram for Aleph::Gen_HLD< GT, T, Op, SA >:
[legend]

Public Types

using Node = typename GT::Node
 

Public Member Functions

template<class NodeValueFn >
 Gen_HLD (const GT &g, Node *root, NodeValueFn &&node_value, const T &identity, Op oper=Op(), SA sa=SA())
 Construct HLD from graph, root, and node value functor.
 
template<class NodeValueFn >
 Gen_HLD (const GT &g, NodeValueFn &&node_value, const T &identity, Op oper=Op(), SA sa=SA())
 Construct with first graph node as root.
 
size_t size () const noexcept
 Total number of nodes in the tree.
 
bool is_empty () const noexcept
 Returns true if the tree has no nodes.
 
Node * root () const noexcept
 Returns the root node.
 
size_t root_id () const noexcept
 Returns the internal ID of the root node.
 
Node * node_of (const size_t id) const
 Map an internal ID back to a Node pointer.
 
size_t id_of (const Node *node) const
 Map a Node pointer to its internal ID.
 
size_t position (const size_t id) const
 HLD flat-array position of node id.
 
size_t chain_head_id (const size_t id) const
 Head of the chain containing node id.
 
size_t depth_of_id (const size_t id) const
 Depth of node id from root.
 
size_t depth_of (const Node *node) const
 Depth of node from root.
 
size_t subtree_size_of_id (const size_t id) const
 Subtree size of node id.
 
size_t subtree_size_of (const Node *node) const
 Subtree size of node.
 
size_t parent_id (const size_t id) const
 Parent ID of node id, or NONE if root.
 
Node * parent_of (const Node *node) const
 Parent of node, or nullptr if root.
 
size_t num_chains () const noexcept
 Number of heavy chains in the decomposition.
 
T path_query_id (size_t u, size_t v) const
 Path query by node IDs in O(log^2 n).
 
T path_query (const Node *u, const Node *v) const
 Path query by node pointers in O(log^2 n).
 
T path_query_edges_id (size_t u, size_t v) const
 Edge-weighted path query by node IDs in O(log^2 n).
 
T path_query_edges (const Node *u, const Node *v) const
 Edge-weighted path query by node pointers.
 
T subtree_query_id (const size_t v) const
 Subtree query by node ID in O(log n).
 
T subtree_query (const Node *v) const
 Subtree query by node pointer in O(log n).
 
void point_update_id (const size_t v, const T &new_value)
 Point update by node ID: set value to new_value.
 
void point_update (const Node *v, const T &new_value)
 Point update by node pointer: set value to new_value.
 
size_t lca_id (size_t u, size_t v) const
 LCA query by node IDs in O(log n).
 
Node * lca (const Node *u, const Node *v) const
 LCA query by node pointers in O(log n).
 
size_t distance_id (const size_t u, const size_t v) const
 Number of edges between two nodes by IDs in O(log n).
 
size_t distance (const Node *u, const Node *v) const
 Number of edges between two nodes in O(log n).
 
const Array< size_t > & position_array () const noexcept
 Read-only access to the flat-position array.
 
const Array< size_t > & chain_head_array () const noexcept
 Read-only access to the chain-head array.
 
const Array< size_t > & depth_array () const noexcept
 Read-only access to the depth array.
 
const Array< size_t > & subtree_size_array () const noexcept
 Read-only access to the subtree-size array.
 
const Array< size_t > & parent_array () const noexcept
 Read-only access to the parent array.
 
T get_value_at_id (const size_t id) const
 Current node value at HLD position pos in the segment tree.
 
T get_value (const Node *v) const
 Current node value at node pointer v.
 

Private Types

using Topology = hld_detail::HLD_Tree_Data< GT, SA >
 

Private Member Functions

size_t n () const noexcept
 
void ensure_not_empty (const char *where) const
 
template<class NodeValueFn >
void build_segment_tree (NodeValueFn &&node_value)
 

Private Attributes

Topology topology_
 
T identity_
 
Op op_
 
Gen_Segment_Tree< T, Op > seg_
 

Static Private Attributes

static constexpr size_t NONE = Topology::NONE
 

Detailed Description

template<AlephGraph GT, typename T, class Op, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
requires SegmentTreeOp<Op, T>
class Aleph::Gen_HLD< GT, T, Op, SA >

Heavy-Light Decomposition with segment tree for path/subtree queries.

Decomposes a rooted tree into heavy chains and maintains a segment tree over the flattened HLD order. Supports path queries, subtree queries, point updates, LCA computation, and distance queries.

Template Parameters
GTGraph type (List_Graph, List_SGraph, Array_Graph).
TValue type stored at each node.
OpAssociative and commutative binary functor over T.
SAArc filter type (default: Dft_Show_Arc<GT>).
Complexity
Operation Time
Construction O(n log n)
path_query O(log^2 n)
subtree_query O(log n)
point_update O(log n)
lca O(log n)
distance O(log n)

Definition at line 528 of file HLD.H.

Member Typedef Documentation

◆ Node

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

Definition at line 531 of file HLD.H.

◆ Topology

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_HLD< GT, T, Op, SA >::Topology = hld_detail::HLD_Tree_Data<GT, SA>
private

Definition at line 534 of file HLD.H.

Constructor & Destructor Documentation

◆ Gen_HLD() [1/2]

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
template<class NodeValueFn >
Aleph::Gen_HLD< GT, T, Op, SA >::Gen_HLD ( const GT &  g,
Node *  root,
NodeValueFn &&  node_value,
const T &  identity,
Op  oper = Op(),
SA  sa = SA() 
)
inline

Construct HLD from graph, root, and node value functor.

Parameters
[in]gTree topology (filtered by sa).
[in]rootRoot node pointer (must belong to g).
[in]node_valueCallable Node* -> T providing node values.
[in]identityIdentity element of the monoid (T, Op).
[in]operAssociative binary functor.
[in]saArc filter.
Exceptions
ah_domain_errorif the filtered graph is not a tree.

Definition at line 582 of file HLD.H.

References Aleph::blossom_maximum_cardinality_matching(), and Aleph::Gen_HLD< GT, T, Op, SA >::build_segment_tree().

◆ Gen_HLD() [2/2]

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
template<class NodeValueFn >
Aleph::Gen_HLD< GT, T, Op, SA >::Gen_HLD ( const GT &  g,
NodeValueFn &&  node_value,
const T &  identity,
Op  oper = Op(),
SA  sa = SA() 
)
inline

Construct with first graph node as root.

Definition at line 594 of file HLD.H.

References Aleph::blossom_maximum_cardinality_matching(), and Aleph::Gen_HLD< GT, T, Op, SA >::build_segment_tree().

Member Function Documentation

◆ build_segment_tree()

◆ chain_head_array()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const Array< size_t > & Aleph::Gen_HLD< GT, T, Op, SA >::chain_head_array ( ) const
inlinenoexcept

Read-only access to the chain-head array.

chain_head_array()(id) gives the ID of the head of the chain containing node id.

Definition at line 892 of file HLD.H.

References Aleph::hld_detail::HLD_Tree_Data< GT, SA >::chain_head(), and Aleph::Gen_HLD< GT, T, Op, SA >::topology_.

◆ chain_head_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::chain_head_id ( const size_t  id) const
inline

◆ depth_array()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const Array< size_t > & Aleph::Gen_HLD< GT, T, Op, SA >::depth_array ( ) const
inlinenoexcept

Read-only access to the depth array.

Definition at line 898 of file HLD.H.

References Aleph::hld_detail::HLD_Tree_Data< GT, SA >::depth(), and Aleph::Gen_HLD< GT, T, Op, SA >::topology_.

◆ depth_of()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::depth_of ( const Node *  node) const
inline

Depth of node from root.

Definition at line 650 of file HLD.H.

References Aleph::Gen_HLD< GT, T, Op, SA >::depth_of_id(), and Aleph::Gen_HLD< GT, T, Op, SA >::id_of().

◆ depth_of_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::depth_of_id ( const size_t  id) const
inline

◆ distance()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::distance ( const Node *  u,
const Node *  v 
) const
inline

Number of edges between two nodes in O(log n).

Definition at line 869 of file HLD.H.

References Aleph::Gen_HLD< GT, T, Op, SA >::distance_id(), and Aleph::Gen_HLD< GT, T, Op, SA >::id_of().

◆ distance_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::distance_id ( const size_t  u,
const size_t  v 
) const
inline

◆ ensure_not_empty()

◆ get_value()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_HLD< GT, T, Op, SA >::get_value ( const Node *  v) const
inline

Current node value at node pointer v.

Definition at line 923 of file HLD.H.

References Aleph::Gen_HLD< GT, T, Op, SA >::get_value_at_id(), and Aleph::Gen_HLD< GT, T, Op, SA >::id_of().

◆ get_value_at_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_HLD< GT, T, Op, SA >::get_value_at_id ( const size_t  id) const
inline

◆ id_of()

◆ is_empty()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Gen_HLD< GT, T, Op, SA >::is_empty ( ) const
inlinenoexcept

◆ lca()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_HLD< GT, T, Op, SA >::lca ( const Node *  u,
const Node *  v 
) const
inline

LCA query by node pointers in O(log n).

Definition at line 851 of file HLD.H.

References Aleph::Gen_HLD< GT, T, Op, SA >::id_of(), Aleph::Gen_HLD< GT, T, Op, SA >::lca_id(), and Aleph::Gen_HLD< GT, T, Op, SA >::node_of().

◆ lca_id()

◆ n()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::n ( ) const
inlineprivatenoexcept

◆ node_of()

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

◆ num_chains()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::num_chains ( ) const
inlinenoexcept

Number of heavy chains in the decomposition.

Definition at line 683 of file HLD.H.

References Aleph::hld_detail::HLD_Tree_Data< GT, SA >::num_chains(), and Aleph::Gen_HLD< GT, T, Op, SA >::topology_.

◆ parent_array()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const Array< size_t > & Aleph::Gen_HLD< GT, T, Op, SA >::parent_array ( ) const
inlinenoexcept

Read-only access to the parent array.

Definition at line 910 of file HLD.H.

References Aleph::hld_detail::HLD_Tree_Data< GT, SA >::parent(), and Aleph::Gen_HLD< GT, T, Op, SA >::topology_.

◆ parent_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::parent_id ( const size_t  id) const
inline

◆ parent_of()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_HLD< GT, T, Op, SA >::parent_of ( const Node *  node) const
inline

◆ path_query()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_HLD< GT, T, Op, SA >::path_query ( const Node *  u,
const Node *  v 
) const
inline

Path query by node pointers in O(log^2 n).

Definition at line 733 of file HLD.H.

References Aleph::Gen_HLD< GT, T, Op, SA >::id_of(), and Aleph::Gen_HLD< GT, T, Op, SA >::path_query_id().

◆ path_query_edges()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_HLD< GT, T, Op, SA >::path_query_edges ( const Node *  u,
const Node *  v 
) const
inline

Edge-weighted path query by node pointers.

Definition at line 775 of file HLD.H.

References Aleph::Gen_HLD< GT, T, Op, SA >::id_of(), and Aleph::Gen_HLD< GT, T, Op, SA >::path_query_edges_id().

◆ path_query_edges_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_HLD< GT, T, Op, SA >::path_query_edges_id ( size_t  u,
size_t  v 
) const
inline

◆ path_query_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_HLD< GT, T, Op, SA >::path_query_id ( size_t  u,
size_t  v 
) const
inline

◆ point_update()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
void Aleph::Gen_HLD< GT, T, Op, SA >::point_update ( const Node *  v,
const T &  new_value 
)
inline

Point update by node pointer: set value to new_value.

Definition at line 822 of file HLD.H.

References Aleph::blossom_maximum_cardinality_matching(), Aleph::Gen_HLD< GT, T, Op, SA >::id_of(), and Aleph::Gen_HLD< GT, T, Op, SA >::point_update_id().

◆ point_update_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
void Aleph::Gen_HLD< GT, T, Op, SA >::point_update_id ( const size_t  v,
const T &  new_value 
)
inline

◆ position()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::position ( const size_t  id) const
inline

◆ position_array()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const Array< size_t > & Aleph::Gen_HLD< GT, T, Op, SA >::position_array ( ) const
inlinenoexcept

Read-only access to the flat-position array.

position_array()(id) gives the segment-tree index of node id.

Definition at line 882 of file HLD.H.

References Aleph::hld_detail::HLD_Tree_Data< GT, SA >::pos(), and Aleph::Gen_HLD< GT, T, Op, SA >::topology_.

◆ root()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_HLD< GT, T, Op, SA >::root ( ) const
inlinenoexcept

Returns the root node.

Definition at line 611 of file HLD.H.

References Aleph::hld_detail::HLD_Tree_Data< GT, SA >::root(), and Aleph::Gen_HLD< GT, T, Op, SA >::topology_.

◆ root_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::root_id ( ) const
inlinenoexcept

Returns the internal ID of the root node.

Definition at line 614 of file HLD.H.

References Aleph::hld_detail::HLD_Tree_Data< GT, SA >::root_id(), and Aleph::Gen_HLD< GT, T, Op, SA >::topology_.

◆ size()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::size ( ) const
inlinenoexcept

Total number of nodes in the tree.

Definition at line 605 of file HLD.H.

References Aleph::hld_detail::HLD_Tree_Data< GT, SA >::size(), and Aleph::Gen_HLD< GT, T, Op, SA >::topology_.

◆ subtree_query()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_HLD< GT, T, Op, SA >::subtree_query ( const Node *  v) const
inline

Subtree query by node pointer in O(log n).

Definition at line 799 of file HLD.H.

References Aleph::Gen_HLD< GT, T, Op, SA >::id_of(), and Aleph::Gen_HLD< GT, T, Op, SA >::subtree_query_id().

◆ subtree_query_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_HLD< GT, T, Op, SA >::subtree_query_id ( const size_t  v) const
inline

◆ subtree_size_array()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const Array< size_t > & Aleph::Gen_HLD< GT, T, Op, SA >::subtree_size_array ( ) const
inlinenoexcept

Read-only access to the subtree-size array.

Definition at line 904 of file HLD.H.

References Aleph::hld_detail::HLD_Tree_Data< GT, SA >::subtree_size(), and Aleph::Gen_HLD< GT, T, Op, SA >::topology_.

◆ subtree_size_of()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::subtree_size_of ( const Node *  node) const
inline

Subtree size of node.

Definition at line 663 of file HLD.H.

References Aleph::Gen_HLD< GT, T, Op, SA >::id_of(), and Aleph::Gen_HLD< GT, T, Op, SA >::subtree_size_of_id().

◆ subtree_size_of_id()

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_HLD< GT, T, Op, SA >::subtree_size_of_id ( const size_t  id) const
inline

Member Data Documentation

◆ identity_

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
T Aleph::Gen_HLD< GT, T, Op, SA >::identity_
private

◆ NONE

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
constexpr size_t Aleph::Gen_HLD< GT, T, Op, SA >::NONE = Topology::NONE
staticconstexprprivate

Definition at line 535 of file HLD.H.

Referenced by Aleph::Gen_HLD< GT, T, Op, SA >::parent_of().

◆ op_

template<AlephGraph GT, typename T , class Op , ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Op Aleph::Gen_HLD< GT, T, Op, SA >::op_
private

◆ seg_

◆ topology_


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