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 can be cut apart by separators of vertices, arranged like a tree, and dynamic programming over those separators costs exponential time in only.
Definition
A tree decomposition of a graph is a tree together with a bag for every node of , such that
- (vertices) every vertex of lies in some bag;
- (edges) for every edge , some bag contains both and ;
- (connectivity) for every vertex , the nodes whose bags contain form a connected subtree of .
The width of the decomposition is , and the treewidth is the minimum width over all tree decompositions. The makes trees have treewidth 1. If 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 is an edge of , then separates the vertices in bags on 's side from those on 's side: any path between them in passes through . This is what dynamic programming exploits.
- Cliques fit in a bag. Every clique of is contained in some single bag (the subtrees of its vertices pairwise intersect, and pairwise intersecting subtrees of a tree share a node). So , 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 per edge, arranged like the tree |
| a cycle , | 2 | bags for , in a path |
| the complete graph | one bag with everything | |
| the grid, | a path of bags, each consecutive vertices in row-major order | |
| series-parallel graphs | at most 2 | |
| planar graphs on vertices | from the planar separator theorem |
For the grid the upper bound is tight: for (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.
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 to the bag of the edge above , 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 vertices, a "staircase" cut through the grid.
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 grid and to a deliberately narrow window that misses the vertical edges.
#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 consecutive vertices contains every edge ; a window of does not, so width fails this construction, consistent with .
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 -trees; Arnborg and Proskurowski). A graph has treewidth at most exactly when it is a subgraph of a chordal graph with clique number ; 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?
- Every vertex is in a bag
- Every edge is in a bag
- The bags containing each vertex form a connected subtree
- The bags have size at most 3
What is the treewidth of the complete graph ?
- 1
- 2
Why does separate the graph when is an edge of the decomposition tree?
- Because bags are independent sets
- 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
- Because the decomposition is a path
- It does not in general
What is the treewidth of a cycle with at least three vertices?
- 1
- 2
- 3