|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Mo's algorithm on trees: offline subtree and path queries. More...
#include <algorithm>#include <cmath>#include <concepts>#include <vector>#include <tpl_array.H>#include <tpl_dynMapOhash.H>#include <tpl_dynList.H>#include <tpl_dynListStack.H>#include <tpl_mo_algorithm.H>#include <tpl_tree_node.H>#include <ah-errors.H>Go to the source code of this file.
Classes | |
| class | Aleph::Gen_Mo_On_Trees< GT, Policy > |
| Offline subtree and path queries on trees via Mo's algorithm. More... | |
| class | Aleph::Gen_Mo_On_Tree_Node< T, Policy > |
| Offline subtree and path queries on N-ary trees (Tree_Node). More... | |
Namespaces | |
| namespace | Aleph |
| Main namespace for Aleph-w library functions. | |
Concepts | |
| concept | Aleph::MoTreeGraph |
| Concept constraining a graph type usable for Mo on Trees. | |
Mo's algorithm on trees: offline subtree and path queries.
Extension of Mo's algorithm to tree structures. Given a rooted tree with N nodes and Q offline queries (either over subtrees or over paths), this class answers all queries in O((N+Q)√N) time by flattening the tree into an array via an Euler tour and then applying the classic Mo sweep.
Many tree problems ask aggregate questions over subtrees or paths (e.g. "how many distinct species live in this subtree?" or "what is the most frequent label on the path from u to v?"). Naïve answers cost O(N) per query, giving O(NQ) total. Mo on Trees reduces this to O((N+Q)√N) — a substantial improvement when Q is large.
Gen_Mo_Algorithm.| Phase | Time | Space |
|---|---|---|
| Build (Euler + LCA) | O(N log N) | O(N log N) |
| Sort queries | O(Q lg Q) | O(Q) |
| Sweep (subtree) | O((N+Q) √N) | O(N+Q) |
| Sweep (path) | O((N+Q) √N) | O(N+Q) |
The class is templated on a graph type GT and a Policy. GT can be any Aleph-w graph: List_Graph, List_SGraph, or Array_Graph (and their digraph variants if the tree is stored as a directed graph). The tree topology is read but never modified; all auxiliary data is stored externally — zero performance impact on existing graph and tree structures.
The Policy must satisfy the same MoPolicy concept used by Gen_Mo_Algorithm (see tpl_mo_algorithm.H).
Definition in file tpl_mo_on_trees.H.