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

Offline subtree and path queries on N-ary trees (Tree_Node). More...

#include <tpl_mo_on_trees.H>

Collaboration diagram for Aleph::Gen_Mo_On_Tree_Node< T, Policy >:
[legend]

Public Types

using Node = Tree_Node< T >
 
using answer_type = typename Policy::answer_type
 

Public Member Functions

 Gen_Mo_On_Tree_Node (Node *root, Policy p=Policy())
 Construct a Mo's algorithm query engine for N-ary trees.
 
size_t size () const noexcept
 Number of nodes in the tree.
 
bool is_empty () const noexcept
 True if the tree is empty.
 
Array< answer_typesubtree_solve (const Array< Node * > &query_roots) const
 Answer subtree queries on the N-ary tree.
 
Array< answer_typesubtree_solve (std::initializer_list< Node * > il) const
 Solve subtree queries (initializer-list overload).
 
Array< answer_typepath_solve (const Array< std::pair< Node *, Node * > > &query_pairs) const
 Answer path queries on the N-ary tree.
 
Array< answer_typepath_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_typemo_sweep (const Array< T > &data, Array< Mo_Query > queries, size_t nn) const
 

Private Attributes

Noderoot_
 
Policy pol_
 
size_t n_ = 0
 
Array< Node * > id_to_node_
 
MapOLhash< Node *, size_t > node_to_id_
 
Array< Tnode_values_
 
Array< size_t > tin_
 
Array< size_t > tout_
 
Array< Tflat_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 = ~size_t(0)
 

Detailed Description

template<typename T, class Policy>
requires MoPolicy<Policy, T>
class Aleph::Gen_Mo_On_Tree_Node< T, Policy >

Offline subtree and path queries on N-ary trees (Tree_Node).

Same algorithm as Gen_Mo_On_Trees but works directly with the first-child / next-sibling representation of Tree_Node<T>. The tree is traversed via get_left_child() / get_right_sibling() instead of graph adjacency iterators.

Template Parameters
TValue type stored in each Tree_Node.
PolicyA type satisfying MoPolicy<Policy, T>.
Example
auto * r = new Tree_Node<int>(10);
auto * a = new Tree_Node<int>(20);
auto * b = new Tree_Node<int>(10);
r->insert_rightmost_child(a);
r->insert_rightmost_child(b);
auto ans = mot.subtree_solve({r}); // ans(0) == 2
Offline subtree and path queries on N-ary trees (Tree_Node).
Generic m-ary trees.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
gsl_rng * r

Definition at line 700 of file tpl_mo_on_trees.H.

Member Typedef Documentation

◆ answer_type

template<typename T , class Policy >
using Aleph::Gen_Mo_On_Tree_Node< T, Policy >::answer_type = typename Policy::answer_type

Definition at line 704 of file tpl_mo_on_trees.H.

◆ Node

Constructor & Destructor Documentation

◆ Gen_Mo_On_Tree_Node()

Construct a Mo's algorithm query engine for N-ary trees.

Performs an O(N log N) preprocessing step. The tree is not modified in any way.

Parameters
rootRoot of the N-ary tree (must not be null).
pPolicy object copied/moved into the query engine.
Exceptions
std::domain_errorIf root is null or tree is inconsistent.
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 995 of file tpl_mo_on_trees.H.

References ah_domain_error_if, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::build(), and root().

Member Function Documentation

◆ build()

template<typename T , class Policy >
void Aleph::Gen_Mo_On_Tree_Node< T, Policy >::build ( )
inlineprivate

◆ is_empty()

template<typename T , class Policy >
bool Aleph::Gen_Mo_On_Tree_Node< T, Policy >::is_empty ( ) const
inlinenoexcept

True if the tree is empty.

Definition at line 1007 of file tpl_mo_on_trees.H.

References Aleph::Gen_Mo_On_Tree_Node< T, Policy >::n_.

◆ lca()

◆ mo_sweep()

◆ path_solve() [1/2]

template<typename T , class Policy >
Array< answer_type > Aleph::Gen_Mo_On_Tree_Node< T, Policy >::path_solve ( const Array< std::pair< Node *, Node * > > &  query_pairs) const
inline

