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

Offline subtree and path queries on trees via Mo's algorithm. More...

#include <tpl_mo_on_trees.H>

Collaboration diagram for Aleph::Gen_Mo_On_Trees< GT, Policy >:
[legend]

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.
 
 Gen_Mo_On_Trees (const Gen_Mo_On_Trees &other)=default
 Copy the tables of another engine, on the same graph.
 
 Gen_Mo_On_Trees (Gen_Mo_On_Trees &&other) noexcept(std::is_nothrow_move_constructible_v< Policy >)
 Take the tables of another engine, which is left empty, on the same graph.
 
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)
 

Detailed Description

template<AlephGraph GT, class Policy>
requires MoTreeGraph<GT>
class Aleph::Gen_Mo_On_Trees< GT, Policy >

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.

Template Parameters
GTGraph type (List_Graph, List_SGraph, or Array_Graph).
PolicyA type satisfying MoPolicy<Policy, GT::Node::Node_Type>.
Example (subtree distinct count)
// Build a tree
auto * n0 = g.insert_node(10);
auto * n1 = g.insert_node(20);
auto * n2 = g.insert_node(10);
g.insert_arc(n0, n1);
g.insert_arc(n0, n2);
// Distinct count on subtree rooted at n0 → 2 (values 10, 20)
using G = decltype(g);
auto ans = mot.subtree_solve({n0});
// ans(0) == 2
Offline subtree and path queries on trees via Mo's algorithm.
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231

Definition at line 172 of file tpl_mo_on_trees.H.

Member Typedef Documentation

◆ answer_type

template<AlephGraph GT, class Policy >
using Aleph::Gen_Mo_On_Trees< GT, Policy >::answer_type = typename Policy::answer_type

Definition at line 178 of file tpl_mo_on_trees.H.

◆ Arc

template<AlephGraph GT, class Policy >
using Aleph::Gen_Mo_On_Trees< GT, Policy >::Arc = typename GT::Arc

Definition at line 176 of file tpl_mo_on_trees.H.

◆ Node

template<AlephGraph GT, class Policy >
using Aleph::Gen_Mo_On_Trees< GT, Policy >::Node = typename GT::Node

Definition at line 175 of file tpl_mo_on_trees.H.

◆ T

template<AlephGraph GT, class Policy >
using Aleph::Gen_Mo_On_Trees< GT, Policy >::T = typename Node::Node_Type

Definition at line 177 of file tpl_mo_on_trees.H.

Constructor & Destructor Documentation

◆ Gen_Mo_On_Trees() [1/3]

template<AlephGraph GT, class Policy >
Aleph::Gen_Mo_On_Trees< GT, Policy >::Gen_Mo_On_Trees ( const GT &  g,
Node *  root,
Policy  p = Policy() 
)
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.

Parameters
gThe tree graph (must be connected with N nodes and N-1 edges).
rootRoot node used to orient parent/child relations.
pPolicy object copied/moved into the query engine.
Exceptions
std::domain_errorIf root is not found in g; an empty g takes only the null root, and gives an empty engine.
std::domain_errorIf g is disconnected (not a valid tree under the chosen root).
std::bad_allocIf auxiliary arrays/tables cannot be allocated.
Note
Complexity: O(N log N) time and O(N log N) space.

Definition at line 456 of file tpl_mo_on_trees.H.

References Aleph::Gen_Mo_On_Trees< GT, Policy >::build().

◆ Gen_Mo_On_Trees() [2/3]

template<AlephGraph GT, class Policy >
Aleph::Gen_Mo_On_Trees< GT, Policy >::Gen_Mo_On_Trees ( const Gen_Mo_On_Trees< GT, Policy > &  other)
default

Copy the tables of another engine, on the same graph.

Parameters
[in]otherengine to copy.

◆ Gen_Mo_On_Trees() [3/3]

template<AlephGraph GT, class Policy >
Aleph::Gen_Mo_On_Trees< GT, Policy >::Gen_Mo_On_Trees ( Gen_Mo_On_Trees< GT, Policy > &&  other)
inlinenoexcept

Take the tables of another engine, which is left empty, on the same graph.

The implicit move took the arrays and the map but copied the number of nodes and the levels of the lifting table: the moved-from engine then reported the old size for a tree without nodes.

