Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Gen_Binary_Lifting_LCA< GT, SA > Class Template Reference

LCA via binary lifting on a rooted tree. More...

#include <LCA.H>

Collaboration diagram for Aleph::Gen_Binary_Lifting_LCA< GT, SA >:
[legend]

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
 

Detailed Description

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
class Aleph::Gen_Binary_Lifting_LCA< GT, SA >

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\):

  1. Lift the deeper node until it is at the same depth as the other.
  2. If they are now the same node, that is the LCA.
  3. Otherwise, jump upwards by the largest possible powers of 2 such that the resulting nodes are still different. The parent of the final nodes is the LCA.

Build time is \(O(n \log n)\), each LCA query costs \(O(\log n)\).

Template Parameters
GTGraph type (List_Graph, List_SGraph, Array_Graph).
SAArc filter type (default: Dft_Show_Arc<GT>).

Definition at line 538 of file LCA.H.

Member Typedef Documentation

◆ Node

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_Binary_Lifting_LCA< GT, SA >::Node = typename GT::Node

Definition at line 541 of file LCA.H.

◆ Topology

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
using Aleph::Gen_Binary_Lifting_LCA< GT, SA >::Topology = lca_detail::Rooted_Tree_Data<GT, SA>
private

Definition at line 544 of file LCA.H.

Constructor & Destructor Documentation

◆ Gen_Binary_Lifting_LCA() [1/4]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Gen_Binary_Lifting_LCA< GT, SA >::Gen_Binary_Lifting_LCA ( const GT &  g,
Node *  root,
SA  sa = SA() 
)
inline

Construct from graph + explicit root.

The constructor validates that the filtered graph is a tree.

Parameters
[in]gRooted tree topology (filtered by sa).
[in]rootRoot node pointer (must belong to g).
[in]saArc filter.
Exceptions
ah_domain_errorif 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().

◆ Gen_Binary_Lifting_LCA() [2/4]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Gen_Binary_Lifting_LCA< GT, SA >::Gen_Binary_Lifting_LCA ( const GT &  g,
SA  sa = SA() 
)
inline

Construct using the first graph node as root.

Parameters
[in]gRooted tree topology (filtered by sa).
[in]saArc filter.

Definition at line 623 of file LCA.H.

References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::build_jump_table().

◆ Gen_Binary_Lifting_LCA() [3/4]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Gen_Binary_Lifting_LCA< GT, SA >::Gen_Binary_Lifting_LCA ( const Gen_Binary_Lifting_LCA< GT, SA > &  other)
default

Copy the tree and the jump table of another engine.

Parameters
[in]otherengine to copy.

◆ Gen_Binary_Lifting_LCA() [4/4]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Aleph::Gen_Binary_Lifting_LCA< GT, SA >::Gen_Binary_Lifting_LCA ( Gen_Binary_Lifting_LCA< GT, SA > &&  other)
inlinenoexcept

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

The implicit move took the jump table but copied its number of levels, which the moved-from engine then reported for an empty tree.

Parameters
[in,out]otherengine to move from.

Definition at line 641 of file LCA.H.

Member Function Documentation

◆ build_jump_table()

◆ depth_of()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::depth_of ( const Node *  node) const
inline

◆ depth_of_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::depth_of_id ( const size_t  id) const
inline

◆ distance()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::distance ( const Node *  u,
const Node *  v 
) const
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().

◆ distance_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::distance_id ( const size_t  u,
const size_t  v 
) const
inline

◆ ensure_not_empty()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
void Aleph::Gen_Binary_Lifting_LCA< GT, SA >::ensure_not_empty ( const char *  where) const
inlineprivate

◆ id_of()

◆ is_ancestor()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Gen_Binary_Lifting_LCA< GT, SA >::is_ancestor ( const Node *  u,
const Node *  v 
) const
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().

◆ is_ancestor_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Gen_Binary_Lifting_LCA< GT, SA >::is_ancestor_id ( const size_t  u,
const size_t  v 
) const
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().

◆ is_empty()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
bool Aleph::Gen_Binary_Lifting_LCA< GT, SA >::is_empty ( ) const
inlinenoexcept

◆ kth_ancestor()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor ( const Node *  node,
const size_t  k 
) const
inline

k-th ancestor by node pointer in O(log n).

Returns
Ancestor node, or 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().

◆ kth_ancestor_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::kth_ancestor_id ( const size_t  id,
const size_t  k 
) const
inline

◆ lca()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lca ( const Node *  u,
const Node *  v 
) const
inline

◆ lca_id()

◆ lift()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::lift ( size_t  v,
size_t  delta 
) const
inlineprivate

◆ n()

◆ node_of()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_Binary_Lifting_LCA< GT, SA >::node_of ( const size_t  id) const
inline

◆ num_levels()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::num_levels ( ) const
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().

◆ operator=() [1/2]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Gen_Binary_Lifting_LCA & Aleph::Gen_Binary_Lifting_LCA< GT, SA >::operator= ( const Gen_Binary_Lifting_LCA< GT, SA > &  other)
default

Copy the tree and the jump table of another engine.

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

◆ operator=() [2/2]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Gen_Binary_Lifting_LCA & Aleph::Gen_Binary_Lifting_LCA< GT, SA >::operator= ( Gen_Binary_Lifting_LCA< GT, SA > &&  other)
inlinenoexcept

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

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

Definition at line 658 of file LCA.H.

References Aleph::Gen_Binary_Lifting_LCA< GT, SA >::swap().

◆ parent_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::parent_id ( const size_t  id) const
inline

◆ parent_of()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_Binary_Lifting_LCA< GT, SA >::parent_of ( const Node *  node) const
inline

◆ root()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
Node * Aleph::Gen_Binary_Lifting_LCA< GT, SA >::root ( ) const
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().

◆ root_id()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::root_id ( ) const
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_.

◆ size()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::size ( ) const
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().

◆ swap()

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
void Aleph::Gen_Binary_Lifting_LCA< GT, SA >::swap ( Gen_Binary_Lifting_LCA< GT, SA > &  other)
inlinenoexcept

Exchange in O(1) this engine and another.

Parameters
[in,out]otherengine to exchange with.
Exceptions
whatswapping 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=().

◆ up_at() [1/2]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_at ( const size_t  k,
const size_t  v 
) const
inlineprivatenoexcept

◆ up_at() [2/2]

template<AlephGraph GT, ArcFilter< GT > SA = Dft_Show_Arc<GT>>
size_t & Aleph::Gen_Binary_Lifting_LCA< GT, SA >::up_at ( const size_t  k,
const size_t  v 
)
inlineprivatenoexcept

Member Data Documentation

◆ levels_

◆ NONE

◆ topology_

◆ up_


The documentation for this class was generated from the following file: