Vertex Cover from a Maximal Matching
A vertex cover of a graph is a set containing at least one endpoint of every edge. Finding a smallest one is NP-hard (Clique and Vertex Cover), and it was the first example in Coping with NP-Hardness. This lesson goes further: why the obvious greedy idea fails, why the matching algorithm's factor 2 is tight, how the analysis looks through LP duality, and what is known about doing better.
The algorithm
MATCHING-COVER(G)
1 M = empty matching, C = empty set
2 for each edge (u, v) of G, in any order
3 if neither u nor v is in C
4 add (u, v) to M; add u and v to C
5 return CThe edges added in line 4 form a maximal matching : no two share an endpoint, and every other edge touches one of them (otherwise it would have been added). The algorithm runs in time.
Theorem. is a vertex cover with .
Proof. An edge that was not added has an endpoint already in , so covers every edge. An optimal cover must contain an endpoint of every edge of ; those endpoints are distinct because is a matching, so .
The proof never needs to know OPT. It uses one lower bound, the size of a matching, and an algorithm whose cost is at most twice that bound.
Tight examples
The factor 2 cannot be improved for this algorithm. On the complete bipartite graph every maximal matching is perfect, so the algorithm takes all vertices, while one side of vertices is a cover. On a disjoint union of edges the same happens with OPT . Even the lower bound itself is off by a factor of 2: on the complete graph a maximum matching has edges but every cover needs vertices.
import random
from itertools import combinations
def matching_cover(edges):
C, M = set(), []
for u, v in edges:
if u not in C and v not in C:
M.append((u, v))
C |= {u, v}
return C, M
def opt_cover(n, edges):
for k in range(n + 1):
for S in combinations(range(n), k):
s = set(S)
if all(u in s or v in s for u, v in edges):
return k
rng = random.Random(5)
worst = 0
print(" n |E| |M| ALG OPT ratio")
for _ in range(6):
n = rng.randint(8, 11)
E = [(u, v) for u in range(n) for v in range(u + 1, n) if rng.random() < 0.3]
rng.shuffle(E)
C, M = matching_cover(E)
o = opt_cover(n, E)
worst = max(worst, len(C) / o)
print(f"{n:2} {len(E):4} {len(M):4} {len(C):4} {o:4} {len(C) / o:6.2f}")
K = [(i, 5 + j) for i in range(5) for j in range(5)] # K_{5,5}
C, M = matching_cover(K)
print("K_{5,5}: ALG", len(C), " OPT", opt_cover(10, K))
print("worst random ratio", round(worst, 2)) n |E| |M| ALG OPT ratio
10 18 4 8 5 1.60
10 14 4 8 5 1.60
8 9 3 6 3 2.00
10 12 3 6 4 1.50
11 15 4 8 5 1.60
9 11 2 4 4 1.00
K_{5,5}: ALG 10 OPT 5
worst random ratio 2.0
In C++
The algorithm needs only one pass over the edge list and a boolean array.
#include <cstdio>
#include <utility>
#include <vector>
int main() {
int n = 10;
std::vector<std::pair<int, int>> edges = {{0, 1}, {0, 2}, {1, 3}, {2, 3}, {3, 4}, {4, 5}, {5, 6},
{6, 7}, {7, 8}, {8, 9}, {9, 5}, {2, 8}};
std::vector<bool> in_cover(n, false);
int matched = 0;
for (auto [u, v] : edges) {
if (!in_cover[u] && !in_cover[v]) {
in_cover[u] = in_cover[v] = true;
matched++;
std::printf("match (%d,%d)\n", u, v);
}
}
std::printf("cover:");
for (int v = 0; v < n; v++) if (in_cover[v]) std::printf(" %d", v);
std::printf("\n|M| = %d, |C| = %d, so OPT >= %d and |C| <= 2 OPT\n", matched, 2 * matched, matched);
}match (0,1)
match (2,3)
match (4,5)
match (6,7)
match (8,9)
cover: 0 1 2 3 4 5 6 7 8 9
|M| = 5, |C| = 10, so OPT >= 5 and |C| <= 2 OPT
Why "take the highest-degree vertex" is worse
Greedily taking a vertex that covers the most uncovered edges sounds smarter, and it is exactly greedy set cover with the edges as elements (Greedy Set Cover). Its ratio is only , and that is tight. Take "left" vertices; for each add "right" vertices, each joined to its own block of left vertices. Left vertices have degree at most , so greedy (breaking ties towards the right side) takes a right vertex of the largest group, after which the left degrees drop and the pattern repeats. It ends up taking all right vertices, while the left vertices are a cover.
def bad_graph(k):
E, r = [], 0
for i in range(2, k + 1):
for g in range(k // i):
E += [(("L", g * i + t), ("R", r)) for t in range(i)]
r += 1
return E
def max_degree_greedy(E):
E, cover = set(E), []
while E:
deg = {}
for u, v in E:
deg[u] = deg.get(u, 0) + 1
deg[v] = deg.get(v, 0) + 1
x = max(deg, key=lambda x: (deg[x], x[0] == "R", -x[1])) # ties go to the right side
cover.append(x)
E = {e for e in E if x not in e}
return cover
def matching_size(E):
C, M = set(), 0
for u, v in E:
if u not in C and v not in C:
C |= {u, v}
M += 1
return 2 * M
for k in [6, 12, 24, 48]:
E = bad_graph(k)
g = max_degree_greedy(E)
print(f"k = {k:2}: OPT <= {k:2}, max-degree greedy {len(g):3} (ratio {len(g) / k:.2f}), matching cover {matching_size(E):3}")k = 6: OPT <= 6, max-degree greedy 8 (ratio 1.33), matching cover 12
k = 12: OPT <= 12, max-degree greedy 23 (ratio 1.92), matching cover 24
k = 24: OPT <= 24, max-degree greedy 60 (ratio 2.50), matching cover 48
k = 48: OPT <= 48, max-degree greedy 150 (ratio 3.12), matching cover 96
The matching algorithm stays within 2 (it can never exceed here), while the "smarter" greedy drifts upward like . A good approximation algorithm is one whose cost is tied to a good lower bound, not one that looks locally clever.
The analysis as LP duality
Relax vertex cover to the LP subject to for each edge, . Its dual is the fractional matching LP: subject to for each vertex, . A matching is an integral dual solution ( on ), so , and the algorithm's cover satisfies . This is the simplest instance of the primal-dual pattern: build a dual solution and an integral primal solution together, and pay for the primal with a constant times the dual (The Primal-Dual Method for Vertex Cover).
For bipartite graphs vertex cover is solvable exactly: by König's theorem the minimum cover equals the maximum matching (Bipartite Matching and König's Theorem), which the practice problem Maximum Bipartite Matching computes.
Can 2 be beaten?
No algorithm with ratio for a constant is known; the best known ratio is (Karakostas, 2009). On the hardness side:
- Unless P = NP, there is no -approximation (Dinur and Safra, 2005), and no -approximation for any (Khot, Minzer and Safra, 2018, building on the 2-to-2 games theorem).
- Assuming the Unique Games Conjecture, no -approximation exists (Khot and Regev, 2008); see The Unique Games Conjecture.
So the simplest algorithm of the course may well be the best possible.
Test yourself
Which fact gives the lower bound in the analysis of the matching-based vertex cover algorithm?
- Every vertex has degree at most
- Every cover contains a distinct endpoint of each edge of a matching, so
- The algorithm picks vertices of maximum degree
- The optimal cover is a matching
On which graph does the matching algorithm return exactly twice the optimum, whatever the edge order?
- A star
- The complete bipartite graph
- A path with three vertices
- A triangle
What is the worst-case ratio of the greedy rule 'take a vertex of maximum remaining degree'?
- 2
- : it is greedy set cover, and bipartite examples force a ratio near
- 1, it is optimal
Under which assumption is it known that vertex cover has no -approximation?
- P NP alone
- The Unique Games Conjecture (Khot and Regev, 2008)
- The exponential time hypothesis
- It is known unconditionally