TExcelTAD
Home › Parameterized & Fine-Grained Complexity › Courcelle's Theorem

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 (x,y,…x, y, \ldots) and for sets of vertices (X,Y,…X, Y, \ldots), the adjacency relation adj⁡(x,y)\operatorname{adj}(x, y), membership x∈Xx \in X, equality, the Boolean connectives, and quantifiers ∃,∀\exists, \forall over both kinds of variables. "Monadic" means the second-order variables range over sets (unary relations) only, not over arbitrary relations. MSO2_2 additionally allows edge variables and edge-set variables with an incidence relation, which makes properties like Hamiltonicity expressible.

Examples:

3-colorable:∃X1∃X2∃X3(∀x (x∈X1∨x∈X2∨x∈X3)  ∧⋀i=13∀x∀y (adj⁡(x,y)→¬(x∈Xi∧y∈Xi)))connected:∀X((∃x x∈X∧∃y y∉X)→∃x∃y (x∈X∧y∉X∧adj⁡(x,y)))\begin{aligned} \text{3-colorable:} \quad & \exists X_1 \exists X_2 \exists X_3 \Big( \forall x \, (x \in X_1 \lor x \in X_2 \lor x \in X_3) \;\land \bigwedge_{i=1}^{3} \forall x \forall y \, \big( \operatorname{adj}(x, y) \to \lnot(x \in X_i \land y \in X_i) \big) \Big) \\ \text{connected:} \quad & \forall X \Big( \big(\exists x\, x \in X \land \exists y\, y \notin X\big) \to \exists x \exists y \, \big( x \in X \land y \notin X \land \operatorname{adj}(x, y) \big) \Big) \end{aligned}

The second formula says "every nonempty proper subset has an edge leaving it". Independent set of size at most or at least kk needs kk in the formula, so the formula length depends on kk; 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 MSO2_2 sentence φ\varphi and every tt, there is an algorithm that, given a graph GG with nn vertices and a tree decomposition of width tt, decides whether G⊨φG \models \varphi in time f(∣φ∣,t)⋅nf(\lvert \varphi \rvert, t) \cdot n.

Combined with Bodlaender's linear-time algorithm for finding a decomposition of width tt (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 XX satisfying an MSO formula φ(X)\varphi(X) that minimizes or maximizes ∣X∣\lvert X \rvert 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 φ\varphi 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 φ\varphi. 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 3t+13^{t+1} colorings of a bag, so at most 23t+12^{3^{t+1}} possible summaries: a large but fixed number for fixed tt. 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 3n3^n colorings.

python · try it yourself
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 34=813^4 = 81 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 ff

The function f(∣φ∣,t)f(\lvert \varphi \rvert, t) produced by the automaton construction is astronomically large: every quantifier alternation can cost an exponential, so ff 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 f(∣φ∣)⋅nO(1)f(\lvert \varphi \rvert) \cdot n^{O(1)} with ff 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 3t3^t table of the hand-written 3-coloring DP.

python · try it yourself
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 nn; it does not guarantee a constant anyone could pay.

Where the theorem is used

  • Classification: dominating set, Hamiltonian cycle, qq-coloring for fixed qq, feedback vertex set, odd cycle transversal: all FPT by treewidth, by writing short MSO2_2 formulas.
  • Clique-width: Courcelle, Makowsky and Rotics (2000) proved an analogue for MSO1_1 (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 HH as a minor" is MSO2_2-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?

  1. Only one quantifier is allowed
  2. Second-order variables range over sets (unary relations), not over arbitrary relations
  3. The formula has one free variable
  4. Only vertex variables are allowed

What does Courcelle's theorem state?

  1. Every NP problem is FPT by treewidth
  2. Every MSO2_2-definable graph property can be decided in time f(∣φ∣,t)⋅nf(\lvert \varphi \rvert, t) \cdot n on graphs with a tree decomposition of width tt
  3. Treewidth can be computed in linear time for every graph
  4. MSO formulas have polynomial-size kernels

Why is Courcelle's theorem mostly a classification tool rather than a practical algorithm?

  1. It only works on trees
  2. The function ff can be a tower of exponentials, and under P ≠\ne NP this cannot be avoided in general even on trees (Frick and Grohe, 2004)
  3. It requires the formula to be first-order
  4. It needs a quantum computer