83 parent.
insert(tree_root,
nullptr);
88 auto [
cur, par] =
stk.pop();
89 for (
auto it =
typename GT::Node_Arc_Iterator(
cur);
90 it.has_curr(); it.next_ne())
92 auto * a = it.get_curr();
109 for (
auto it =
typename GT::Node_Arc_Iterator(
cur);
110 it.has_curr(); it.next_ne())
112 auto * a = it.get_curr();
134 while (
not stk.is_empty())
136 auto [
cur, par] =
stk.pop();
137 for (
auto it =
typename GT::Node_Arc_Iterator(
cur);
138 it.has_curr(); it.next_ne())
140 auto * a = it.get_curr();
151 for (
auto * p = u; p; p = parent.
find(p))
162 vals.insert(p->get_info());
173 printf(
"=== SCENARIO 1: Rainforest Biodiversity (List_Graph) ===\n\n");
174 printf(
"A hierarchical forest canopy: each section stores its\n");
175 printf(
"dominant species ID. Subtree queries count distinct\n");
176 printf(
"species beneath each section.\n\n");
213 auto ans =
mot.subtree_solve({
r, a, b, c, d});
215 printf(
"%-25s %s\n",
"Subtree Root (species)",
"Distinct species");
216 printf(
"%-25s %s\n",
"------------------------",
"----------------");
217 printf(
"%-25s %zu\n",
"root (3)",
ans(0));
229 printf(
"\nAll assertions passed!\n\n");
238 printf(
"=== SCENARIO 2: Network Latency Analysis (List_SGraph) ===\n\n");
239 printf(
"A tree-shaped data-centre network. Each router has a\n");
240 printf(
"latency class (1-5). Path queries count distinct latency\n");
241 printf(
"classes between pairs of routers.\n\n");
278 auto ans =
mot.path_solve({
286 printf(
"%-20s %s\n",
"Path (routers)",
"Distinct classes");
287 printf(
"%-20s %s\n",
"-------------------",
"----------------");
288 printf(
"%-20s %zu\n",
"R6 → R4",
ans(0));
289 printf(
"%-20s %zu\n",
"R6 → R7",
ans(1));
290 printf(
"%-20s %zu\n",
"R0 → R0",
ans(2));
291 printf(
"%-20s %zu\n",
"R3 → R5",
ans(3));
292 printf(
"%-20s %zu\n",
"R1 → R2",
ans(4));
300 printf(
"\nAll assertions passed!\n\n");
309 printf(
"=== SCENARIO 3: Corporate Org Chart (Array_Graph) ===\n\n");
310 printf(
"Org chart: each node stores a department ID.\n");
311 printf(
"Subtree queries: distinct departments under a VP.\n");
312 printf(
"Path queries: distinct departments between employees.\n\n");
350 printf(
"--- Subtree queries ---\n");
351 printf(
"%-20s %s\n",
"Root",
"Distinct depts");
352 printf(
"%-20s %s\n",
"-------------------",
"--------------");
368 printf(
"\n--- Path queries ---\n");
369 printf(
"%-20s %s\n",
"Path",
"Distinct depts");
370 printf(
"%-20s %s\n",
"-------------------",
"--------------");
379 printf(
"\nAll assertions passed!\n\n");
388 printf(
"=== SCENARIO 4: File-system Inode Types (Tree_Node) ===\n\n");
389 printf(
"A directory tree where each node stores a file-type ID.\n");
390 printf(
"Subtree queries count distinct file types under a dir.\n");
391 printf(
"Path queries count distinct types between two files.\n\n");
404 auto * a =
new TN(2);
405 auto * b =
new TN(1);
406 auto * c =
new TN(3);
407 auto * d =
new TN(4);
408 auto * e =
new TN(2);
409 auto * f =
new TN(1);
410 auto * g =
new TN(5);
412 root->insert_rightmost_child(a);
413 root->insert_rightmost_child(b);
414 root->insert_rightmost_child(c);
415 a->insert_rightmost_child(d);
416 a->insert_rightmost_child(e);
417 c->insert_rightmost_child(f);
418 d->insert_rightmost_child(g);
427 printf(
"--- Subtree queries ---\n");
428 printf(
"Root Distinct types\n");
429 printf(
"------------------- --------------\n");
445 auto path_ans =
mot.path_solve({{g, f}, {e, b}, {d, c}});
447 printf(
"\n--- Path queries ---\n");
448 printf(
"Path Distinct types\n");
449 printf(
"------------------- --------------\n");
461 printf(
"\nAll assertions passed!\n\n");
473 printf(
"Mo's Algorithm on Trees — Offline Subtree & Path Queries\n");
474 printf(
"========================================================\n\n");
481 printf(
"All scenarios completed successfully.\n");
WeightedDigraph::Node Node
Self-adjusting dynamic hash table.
Key * insert(const Key &key)
Inserts key into the hash set.
Dynamic stack of elements of generic type T based on a singly linked list.
T & push(const T &data)
Push an item by copy onto the top of the stack.
Pair * insert(const Key &key, const Data &data)
Inserts into the hash map the pair (key, record) indexed by key.
Data & find(const Key &key)
Finds and returns a reference to the value associated with key.
Offline subtree and path queries on N-ary trees (Tree_Node).
Offline subtree and path queries on trees via Mo's algorithm.
Arc for graphs implemented with simple adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Graph_Node< Node_Info > Node
The graph type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
constexpr size_t vsize() const noexcept
size_t esize() const noexcept
Return the total of arcs of graph.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
static void rainforest_biodiversity()
static size_t brute_subtree_distinct(const GT &g, typename GT::Node *tree_root, typename GT::Node *subtree_root)
static size_t brute_path_distinct(const GT &g, typename GT::Node *root, typename GT::Node *u, typename GT::Node *v)
static void network_latency()
static void filesystem_inodes()
Main namespace for Aleph-w library functions.
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.
Arc of graph implemented with double-linked adjacency lists.
Array-based graph implementation.
Dynamic stack implementation based on linked lists.
Dynamic set implementations based on hash tables.
Generic graph and digraph implementations.
Mo's algorithm on trees: offline subtree and path queries.
Simple graph implementation with adjacency lists.