83 parent.
insert(tree_root,
nullptr);
85 stk.
push({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();
104 stk2.
push(subtree_root);
107 auto * cur = stk2.
pop();
108 seen.
insert(cur->get_info());
109 for (
auto it =
typename GT::Node_Arc_Iterator(cur);
110 it.has_curr(); it.next_ne())
112 auto * a = it.get_curr();
114 if (nb != parent.
find(cur))
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))
155 while (!ancestors_u.
has(lca_node))
156 lca_node = parent.
find(lca_node);
159 for (
auto * p = u; p != lca_node; p = parent.
find(p))
160 vals.
insert(p->get_info());
161 for (
auto * p = v; p != lca_node; p = parent.
find(p))
162 vals.
insert(p->get_info());
163 vals.
insert(lca_node->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");
209 printf(
" Nodes: %zu, Edges: %zu\n\n", g.
vsize(), g.
esize());
215 printf(
"%-25s %s\n",
"Subtree Root (species)",
"Distinct species");
216 printf(
"%-25s %s\n",
"------------------------",
"----------------");
217 printf(
"%-25s %zu\n",
"root (3)", ans(0));
218 printf(
"%-25s %zu\n",
"a (1)", ans(1));
219 printf(
"%-25s %zu\n",
"b (4)", ans(2));
220 printf(
"%-25s %zu\n",
"c (1)", ans(3));
221 printf(
"%-25s %zu\n",
"d (5)", ans(4));
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");
274 printf(
" Nodes: %zu, Edges: %zu\n\n", g.
vsize(), g.
esize());
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");
343 printf(
" Nodes: %zu, Edges: %zu\n\n", g.
vsize(), g.
esize());
350 printf(
"--- Subtree queries ---\n");
351 printf(
"%-20s %s\n",
"Root",
"Distinct depts");
352 printf(
"%-20s %s\n",
"-------------------",
"--------------");
353 printf(
"%-20s %zu\n",
"CEO (10)", sub_ans(0));
354 printf(
"%-20s %zu\n",
"VP1 (20)", sub_ans(1));
355 printf(
"%-20s %zu\n",
"VP2 (30)", sub_ans(2));
368 printf(
"\n--- Path queries ---\n");
369 printf(
"%-20s %s\n",
"Path",
"Distinct depts");
370 printf(
"%-20s %s\n",
"-------------------",
"--------------");
371 printf(
"%-20s %zu\n",
"D4 → D2", path_ans(0));
372 printf(
"%-20s %zu\n",
"D4 → D3", path_ans(1));
373 printf(
"%-20s %zu\n",
"VP1 → VP2", path_ans(2));
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");
403 auto *
root =
new TN(1);
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);
420 printf(
" Nodes: 8\n\n");
427 printf(
"--- Subtree queries ---\n");
428 printf(
"Root Distinct types\n");
429 printf(
"------------------- --------------\n");
430 printf(
"root (1) %zu\n", sub_ans(0));
431 printf(
"a (2) %zu\n", sub_ans(1));
432 printf(
"c (3) %zu\n", sub_ans(2));
433 printf(
"d (4) %zu\n", sub_ans(3));
436 assert(sub_ans(0) == 5);
438 assert(sub_ans(1) == 3);
440 assert(sub_ans(2) == 2);
442 assert(sub_ans(3) == 2);
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");
450 printf(
"g → f %zu\n", path_ans(0));
451 printf(
"e → b %zu\n", path_ans(1));
452 printf(
"d → c %zu\n", path_ans(2));
455 assert(path_ans(0) == 5);
457 assert(path_ans(1) == 2);
459 assert(path_ans(2) == 4);
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.
bool has(const Key &key) const noexcept
Checks if a key exists in the hash table.
Dynamic stack of elements of generic type T based on a singly linked list.
bool is_empty() const noexcept
Check if the stack is empty.
T pop()
Remove and return the top item of the stack.
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).
Array< answer_type > path_solve(const Array< std::pair< Node *, Node * > > &query_pairs) const
Answer path queries on the N-ary tree.
Array< answer_type > subtree_solve(const Array< Node * > &query_roots) const
Answer subtree queries on the N-ary tree.
Offline subtree and path queries on trees via Mo's algorithm.
Array< answer_type > subtree_solve(const Array< Node * > &query_roots) const
Answer subtree queries using Mo's algorithm.
Array< answer_type > path_solve(const Array< std::pair< Node *, Node * > > &query_pairs) const
Answer path queries between node pairs.
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.
Forward declaration used by CRTP helpers before the full node definition.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
constexpr size_t vsize() const noexcept
See get_num_nodes().
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.
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.