98# include <type_traits>
121 template <AlephGraph GT, ArcFilter<GT> SA>
129 static constexpr size_t NONE = std::numeric_limits<size_t>::max();
161 return std::make_pair(u, v);
171 <<
"HLD_Tree_Data: root node provided but graph is empty";
184 Node * p = it.get_curr();
191 <<
"HLD_Tree_Data: failed to index all graph nodes";
193 if (
root_ ==
nullptr)
197 <<
"HLD_Tree_Data: root node does not belong to graph";
209 for (
size_t i = 0; i <
n_; ++i)
213 size_t edge_count = 0;
217 Arc * a = it.get_curr_ne();
225 <<
"HLD_Tree_Data: arc endpoint is not indexed";
227 const size_t u = src_item->second;
228 const size_t v = tgt_item->second;
231 <<
"HLD_Tree_Data: self-loop detected in filtered graph";
235 <<
"HLD_Tree_Data: parallel/duplicate edge detected in filtered graph";
237 unique_edges.
insert(key, 1);
242 <<
"HLD_Tree_Data: filtered graph is not a tree (expected "
243 << (
n_ - 1) <<
" edges, got " << edge_count <<
")";
246 it.has_curr(); it.next_ne())
248 const auto & [fst, snd] = it.get_curr();
249 const size_t u = fst.first;
250 const size_t v = fst.second;
265 for (
size_t i = 0; i <
n_; ++i)
272 for (
size_t i = 0; i <
n_; ++i)
284 size_t visited_count = 1;
293 auto & fr = stack.
top();
295 if (fr.next_child ==
adjacency_(fr.node).size())
299 const size_t done = fr.node;
306 const size_t nxt =
adjacency_(fr.node)(fr.next_child++);
307 if (nxt == fr.parent)
311 <<
"HLD_Tree_Data: filtered graph is not acyclic";
319 stack.
push({nxt, fr.node, 0});
323 <<
"HLD_Tree_Data: filtered graph is not connected";
331 for (
size_t u = 0; u <
n_; ++u)
338 size_t best_idx =
NONE;
339 size_t best_size = 0;
341 for (
size_t i = 0; i < adj.size(); ++i)
343 const size_t v = adj(i);
353 if (best_idx ==
NONE)
358 size_t first_child_pos = 0;
362 if (best_idx != first_child_pos)
363 std::swap(adj(best_idx), adj(first_child_pos));
401 auto & fr = stack.
top();
403 if (fr.next_child ==
adjacency_(fr.node).size())
405 tout_(fr.node) = timer - 1;
410 const size_t nxt =
adjacency_(fr.node)(fr.next_child);
413 if (nxt == fr.parent)
419 bool is_heavy =
false;
422 size_t first_child_pos = 0;
423 if (adj.size() > 0
and adj(0) == fr.parent)
425 if (first_child_pos < adj.size()
and adj(first_child_pos) == nxt)
447 void check_id(
const size_t id,
const char * where)
const
450 << where <<
": id=" <<
id <<
" is out of range [0, " <<
n_ <<
")";
540 std::swap(
graph_, other.graph_);
541 std::swap(
root_, other.root_);
542 std::swap(
root_id_, other.root_id_);
543 std::swap(
n_, other.n_);
557 [[nodiscard]]
size_t size() const noexcept {
return n_; }
558 [[nodiscard]]
bool is_empty() const noexcept {
return n_ == 0; }
575 <<
"HLD_Tree_Data::id_of: null node";
579 <<
"HLD_Tree_Data::id_of: node does not belong to graph";
586 check_id(
id,
"HLD_Tree_Data::node_of");
621 template <AlephGraph GT,
typename T,
class Op, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
622 requires SegmentTreeOp<Op, T>
645 template <
class NodeValueFn>
652 for (
size_t i = 0; i <
n(); ++i)
655 for (
size_t id = 0;
id <
n(); ++id)
676 template <
class NodeValueFn>
678 const T & identity, Op oper = Op(), SA sa = SA())
682 seg_(1, identity, identity, oper)
688 template <
class NodeValueFn>
690 const T & identity, Op oper = Op(), SA sa = SA())
694 seg_(1, identity, identity, oper)
748 noexcept(std::is_nothrow_swappable_v<T>
and std::is_nothrow_swappable_v<Op>
749 and std::is_nothrow_swappable_v<SA>)
754 if constexpr (std::is_nothrow_swappable_v<T>
and std::is_nothrow_swappable_v<Op>
755 and std::is_nothrow_swappable_v<SA>)
762 seg_.swap(other.seg_);
830 [[nodiscard]]
size_t position(
const size_t id)
const
1033 [[nodiscard]]
size_t lca_id(
size_t u,
size_t v)
const
1062 [[nodiscard]]
size_t distance_id(
const size_t u,
const size_t v)
const
1064 const size_t a =
lca_id(u, v);
1143 template <AlephGraph GT,
typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1149 template <
class NodeValueFn>
1155 template <
class NodeValueFn>
1157 :
Base(g,
std::forward<NodeValueFn>(nv),
1170 template <AlephGraph GT,
typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1176 template <
class NodeValueFn>
1182 template <
class NodeValueFn>
1184 :
Base(g,
std::forward<NodeValueFn>(nv),
1197 template <AlephGraph GT,
typename T, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1203 template <
class NodeValueFn>
1209 template <
class NodeValueFn>
1211 :
Base(g,
std::forward<NodeValueFn>(nv),
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.
Heavy-Light Decomposition with segment tree for path/subtree queries.
static constexpr size_t NONE
void point_update_id(const size_t v, const T &new_value)
Point update by node ID: set value to new_value.
void swap_but_topology_storage(Gen_HLD &other)
void swap(Gen_HLD &other) noexcept(std::is_nothrow_swappable_v< T > and std::is_nothrow_swappable_v< Op > and std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this HLD and another.
size_t distance(const Node *u, const Node *v) const
Number of edges between two nodes in O(log n).
T path_query(const Node *u, const Node *v) const
Path query by node pointers in O(log^2 n).
T path_query_id(size_t u, size_t v) const
Path query by node IDs in O(log^2 n).
size_t chain_head_id(const size_t id) const
Head of the chain containing node id.
Node * node_of(const size_t id) const
Map an internal ID back to a Node pointer.
size_t depth_of_id(const size_t id) const
Depth of node id from root.
size_t subtree_size_of_id(const size_t id) const
Subtree size of node id.
size_t num_chains() const noexcept
Number of heavy chains in the decomposition.
T get_value(const Node *v) const
Current node value at node pointer v.
size_t id_of(const Node *node) const
Map a Node pointer to its internal ID.
size_t size() const noexcept
Total number of nodes in the tree.
const Array< size_t > & position_array() const noexcept
Read-only access to the flat-position array.
Node * lca(const Node *u, const Node *v) const
LCA query by node pointers in O(log n).
size_t parent_id(const size_t id) const
Parent ID of node id, or NONE if root.
Gen_HLD(Gen_HLD &&other)=default
Take the decomposition and the values of another HLD, which is left empty.
Gen_HLD(const GT &g, Node *root, NodeValueFn &&node_value, const T &identity, Op oper=Op(), SA sa=SA())
Construct HLD from graph, root, and node value functor.
Gen_HLD & operator=(const Gen_HLD &other)=default
Copy the decomposition and the values of another HLD.
void point_update(const Node *v, const T &new_value)
Point update by node pointer: set value to new_value.
size_t position(const size_t id) const
HLD flat-array position of node id.
size_t lca_id(size_t u, size_t v) const
LCA query by node IDs in O(log n).
void ensure_not_empty(const char *where) const
size_t root_id() const noexcept
Returns the internal ID of the root node.
const Array< size_t > & subtree_size_array() const noexcept
Read-only access to the subtree-size array.
void build_segment_tree(NodeValueFn &&node_value)
T subtree_query(const Node *v) const
Subtree query by node pointer in O(log n).
size_t subtree_size_of(const Node *node) const
Subtree size of node.
T path_query_edges_id(size_t u, size_t v) const
Edge-weighted path query by node IDs in O(log^2 n).
Gen_HLD(const Gen_HLD &other)=default
Copy the decomposition and the values of another HLD.
bool is_empty() const noexcept
Returns true if the tree has no nodes.
size_t depth_of(const Node *node) const
Depth of node from root.
Gen_Segment_Tree< T, Op > seg_
const Array< size_t > & depth_array() const noexcept
Read-only access to the depth array.
size_t n() const noexcept
T subtree_query_id(const size_t v) const
Subtree query by node ID in O(log n).
const Array< size_t > & parent_array() const noexcept
Read-only access to the parent array.
size_t distance_id(const size_t u, const size_t v) const
Number of edges between two nodes by IDs in O(log n).
Gen_HLD(const GT &g, NodeValueFn &&node_value, const T &identity, Op oper=Op(), SA sa=SA())
Construct with first graph node as root.
Node * root() const noexcept
Returns the root node.
Node * parent_of(const Node *node) const
Parent of node, or nullptr if root.
T get_value_at_id(const size_t id) const
Current node value at HLD position pos in the segment tree.
T path_query_edges(const Node *u, const Node *v) const
Edge-weighted path query by node pointers.
const Array< size_t > & chain_head_array() const noexcept
Read-only access to the chain-head array.
Segment tree over an arbitrary associative 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.
const Array< size_t > & tout() const noexcept
Array< Node * > id_to_node_
const Array< size_t > & chain_head() const noexcept
HLD_Tree_Data & operator=(const HLD_Tree_Data &other)=default
Copy the decomposition of another tree.
void validate_id(const size_t id, const char *where) const
const Array< size_t > & parent() const noexcept
void swap_filter(HLD_Tree_Data &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange the filters of this topology and another, for an owner that exchanges something else before ...
void compute_hld_positions()
void swap_but_filter(HLD_Tree_Data &other) noexcept
Exchange in O(1) all but the filters of this topology and another.
const Array< size_t > & tin() const noexcept
HLD_Tree_Data(HLD_Tree_Data &&other) noexcept(std::is_nothrow_move_constructible_v< SA >)
Take the decomposition of another tree, which is left empty.
HLD_Tree_Data(const GT &g, Node *root, SA sa=SA())
const Array< size_t > & subtree_size() const noexcept
static Pair_Key normalize_pair(size_t u, size_t v) noexcept
void compute_sizes_and_parents()
Array< Array< size_t > > adjacency_
size_t root_id() const noexcept
const Array< Node * > & id_to_node() const noexcept
Node * node_of(const size_t id) const
Array< size_t > chain_head_
Node * root() const noexcept
size_t id_of(const Node *node) const
static constexpr size_t NONE
const Array< size_t > & depth() const noexcept
void build_simple_adjacency()
MapOLhash< Node *, size_t > node_to_id_
size_t size() const noexcept
size_t num_chains() const noexcept
HLD_Tree_Data(const HLD_Tree_Data &other)=default
Copy the decomposition of another tree.
void reorder_adjacency_heavy_first()
bool is_empty() const noexcept
void swap(HLD_Tree_Data &other) noexcept(std::is_nothrow_swappable_v< SA >)
Exchange in O(1) this topology and another.
void check_id(const size_t id, const char *where) const
Array< size_t > subtree_size_
const Array< size_t > & pos() const noexcept
std::pair< size_t, size_t > Pair_Key
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)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
Filtered iterator on all the arcs of a graph.
HLD with path/subtree max queries.
HLD_Max(const GT &g, Node *root, NodeValueFn &&nv, SA sa=SA())
HLD_Max(const GT &g, NodeValueFn &&nv, SA sa=SA())
HLD with path/subtree min queries.
HLD_Min(const GT &g, Node *root, NodeValueFn &&nv, SA sa=SA())
HLD_Min(const GT &g, NodeValueFn &&nv, SA sa=SA())
HLD with path/subtree sum queries.
HLD_Sum(const GT &g, Node *root, NodeValueFn &&nv, SA sa=SA())
HLD_Sum(const GT &g, NodeValueFn &&nv, SA sa=SA())
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).
Functor returning the maximum of two values.
Functor returning the minimum of two values.
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.
Segment trees for dynamic range queries with lazy propagation.