TExcelTAD
Home › Parameterized & Fine-Grained Complexity › Bounded Search Trees for Vertex Cover

Bounded Search Trees for Vertex Cover

Backtracking is the oldest exact method for hard problems: make a choice, recurse, undo. In general its running time is hard to bound. The bounded search tree technique adds a discipline that makes the analysis easy: every branching step must decrease the parameter, and every node must branch into a bounded number of children. Then the tree has at most bkb^k leaves, where bb is the branching factor and kk the parameter, and the algorithm is FPT.

Vertex cover is the standard first example (Clique and Vertex Cover proves it NP-complete). We want to decide whether a graph G=(V,E)G = (V, E) has a vertex cover of at most kk vertices.

The algorithm

Take any edge uvuv. Every vertex cover contains uu or vv. So GG has a cover of size at most kk if and only if G−uG - u has one of size at most k−1k - 1 or G−vG - v has one of size at most k−1k - 1. Here G−uG - u deletes uu and all edges at uu: those edges are covered.

text
VC-BRANCH(G, k)
1  if G has no edges:  return YES
2  if k = 0:           return NO
3  pick any edge uv of G
4  if VC-BRANCH(G - u, k - 1) = YES:  return YES
5  return VC-BRANCH(G - v, k - 1)

Correctness. By induction on kk. Lines 1 and 2 are correct base cases: no edges means the empty set is a cover; with edges and no budget there is no cover. For line 3, any cover CC with ∣C∣≤k\lvert C \rvert \le k contains uu or vv; if it contains uu, then C∖{u}C \setminus \{u\} covers G−uG - u with at most k−1k - 1 vertices, and similarly for vv. Conversely a cover of G−uG - u plus uu covers GG. □\square

Running time. Every call either stops or makes two calls with parameter k−1k - 1, so the recursion tree is a binary tree of depth at most kk, with at most 2k+1−12^{k+1} - 1 nodes. Each node does O(n+m)O(n + m) work to delete a vertex, so the total is O(2k(n+m))O(2^k (n + m)). The running time does not depend on how the edge in line 3 is chosen; the size of the tree does, and later lessons choose cleverly.

To find a minimum cover rather than decide, call the algorithm for k=0,1,2,…k = 0, 1, 2, \ldots until it answers YES. The costs form a geometric series, so the total is still O(2k∗(n+m))O(2^{k^*}(n + m)) for the optimum k∗k^*.

Counting the search tree

The program implements VC-BRANCH on graphs stored as edge lists, records the number of tree nodes and the cover found, and checks the answer against exhaustive search over all vertex subsets of size at most kk.

python · try it yourself
import random
from itertools import combinations

def random_graph(n, p, seed):
    rng = random.Random(seed)
    return [(u, v) for u in range(n) for v in range(u + 1, n) if rng.random() < p]

def vc_branch(edges, k, stats):
    stats["nodes"] += 1
    if not edges:
        return []
    if k == 0:
        return None
    u, v = edges[0]
    for w in (u, v):
        rest = vc_branch([e for e in edges if w not in e], k - 1, stats)
        if rest is not None:
            return [w] + rest
    return None

def min_cover_brute(n, edges):
    for size in range(n + 1):
        for S in combinations(range(n), size):
            s = set(S)
            if all(a in s or b in s for a, b in edges):
                return size

print(" n  |E|  OPT   k  answer  nodes  bound 2^(k+1)-1")
for seed in range(4):
    n = 12
    E = random_graph(n, 0.25, seed)
    opt = min_cover_brute(n, E)
    for k in (opt - 1, opt):
        stats = {"nodes": 0}
        C = vc_branch(E, k, stats)
        ok = C is not None and all(a in C or b in C for a, b in E)
        line = f"{n:2} {len(E):4} {opt:4} {k:3}  {'YES' if C else 'NO ':3} {stats['nodes']:7} {2 ** (k + 1) - 1:9}"
        print(line + ("  valid cover" if ok else ""))
 n  |E|  OPT   k  answer  nodes  bound 2^(k+1)-1
12    8    4   3  NO       15        15
12    8    4   4  YES      27        31  valid cover
12   19    6   5  NO       63        63
12   19    6   6  YES      32       127  valid cover
12   12    5   4  NO       31        31
12   12    5   5  YES      28        63  valid cover
12   13    6   5  NO       63        63
12   13    6   6  YES      14       127  valid cover

When the answer is NO (here k=OPT−1k = \text{OPT} - 1) the algorithm must explore the whole tree, and the count is close to the bound. When it is YES, the search stops at the first leaf that succeeds, often much sooner. The bound is the same in both cases: it does not depend on nn.

In C++

An implementation with an adjacency matrix and a removed flag per vertex: deleting a vertex and restoring it on the way back costs O(1)O(1), and finding an edge costs O(n2)O(n^2) in the worst case. The same tree as in Python, explored for every kk up to the optimum on a 30-vertex graph.

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

int n;
std::vector<std::vector<bool>> adj;
std::vector<bool> removed;
long nodes = 0;

bool branch(int k) {
    nodes++;
    int eu = -1, ev = -1;
    for (int u = 0; u < n && eu < 0; u++)
        if (!removed[u])
            for (int v = u + 1; v < n; v++)
                if (!removed[v] && adj[u][v]) { eu = u; ev = v; break; }
    if (eu < 0) return true;
    if (k == 0) return false;
    for (int w : {eu, ev}) {
        removed[w] = true;
        bool ok = branch(k - 1);
        removed[w] = false;
        if (ok) return true;
    }
    return false;
}

int main() {
    n = 30;
    adj.assign(n, std::vector<bool>(n, false));
    removed.assign(n, false);
    unsigned s = 11;
    int m = 0;
    for (int u = 0; u < n; u++)
        for (int v = u + 1; v < n; v++) {
            s = s * 1103515245u + 12345u;
            if ((s >> 16) % 100 < 6) { adj[u][v] = adj[v][u] = true; m++; }
        }
    std::printf("n = %d, m = %d\n", n, m);
    for (int k = 0;; k++) {
        nodes = 0;
        bool ok = branch(k);
        std::printf("k = %2d: %s, %ld search-tree nodes\n", k, ok ? "YES" : "NO ", nodes);
        if (ok) break;
    }
}
n = 30, m = 31
k =  0: NO , 1 search-tree nodes
k =  1: NO , 3 search-tree nodes
k =  2: NO , 7 search-tree nodes
k =  3: NO , 15 search-tree nodes
k =  4: NO , 31 search-tree nodes
k =  5: NO , 63 search-tree nodes
k =  6: NO , 127 search-tree nodes
k =  7: NO , 255 search-tree nodes
k =  8: NO , 511 search-tree nodes
k =  9: NO , 1023 search-tree nodes
k = 10: NO , 2047 search-tree nodes
k = 11: YES, 3873 search-tree nodes

The node counts grow by a factor of about 2 per unit of kk while the answer is NO, as the analysis predicts.

Simple reduction rules shrink the tree

Branching on an arbitrary edge wastes effort. Two observations are free:

  • A vertex of degree 0 is never needed in a cover; delete it.
  • If vv has degree 1 with neighbour uu, some minimum cover contains uu rather than vv (swap vv for uu in any cover; uu covers everything vv did). So take uu, delete it, and lower kk by one, without branching.

These are reduction rules: polynomial-time transformations that keep the answer and never increase kk. Applied exhaustively at every node they leave a graph of minimum degree 2. The next lesson pushes further and branches only on vertices of degree at least 3, which gives a provably smaller tree. Here is the effect of the degree-1 rule alone on sparse random graphs, where degree-1 vertices are common.

python · try it yourself
import random

def random_graph(n, p, seed):
    rng = random.Random(seed)
    return [(u, v) for u in range(n) for v in range(u + 1, n) if rng.random() < p]

def plain(edges, k, stats):
    stats[0] += 1
    if not edges:
        return True
    if k == 0:
        return False
    u, v = edges[0]
    return any(plain([e for e in edges if w not in e], k - 1, stats) for w in (u, v))

def with_rule(edges, k, stats):
    stats[0] += 1
    while edges:                                     # degree-1 rule, applied exhaustively
        deg = {}
        for a, b in edges:
            deg[a] = deg.get(a, 0) + 1
            deg[b] = deg.get(b, 0) + 1
        leaf = next((x for x in sorted(deg) if deg[x] == 1), None)
        if leaf is None or k == 0:
            break
        u = next(b if a == leaf else a for a, b in edges if leaf in (a, b))
        edges = [e for e in edges if u not in e]
        k -= 1
    if not edges:
        return True
    if k == 0:
        return False
    u, v = edges[0]
    return any(with_rule([e for e in edges if w not in e], k - 1, stats) for w in (u, v))

def min_k(edges, solver):
    k, stats = 0, [0]                                # stats[0] counts nodes over all calls
    while not solver(edges, k, stats):
        k += 1
    return k, stats[0]

print(" n  |E|  OPT  nodes(plain)  nodes(degree-1 rule)")
for seed in range(5):
    E = random_graph(30, 0.07, seed)
    k1, a = min_k(E, plain)
    k2, b = min_k(E, with_rule)
    assert k1 == k2
    print(f"30 {len(E):4} {k1:4} {a:13} {b:21}")
 n  |E|  OPT  nodes(plain)  nodes(degree-1 rule)
30   37   14         33247                    94
30   37   14         39011                    26
30   27   12          8213                    13
30   31   11          4876                    12
30   33   12         16226                    13

Each row counts all nodes over the calls k=0,1,…,OPTk = 0, 1, \ldots, \text{OPT}. The rule removes whole pendant paths and trees without branching, so the counts drop sharply, and the answers agree.

Bounded search trees in general

The pattern works whenever a small obstruction must be hit by any solution:

  • dd-HITTING SET: sets of size at most dd, choose kk elements meeting all of them. Branch on the dd elements of an unhit set: O∗(dk)O^*(d^k).
  • CLUSTER EDITING: an induced path on three vertices must be destroyed by one of three edits: O∗(3k)O^*(3^k) (Cluster Editing and Feedback Vertex Set by Branching).
  • Puzzles: solvers for N-Queens or Sudoku branch on the possible values of a cell. There the number of choices per level is bounded, but no parameter bounds the depth below the input size, so no FPT bound follows.

What makes a bound FPT is that both the branching factor and the depth are functions of kk only.

Test yourself

Why may VC-BRANCH branch on the two endpoints of any edge uvuv?

  1. Because uu and vv have maximum degree
  2. Because every vertex cover contains uu or vv
  3. Because removing an edge preserves the cover size
  4. Because the graph is bipartite

What is the running time of VC-BRANCH on a graph with nn vertices and mm edges?

  1. O(nk)O(n^k)
  2. O(2k(n+m))O(2^k (n + m))
  3. O(kn)O(k^n)
  4. O(2n)O(2^n)

Why is it safe to put the neighbour uu of a degree-1 vertex vv into the cover without branching?

  1. Because uu always has degree 1 too
  2. Because any cover containing vv stays a cover of the same size after swapping vv for uu
  3. Because vv is never in a minimum cover of any graph
  4. Because it lowers nn

When the answer is NO, why does the search tree come close to its bound 2k+1−12^{k+1} - 1 nodes?

  1. Because it explores branches in random order
  2. Because it must rule out every branch before answering NO, so no part of the tree can be skipped
  3. Because the graph grows during the search
  4. It does not; NO answers are fast