Nice Tree Decompositions
A dynamic program over a tree decomposition has to say how the table of a bag is computed from the tables of its children. With arbitrary decompositions, a parent bag can differ from its children in many vertices at once and have any number of children, and every DP would need complicated transition rules. Nice tree decompositions break every change into single steps of four kinds, so that a DP needs exactly four simple rules. Converting to this form costs nothing in width.
The four node types
A nice tree decomposition is a rooted tree decomposition in which the root and every leaf have empty bags, and every other node is one of:
| Node | Children | Bag |
|---|---|---|
| introduce | one child with bag | , |
| forget | one child with bag | , |
| join | two children with bags , | |
| leaf | none |
By the connectivity condition, each vertex is forgotten exactly once (on the way to the empty root it must leave the bags, and it cannot leave at two different nodes without its subtree being disconnected), but it may be introduced several times in different branches below a join.
A useful variant adds introduce-edge nodes, which introduce one edge of at a node whose bag contains and , exactly once per edge. Then each edge is "seen" by the DP at one place; this is convenient for problems such as counting or Steiner tree. This lesson uses the basic form.
Lemma (stated with its construction). A tree decomposition of width with nodes can be turned in polynomial time into a nice tree decomposition of width with nodes; since a decomposition can be assumed to have nodes, the nice one has nodes.
The construction
Root the decomposition anywhere. Process each node with bag bottom-up:
NICE(i)
1 if i has no children: start from a leaf (empty bag) and introduce the vertices of X_i one at a time
2 for each child c: build NICE(c), which ends with bag X_c,
3 then forget the vertices of X_c - X_i, then introduce those of X_i - X_c // now its bag is X_i
4 if there are several children: combine the chains pairwise with join nodes (all have bag X_i)
5 at the root: forget the vertices of X_root one at a time, ending with the empty bagForgetting first means the chain shrinks from down to and then grows up to , so every intermediate bag is a subset of or of and the width does not grow. The conditions of a tree decomposition still hold, because each vertex is added or removed along a path of nodes on which it was already present in the original. The program converts a small tree decomposition and prints the nice one as an indented tree.
def nice(children, bags, i, out, depth=0):
"""Returns the nice subtree for node i as a nested list of (label, bag, kids)."""
chains = []
kids = children.get(i, [])
if not kids:
node, bag = ("leaf", frozenset(), []), frozenset()
for v in sorted(bags[i]):
bag = bag | {v}
node = (f"introduce {v}", bag, [node])
chains.append(node)
for c in kids:
node = nice(children, bags, c, out)
bag = node[1]
for v in sorted(bag - bags[i]):
bag = bag - {v}
node = (f"forget {v}", bag, [node])
for v in sorted(bags[i] - bag):
bag = bag | {v}
node = (f"introduce {v}", bag, [node])
chains.append(node)
node = chains[0]
for other in chains[1:]:
node = ("join", bags[i], [node, other])
return node
def show(node, depth=0, counts=None):
label, bag, kids = node
counts[label.split()[0]] = counts.get(label.split()[0], 0) + 1
print(" " * depth + f"{label:12} {sorted(bag)}")
for k in kids:
show(k, depth + 1, counts)
# graph: a triangle 0-1-2 with a pendant path 2-3-4 and a pendant vertex 5 on 1
bags = {0: frozenset({0, 1, 2}), 1: frozenset({2, 3}), 2: frozenset({3, 4}), 3: frozenset({1, 5})}
children = {0: [1, 3], 1: [2]}
root = nice(children, bags, 0, None)
for v in sorted(bags[0]): # forget everything at the root
root = (f"forget {v}", root[1] - {v}, [root])
counts = {}
show(root, 0, counts)
print(counts)forget 2 []
forget 1 [2]
forget 0 [1, 2]
join [0, 1, 2]
introduce 1 [0, 1, 2]
introduce 0 [0, 2]
forget 3 [2]
introduce 2 [2, 3]
forget 4 [3]
introduce 4 [3, 4]
introduce 3 [3]
leaf []
introduce 2 [0, 1, 2]
introduce 0 [0, 1]
forget 5 [1]
introduce 5 [1, 5]
introduce 1 [1]
leaf []
{'forget': 6, 'join': 1, 'introduce': 9, 'leaf': 2}
Read the printed tree from the bottom up: each leaf starts empty, vertices enter one at a time, and the two branches meet at a join whose children both have bag . Every vertex is forgotten exactly once (six forget nodes for six vertices), while vertices 0, 1 and 2, which sit in the join bag, are each introduced in both branches.
How a DP uses it
For a node let be the set of vertices in the bags of 's subtree, and the graph they induce. A dynamic program stores, for each node and each "state" of its bag (which bag vertices are in the solution, what color they have, …), the best partial solution in consistent with that state. The four rules:
- leaf: one empty state, value 0.
- introduce : extend each child state by the choices for . Vertex appears nowhere below this node, so by the edge and connectivity conditions all its neighbours in lie in the current bag, and every constraint involving can be checked locally.
- forget : for each state of the smaller bag, take the best over the choices for . This is the only place where 's decision is finalized.
- join: the two subtrees share only the bag, so combine states that agree on the bag and add their values (subtracting the bag's contribution counted twice).
The table at a node has at most states for a problem with choices per vertex, and there are nodes, so such a DP runs in time. The next lesson carries this out for maximum independent set, with .
In C++
For a path decomposition the conversion produces a nice path decomposition: a single chain without join nodes. The program converts the row-major window decomposition of a grid and prints the sequence of operations with the bag size after each step.
#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
int main() {
const int r = 3, c = 4, n = r * c;
std::vector<std::set<int>> bags;
for (int t = 0; t + c + 1 <= n; t++) {
std::set<int> B;
for (int x = t; x <= t + c; x++) B.insert(x);
bags.push_back(B);
}
bags.push_back({}); // the empty root
std::set<int> cur;
int steps = 0, width = 0, forgets = 0;
for (auto& B : bags) {
std::vector<int> out, in;
for (int v : cur) if (!B.count(v)) out.push_back(v);
for (int v : B) if (!cur.count(v)) in.push_back(v);
for (int v : out) { cur.erase(v); steps++; forgets++; std::printf("forget %-3d |bag| = %zu\n", v, cur.size()); }
for (int v : in) {
cur.insert(v); steps++;
width = std::max(width, (int)cur.size() - 1);
std::printf("introduce %-3d |bag| = %zu\n", v, cur.size());
}
}
std::printf("%d nice nodes (plus the leaf), %d forgets for %d vertices, width %d\n", steps, forgets, n, width);
}introduce 0 |bag| = 1
introduce 1 |bag| = 2
introduce 2 |bag| = 3
introduce 3 |bag| = 4
introduce 4 |bag| = 5
forget 0 |bag| = 4
introduce 5 |bag| = 5
forget 1 |bag| = 4
introduce 6 |bag| = 5
forget 2 |bag| = 4
introduce 7 |bag| = 5
forget 3 |bag| = 4
introduce 8 |bag| = 5
forget 4 |bag| = 4
introduce 9 |bag| = 5
forget 5 |bag| = 4
introduce 10 |bag| = 5
forget 6 |bag| = 4
introduce 11 |bag| = 5
forget 7 |bag| = 4
forget 8 |bag| = 3
forget 9 |bag| = 2
forget 10 |bag| = 1
forget 11 |bag| = 0
24 nice nodes (plus the leaf), 12 forgets for 12 vertices, width 4
The chain alternates "forget the oldest vertex, introduce the next one" once the window is full, so the width stays at , and there are exactly introduce and forget nodes.
Test yourself
Which is NOT one of the node types of a nice tree decomposition?
- introduce
- forget
- join
- contract
How many times is each vertex forgotten in a nice tree decomposition with an empty root bag?
- Once for every bag that contains it
- Exactly once
- Once per child of the root
- Never
What do the two children of a join node have in common?
- Nothing
- Exactly the same bag as the join node
- Disjoint bags
- The same number of descendants
Does converting a decomposition into a nice one increase its width?
- Yes, by one
- No: forgetting before introducing keeps every bag within the larger of the two original bags
- Yes, it doubles
- Only for path decompositions