|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Offline subtree and path queries on trees via Mo's algorithm. More...
#include <tpl_mo_on_trees.H>
Public Types | |
| using | Node = typename GT::Node |
| using | Arc = typename GT::Arc |
| using | T = typename Node::Node_Type |
| using | answer_type = typename Policy::answer_type |
Public Member Functions | |
| Gen_Mo_On_Trees (const GT &g, Node *root, Policy p=Policy()) | |
| Construct a Mo's algorithm query engine for tree queries. | |
| size_t | size () const noexcept |
| Number of nodes in the tree. | |
| bool | is_empty () const noexcept |
| True if the tree is empty. | |
| Array< answer_type > | subtree_solve (const Array< Node * > &query_roots) const |
| Answer subtree queries using Mo's algorithm. | |
| Array< answer_type > | subtree_solve (std::initializer_list< Node * > il) const |
| Answer subtree queries from an initializer list. | |
| Array< answer_type > | path_solve (const Array< std::pair< Node *, Node * > > &query_pairs) const |
| Answer path queries between node pairs. | |
| Array< answer_type > | path_solve (std::initializer_list< std::pair< Node *, Node * > > il) const |
| Solve path queries (initializer-list overload). | |
Private Member Functions | |
| void | build () |
| size_t | lca (size_t u, size_t v) const |
| Array< answer_type > | mo_sweep (const Array< T > &data, Array< Mo_Query > queries, size_t n) const |
Private Attributes | |
| const GT & | g_ |
| Node * | root_ |
| Policy | pol_ |
| size_t | n_ = 0 |
| Array< Node * > | id_to_node_ |
| MapOLhash< Node *, size_t > | node_to_id_ |
| Array< T > | node_values_ |
| Array< size_t > | tin_ |
| Array< size_t > | tout_ |
| Array< T > | flat_sub_ |
| Array< size_t > | first_ |
| Array< size_t > | last_ |
| Array< size_t > | flat_node_ |
| size_t | log_n_ = 0 |
| Array< size_t > | depth_ |
| Array< size_t > | parent_ |
| Array< size_t > | up_ |
Static Private Attributes | |
| static constexpr size_t | NONE = ~static_cast<size_t>(0) |
Offline subtree and path queries on trees via Mo's algorithm.
Given a tree (represented as any Aleph-w graph type), a root node, and a Policy satisfying MoPolicy, this class preprocesses the tree in O(N log N) and then answers batches of subtree or path queries in O((N+Q)√N) time.
| GT | Graph type (List_Graph, List_SGraph, or Array_Graph). |
| Policy | A type satisfying MoPolicy<Policy, GT::Node::Node_Type>. |
Definition at line 167 of file tpl_mo_on_trees.H.
| using Aleph::Gen_Mo_On_Trees< GT, Policy >::answer_type = typename Policy::answer_type |
Definition at line 173 of file tpl_mo_on_trees.H.
Definition at line 171 of file tpl_mo_on_trees.H.
Definition at line 170 of file tpl_mo_on_trees.H.
Definition at line 172 of file tpl_mo_on_trees.H.
|
inline |
Construct a Mo's algorithm query engine for tree queries.
Performs an O(N log N) preprocessing step (Euler tour + binary lifting for LCA). The graph is not modified in any way.
| g | The tree graph (must be connected with N nodes and N-1 edges). |
| root | Root node used to orient parent/child relations. |
| p | Policy object copied/moved into the query engine. |
| std::domain_error | If root is not found in g. |
| std::domain_error | If g is disconnected (not a valid tree under the chosen root). |
| std::bad_alloc | If auxiliary arrays/tables cannot be allocated. |
Definition at line 447 of file tpl_mo_on_trees.H.
References Aleph::Gen_Mo_On_Trees< GT, Policy >::build().
|
inlineprivate |
Definition at line 206 of file tpl_mo_on_trees.H.
References ah_domain_error_if, Aleph::Array< T >::create(), Aleph::Gen_Mo_On_Trees< GT, Policy >::depth_, Aleph::divide_and_conquer_partition_dp(), Aleph::MapOpenHash< Key, Data, Cmp, HashTable >::find(), Aleph::Gen_Mo_On_Trees< GT, Policy >::first_, Aleph::Gen_Mo_On_Trees< GT, Policy >::flat_node_, Aleph::Gen_Mo_On_Trees< GT, Policy >::flat_sub_, GraphCommon< GT, Node, Arc >::for_each_node(), Aleph::Gen_Mo_On_Trees< GT, Policy >::g_, GraphCommon< GT, Node, Arc >::get_connected_node(), Aleph::Gen_Mo_On_Trees< GT, Policy >::id_to_node_, Aleph::MapOpenHash< Key, Data, Cmp, HashTable >::insert(), k, Aleph::Gen_Mo_On_Trees< GT, Policy >::last_, Aleph::Gen_Mo_On_Trees< GT, Policy >::log_n_, Aleph::Gen_Mo_On_Trees< GT, Policy >::n_, Aleph::Gen_Mo_On_Trees< GT, Policy >::node_to_id_, Aleph::Gen_Mo_On_Trees< GT, Policy >::node_values_, Aleph::Gen_Mo_On_Trees< GT, Policy >::NONE, Aleph::Gen_Mo_On_Trees< GT, Policy >::parent_, Aleph::Gen_Mo_On_Trees< GT, Policy >::root_, Aleph::MapOpenHash< Key, Data, Cmp, HashTable >::search(), Aleph::Gen_Mo_On_Trees< GT, Policy >::tin_, Aleph::Gen_Mo_On_Trees< GT, Policy >::tout_, Aleph::Gen_Mo_On_Trees< GT, Policy >::up_, and GraphCommon< GT, Node, Arc >::vsize().
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::Gen_Mo_On_Trees().
|
inlinenoexcept |
True if the tree is empty.
Definition at line 457 of file tpl_mo_on_trees.H.
References Aleph::Gen_Mo_On_Trees< GT, Policy >::n_.
|
inlineprivate |
Definition at line 351 of file tpl_mo_on_trees.H.
References Aleph::Gen_Mo_On_Trees< GT, Policy >::depth_, Aleph::diff(), k, Aleph::Gen_Mo_On_Trees< GT, Policy >::log_n_, Aleph::Gen_Mo_On_Trees< GT, Policy >::n_, and Aleph::Gen_Mo_On_Trees< GT, Policy >::up_.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().
|
inlineprivate |
Definition at line 375 of file tpl_mo_on_trees.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), Aleph::Mo_Query::l, Aleph::Gen_Mo_On_Trees< GT, Policy >::pol_, r, and Aleph::Mo_Query::r.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve().
|
inline |
Answer path queries between node pairs.
For each pair (u, v), answers the policy query over all nodes on the unique path from u to v (both endpoints included).
Uses the double-occurrence Euler tour with a toggle trick: each node appears twice (entry + exit). A node is "active" when it has been toggled an odd number of times within the current window. The LCA of each query is handled separately.
| query_pairs | Array of (u, v) node-pointer pairs. |
| std::domain_error | If any query endpoint is not in the tree. |
| Any | exception thrown by Policy::init, Policy::add, Policy::remove, or Policy::answer. |
Definition at line 539 of file tpl_mo_on_trees.H.
References ah_domain_error_if, Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Mo_On_Trees< GT, Policy >::first_, Aleph::Gen_Mo_On_Trees< GT, Policy >::flat_node_, l, Aleph::Gen_Mo_On_Trees< GT, Policy >::last_, Aleph::Gen_Mo_On_Trees< GT, Policy >::lca(), Aleph::Gen_Mo_On_Trees< GT, Policy >::n_, Aleph::Gen_Mo_On_Trees< GT, Policy >::node_to_id_, Aleph::Gen_Mo_On_Trees< GT, Policy >::node_values_, Aleph::Gen_Mo_On_Trees< GT, Policy >::NONE, Aleph::Gen_Mo_On_Trees< GT, Policy >::pol_, r, and Aleph::MapOpenHash< Key, Data, Cmp, HashTable >::search().
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().
|
inline |
Solve path queries (initializer-list overload).
| il | Initializer list of (u, v) node-pointer pairs. |
| std::domain_error | If any query endpoint is not in the tree. |
| Any | exception propagated from path_solve(Array<pair<...>>) and policy callbacks. |
Definition at line 662 of file tpl_mo_on_trees.H.
References Aleph::divide_and_conquer_partition_dp(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().
|
inlinenoexcept |
Number of nodes in the tree.
Definition at line 454 of file tpl_mo_on_trees.H.
References Aleph::Gen_Mo_On_Trees< GT, Policy >::n_.
|
inline |
Answer subtree queries using Mo's algorithm.
For each node in query_roots, answers the policy query over the entire subtree rooted at that node.
Uses the single-occurrence Euler tour: the subtree of u is the contiguous range [tin(u), tout(u)] in DFS entry-time order.
| query_roots | Array of subtree root nodes to query. |
| std::domain_error | If any query root is not in the tree. |
| Any | exception thrown by Policy::init, Policy::add, Policy::remove, or Policy::answer. |
Definition at line 479 of file tpl_mo_on_trees.H.
References ah_domain_error_if, Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), Aleph::Gen_Mo_On_Trees< GT, Policy >::flat_sub_, Aleph::Gen_Mo_On_Trees< GT, Policy >::mo_sweep(), Aleph::Gen_Mo_On_Trees< GT, Policy >::n_, Aleph::Gen_Mo_On_Trees< GT, Policy >::node_to_id_, Aleph::MapOpenHash< Key, Data, Cmp, HashTable >::search(), Aleph::Gen_Mo_On_Trees< GT, Policy >::tin_, and Aleph::Gen_Mo_On_Trees< GT, Policy >::tout_.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve().
|
inline |
Answer subtree queries from an initializer list.
| il | Initializer list of subtree root nodes. |
| std::domain_error | If any query root is not in the tree. |
| Any | exception propagated from subtree_solve(Array<Node*>). |
Definition at line 510 of file tpl_mo_on_trees.H.
References Aleph::divide_and_conquer_partition_dp(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve().
|
private |
Definition at line 200 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::lca().
|
private |
Definition at line 194 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().
|
private |
Definition at line 196 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().
Definition at line 191 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve().
Definition at line 178 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build().
|
private |
Definition at line 184 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build().
|
private |
Definition at line 195 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().
Definition at line 199 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::lca().
Definition at line 181 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), Aleph::Gen_Mo_On_Trees< GT, Policy >::is_empty(), Aleph::Gen_Mo_On_Trees< GT, Policy >::lca(), Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve(), Aleph::Gen_Mo_On_Trees< GT, Policy >::size(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve().
|
private |
Definition at line 185 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve().
|
private |
Definition at line 186 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().
|
staticconstexprprivate |
Definition at line 176 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().
|
private |
Definition at line 201 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build().
Definition at line 180 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::mo_sweep(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().
Definition at line 179 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build().
Definition at line 189 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve().
|
private |
Definition at line 190 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve().
Definition at line 202 of file tpl_mo_on_trees.H.
Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build(), and Aleph::Gen_Mo_On_Trees< GT, Policy >::lca().