62 std::cout <<
"\n=== Tree DP / Rerooting DP ===\n\n";
83 std::array<G::Node *, 6>
nodes = {n0, n1, n2, n3, n4, n5};
85 std::cout <<
"Tree (root=0):\n";
87 std::cout <<
" / \\\n";
88 std::cout <<
" 1 2\n";
89 std::cout <<
" / \\ \\\n";
90 std::cout <<
" 3 4 5\n\n";
100 [](
auto *) ->
size_t {
return 1; },
101 [](
auto *,
const size_t & a,
auto *,
const size_t & c) ->
size_t {
106 [](
auto * p) ->
int {
return p->get_info(); },
107 [](
auto *,
const int &
acc,
auto *,
const int & child) ->
int {
111 std::cout <<
"Per-node metrics:\n";
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"
122 for (
size_t i = 0; i <
nodes.size(); ++i)
125 std::cout << std::left
135 std::cout <<
"\nDone.\n";
Generic tree dynamic programming and rerooting algorithms.
Generic bottom-up tree dynamic programming.
const T & value(Node *node) const
Returns the DP value for a given node.
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
DynArray< Graph::Node * > nodes
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
Array< size_t > tree_subtree_sizes(const GT &g, typename GT::Node *root, SA sa=SA())
Compute subtree sizes for every node.
void print_rule()
Prints a horizontal rule for example output separation.
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.
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.
Arc of graph implemented with double-linked adjacency lists.
int main()
Example program demonstrating tree dynamic programming (DP) and rerooting DP.