Courcelle's Theorem
The dynamic program of Independent Set on Bounded Treewidth stores, for each bag, a bounded amount of information about the part of the graph below it. The same works for dominating set, coloring, Hamiltonicity, and many more problems, each with its own table. Courcelle's theorem (1990) explains why: every graph property that can be written in a certain logic has such a dynamic program, automatically. It is a meta-theorem, a theorem about a whole class of problems at once.
Monadic second-order logic in brief
MSO logic on graphs has variables for vertices () and for sets of vertices (), the adjacency relation , membership , equality, the Boolean connectives, and quantifiers over both kinds of variables. "Monadic" means the second-order variables range over sets (unary relations) only, not over arbitrary relations. MSO additionally allows edge variables and edge-set variables with an incidence relation, which makes properties like Hamiltonicity expressible.
Examples:
The second formula says "every nonempty proper subset has an edge leaving it". Independent set of size at most or at least needs in the formula, so the formula length depends on ; optimization versions are handled by an extension of the theorem (below).
What MSO cannot express: properties that need counting beyond a constant, such as "the number of vertices is even" (with plain MSO on general graphs), or comparing the sizes of two sets.
The theorem
Courcelle's theorem (1990; stated). For every MSO sentence and every , there is an algorithm that, given a graph with vertices and a tree decomposition of width , decides whether in time .
Combined with Bodlaender's linear-time algorithm for finding a decomposition of width (when one exists; see Computing Treewidth and Elimination Orderings), the decomposition need not even be given. Optimization (stated): Arnborg, Lagergren and Seese (1991) extended the theorem to finding a set satisfying an MSO formula that minimizes or maximizes or a weight, again in linear time for bounded treewidth. This covers maximum independent set, minimum dominating set, minimum feedback vertex set and many others.
Proof idea. Translate into a finite tree automaton that reads a nice tree decomposition (with its bags labelled by how the bag's vertices relate to each other) bottom-up. The automaton's states play the role of the DP table entries: each state summarizes everything about the processed subgraph that matters for the truth of . Finiteness of the automaton is the heart of the proof and comes from the logic's limited power to distinguish graphs glued along a small boundary.
What "finitely many states" looks like
For 3-coloring the summary of a processed part is simply the set of colorings of the current bag that extend to a proper coloring below it. There are at most colorings of a bag, so at most possible summaries: a large but fixed number for fixed . The program runs this DP on a path decomposition of a "ladder" graph (two parallel paths with rungs, plus random diagonals), records how many different summaries actually occur, and compares the verdict with brute force over all colorings.
import random
from itertools import product
def ladder(n_rungs, rng):
adj = {v: set() for v in range(2 * n_rungs)} # vertex 2i is top, 2i + 1 bottom
def add(a, b):
adj[a].add(b); adj[b].add(a)
for i in range(n_rungs):
add(2 * i, 2 * i + 1)
if i + 1 < n_rungs:
add(2 * i, 2 * i + 2); add(2 * i + 1, 2 * i + 3)
if rng.random() < 0.5:
add(2 * i, 2 * i + 3)
return adj
def three_color_dp(adj, n, window=4):
"""path decomposition: sliding window of 4 consecutive vertices (edges join v and v + 1, 2 or 3)"""
states = {()} # colorings of the current bag (as tuples)
bag, seen = [], set()
for v in range(n):
if len(bag) == window:
bag.pop(0)
states = {s[1:] for s in states} # forget the oldest vertex
new = set()
for s in states:
for col in range(3):
if all(not (u in adj[v] and s[i] == col) for i, u in enumerate(bag)):
new.add(s + (col,))
states = new
bag.append(v)
seen.add(frozenset(states))
return bool(states), len(seen)
def brute(adj, n):
return any(all(c[a] != c[b] for a in adj for b in adj[a]) for c in product(range(3), repeat=n))
rng = random.Random(3)
for rungs in [3, 4, 5]:
adj = ladder(rungs, rng)
for extra in [False, True]:
g = {v: set(nb) for v, nb in adj.items()}
if extra: # chords bottom(i) - top(i + 1): many triangles
for i in range(0, 2 * rungs - 2, 2):
g[i + 1].add(i + 2); g[i + 2].add(i + 1)
ok, distinct = three_color_dp(g, 2 * rungs)
print(f"{2 * rungs:2} vertices, extra chords {extra!s:5}: DP says {ok!s:5}, brute force {brute(g, 2 * rungs)!s:5},"
f" distinct bag summaries {distinct}") 6 vertices, extra chords False: DP says True , brute force True , distinct bag summaries 6
6 vertices, extra chords True : DP says False, brute force False, distinct bag summaries 4
8 vertices, extra chords False: DP says True , brute force True , distinct bag summaries 7
8 vertices, extra chords True : DP says False, brute force False, distinct bag summaries 4
10 vertices, extra chords False: DP says True , brute force True , distinct bag summaries 7
10 vertices, extra chords True : DP says False, brute force False, distinct bag summaries 4
However long the ladder, the DP only meets a bounded number of distinct summaries, because the bag has four vertices and so at most colorings. A Courcelle-style algorithm precomputes the transitions between such summaries once for the formula and then runs in linear time.
The catch: the function
The function produced by the automaton construction is astronomically large: every quantifier alternation can cost an exponential, so is a tower of exponentials whose height grows with the formula. This is not an artifact of the proof. Frick and Grohe (2004) showed that, unless P = NP, no algorithm checks MSO properties even on trees (treewidth 1) in time with elementary, that is, bounded by a tower of fixed height (stated). The theorem is therefore best read as a classification tool: to show that a problem is FPT parameterized by treewidth, write it in MSO. To get a usable algorithm, design the DP by hand, as in the previous lesson.
The program shows how fast a tower grows next to the table of the hand-written 3-coloring DP.
def tower(height, top):
x = top
for _ in range(height):
if x > 64:
return None # too large to print
x = 2 ** x
return x
for t in [1, 2, 3]:
hand = 3 ** (t + 1)
print(f"t = {t}: hand-written DP {hand} colorings per bag, at most 2^{hand} summaries"
f" ({len(str(2 ** hand))} digits)")
for top in [2, 3, 4]:
print(f"2^(2^(2^{top})) = {tower(3, top)}")t = 1: hand-written DP 9 colorings per bag, at most 2^9 summaries (3 digits)
t = 2: hand-written DP 27 colorings per bag, at most 2^27 summaries (9 digits)
t = 3: hand-written DP 81 colorings per bag, at most 2^81 summaries (25 digits)
2^(2^(2^2)) = 65536
2^(2^(2^3)) = None
2^(2^(2^4)) = None
Height 3 with top value 2 is already 65536; with top value 3 or 4 it is far beyond any computer (printed as None). Courcelle's theorem guarantees linear time in ; it does not guarantee a constant anyone could pay.
Where the theorem is used
- Classification: dominating set, Hamiltonian cycle, -coloring for fixed , feedback vertex set, odd cycle transversal: all FPT by treewidth, by writing short MSO formulas.
- Clique-width: Courcelle, Makowsky and Rotics (2000) proved an analogue for MSO (vertex-set quantification only) on graphs of bounded clique-width, a parameter that is also small on dense structured graphs.
- Minor-closed properties: by the Robertson-Seymour theorem, each such property is characterized by finitely many forbidden minors, and "contains as a minor" is MSO-expressible; combined with the grid minor theorem this yields polynomial algorithms whose existence is known but whose code is not.
Test yourself
What does 'monadic' mean in monadic second-order logic?
- Only one quantifier is allowed
- Second-order variables range over sets (unary relations), not over arbitrary relations
- The formula has one free variable
- Only vertex variables are allowed
What does Courcelle's theorem state?
- Every NP problem is FPT by treewidth
- Every MSO-definable graph property can be decided in time on graphs with a tree decomposition of width
- Treewidth can be computed in linear time for every graph
- MSO formulas have polynomial-size kernels
Why is Courcelle's theorem mostly a classification tool rather than a practical algorithm?
- It only works on trees
- The function can be a tower of exponentials, and under P NP this cannot be avoided in general even on trees (Frick and Grohe, 2004)
- It requires the formula to be first-order
- It needs a quantum computer