|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
LCA via Euler tour + RMQ on depth in a rooted tree. More...
#include <LCA.H>
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 |
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:
first[v] of each node v in the tour.[first[u], first[v]].By using a Sparse Table for RMQ, we achieve constant time \(O(1)\)queries after \(O(n \log n)\)preprocessing.
| GT | Graph type (List_Graph, List_SGraph, Array_Graph). |
| SA | Arc filter type (default: Dft_Show_Arc<GT>). |
|
private |
|
private |
| using Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Node = typename GT::Node |
|
private |
|
inline |
Construct from graph + explicit root.
The constructor validates that the filtered graph is a tree.
| ah_domain_error | if 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().
|
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().
|
default |
Copy the tree and the tables of another engine.
| [in] | other | engine to copy. |
|
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.
| [in,out] | other | engine to move from. |
|
inlineprivate |
Definition at line 877 of file LCA.H.
References Aleph::Array< T >::create(), Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::depth(), Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::euler(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::euler_depth_, Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::euler_size(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_empty(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::rmq_, and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Gen_Euler_RMQ_LCA(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::Gen_Euler_RMQ_LCA().
|
inline |
Distance from root to node.
Definition at line 1008 of file LCA.H.
References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::depth_of_id(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::id_of().
Referenced by TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().
|
inline |
Distance from root to node by ID.
Definition at line 1001 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::depth(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_, and Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::validate_id().
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::depth_of().
|
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().
|
inline |
Number of edges on the path between two ids in O(1).
Definition at line 1061 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::depth(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::lca_id(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::distance().
|
inlineprivate |
Definition at line 871 of file LCA.H.
References ah_domain_error_if, and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_empty().
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::lca_id().
|
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_.
|
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().
|
inline |
Map a Node pointer to its internal ID [0, n-1].
Definition at line 995 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::id_of(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::depth_of(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::distance(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_ancestor(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::lca(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::parent_of(), and TYPED_TEST().
|
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().
|
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().
|
inlinenoexcept |
Returns true if the tree has no nodes.
Definition at line 971 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::is_empty(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::build_rmq(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::ensure_not_empty(), TYPED_TEST(), and TYPED_TEST().
|
inline |
LCA query by node pointers in O(1).
Definition at line 1055 of file LCA.H.
References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::id_of(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::lca_id(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::node_of().
Referenced by TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().
|
inline |
LCA query by node ids in O(1).
Definition at line 1040 of file LCA.H.
References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::ensure_not_empty(), Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::first(), l, r, Aleph::Gen_Euler_RMQ_LCA< GT, SA >::rmq_, Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_, and Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::validate_id().
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::distance_id(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::lca(), TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().
|
inline |
Map an internal ID [0, n-1] back to a Node pointer.
Definition at line 989 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::node_of(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::lca(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::parent_of(), TYPED_TEST(), and TYPED_TEST().
|
default |
Copy the tree and the tables of another engine.
| [in] | other | engine to copy. |
|
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.
| [in,out] | other | engine to move from. |
Definition at line 944 of file LCA.H.
References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::swap().
|
inline |
Parent ID of node id, or NONE if root.
Definition at line 1014 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::parent(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::topology_, and Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::validate_id().
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::parent_of().
|
inline |
Parent of node, or nullptr if root.
Definition at line 1021 of file LCA.H.
References Aleph::Gen_Euler_RMQ_LCA< GT, SA >::id_of(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::node_of(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::NONE, and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::parent_id().
Referenced by TYPED_TEST().
|
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().
|
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_.
|
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().
|
inlinenoexcept |
Exchange in O(1) this engine and another.
| [in,out] | other | engine to exchange with. |
| what | swapping 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=().
|
private |
Definition at line 868 of file LCA.H.
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::build_rmq(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::swap().
|
staticconstexprprivate |
Definition at line 863 of file LCA.H.
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::parent_of().
|
private |
Definition at line 869 of file LCA.H.
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::build_rmq(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::lca_id(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::swap().
|
private |
Definition at line 867 of file LCA.H.
Referenced by Aleph::Gen_Euler_RMQ_LCA< GT, SA >::build_rmq(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::depth_of_id(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::distance_id(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::euler_tour(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::euler_tour_size(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::id_of(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_ancestor_id(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::is_empty(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::lca_id(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::node_of(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::parent_id(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::root(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::root_id(), Aleph::Gen_Euler_RMQ_LCA< GT, SA >::size(), and Aleph::Gen_Euler_RMQ_LCA< GT, SA >::swap().