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

LCA via Euler tour + RMQ on depth in a rooted tree. More...

#include <LCA.H>

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

Public Types

using Node = typename GT::Node
 

Public Member Functions

 Gen_Euler_RMQ_LCA (const GT &g, Node *root, SA sa=SA())
 Construct from graph + explicit root.
 
 Gen_Euler_RMQ_LCA (const GT &g, SA sa=SA())
 Construct using the first graph node as root.
 
 Gen_Euler_RMQ_LCA (const Gen_Euler_RMQ_LCA &other)=default
 Copy the tree and the tables of another engine.
 
 Gen_Euler_RMQ_LCA (Gen_Euler_RMQ_LCA &&other)=default
 Take the tree and the tables of another engine, which is left empty.
 
Gen_Euler_RMQ_LCA & operator= (const Gen_Euler_RMQ_LCA &other)=default
 Copy the tree and the tables of another engine.
 
Gen_Euler_RMQ_LCA & operator= (Gen_Euler_RMQ_LCA &&other) noexcept(noexcept(swap(other)))
 Exchange this engine with another, which is left with the previous tree of this one.
 
void swap (Gen_Euler_RMQ_LCA &other) noexcept(std::is_nothrow_swappable_v< SA >)
 Exchange in O(1) this engine and another.
 
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 of the tree.
 
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 [0, n-1] back to a Node pointer.
 
size_t id_of (const Node *node) const
 Map a Node pointer to its internal ID [0, n-1].
 
size_t depth_of_id (const size_t id) const
 Distance from root to node by ID.
 
size_t depth_of (const Node *node) const
 Distance from root to 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.
 
bool is_ancestor_id (const size_t u, const size_t v) const
 Returns true if node u is an ancestor of v (or u == v).
 
bool is_ancestor (const Node *u, const Node *v) const
 Returns true if node u is an ancestor of v (or u == v).
 
size_t lca_id (const size_t u, const size_t v) const
 LCA query by node ids in O(1).
 
Node * lca (const Node *u, const Node *v) const
 LCA query by node pointers in O(1).
 
size_t distance_id (const size_t u, const size_t v) const
 Number of edges on the path between two ids in O(1).
 
size_t distance (const Node *u, const Node *v) const
 Number of edges on the path between two nodes in O(1).
 
const Array< size_t > & euler_tour () const noexcept
 Euler tour sequence of node ids ( \(2n - 1\)entries).
 
size_t euler_tour_size () const noexcept
 Euler tour size ( \(0\)for empty tree, else \(2n - 1\)).
 

Private Types

using Topology = lca_detail::Rooted_Tree_Data< GT, SA >
 
using Depth_Node = lca_detail::Depth_Node
 
using Depth_Node_Min_Op = lca_detail::Depth_Node_Min_Op
 

Private Member Functions

void ensure_not_empty (const char *where) const
 
void build_rmq ()
 

Private Attributes

Topology topology_
 
Array< Depth_Node > euler_depth_
 
Gen_Sparse_Table< Depth_Node, Depth_Node_Min_Op > rmq_
 

Static Private Attributes

static constexpr size_t NONE = Topology::NONE
 

Detailed Description

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
class Aleph::Gen_Euler_RMQ_LCA< GT, SA >

LCA via Euler tour + RMQ on depth in a rooted tree.

This engine reduces the LCA problem to a Range Minimum Query (RMQ) problem.

The Reduction:

  1. Perform an Euler tour of the tree, recording each node as it is visited and as we return from its children. The tour has size \(2n - 1\).
  2. For each node in the tour, store its depth.
  3. Store the first occurrence index first[v] of each node v in the tour.
  4. The LCA of \(u\)and \(v\)is the node with the minimum depth in the Euler tour range [first[u], first[v]].

By using a Sparse Table for RMQ, we achieve constant time \(O(1)\)queries after \(O(n \log n)\)preprocessing.

Template Parameters
GTGraph type (List_Graph, List_SGraph, Array_Graph).
SAArc filter type (default: Dft_Show_Arc<GT>).

Definition at line 856 of file LCA.H.

Member Typedef Documentation

◆ Depth_Node

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Depth_Node = lca_detail::Depth_Node
private

Definition at line 864 of file LCA.H.

◆ Depth_Node_Min_Op

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Depth_Node_Min_Op = lca_detail::Depth_Node_Min_Op
private

Definition at line 865 of file LCA.H.

◆ Node

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

Definition at line 859 of file LCA.H.

◆ Topology

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Topology = lca_detail::Rooted_Tree_Data<GT, SA>
private

Definition at line 862 of file LCA.H.

Constructor & Destructor Documentation

◆ Gen_Euler_RMQ_LCA() [1/4]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Gen_Euler_RMQ_LCA ( const GT &  g,
Node *  root,
SA  sa = SA() 
)
inline

Construct from graph + explicit root.

The constructor validates that the filtered graph is a tree.

Exceptions
ah_domain_errorif the filtered graph is not a tree.

Definition at line 899 of file LCA.H.

References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::build_rmq().

◆ Gen_Euler_RMQ_LCA() [2/4]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Gen_Euler_RMQ_LCA ( const GT &  g,
SA  sa = SA() 
)
inline

Construct using the first graph node as root.

Definition at line 906 of file LCA.H.

References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::build_rmq().

◆ Gen_Euler_RMQ_LCA() [3/4]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Gen_Euler_RMQ_LCA ( const Gen_Euler_RMQ_LCA< GT, SA > &  other)
default

Copy the tree and the tables of another engine.

Parameters
[in]otherengine to copy.

◆ Gen_Euler_RMQ_LCA() [4/4]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Gen_Euler_RMQ_LCA ( Gen_Euler_RMQ_LCA< GT, SA > &&  other)
default