Parameters
[in,out]otherengine to move from.
Note
The engine refers to its graph, so that it has no assignment.

Definition at line 477 of file tpl_mo_on_trees.H.

Member Function Documentation

◆ build()

template<AlephGraph GT, class Policy >
void Aleph::Gen_Mo_On_Trees< GT, Policy >::build ( )
inlineprivate

◆ is_empty()

template<AlephGraph GT, class Policy >
bool Aleph::Gen_Mo_On_Trees< GT, Policy >::is_empty ( ) const
inlinenoexcept

True if the tree is empty.

Definition at line 494 of file tpl_mo_on_trees.H.

References Aleph::Gen_Mo_On_Trees< GT, Policy >::n_.

Referenced by TEST(), and TYPED_TEST().

◆ lca()

template<AlephGraph GT, class Policy >
size_t Aleph::Gen_Mo_On_Trees< GT, Policy >::lca ( size_t  u,
size_t  v 
) const
inlineprivate

◆ mo_sweep()

template<AlephGraph GT, class Policy >
Array< answer_type > Aleph::Gen_Mo_On_Trees< GT, Policy >::mo_sweep ( const Array< T > &  data,
Array< Mo_Query >  queries,
size_t  n 
) const
inlineprivate

◆ path_solve() [1/2]

template<AlephGraph GT, class Policy >
Array< answer_type > Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve ( const Array< std::pair< Node *, Node * > > &  query_pairs) const
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.

Parameters
query_pairsArray of (u, v) node-pointer pairs.
Returns
Array<answer_type> with answers in the same order as query_pairs.
Exceptions
std::domain_errorIf any query endpoint is not in the tree.
Anyexception thrown by Policy::init, Policy::add, Policy::remove, or Policy::answer.
Note
Complexity: O((N + Q) sqrt(N)) time after preprocessing.
Extra space: O(N + Q) (queries, answers, activity flags, Mo state).

Definition at line 576 of file tpl_mo_on_trees.H.

References ah_domain_error_if, Aleph::Array< T >::create(), Aleph::Gen_Mo_On_Trees< GT, Policy >::first_, Aleph::Gen_Mo_On_Trees< GT, Policy >::flat_node_, Aleph::introsort(), 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 network_latency(), org_chart(), Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().

◆ path_solve() [2/2]

template<AlephGraph GT, class Policy >
Array< answer_type > Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve ( std::initializer_list< std::pair< Node *, Node * > >  il) const
inline

Solve path queries (initializer-list overload).

Parameters
ilInitializer list of (u, v) node-pointer pairs.
Returns
Array<answer_type> with answers in the same order as il.
Exceptions
std::domain_errorIf any query endpoint is not in the tree.
Anyexception propagated from path_solve(Array<pair<...>>) and policy callbacks.
Note
Complexity: O((N + Q) sqrt(N)) time after preprocessing.
Extra space: O(N + Q) (including temporary query array).

Definition at line 699 of file tpl_mo_on_trees.H.

References Aleph::Gen_Mo_On_Trees< GT, Policy >::path_solve().

◆ size()

template<AlephGraph GT, class Policy >
size_t Aleph::Gen_Mo_On_Trees< GT, Policy >::size ( ) const
inlinenoexcept

Number of nodes in the tree.

Definition at line 491 of file tpl_mo_on_trees.H.

References Aleph::Gen_Mo_On_Trees< GT, Policy >::n_.

Referenced by TEST(), and TYPED_TEST().

◆ subtree_solve() [1/2]

template<AlephGraph GT, class Policy >
Array< answer_type > Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve ( const Array< Node * > &  query_roots) const
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.

Parameters
query_rootsArray of subtree root nodes to query.
Returns
Array<answer_type> with answers in the same order as query_roots.
Exceptions
std::domain_errorIf any query root is not in the tree.
Anyexception thrown by Policy::init, Policy::add, Policy::remove, or Policy::answer.
Note
Complexity: O((N + Q) sqrt(N)) time after preprocessing.
Extra space: O(N + Q) (queries, answers, and Mo state).

Definition at line 516 of file tpl_mo_on_trees.H.

References ah_domain_error_if, Aleph::Array< T >::create(), 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::Array< T >::size(), Aleph::Gen_Mo_On_Trees< GT, Policy >::tin_, and Aleph::Gen_Mo_On_Trees< GT, Policy >::tout_.

