|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
LCA via binary lifting on a rooted tree. More...
#include <LCA.H>
Public Types | |
| using | Node = typename GT::Node |
Public Member Functions | |
| Gen_Binary_Lifting_LCA (const GT &g, Node *root, SA sa=SA()) | |
| Construct from graph + explicit root. | |
| Gen_Binary_Lifting_LCA (const GT &g, SA sa=SA()) | |
| Construct using the first graph node as root. | |
| Gen_Binary_Lifting_LCA (const Gen_Binary_Lifting_LCA &other)=default | |
| Copy the tree and the jump table of another engine. | |
| Gen_Binary_Lifting_LCA (Gen_Binary_Lifting_LCA &&other) noexcept(std::is_nothrow_move_constructible_v< Topology >) | |
| Take the tree and the jump table of another engine, which is left empty. | |
| Gen_Binary_Lifting_LCA & | operator= (const Gen_Binary_Lifting_LCA &other)=default |
| Copy the tree and the jump table of another engine. | |
| Gen_Binary_Lifting_LCA & | operator= (Gen_Binary_Lifting_LCA &&other) noexcept(noexcept(swap(other))) |
| Exchange this engine with another, which is left with the previous tree of this one. | |
| void | swap (Gen_Binary_Lifting_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. | |
| size_t | num_levels () const noexcept |
| Returns the number of levels in the jump table: \(\lfloor \log_2 n \rfloor + 1\), or 0 if the tree is empty. | |
| 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 | kth_ancestor_id (const size_t id, const size_t k) const |
| k-th ancestor by id in O(log n). | |
| Node * | kth_ancestor (const Node *node, const size_t k) const |
| k-th ancestor by node pointer in O(log n). | |
| size_t | lca_id (size_t u, size_t v) const |
| LCA query by node ids in O(log n). | |
| Node * | lca (const Node *u, const Node *v) const |
| LCA query by node pointers in O(log n). | |
| size_t | distance_id (const size_t u, const size_t v) const |
| Number of edges on the path between two ids in O(log n). | |
| size_t | distance (const Node *u, const Node *v) const |
| Number of edges on the path between two nodes in O(log n). | |
Private Types | |
| using | Topology = lca_detail::Rooted_Tree_Data< GT, SA > |
Private Member Functions | |
| size_t | n () const noexcept |
| size_t & | up_at (const size_t k, const size_t v) noexcept |
| size_t | up_at (const size_t k, const size_t v) const noexcept |
| void | ensure_not_empty (const char *where) const |
| void | build_jump_table () |
| size_t | lift (size_t v, size_t delta) const |
Private Attributes | |
| Topology | topology_ |
| size_t | levels_ = 0 |
| Array< size_t > | up_ |
Static Private Attributes | |
| static constexpr size_t | NONE = Topology::NONE |
LCA via binary lifting on a rooted tree.
Binary lifting is a technique that uses a jump table (sparse table) to find ancestors efficiently. For each node, we precompute its 1st, 2nd, 4th, 8th, ... \(2^k\)-th ancestor.
To find the LCA of \(u\)and \(v\):
Build time is \(O(n \log n)\), each LCA query costs \(O(\log n)\).
| GT | Graph type (List_Graph, List_SGraph, Array_Graph). |
| SA | Arc filter type (default: Dft_Show_Arc<GT>). |
| using Aleph::Gen_Binary_Lifting_LCA< GT, SA >::Node = typename GT::Node |
|
private |
|
inline |
Construct from graph + explicit root.
The constructor validates that the filtered graph is a tree.
| [in] | g | Rooted tree topology (filtered by sa). |
| [in] | root | Root node pointer (must belong to g). |
| [in] | sa | Arc filter. |
| ah_domain_error | if the filtered graph is not a tree. |
Definition at line 613 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table().
|
inline |
Construct using the first graph node as root.
| [in] | g | Rooted tree topology (filtered by sa). |
| [in] | sa | Arc filter. |
Definition at line 623 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table().
|
default |
Copy the tree and the jump table of another engine.
| [in] | other | engine to copy. |
|
inlinenoexcept |
|
inlineprivate |
Definition at line 572 of file LCA.H.
References Aleph::Array< T >::create(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::is_empty(), k, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::levels_, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::n(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::NONE, Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::parent(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_, and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_at().
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::Gen_Binary_Lifting_LCA(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::Gen_Binary_Lifting_LCA().
|
inline |
Distance from root to node.
Definition at line 729 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::depth_of_id(), and Aleph::Gen_Binary_Lifting_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 722 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::depth(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_, and Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::validate_id().
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::depth_of(), and TYPED_TEST().
|
inline |
Number of edges on the path between two nodes in O(log n).
Definition at line 829 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::distance_id(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::id_of().
Referenced by TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().
|
inline |
Number of edges on the path between two ids in O(log n).
Definition at line 822 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::depth(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca_id(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::distance().
|
inlineprivate |
Definition at line 566 of file LCA.H.
References ah_domain_error_if, and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::is_empty().
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca_id().
|
inline |
Map a Node pointer to its internal ID [0, n-1].
Definition at line 716 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::id_of(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::depth_of(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::distance(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::is_ancestor(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca(), Aleph::Gen_Binary_Lifting_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 755 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::id_of(), and Aleph::Gen_Binary_Lifting_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 749 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::is_ancestor(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::is_ancestor(), and TYPED_TEST().
|
inlinenoexcept |
Returns true if the tree has no nodes.
Definition at line 685 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::is_empty(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::ensure_not_empty(), TYPED_TEST(), and TYPED_TEST().
|
inline |
k-th ancestor by node pointer in O(log n).
nullptr if it does not exist. Definition at line 778 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::id_of(), k, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::node_of(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::NONE.
Referenced by TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().
|
inline |
k-th ancestor by id in O(log n).
| [in] | id | Node id. |
| [in] | k | Number of steps up. |
NONE if k > depth(id). Definition at line 766 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::depth(), k, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lift(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::NONE, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_, and Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::validate_id().
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor(), and TYPED_TEST().
|
inline |
LCA query by node pointers in O(log n).
Definition at line 816 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::id_of(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca_id(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::node_of().
Referenced by TYPED_TEST(), 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(log n).
Definition at line 785 of file LCA.H.
References ah_runtime_error_unless, Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::depth(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::ensure_not_empty(), k, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::levels_, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lift(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::NONE, Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::parent(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_at(), and Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::validate_id().
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::distance_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca(), TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().
|
inlineprivate |
Definition at line 595 of file LCA.H.
References Aleph::and, k, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::NONE, and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_at().
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor_id(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca_id().
|
inlineprivatenoexcept |
Definition at line 551 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::size(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_at(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_at().
|
inline |
Map an internal ID [0, n-1] back to a Node pointer.
Definition at line 710 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::node_of(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::parent_of(), TYPED_TEST(), and TYPED_TEST().
|
inlinenoexcept |
Returns the number of levels in the jump table: \(\lfloor \log_2 n \rfloor + 1\), or 0 if the tree is empty.
Definition at line 704 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::levels_.
Referenced by TYPED_TEST(), and TYPED_TEST().
|
default |
Copy the tree and the jump table of another engine.
| [in] | other | engine to copy. |
|
inlinenoexcept |
Exchange this engine with another, which is left with the previous tree of this one.
| [in,out] | other | engine to move from. |
Definition at line 658 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::swap().
|
inline |
Parent ID of node id, or NONE if root.
Definition at line 735 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::parent(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_, and Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::validate_id().
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::parent_of().
|
inline |
Parent of node, or nullptr if root.
Definition at line 742 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::id_of(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::node_of(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::NONE, and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::parent_id().
Referenced by TYPED_TEST().
|
inlinenoexcept |
Returns the root node of the tree.
Definition at line 691 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::root(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_.
Referenced by TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().
|
inlinenoexcept |
Returns the internal ID of the root node.
Definition at line 697 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::root_id(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_.
|
inlinenoexcept |
Total number of nodes in the tree.
Definition at line 679 of file LCA.H.
References Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::size(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_.
Referenced by TYPED_TEST(), 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 671 of file LCA.H.
References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::levels_, Aleph::Array< T >::swap(), Aleph::lca_detail::Rooted_Tree_Data< GT, SA >::swap(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::topology_, and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::operator=().
|
inlineprivatenoexcept |
Definition at line 561 of file LCA.H.
References k, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::n(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_.
|
inlineprivatenoexcept |
Definition at line 556 of file LCA.H.
References k, Aleph::Gen_Binary_Lifting_LCA< GT, SA >::n(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca_id(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lift().
|
private |
Definition at line 548 of file LCA.H.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::num_levels(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::swap().
|
staticconstexprprivate |
Definition at line 545 of file LCA.H.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lift(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::parent_of().
|
private |
Definition at line 547 of file LCA.H.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::depth_of_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::distance_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::id_of(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::is_ancestor_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::is_empty(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::n(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::node_of(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::parent_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::root(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::root_id(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::size(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::swap().
|
private |
Definition at line 549 of file LCA.H.
Referenced by Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::swap(), Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_at(), and Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_at().