Take the tree and the tables of another engine, which is left empty.

The topology of the moved-from engine is empty, so that the engine never queries the sparse table that the move leaves to it.

Parameters
[in,out]otherengine to move from.

Member Function Documentation

◆ build_rmq()

◆ depth_of()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Euler_RMQ_LCA< GT, SA >::depth_of ( const Node *  node) const
inline

◆ depth_of_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Euler_RMQ_LCA< GT, SA >::depth_of_id ( const size_t  id) const
inline

◆ distance()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Euler_RMQ_LCA< GT, SA >::distance ( const Node *  u,
const Node *  v 
) const
inline

Number of edges on the path between two nodes in O(1).

Definition at line 1068 of file LCA.H.

References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::distance_id(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::id_of().

Referenced by TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().

◆ distance_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Euler_RMQ_LCA< GT, SA >::distance_id ( const size_t  u,
const size_t  v 
) const
inline

◆ ensure_not_empty()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
void Aleph::Gen_Euler_RMQ_LCA< GT, SA >::ensure_not_empty ( const char *  where) const
inlineprivate

◆ euler_tour()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
const Array< size_t > & Aleph::Gen_Euler_RMQ_LCA< GT, SA >::euler_tour ( ) const
inlinenoexcept

Euler tour sequence of node ids ( \(2n - 1\)entries).

Definition at line 1074 of file LCA.H.

References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::euler(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.

◆ euler_tour_size()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Euler_RMQ_LCA< GT, SA >::euler_tour_size ( ) const
inlinenoexcept

Euler tour size ( \(0\)for empty tree, else \(2n - 1\)).

Definition at line 1080 of file LCA.H.

References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::euler_size(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.

Referenced by TYPED_TEST(), and TYPED_TEST().

◆ id_of()

◆ is_ancestor()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_ancestor ( const Node *  u,
const Node *  v 
) const
inline

Returns true if node u is an ancestor of v (or u == v).

Definition at line 1034 of file LCA.H.

References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::id_of(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_ancestor_id().

Referenced by TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().

◆ is_ancestor_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_ancestor_id ( const size_t  u,
const size_t  v 
) const
inline

Returns true if node u is an ancestor of v (or u == v).

Definition at line 1028 of file LCA.H.

References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::is_ancestor(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.

Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_ancestor(), and TYPED_TEST().

◆ is_empty()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_empty ( ) const
inlinenoexcept

◆ lca()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_Euler_RMQ_LCA< GT, SA >::lca ( const Node *  u,
const Node *  v 
) const
inline

◆ lca_id()

◆ node_of()

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

◆ operator=() [1/2]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Gen_Euler_RMQ_LCA & Aleph::Gen_Euler_RMQ_LCA< GT, SA >::operator= ( const Gen_Euler_RMQ_LCA< GT, SA > &  other)
default

Copy the tree and the tables of another engine.

Parameters
[in]otherengine to copy.
Returns
this engine.

◆ operator=() [2/2]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Gen_Euler_RMQ_LCA & Aleph::Gen_Euler_RMQ_LCA< GT, SA >::operator= ( Gen_Euler_RMQ_LCA< GT, SA > &&  other)
inlinenoexcept

Exchange this engine with another, which is left with the previous tree of this one.

The implicit assignment left the sparse table to its own move assignment, which exchanged its arrays but copied their size: the moved-from engine then queried the tables that it received with the size of its own.

Parameters
[in,out]otherengine to move from.
Returns
this engine.

Definition at line 944 of file LCA.H.

References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::swap().

◆ parent_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Euler_RMQ_LCA< GT, SA >::parent_id ( const size_t  id) const
inline

◆ parent_of()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_Euler_RMQ_LCA< GT, SA >::parent_of ( const Node *  node) const
inline

◆ root()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_Euler_RMQ_LCA< GT, SA >::root ( ) const
inlinenoexcept

Returns the root node of the tree.

Definition at line 977 of file LCA.H.

References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::root(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.

Referenced by TYPED_TEST(), and TYPED_TEST().

◆ root_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Euler_RMQ_LCA< GT, SA >::root_id ( ) const
inlinenoexcept

Returns the internal ID of the root node.

Definition at line 983 of file LCA.H.

References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::root_id(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.

◆ size()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Euler_RMQ_LCA< GT, SA >::size ( ) const
inlinenoexcept

Total number of nodes in the tree.

Definition at line 965 of file LCA.H.

References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::size(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.

Referenced by TYPED_TEST(), and TYPED_TEST().

◆ swap()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
void Aleph::Gen_Euler_RMQ_LCA< GT, SA >::swap ( Gen_Euler_RMQ_LCA< GT, SA > &  other)
inlinenoexcept

Exchange in O(1) this engine and another.

Parameters
[in,out]otherengine to exchange with.
Exceptions
whatswapping the filters throws. They are exchanged first, so that then nothing is exchanged: both engines are left as they were, if that swap left its arguments as they were.

Definition at line 957 of file LCA.H.

References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::euler_depth_, Aleph::Gen_Euler_RMQ_LCA< GT, SA >::rmq_, Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::swap(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.

Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::operator=().

Member Data Documentation

◆ euler_depth_

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Array<Depth_Node> Aleph::Gen_Euler_RMQ_LCA< GT, SA >::euler_depth_
private

◆ NONE

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
constexpr size_t Aleph::Gen_Euler_RMQ_LCA< GT, SA >::NONE = Topology::NONE
staticconstexprprivate

Definition at line 863 of file LCA.H.

Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::parent_of().

◆ rmq_

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Gen_Sparse_Table<Depth_Node, Depth_Node_Min_Op> Aleph::Gen_Euler_RMQ_LCA< GT, SA >::rmq_
private

◆ topology_


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