Referenced by org_chart(), rainforest_biodiversity(), Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TYPED_TEST(), TYPED_TEST(), TYPED_TEST(), and TYPED_TEST().

◆ subtree_solve() [2/2]

template<AlephGraph GT, class Policy >
Array< answer_type > Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve ( std::initializer_list< Node * >  il) const
inline

Answer subtree queries from an initializer list.

Parameters
ilInitializer list of subtree root nodes.
Returns
Array<answer_type> with answers in the same order as il.
Exceptions
std::domain_errorIf any query root is not in the tree.
Anyexception propagated from subtree_solve(Array<Node*>).
Note
Complexity: O((N + Q) sqrt(N)) time after preprocessing.
Extra space: O(N + Q) (including temporary query array).

Definition at line 547 of file tpl_mo_on_trees.H.

References Aleph::Gen_Mo_On_Trees< GT, Policy >::subtree_solve().

Member Data Documentation

◆ depth_

template<AlephGraph GT, class Policy >
Array<size_t> Aleph::Gen_Mo_On_Trees< GT, Policy >::depth_
private

◆ first_

template<AlephGraph GT, class Policy >
Array<size_t> Aleph::Gen_Mo_On_Trees< GT, Policy >::first_
private

◆ flat_node_

template<AlephGraph GT, class Policy >
Array<size_t> Aleph::Gen_Mo_On_Trees< GT, Policy >::flat_node_
private

◆ flat_sub_

template<AlephGraph GT, class Policy >
Array<T> Aleph::Gen_Mo_On_Trees< GT, Policy >::flat_sub_
private

◆ g_

template<AlephGraph GT, class Policy >
const GT& Aleph::Gen_Mo_On_Trees< GT, Policy >::g_
private

Definition at line 183 of file tpl_mo_on_trees.H.

Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build().

◆ id_to_node_

template<AlephGraph GT, class Policy >
Array<Node*> Aleph::Gen_Mo_On_Trees< GT, Policy >::id_to_node_
private

Definition at line 189 of file tpl_mo_on_trees.H.

Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build().

◆ last_

template<AlephGraph GT, class Policy >
Array<size_t> Aleph::Gen_Mo_On_Trees< GT, Policy >::last_
private

◆ log_n_

template<AlephGraph GT, class Policy >
size_t Aleph::Gen_Mo_On_Trees< GT, Policy >::log_n_ = 0
private

◆ n_

◆ node_to_id_

template<AlephGraph GT, class Policy >
MapOLhash<Node*, size_t> Aleph::Gen_Mo_On_Trees< GT, Policy >::node_to_id_
private

◆ node_values_

template<AlephGraph GT, class Policy >
Array<T> Aleph::Gen_Mo_On_Trees< GT, Policy >::node_values_
private

◆ NONE

template<AlephGraph GT, class Policy >
constexpr size_t Aleph::Gen_Mo_On_Trees< GT, Policy >::NONE = ~static_cast<size_t>(0)
staticconstexprprivate

◆ parent_

template<AlephGraph GT, class Policy >
Array<size_t> Aleph::Gen_Mo_On_Trees< GT, Policy >::parent_
private

Definition at line 206 of file tpl_mo_on_trees.H.

Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build().

◆ pol_

template<AlephGraph GT, class Policy >
Policy Aleph::Gen_Mo_On_Trees< GT, Policy >::pol_
mutableprivate

◆ root_

template<AlephGraph GT, class Policy >
Node* Aleph::Gen_Mo_On_Trees< GT, Policy >::root_
private

Definition at line 184 of file tpl_mo_on_trees.H.

Referenced by Aleph::Gen_Mo_On_Trees< GT, Policy >::build().

◆ tin_

template<AlephGraph GT, class Policy >
Array<size_t> Aleph::Gen_Mo_On_Trees< GT, Policy >::tin_
private

◆ tout_

template<AlephGraph GT, class Policy >
Array<size_t> Aleph::Gen_Mo_On_Trees< GT, Policy >::tout_
private

◆ up_

template<AlephGraph GT, class Policy >
Array<size_t> Aleph::Gen_Mo_On_Trees< GT, Policy >::up_
private

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