On this page
A tree has no loops and one route between any two vertices, so many hard questions about it can be answered by working up from the leaves. Treewidth is a whole number that says how far any other graph is from being a tree (Wikipedia: Treewidth).
What is a tree decomposition?
A tree decomposition of a graph G is a tree whose nodes each carry a bag, a set of vertices of G, obeying three rules (Wikipedia: Treewidth):
Every vertex of G is in at least one bag.
For every edge of G, some bag contains both of its ends.
The bags containing any one vertex form a connected piece of the tree.
The width of a decomposition is the size of its largest bag minus one, and the treewidth of G is the smallest width any decomposition of G achieves (same source).
Why do trees have treewidth 1?
Root the tree at r. Give r the bag {r}, every other vertex v the bag {v, parent of v}, and join each bag to its parent's bag. Every edge sits in its child's bag, and a vertex appears only in its own bag and its children's bags, which hang off its own. No bag exceeds 2 vertices: width 1. Width 0 is impossible once the tree has an edge, since a one-vertex bag holds no edge. Conversely, a connected graph with at least two vertices has treewidth 1 exactly when it is a tree (Wikipedia: Treewidth).
How do you find a tree decomposition of a cycle?
Take the 6-cycle with vertices 1 to 6 in order. Use four bags joined in a path:
{1, 2, 3} – {1, 3, 4} – {1, 4, 5} – {1, 5, 6}
Edges 1–2 and 2–3 are in the first bag, 3–4 in the second, 4–5 in the third, 5–6 and 6–1 in the last. Vertex 1 is in every bag; 3, 4 and 5 each sit in two neighbouring bags. Largest bag: 3 vertices, so width 2.
Any graph containing a cycle has treewidth at least 2 (Wikipedia: Treewidth), and the same fan, bags {1, i, i + 1}, works for any cycle. So every cycle has treewidth exactly 2.
What is the treewidth of an n × n grid?
The n × n grid graph has treewidth exactly n (Wikipedia: Treewidth). For n = 3, number the vertices 1 to 9 row by row and slide a window of n + 1 = 4 consecutive numbers:
{1, 2, 3, 4} – {2, 3, 4, 5} – {3, 4, 5, 6} – {4, 5, 6, 7} – {5, 6, 7, 8} – {6, 7, 8, 9}
A vertical edge joins v to v + 3 and a horizontal edge joins v to v + 1; both fit in the window starting at v. Each number stays in the window for consecutive steps, so rule 3 holds. Width: 4 − 1 = 3. For general n, the window holds n + 1 vertices and the width is n.
Since grids are planar, planar graphs have unbounded treewidth, a fact tied to graph minors and Wagner's theorem.
Why do NP-hard problems get easy on graphs of small treewidth?
Each bag is a separator, the kind of cut in Menger's theorem and graph connectivity: vertices below it reach the rest only through it. So you can work from the leaves upward with one small table per bag.
Take 3-colouring. A bag of k + 1 vertices has at most 3^(k+1) colourings; for each, record whether it extends to everything below. For a cycle that is at most 27 rows per bag, however long the cycle. Many problems that are NP-complete on general graphs yield to this dynamic programming once treewidth is bounded (Wikipedia: Treewidth); compare greedy colouring and Brooks' theorem.
Three papers make this general:
Bruno Courcelle's 1990 paper in Information and Computation90043-H) underlies Courcelle's theorem: any problem expressible in monadic second-order logic, 3-colouring included, is solvable in linear time on graphs of bounded treewidth (Wikipedia: Treewidth).
Hans Bodlaender gave a linear-time algorithm that, for constant k, decides whether treewidth is at most k and, if so, finds such a decomposition (SIAM Journal on Computing, 1996).
Arnborg, Corneil and Proskurowski showed that deciding whether a graph has treewidth at most a given k is NP-complete; for fixed k their algorithm runs in polynomial time, though exponential in k (SIAM Journal on Algebraic and Discrete Methods, 1987).
Try it yourself
Decompose the 5-cycle with three bags. Answer: {1, 2, 3}, {1, 3, 4}, {1, 4, 5}.
Show the complete graph on four vertices has treewidth 3. Hint: the complete graph on n vertices has treewidth n − 1 (Wikipedia: Treewidth).
Find a width-2 decomposition of a 2 × 6 ladder. Hint: number the top row 1, 3, 5…, the bottom 2, 4, 6…, then slide a window of three.
If the rules still feel slippery, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing, and the Fermi Problem Library has a step-by-step solver that explains the reasoning.



