102#include <type_traits>
114namespace lca_detail {
126template <AlephGraph GT, ArcFilter<GT> SA>
135 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
163 return std::make_pair(u, v);
173 <<
"Rooted_Tree_Data: root node provided but graph is empty";
186 Node *p = it.get_curr();
194 if (
root_ ==
nullptr)
198 <<
"Rooted_Tree_Data: root node does not belong to graph";
210 for (
size_t i = 0; i <
n_; ++i)
214 size_t edge_count = 0;
218 Arc *a = it.get_curr_ne();
226 <<
"Rooted_Tree_Data: arc endpoint is not indexed";
228 const size_t u = src_item->second;
229 const size_t v = tgt_item->second;
231 ah_domain_error_if(u == v) <<
"Rooted_Tree_Data: self-loop detected in filtered graph";
235 <<
"Rooted_Tree_Data: parallel/duplicate edge detected in filtered graph";
237 unique_edges.
insert(key, 1);
242 <<
"Rooted_Tree_Data: filtered graph is not a tree (expected " << (
n_ - 1) <<
" edges, got "
243 << edge_count <<
")";
247 const auto &[fst, snd] = it.get_curr();
248 const size_t u = fst.first;
249 const size_t v = fst.second;
267 for (
size_t i = 0; i <
n_; ++i)
271 for (
size_t i = 0; i <
n_; ++i)
284 size_t visited_count = 1;
299 auto &fr = stack.
top();
301 if (fr.next_child ==
adjacency_(fr.node).size())
303 tout_(fr.node) = timer - 1;
310 const size_t nxt =
adjacency_(fr.node)(fr.next_child++);
311 if (nxt == fr.parent)
314 ah_domain_error_if(visited(nxt)) <<
"Rooted_Tree_Data: filtered graph is not acyclic";
326 stack.
push({nxt, fr.node, 0});
332 <<
"Rooted_Tree_Data: unexpected Euler tour size";
335 void check_id(
const size_t id,
const char *where)
const
338 << where <<
": id=" <<
id <<
" is out of range [0, " <<
n_ <<
")";
403 std::swap(
graph_, other.graph_);
404 std::swap(
root_, other.root_);
405 std::swap(
root_id_, other.root_id_);
406 std::swap(
n_, other.n_);
419 [[nodiscard]]
size_t size() const noexcept
474 ah_domain_error_if(item ==
nullptr) <<
"Rooted_Tree_Data::id_of: node does not belong to graph";
481 check_id(
id,
"Rooted_Tree_Data::node_of");
490 [[nodiscard]]
bool is_ancestor(
const size_t u,
const size_t v)
const
492 check_id(u,
"Rooted_Tree_Data::is_ancestor");
493 check_id(v,
"Rooted_Tree_Data::is_ancestor");
508 if (a.depth < b.depth)
510 if (b.depth < a.depth)
512 return (a.node <= b.node) ? a : b;
537template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
551 [[nodiscard]]
size_t n() const noexcept
556 size_t &
up_at(
const size_t k,
const size_t v)
noexcept
558 return up_(
k *
n() + v);
561 [[nodiscard]]
size_t up_at(
const size_t k,
const size_t v)
const noexcept
563 return up_(
k *
n() + v);
577 levels_ =
static_cast<size_t>(std::bit_width(
n()));
580 for (
size_t i = 0; i <
levels_ *
n(); ++i)
583 for (
size_t v = 0; v <
n(); ++v)
587 for (
size_t v = 0; v <
n(); ++v)
589 const size_t mid =
up_at(
k - 1, v);
595 [[nodiscard]]
size_t lift(
size_t v,
size_t delta)
const
597 for (
size_t k = 0; k < levels_ and delta > 0
and v !=
NONE; ++
k)
598 if ((delta >>
k) & 1U)
642 noexcept(std::is_nothrow_move_constructible_v<Topology>)
659 noexcept(
noexcept(
swap(other)))
674 std::swap(
levels_, other.levels_);
679 [[nodiscard]]
size_t size() const noexcept
785 [[nodiscard]]
size_t lca_id(
size_t u,
size_t v)
const
800 const size_t uu =
up_at(
k, u);
801 const size_t vv =
up_at(
k, v);
811 <<
"Gen_Binary_Lifting_LCA::lca_id: internal invalid parent";
822 [[nodiscard]]
size_t distance_id(
const size_t u,
const size_t v)
const
824 const size_t a =
lca_id(u, v);
855template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
945 noexcept(
noexcept(
swap(other)))
961 rmq_.swap(other.rmq_);
965 [[nodiscard]]
size_t size() const noexcept
1040 [[nodiscard]]
size_t lca_id(
const size_t u,
const size_t v)
const
1051 return rmq_.query(
l,
r).node;
1061 [[nodiscard]]
size_t distance_id(
const size_t u,
const size_t v)
const
1063 const size_t a =
lca_id(u, v);
1087template <
class GT,
class SA = Dft_Show_Arc<GT>>
1091template <
class GT,
class SA = Dft_Show_Arc<GT>>
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
Simple dynamic array with automatic resizing and functional operations.
void swap(Array &s) noexcept
Swap this with s
static Array create(const size_t n)
Create an array with n logical elements.
Dynamic stack of elements of generic type T based on a singly linked list.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
Generic key-value map implemented on top of a binary search tree.
typename Base::Iterator Iterator
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
LCA via binary lifting on a rooted tree.
size_t depth_of(const Node *node) const
Distance from root to node.
size_t depth_of_id(const size_t id) const
Distance from root to node by ID.
Gen_Binary_Lifting_LCA(const GT &g, Node *root, SA sa=SA())
Construct from graph + explicit root.
Gen_Binary_Lifting_LCA(const Gen_Binary_Lifting_LCA &other)=default
Copy the tree and the jump table of another engine.
bool is_empty() const noexcept
Returns true if the tree has no nodes.
Node * node_of(const size_t id) const
Map an internal ID [0, n-1] back to a Node pointer.
size_t parent_id(const size_t id) const
Parent ID of node id, or NONE if root.
size_t n() const noexcept
size_t num_levels() const noexcept
Returns the number of levels in the jump table: , or 0 if the tree is empty.
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.
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).
bool is_ancestor(const Node *u, const Node *v) const
Returns true if node u is an ancestor of v (or u == v).
Node * kth_ancestor(const Node *node, const size_t k) const
k-th ancestor by node pointer in O(log n).
void swap(Gen_Binary_Lifting_LCA &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this engine and another.
Gen_Binary_Lifting_LCA(const GT &g, SA sa=SA())
Construct using the first graph node as root.
size_t kth_ancestor_id(const size_t id, const size_t k) const
k-th ancestor by id in O(log n).
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
size_t id_of(const Node *node) const
Map a Node pointer to its internal ID [0, n-1].
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.
Node * root() const noexcept
Returns the root node of the tree.
size_t lift(size_t v, size_t delta) const
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).
size_t distance(const Node *u, const Node *v) const
Number of edges on the path between two nodes in O(log n).
size_t size() const noexcept
Total number of nodes in the tree.
static constexpr size_t NONE
size_t root_id() const noexcept
Returns the internal ID of the root node.
size_t lca_id(size_t u, size_t v) const
LCA query by node ids in O(log n).
Node * parent_of(const Node *node) const
Parent of node, or nullptr if root.
Node * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(log n).
void ensure_not_empty(const char *where) const
LCA via Euler tour + RMQ on depth in a rooted tree.
Node * parent_of(const Node *node) const
Parent of node, or nullptr if root.
static constexpr size_t NONE
Gen_Euler_RMQ_LCA(Gen_Euler_RMQ_LCA &&other)=default
Take the tree and the tables of another engine, which is left empty.
Node * node_of(const size_t id) const
Map an internal ID [0, n-1] back to a Node pointer.
bool is_empty() const noexcept
Returns true if the tree has no nodes.
lca_detail::Depth_Node_Min_Op Depth_Node_Min_Op
Gen_Euler_RMQ_LCA(const GT &g, SA sa=SA())
Construct using the first graph node as root.
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 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.
void swap(Gen_Euler_RMQ_LCA &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this engine and another.
Gen_Euler_RMQ_LCA(const Gen_Euler_RMQ_LCA &other)=default
Copy the tree and the tables of another engine.
size_t lca_id(const size_t u, const size_t v) const
LCA query by node ids in O(1).
void ensure_not_empty(const char *where) const
size_t size() const noexcept
Total number of nodes in the tree.
Node * root() const noexcept
Returns the root node of the tree.
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).
Gen_Euler_RMQ_LCA(const GT &g, Node *root, SA sa=SA())
Construct from graph + explicit root.
size_t root_id() const noexcept
Returns the internal ID of the root node.
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 * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(1).
Gen_Euler_RMQ_LCA & operator=(const Gen_Euler_RMQ_LCA &other)=default
Copy the tree and the tables of another engine.
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).
size_t euler_tour_size() const noexcept
Euler tour size ( for empty tree, else ).
const Array< size_t > & euler_tour() const noexcept
Euler tour sequence of node ids ( entries).
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.
size_t distance(const Node *u, const Node *v) const
Number of edges on the path between two nodes in O(1).
Gen_Sparse_Table< Depth_Node, Depth_Node_Min_Op > rmq_
Array< Depth_Node > euler_depth_
Sparse Table over an arbitrary associative and idempotent binary operation.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Filtered iterator on the nodes of a graph.
void build_simple_adjacency()
Rooted_Tree_Data(const GT &g, Node *root, SA sa=SA())
size_t root_id() const noexcept
const Array< size_t > & depth() const noexcept
Array< Array< size_t > > adjacency_
MapOLhash< Node *, size_t > node_to_id_
size_t size() const noexcept
const Array< Node * > & id_to_node() const noexcept
size_t euler_size() const noexcept
Array< Node * > id_to_node_
Rooted_Tree_Data & operator=(Rooted_Tree_Data &&other) noexcept(noexcept(swap(other)))
Exchange this topology with another, which is left with the previous topology of this one.
const Array< size_t > & euler() const noexcept
static constexpr size_t NONE
static Pair_Key normalize_pair(size_t u, size_t v) noexcept
void check_id(const size_t id, const char *where) const
void swap(Rooted_Tree_Data &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this topology and another.
Node * root() const noexcept
Rooted_Tree_Data & operator=(const Rooted_Tree_Data &other)=default
Copy the topology of another tree.
bool is_ancestor(const size_t u, const size_t v) const
Rooted_Tree_Data(const Rooted_Tree_Data &other)=default
Copy the topology of another tree.
const Array< size_t > & first() const noexcept
const Array< size_t > & tout() const noexcept
Rooted_Tree_Data(Rooted_Tree_Data &&other) noexcept(std::is_nothrow_move_constructible_v< SA >)
Take the topology of another tree, which is left empty.
const Array< size_t > & tin() const noexcept
Node * node_of(const size_t id) const
const Array< size_t > & parent() const noexcept
void validate_id(const size_t id, const char *where) const
size_t id_of(const Node *node) const
std::pair< size_t, size_t > Pair_Key
bool is_empty() const noexcept
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Filtered iterator on all the arcs of a graph.
Open addressing hash map using linear probing.
Data & find(const Key &key)
Find and return the value for a key.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Search for a key in the map.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair (copy semantics).
Depth_Node operator()(const Depth_Node &a, const Depth_Node &b) const noexcept
Dynamic array container with automatic resizing.
Dynamic stack implementation based on linked lists.
Dynamic map with open hashing.
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.
Sparse Table for static range queries in O(1).