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

Lowest Common Ancestor queries in O(1) via Euler Tour reduction to RMQ on a Cartesian Tree. More...

#include <tpl_cartesian_tree.H>

Inheritance diagram for Aleph::Gen_Euler_Tour_LCA< T, Comp >:
[legend]
Collaboration diagram for Aleph::Gen_Euler_Tour_LCA< T, Comp >:
[legend]

Classes

struct  DepthEntry
 Entry for the depth-based Sparse Table: (depth, euler_node_index). More...
 
struct  DepthMinOp
 Min-op on DepthEntry: compares by depth. More...
 

Public Member Functions

 Gen_Euler_Tour_LCA (const Array< T > &values, Comp c=Comp())
 Construct from an Array<T>.
 
 Gen_Euler_Tour_LCA (const std::vector< T > &values, Comp c=Comp())
 Construct from a std::vector<T>.
 
 Gen_Euler_Tour_LCA (std::initializer_list< T > il, Comp c=Comp())
 Construct from an initializer_list<T>.
 
 Gen_Euler_Tour_LCA (const DynList< T > &values, Comp c=Comp())
 Construct from a DynList<T>.
 
 Gen_Euler_Tour_LCA (const size_t num, const T &init, Comp c=Comp())
 Construct with num elements all equal to init.
 
size_t lca (const size_t u, const size_t v) const
 Lowest Common Ancestor of nodes u and v in O(1).
 
size_t depth_of (const size_t u) const
 Depth of node u in the Cartesian Tree.
 
size_t distance (const size_t u, const size_t v) const
 Distance between nodes u and v in the tree.
 
constexpr size_t size () const noexcept
 Number of nodes.
 
constexpr bool is_empty () const noexcept
 True if empty.
 
const Gen_Cartesian_Tree< T, Comp > & tree () const noexcept
 Const reference to the internal Cartesian Tree.
 
Array< size_t > euler_tour () const
 Copy of the Euler Tour array (for inspection/debugging).
 
size_t euler_tour_size () const noexcept
 Size of the Euler Tour (should be 2n-1 for n > 0).
 

Private Member Functions

void build_euler_tour ()
 Build Euler Tour iteratively using an explicit stack with phases.
 
void build_sparse_table ()
 Build the Sparse Table over the depth entries.
 

Private Attributes

Gen_Cartesian_Tree< T, Comptree_
 
Array< size_t > euler_
 
Array< size_t > depth_arr_
 
Array< size_t > first_
 
Array< size_t > node_depth_
 
Gen_Sparse_Table< DepthEntry, DepthMinOpsparse_
 
size_t euler_size_
 

Detailed Description

template<typename T, class Comp>
requires StrictWeakOrder<Comp, T>
class Aleph::Gen_Euler_Tour_LCA< T, Comp >

Lowest Common Ancestor queries in O(1) via Euler Tour reduction to RMQ on a Cartesian Tree.

Given an array, this class:

  1. Builds a Cartesian Tree in O(n).
  2. Performs an Euler Tour of the tree, producing euler[] (2n-1 node visits) and depth[] arrays.
  3. Builds a Sparse Table over the depth array for O(1) RMQ.

The LCA of two nodes u, v corresponds to the minimum-depth node in the Euler Tour between the first occurrences of u and v.

Template Parameters
Telement type.
Compcomparator for the Cartesian Tree heap property.
Example
Euler_Tour_LCA<int> lca = {3, 2, 6, 1, 9};
size_t ancestor = lca.lca(0, 2); // LCA of positions 0 and 2
size_t lca(const size_t u, const size_t v) const
Lowest Common Ancestor of nodes u and v in O(1).
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
LCA on a min-heap Cartesian Tree.

Definition at line 521 of file tpl_cartesian_tree.H.

Constructor & Destructor Documentation

◆ Gen_Euler_Tour_LCA() [1/5]

template<typename T , class Comp >
Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA ( const Array< T > &  values,
Comp  c = Comp() 
)
inline

Construct from an Array<T>.

Parameters
valuessource array.
ccomparator.

Definition at line 656 of file tpl_cartesian_tree.H.

References Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_euler_tour(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_sparse_table().

◆ Gen_Euler_Tour_LCA() [2/5]

template<typename T , class Comp >
Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA ( const std::vector< T > &  values,
Comp  c = Comp() 
)
inline

◆ Gen_Euler_Tour_LCA() [3/5]

template<typename T , class Comp >
Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA ( std::initializer_list< T il,
Comp  c = Comp() 
)
inline

◆ Gen_Euler_Tour_LCA() [4/5]

◆ Gen_Euler_Tour_LCA() [5/5]

template<typename T , class Comp >
Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA ( const size_t  num,
const T init,
Comp  c = Comp() 
)
inline

Construct with num elements all equal to init.

Definition at line 688 of file tpl_cartesian_tree.H.

References Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_euler_tour(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_sparse_table().

Member Function Documentation

◆ build_euler_tour()

◆ build_sparse_table()

◆ depth_of()

template<typename T , class Comp >
size_t Aleph::Gen_Euler_Tour_LCA< T, Comp >::depth_of ( const size_t  u) const
inline

Depth of node u in the Cartesian Tree.

Exceptions
std::out_of_rangeif u >= size().

Definition at line 722 of file tpl_cartesian_tree.H.

References ah_out_of_range_error_if, Aleph::Gen_Euler_Tour_LCA< T, Comp >::node_depth_, and Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree_.

Referenced by scenario_2(), TEST(), TEST(), and TEST().

◆ distance()

template<typename T , class Comp >
size_t Aleph::Gen_Euler_Tour_LCA< T, Comp >::distance ( const size_t  u,
const size_t  v 
) const
inline

Distance between nodes u and v in the tree.

Equals depth(u) + depth(v) - 2 * depth(lca(u, v)).

Definition at line 733 of file tpl_cartesian_tree.H.

References Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::lca(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::node_depth_.

Referenced by scenario_2(), TEST(), and TEST().

◆ euler_tour()

template<typename T , class Comp >
Array< size_t > Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_tour ( ) const
inline

◆ euler_tour_size()

template<typename T , class Comp >
size_t Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_tour_size ( ) const
inlinenoexcept

Size of the Euler Tour (should be 2n-1 for n > 0).

Definition at line 767 of file tpl_cartesian_tree.H.

References Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_size_.

Referenced by scenario_2(), and TEST().

◆ is_empty()

template<typename T , class Comp >
constexpr bool Aleph::Gen_Euler_Tour_LCA< T, Comp >::is_empty ( ) const
inlineconstexprnoexcept

True if empty.

Definition at line 746 of file tpl_cartesian_tree.H.

References Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree_.

Referenced by TEST().

◆ lca()

template<typename T , class Comp >
size_t Aleph::Gen_Euler_Tour_LCA< T, Comp >::lca ( const size_t  u,
const size_t  v 
) const
inline

Lowest Common Ancestor of nodes u and v in O(1).

Returns the index of the LCA node in the original array.

Parameters
u0-based node index.
v0-based node index.
Exceptions
std::out_of_rangeif u or v are out of range.

Definition at line 703 of file tpl_cartesian_tree.H.

References ah_out_of_range_error_if, Aleph::Gen_Euler_Tour_LCA< T, Comp >::first_, l, r, Aleph::Gen_Euler_Tour_LCA< T, Comp >::sparse_, and Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree_.

Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::distance(), scenario_2(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ size()

template<typename T , class Comp >
constexpr size_t Aleph::Gen_Euler_Tour_LCA< T, Comp >::size ( ) const
inlineconstexprnoexcept

Number of nodes.

Definition at line 740 of file tpl_cartesian_tree.H.

References Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree_.

Referenced by scenario_2(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ tree()

template<typename T , class Comp >
const Gen_Cartesian_Tree< T, Comp > & Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree ( ) const
inlinenoexcept

Const reference to the internal Cartesian Tree.

Definition at line 752 of file tpl_cartesian_tree.H.

References Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree_.

Referenced by scenario_2(), TEST(), and TEST().

Member Data Documentation

◆ depth_arr_

◆ euler_

◆ euler_size_

◆ first_

◆ node_depth_

◆ sparse_

◆ tree_


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