Answer path queries on the N-ary tree.

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 1075 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_Tree_Node< T, Policy >::first_, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::flat_node_, l, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::last_, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::lca(), Aleph::Gen_Mo_On_Tree_Node< T, Policy >::n_, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::node_to_id_, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::node_values_, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::NONE, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::pol_, r, and Aleph::MapOpenHash< Key, Data, Cmp, HashTable >::search().

Referenced by Aleph::Gen_Mo_On_Tree_Node< T, Policy >::path_solve().

◆ path_solve() [2/2]

template<typename T , class Policy >
Array< answer_type > Aleph::Gen_Mo_On_Tree_Node< T, 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 1197 of file tpl_mo_on_trees.H.

References Aleph::divide_and_conquer_partition_dp(), and Aleph::Gen_Mo_On_Tree_Node< T, Policy >::path_solve().

◆ size()

template<typename T , class Policy >
size_t Aleph::Gen_Mo_On_Tree_Node< T, Policy >::size ( ) const
inlinenoexcept

Number of nodes in the tree.

Definition at line 1004 of file tpl_mo_on_trees.H.

References Aleph::Gen_Mo_On_Tree_Node< T, Policy >::n_.

◆ subtree_solve() [1/2]

template<typename T , class Policy >
Array< answer_type > Aleph::Gen_Mo_On_Tree_Node< T, Policy >::subtree_solve ( const Array< Node * > &  query_roots) const
inline

Answer subtree queries on the N-ary tree.

Parameters
query_rootsArray of subtree root nodes.
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 1023 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_Tree_Node< T, Policy >::flat_sub_, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::mo_sweep(), Aleph::Gen_Mo_On_Tree_Node< T, Policy >::n_, Aleph::Gen_Mo_On_Tree_Node< T, Policy >::node_to_id_, Aleph::MapOpenHash< Key, Data, Cmp, HashTable >::search(), Aleph::Gen_Mo_On_Tree_Node< T, Policy >::tin_, and Aleph::Gen_Mo_On_Tree_Node< T, Policy >::tout_.

Referenced by Aleph::Gen_Mo_On_Tree_Node< T, Policy >::subtree_solve().

◆ subtree_solve() [2/2]

template<typename T , class Policy >
Array< answer_type > Aleph::Gen_Mo_On_Tree_Node< T, Policy >::subtree_solve ( std::initializer_list< Node * >  il) const
inline

Solve subtree queries (initializer-list overload).

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 1054 of file tpl_mo_on_trees.H.

References Aleph::divide_and_conquer_partition_dp(), and Aleph::Gen_Mo_On_Tree_Node< T, Policy >::subtree_solve().

Member Data Documentation

◆ depth_

◆ first_

◆ flat_node_

◆ flat_sub_

◆ id_to_node_

template<typename T , class Policy >
Array<Node*> Aleph::Gen_Mo_On_Tree_Node< T, Policy >::id_to_node_
private

Definition at line 714 of file tpl_mo_on_trees.H.

Referenced by Aleph::Gen_Mo_On_Tree_Node< T, Policy >::build().

◆ last_

◆ log_n_

◆ n_

◆ node_to_id_

◆ node_values_

◆ NONE

template<typename T , class Policy >
constexpr size_t Aleph::Gen_Mo_On_Tree_Node< T, Policy >::NONE = ~size_t(0)
staticconstexprprivate

◆ parent_

template<typename T , class Policy >
Array<size_t> Aleph::Gen_Mo_On_Tree_Node< T, Policy >::parent_
private

Definition at line 731 of file tpl_mo_on_trees.H.

Referenced by Aleph::Gen_Mo_On_Tree_Node< T, Policy >::build().

◆ pol_

◆ root_

template<typename T , class Policy >
Node* Aleph::Gen_Mo_On_Tree_Node< T, Policy >::root_
private

Definition at line 709 of file tpl_mo_on_trees.H.

Referenced by Aleph::Gen_Mo_On_Tree_Node< T, Policy >::build().

◆ tin_

◆ tout_

◆ up_


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