TExcelTAD
Home › Parameterized & Fine-Grained Complexity › Nice Tree Decompositions

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 vv one child with bag X′X' X=X′∪{v}X = X' \cup \{v\}, v∉X′v \notin X'
forget vv one child with bag X′X' X=X′∖{v}X = X' \setminus \{v\}, v∈X′v \in X'
join two children with bags X1X_1, X2X_2 X=X1=X2X = X_1 = X_2
leaf none X=∅X = \emptyset

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 uvuv of GG at a node whose bag contains uu and vv, 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 tt with NN nodes can be turned in polynomial time into a nice tree decomposition of width tt with O(t⋅N)O(t \cdot N) nodes; since a decomposition can be assumed to have O(n)O(n) nodes, the nice one has O(t⋅n)O(t \cdot n) nodes.

The construction

Root the decomposition anywhere. Process each node ii with bag XiX_i bottom-up:

text
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 bag

Forgetting first means the chain shrinks from XcX_c down to Xc∩XiX_c \cap X_i and then grows up to XiX_i, so every intermediate bag is a subset of XcX_c or of XiX_i 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.

python · try it yourself
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 {0,1,2}\{0, 1, 2\}. 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 ii let ViV_i be the set of vertices in the bags of ii's subtree, and GiG_i 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 GiG_i consistent with that state. The four rules:

  • leaf: one empty state, value 0.
  • introduce vv: extend each child state by the choices for vv. Vertex vv appears nowhere below this node, so by the edge and connectivity conditions all its neighbours in GiG_i lie in the current bag, and every constraint involving vv can be checked locally.
  • forget vv: for each state of the smaller bag, take the best over the choices for vv. This is the only place where vv'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 ct+1c^{t+1} states for a problem with cc choices per vertex, and there are O(tn)O(tn) nodes, so such a DP runs in ct⋅tO(1)⋅nc^{t} \cdot t^{O(1)} \cdot n time. The next lesson carries this out for maximum independent set, with c=2c = 2.

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 3×43 \times 4 grid and prints the sequence of operations with the bag size after each step.

cpp · try it yourself
#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 c=4c = 4, and there are exactly 2n2n introduce and forget nodes.

Test yourself

Which is NOT one of the node types of a nice tree decomposition?

  1. introduce
  2. forget
  3. join
  4. contract

How many times is each vertex forgotten in a nice tree decomposition with an empty root bag?

  1. Once for every bag that contains it
  2. Exactly once
  3. Once per child of the root
  4. Never

What do the two children of a join node have in common?

  1. Nothing
  2. Exactly the same bag as the join node
  3. Disjoint bags
  4. The same number of descendants

Does converting a decomposition into a nice one increase its width?

  1. Yes, by one
  2. No: forgetting before introducing keeps every bag within the larger of the two original bags
  3. Yes, it doubles
  4. Only for path decompositions