|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Lowest Common Ancestor queries in O(1) via Euler Tour reduction to RMQ on a Cartesian Tree. More...
#include <tpl_cartesian_tree.H>
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, 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_ |
Lowest Common Ancestor queries in O(1) via Euler Tour reduction to RMQ on a Cartesian Tree.
Given an array, this class:
euler[] (2n-1 node visits) and depth[] arrays.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.
| T | element type. |
| Comp | comparator for the Cartesian Tree heap property. |
Definition at line 521 of file tpl_cartesian_tree.H.
|
inline |
Construct from an Array<T>.
| values | source array. |
| c | comparator. |
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().
|
inline |
Construct from a std::vector<T>.
Definition at line 664 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().
|
inline |
Construct from an initializer_list<T>.
Definition at line 672 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().
|
inline |
Construct from a DynList<T>.
Definition at line 680 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().
|
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().
|
inlineprivate |
Build Euler Tour iteratively using an explicit stack with phases.
Definition at line 549 of file tpl_cartesian_tree.H.
References Aleph::Array< T >::create(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::depth_arr_, Aleph::divide_and_conquer_partition_dp(), 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_, and Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree_.
Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA().
|
inlineprivate |
Build the Sparse Table over the depth entries.
Definition at line 639 of file tpl_cartesian_tree.H.
References Aleph::Array< T >::create(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::depth_arr_, Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_, Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_size_, and Aleph::Gen_Euler_Tour_LCA< T, Comp >::sparse_.
Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::Gen_Euler_Tour_LCA().
|
inline |
Depth of node u in the Cartesian Tree.
| std::out_of_range | if 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().
|
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().
|
inline |
Copy of the Euler Tour array (for inspection/debugging).
Definition at line 758 of file tpl_cartesian_tree.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_, and Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_size_.
Referenced by scenario_2().
|
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().
|
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().
|
inline |
Lowest Common Ancestor of nodes u and v in O(1).
Returns the index of the LCA node in the original array.
| u | 0-based node index. |
| v | 0-based node index. |
| std::out_of_range | if 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().
|
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().
|
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().
|
private |
Definition at line 542 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_euler_tour(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_sparse_table().
|
private |
Definition at line 541 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_euler_tour(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_sparse_table(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_tour().
|
private |
Definition at line 543 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_euler_tour(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::lca().
|
private |
Definition at line 544 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_euler_tour(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::depth_of(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::distance().
|
private |
Definition at line 545 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_sparse_table(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::lca().
|
private |
Definition at line 540 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_euler_tour(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::depth_of(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::is_empty(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::lca(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::size(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree().