TExcelTAD
Home › Parameterized & Fine-Grained Complexity › Tree Decompositions and Treewidth

Tree Decompositions and Treewidth

Many NP-hard graph problems are easy on trees: maximum independent set, minimum dominating set and 3-coloring all fall to a simple dynamic program from the leaves up (Choosing the State shows the style). The reason is that every vertex of a tree separates it: the subtrees below a vertex interact with the rest only through that vertex. Treewidth measures how far a graph is from having this property. A graph of treewidth tt can be cut apart by separators of t+1t + 1 vertices, arranged like a tree, and dynamic programming over those separators costs exponential time in tt only.

Definition

A tree decomposition of a graph G=(V,E)G = (V, E) is a tree TT together with a bag Xi⊆VX_i \subseteq V for every node ii of TT, such that

  1. (vertices) every vertex of GG lies in some bag;
  2. (edges) for every edge uvuv, some bag contains both uu and vv;
  3. (connectivity) for every vertex vv, the nodes whose bags contain vv form a connected subtree of TT.

The width of the decomposition is max⁡i∣Xi∣−1\max_i \lvert X_i \rvert - 1, and the treewidth tw⁡(G)\operatorname{tw}(G) is the minimum width over all tree decompositions. The −1-1 makes trees have treewidth 1. If TT is a path, the decomposition is a path decomposition, and the minimum width over those is the pathwidth.

Two facts make the definition useful:

  • Separation. If ijij is an edge of TT, then Xi∩XjX_i \cap X_j separates the vertices in bags on ii's side from those on jj's side: any path between them in GG passes through Xi∩XjX_i \cap X_j. This is what dynamic programming exploits.
  • Cliques fit in a bag. Every clique of GG is contained in some single bag (the subtrees of its vertices pairwise intersect, and pairwise intersecting subtrees of a tree share a node). So tw⁡(Kn)=n−1\operatorname{tw}(K_n) = n - 1, and treewidth is at least the size of the largest clique minus 1.

Examples

Graph Treewidth Decomposition
a forest with at least one edge 1 one bag {v,parent(v)}\{v, \text{parent}(v)\} per edge, arranged like the tree
a cycle CnC_n, n≥3n \ge 3 2 bags {v0,vi,vi+1}\{v_0, v_i, v_{i+1}\} for i=1,…,n−2i = 1, \ldots, n - 2, in a path
the complete graph KnK_n n−1n - 1 one bag with everything
the r×cr \times c grid, r≥cr \ge c cc a path of bags, each c+1c + 1 consecutive vertices in row-major order
series-parallel graphs at most 2
planar graphs on nn vertices O(n)O(\sqrt{n}) from the planar separator theorem

For the k×kk \times k grid the upper bound kk is tight: tw⁡=k\operatorname{tw} = k for k≥2k \ge 2 (stated; the lower bound uses brambles, a min-max dual of treewidth). Grids are the canonical "wide" graphs: by the grid minor theorem (Robertson and Seymour, 1986; polynomial bounds by Chekuri and Chuzhoy, 2016), every graph of large treewidth contains a large grid as a minor.

The program builds the decompositions from the table, checks the three conditions and prints the widths.

python · try it yourself
def check(n, edges, bags, tree):
    """bags: list of sets; tree: list of (i, j) edges between bag indices."""
    if set().union(*bags) != set(range(n)):
        return "a vertex is in no bag"
    for u, v in edges:
        if not any(u in B and v in B for B in bags):
            return f"edge {u}-{v} is in no bag"
    nbr = {i: set() for i in range(len(bags))}
    for i, j in tree:
        nbr[i].add(j); nbr[j].add(i)
    for v in range(n):
        holders = {i for i, B in enumerate(bags) if v in B}
        start = min(holders)
        seen, stack = {start}, [start]
        while stack:
            for j in nbr[stack.pop()]:
                if j in holders and j not in seen:
                    seen.add(j); stack.append(j)
        if seen != holders:
            return f"bags of vertex {v} are not connected"
    return f"valid, width {max(len(B) for B in bags) - 1}"

# a tree: parent of v is (v - 1) // 2
n = 10
tree_edges = [((v - 1) // 2, v) for v in range(1, n)]
bags = [{(v - 1) // 2, v} for v in range(1, n)]
links = [(v - 1, (v - 1) // 2 - 1) for v in range(3, n)] + [(0, 1)]   # bag v-1 holds edge (parent, v)
print("binary tree :", check(n, tree_edges, bags, links))

# a cycle
n = 9
cyc = [(i, (i + 1) % n) for i in range(n)]
bags = [{0, i, i + 1} for i in range(1, n - 1)]
print("cycle C_9   :", check(n, cyc, bags, [(i, i + 1) for i in range(len(bags) - 1)]))

# a 5 x 4 grid, row-major numbering, window of c + 1 vertices
r, c = 5, 4
grid = [(i * c + j, i * c + j + 1) for i in range(r) for j in range(c - 1)] + \
       [(i * c + j, (i + 1) * c + j) for i in range(r - 1) for j in range(c)]
bags = [set(range(t, t + c + 1)) for t in range(r * c - c)]
print("grid 5 x 4  :", check(r * c, grid, bags, [(i, i + 1) for i in range(len(bags) - 1)]))

# a broken decomposition of the cycle: vertex 0 left out of the middle bag
bad = [{0, 1, 2}, {2, 3, 4}, {0, 4, 5}, {0, 5, 6}, {0, 6, 7}, {0, 7, 8}]
print("bad cycle   :", check(9, cyc, bad, [(i, i + 1) for i in range(5)]))
binary tree : valid, width 1
cycle C_9   : valid, width 2
grid 5 x 4  : valid, width 4
bad cycle   : bags of vertex 0 are not connected

The decomposition of the binary tree links the bag of edge (p(v),v)(p(v), v) to the bag of the edge above p(v)p(v), and the two bags at the root to each other; each vertex appears in the bag of its parent edge and the bags of its child edges, which are adjacent in the decomposition tree. The broken example violates connectivity: vertex 0 appears in the first and third bags but not in the second.

Separators in action

The separation property is easy to test: delete the intersection of two adjacent bags and check that the two sides fall apart. On the grid decomposition, every intersection of consecutive bags is a set of cc vertices, a "staircase" cut through the grid.

python · try it yourself
r, c = 5, 4
adj = {v: set() for v in range(r * c)}
for i in range(r):
    for j in range(c):
        v = i * c + j
        if j + 1 < c:
            adj[v].add(v + 1); adj[v + 1].add(v)
        if i + 1 < r:
            adj[v].add(v + c); adj[v + c].add(v)
bags = [set(range(t, t + c + 1)) for t in range(r * c - c)]

def component(start, removed):
    seen, stack = {start}, [start]
    while stack:
        for u in adj[stack.pop()]:
            if u not in removed and u not in seen:
                seen.add(u); stack.append(u)
    return seen

for t in [2, 7, 11]:
    S = bags[t] & bags[t + 1]
    left = set().union(*bags[:t + 1]) - S
    right = set().union(*bags[t + 1:]) - S
    comp = component(min(left), S)
    print(f"bags {t},{t + 1}: separator {sorted(S)}; left side {len(left)} vertices,"
          f" right side {len(right)}; left component reaches right: {bool(comp & right)}")
bags 2,3: separator [3, 4, 5, 6]; left side 3 vertices, right side 13; left component reaches right: False
bags 7,8: separator [8, 9, 10, 11]; left side 8 vertices, right side 8; left component reaches right: False
bags 11,12: separator [12, 13, 14, 15]; left side 12 vertices, right side 4; left component reaches right: False

In C++

A checker for the three conditions, applied to the row-major window decomposition of a 6×66 \times 6 grid and to a deliberately narrow window that misses the vertical edges.

cpp · try it yourself
#include <cstdio>
#include <set>
#include <utility>
#include <vector>

// path decompositions only: bag t is adjacent to bag t + 1
const char* check(int n, const std::vector<std::pair<int, int>>& edges, const std::vector<std::set<int>>& bags) {
    for (int v = 0; v < n; v++) {
        int first = -1, last = -1, count = 0;
        for (int t = 0; t < (int)bags.size(); t++)
            if (bags[t].count(v)) { if (first < 0) first = t; last = t; count++; }
        if (first < 0) return "a vertex is in no bag";
        if (last - first + 1 != count) return "a vertex's bags are not contiguous";
    }
    for (auto [u, v] : edges) {
        bool ok = false;
        for (auto& B : bags) if (B.count(u) && B.count(v)) { ok = true; break; }
        if (!ok) return "an edge is in no bag";
    }
    return "valid";
}

int main() {
    const int r = 6, c = 6, n = r * c;
    std::vector<std::pair<int, int>> edges;
    for (int i = 0; i < r; i++)
        for (int j = 0; j < c; j++) {
            if (j + 1 < c) edges.push_back({i * c + j, i * c + j + 1});
            if (i + 1 < r) edges.push_back({i * c + j, (i + 1) * c + j});
        }
    for (int w : {c + 1, c}) {
        std::vector<std::set<int>> bags;
        for (int t = 0; t + w <= n; t++) {
            std::set<int> B;
            for (int x = t; x < t + w; x++) B.insert(x);
            bags.push_back(B);
        }
        std::printf("window of %d vertices: %zu bags, width %d, %s\n", w, bags.size(), w - 1, check(n, edges, bags));
    }
}
window of 7 vertices: 30 bags, width 6, valid
window of 6 vertices: 31 bags, width 5, an edge is in no bag

A window of c+1c + 1 consecutive vertices contains every edge (v,v+c)(v, v + c); a window of cc does not, so width c−1c - 1 fails this construction, consistent with tw⁡(grid)=c\operatorname{tw}(\text{grid}) = c.

Why treewidth is the right measure

Treewidth was introduced in Robertson and Seymour's graph minors project (1984 onward), and earlier in equivalent forms (partial kk-trees; Arnborg and Proskurowski). A graph has treewidth at most tt exactly when it is a subgraph of a chordal graph with clique number t+1t + 1; that equivalent view leads to the elimination orderings of Computing Treewidth and Elimination Orderings. Many real graph families have small treewidth: control-flow graphs of structured programs have treewidth at most 6 (Thorup, 1998), and many road networks and molecular graphs are far below their size. The next three lessons turn small treewidth into fast algorithms.

Test yourself

Which condition of a tree decomposition does the broken cycle example violate?

  1. Every vertex is in a bag
  2. Every edge is in a bag
  3. The bags containing each vertex form a connected subtree
  4. The bags have size at most 3

What is the treewidth of the complete graph KnK_n?

  1. 1
  2. 2
  3. n−1n - 1
  4. nn

Why does Xi∩XjX_i \cap X_j separate the graph when ijij is an edge of the decomposition tree?

  1. Because bags are independent sets
  2. Because a vertex on both sides would have a disconnected set of bags, and an edge between the sides would need a bag containing both ends, so every path crosses the intersection
  3. Because the decomposition is a path
  4. It does not in general

What is the treewidth of a cycle with at least three vertices?

  1. 1
  2. 2
  3. 3
  4. n/2n/2