Aleph-w 7.10.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.
 
 Gen_Euler_Tour_LCA (const Gen_Euler_Tour_LCA &other)=default
 Copy the tree and the tour of another engine.
 
 Gen_Euler_Tour_LCA (Gen_Euler_Tour_LCA &&other) noexcept(std::is_nothrow_move_constructible_v< Gen_Cartesian_Tree< T, Comp > >)
 Take the tree and the tour of another engine, which is left empty.
 
Gen_Euler_Tour_LCA & operator= (const Gen_Euler_Tour_LCA &other)=default
 Copy the tree and the tour of another engine.
 
Gen_Euler_Tour_LCA & operator= (Gen_Euler_Tour_LCA &&other) noexcept(noexcept(swap(other)))
 Exchange this engine with another, which is left with the previous tree and tour of this one.
 
void swap (Gen_Euler_Tour_LCA &other) noexcept(std::is_nothrow_swappable_v< Comp >)
 Exchange in O(1) the tree and the tour of this engine and another.
 
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, Comp > tree_
 
Array< size_t > euler_
 
Array< size_t > depth_arr_
 
Array< size_t > first_
 
Array< size_t > node_depth_
 
Gen_Sparse_Table< DepthEntry, DepthMinOp > sparse_
 
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).
LCA on a min-heap Cartesian Tree.

Definition at line 551 of file tpl_cartesian_tree.H.

Constructor & Destructor Documentation

◆ Gen_Euler_Tour_LCA() [1/7]

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 686 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/7]

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/7]

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/7]

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

◆ Gen_Euler_Tour_LCA() [5/7]

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 718 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() [6/7]

template<typename T , class Comp >
Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA ( const Gen_Euler_Tour_LCA< T, Comp > &  other)
default

Copy the tree and the tour of another engine.

Parameters
[in]otherengine to copy.

◆ Gen_Euler_Tour_LCA() [7/7]

template<typename T , class Comp >
Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA ( Gen_Euler_Tour_LCA< T, Comp > &&  other)
inlinenoexcept

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

The implicit move took the arrays of the tour but copied its size: euler_tour() of the moved-from engine then read a tour that it no longer had.

Parameters
[in,out]otherengine to move from.

Definition at line 739 of file tpl_cartesian_tree.H.

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 810 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 821 of file tpl_cartesian_tree.H.

References 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

Copy of the Euler Tour array (for inspection/debugging).

Definition at line 846 of file tpl_cartesian_tree.H.

References Aleph::Array< T >::create(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_, and Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_size_.

Referenced by scenario_2().

◆ 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 855 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 834 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 791 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(), scenario_4(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ operator=() [1/2]

template<typename T , class Comp >
Gen_Euler_Tour_LCA & Aleph::Gen_Euler_Tour_LCA< T, Comp >::operator= ( const Gen_Euler_Tour_LCA< T, Comp > &  other)
default

Copy the tree and the tour of another engine.

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

◆ operator=() [2/2]

template<typename T , class Comp >
Gen_Euler_Tour_LCA & Aleph::Gen_Euler_Tour_LCA< T, Comp >::operator= ( Gen_Euler_Tour_LCA< T, Comp > &&  other)
inlinenoexcept

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

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

Definition at line 758 of file tpl_cartesian_tree.H.

References Aleph::Gen_Euler_Tour_LCA< T, Comp >::swap().

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

◆ swap()

template<typename T , class Comp >
void Aleph::Gen_Euler_Tour_LCA< T, Comp >::swap ( Gen_Euler_Tour_LCA< T, Comp > &  other)
inlinenoexcept

Exchange in O(1) the tree and the tour of this engine and another.

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

Definition at line 772 of file tpl_cartesian_tree.H.

References Aleph::Gen_Euler_Tour_LCA< T, Comp >::depth_arr_, Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_, Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_size_, Aleph::Gen_Euler_Tour_LCA< T, Comp >::first_, Aleph::Gen_Euler_Tour_LCA< T, Comp >::node_depth_, Aleph::Gen_Euler_Tour_LCA< T, Comp >::sparse_, Aleph::Array< T >::swap(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree_.

Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::operator=().

◆ 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 840 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: