Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tree_dp_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <iostream>
38# include <iomanip>
39# include <array>
40
41# include <Tree_DP.H>
42# include <print_rule.H>
43
44using namespace Aleph;
45
47
60int main()
61{
62 std::cout << "\n=== Tree DP / Rerooting DP ===\n\n";
63
64 // Build a tree:
65 // 0
66 // / \_
67 // 1 2
68 // / \ \_
69 // 3 4 5
70 G g;
71 auto n0 = g.insert_node(0);
72 auto n1 = g.insert_node(1);
73 auto n2 = g.insert_node(2);
74 auto n3 = g.insert_node(3);
75 auto n4 = g.insert_node(4);
76 auto n5 = g.insert_node(5);
77 g.insert_arc(n0, n1, 1);
78 g.insert_arc(n0, n2, 1);
79 g.insert_arc(n1, n3, 1);
80 g.insert_arc(n1, n4, 1);
81 g.insert_arc(n2, n5, 1);
82
83 std::array<G::Node *, 6> nodes = {n0, n1, n2, n3, n4, n5};
84
85 std::cout << "Tree (root=0):\n";
86 std::cout << " 0\n";
87 std::cout << " / \\\n";
88 std::cout << " 1 2\n";
89 std::cout << " / \\ \\\n";
90 std::cout << " 3 4 5\n\n";
91
92 // Rerooting DP examples:
93 const auto max_dist = tree_max_distance(g, n0);
94 const auto sum_dist = tree_sum_of_distances(g, n0);
95
96 // Bottom-up DP examples:
97 const auto subtree_sizes = tree_subtree_sizes(g, n0);
98
100 [](auto *) -> size_t { return 1; },
101 [](auto *, const size_t & a, auto *, const size_t & c) -> size_t {
102 return a + c;
103 });
104
106 [](auto * p) -> int { return p->get_info(); },
107 [](auto *, const int & acc, auto *, const int & child) -> int {
108 return acc + child;
109 });
110
111 std::cout << "Per-node metrics:\n";
112 print_rule();
113 std::cout << std::left
114 << std::setw(8) << "Node"
115 << std::setw(16) << "Subtree size"
116 << std::setw(16) << "Max distance"
117 << std::setw(18) << "Sum distances"
118 << std::setw(18) << "Subtree value sum"
119 << "\n";
120 print_rule();
121
122 for (size_t i = 0; i < nodes.size(); ++i)
123 {
124 const size_t id = id_map.id_of(nodes[i]);
125 std::cout << std::left
126 << std::setw(8) << i
127 << std::setw(16) << subtree_sizes[id]
128 << std::setw(16) << max_dist[id]
129 << std::setw(18) << sum_dist[id]
130 << std::setw(18) << value_sum_dp.value(nodes[i])
131 << "\n";
132 }
133 print_rule();
134
135 std::cout << "\nDone.\n";
136 return 0;
137}
Generic tree DP and rerooting DP on Aleph graph trees.
Generic bottom-up tree DP.
Definition Tree_DP.H:333
const T & value(Node *node) const
Returns the DP value for a given node.
Definition Tree_DP.H:391
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:428
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Array< size_t > tree_subtree_sizes(const GT &g, typename GT::Node *root, SA sa=SA())
Compute subtree sizes for every node.
Definition Tree_DP.H:647
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.
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
Array< size_t > tree_max_distance(const GT &g, typename GT::Node *root, SA sa=SA())
Compute the maximum distance from each node to any leaf.
Definition Tree_DP.H:680
Array< size_t > tree_sum_of_distances(const GT &g, typename GT::Node *root, SA sa=SA())
Compute sum of distances from each node to all others.
Definition Tree_DP.H:712
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
int main()
Example program demonstrating tree dynamic programming (DP) and rerooting DP.