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 leaves, where is the branching factor and 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 has a vertex cover of at most vertices.
The algorithm
Take any edge . Every vertex cover contains or . So has a cover of size at most if and only if has one of size at most or has one of size at most . Here deletes and all edges at : those edges are covered.
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 . 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 with contains or ; if it contains , then covers with at most vertices, and similarly for . Conversely a cover of plus covers .
Running time. Every call either stops or makes two calls with parameter , so the recursion tree is a binary tree of depth at most , with at most nodes. Each node does work to delete a vertex, so the total is . 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 until it answers YES. The costs form a geometric series, so the total is still for the optimum .
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 .
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 ) 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 .
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 , and finding an edge costs in the worst case. The same tree as in Python, explored for every up to the optimum on a 30-vertex graph.
#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 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 has degree 1 with neighbour , some minimum cover contains rather than (swap for in any cover; covers everything did). So take , delete it, and lower by one, without branching.
These are reduction rules: polynomial-time transformations that keep the answer and never increase . 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.
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 . 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:
- -HITTING SET: sets of size at most , choose elements meeting all of them. Branch on the elements of an unhit set: .
- CLUSTER EDITING: an induced path on three vertices must be destroyed by one of three edits: (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 only.
Test yourself
Why may VC-BRANCH branch on the two endpoints of any edge ?
- Because and have maximum degree
- Because every vertex cover contains or
- Because removing an edge preserves the cover size
- Because the graph is bipartite
What is the running time of VC-BRANCH on a graph with vertices and edges?
Why is it safe to put the neighbour of a degree-1 vertex into the cover without branching?
- Because always has degree 1 too
- Because any cover containing stays a cover of the same size after swapping for
- Because is never in a minimum cover of any graph
- Because it lowers
When the answer is NO, why does the search tree come close to its bound nodes?
- Because it explores branches in random order
- Because it must rule out every branch before answering NO, so no part of the tree can be skipped
- Because the graph grows during the search
- It does not; NO answers are fast