|
Aleph-w 7.10.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. | |
| 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_ |
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 551 of file tpl_cartesian_tree.H.
|
inline |
Construct from an Array<T>.
| values | source array. |
| c | comparator. |
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().
|
inline |
Construct from a std::vector<T>.
Definition at line 694 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 702 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 710 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 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().
|
default |
Copy the tree and the tour of another engine.
| [in] | other | engine to copy. |
|
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.
| [in,out] | other | engine to move from. |
Definition at line 739 of file tpl_cartesian_tree.H.
|
inlineprivate |
Build Euler Tour iteratively using an explicit stack with phases.
Definition at line 579 of file tpl_cartesian_tree.H.
References Aleph::Array< T >::create(), 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_, 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 669 of file tpl_cartesian_tree.H.
References Aleph::Array< T >::create(), 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_, 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 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().
|
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().
|
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().
|
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().
|
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().
|
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 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().
|
default |
Copy the tree and the tour of another engine.
| [in] | other | engine to copy. |
|
inlinenoexcept |
Exchange this engine with another, which is left with the previous tree and tour of this one.
| [in,out] | other | engine to move from. |
Definition at line 758 of file tpl_cartesian_tree.H.
References Aleph::Gen_Euler_Tour_LCA< T, Comp >::swap().
|
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().
|
inlinenoexcept |
Exchange in O(1) the tree and the tour of this engine and another.
| [in,out] | other | engine to exchange with. |
| what | swapping 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=().
|
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().
|
private |
Definition at line 572 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 >::swap().
|
private |
|
private |
Definition at line 576 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(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_tour(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::euler_tour_size(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::swap().
|
private |
Definition at line 573 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 >::lca(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::swap().
|
private |
Definition at line 574 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 >::distance(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::swap().
|
private |
Definition at line 575 of file tpl_cartesian_tree.H.
Referenced by Aleph::Gen_Euler_Tour_LCA< T, Comp >::build_sparse_table(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::lca(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::swap().
|
private |
Definition at line 570 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(), Aleph::Gen_Euler_Tour_LCA< T, Comp >::swap(), and Aleph::Gen_Euler_Tour_LCA< T, Comp >::tree().