Sub topic 4
A 2-Approximation for Vertex Cover
This chapter assumes you know what a matching is in a graph and that minimum vertex cover is NP-hard. It also builds on the previous chapter's idea of proving a guarantee by comparing the algorithm's output with a lower bound on the optimum, written OPT. Here that idea becomes a complete algorithm. The algorithm is short, but the reason it works applies well beyond vertex cover.
The Problem
A vertex cover of an undirected graph G = (V, E) is a set C ⊆ V such that every edge has at least one endpoint in C. The minimum vertex cover problem asks for a cover with as few vertices as possible. Its decision version ("is there a cover of size at most k?") is one of Karp's original NP-complete problems. Unless P = NP, no polynomial-time algorithm finds the minimum on every graph.
Vertex cover shows up whenever you must "touch" every pairwise conflict. Examples include placing monitors on network links, choosing which of two conflicting tasks to drop, or picking a set of genes that hits every interaction in a pathway.
We want a polynomial-time algorithm that always returns a valid cover of size at most 2 · OPT.
Lower Bounds from Matchings
Proving a ratio requires a lower bound on OPT that we can compute. Matchings provide one.
A matching M is a set of edges in which no two edges share an endpoint. Take any vertex cover C, including an optimal one. Every edge of M must have an endpoint in C. The edges of M are pairwise disjoint, so no single vertex of C can cover two of them. Therefore:
- |C| ≥ |M| for every vertex cover C and every matching M.
- In particular, OPT ≥ |M| for every matching M.
This holds for any matching, even a small one. If we can build a matching and a cover at the same time, with the cover's size tied to the matching's size, the proof follows.
The Algorithm
A matching is maximal if you cannot add another edge to it. Every edge outside M then shares an endpoint with some edge in M. A maximal matching is not necessarily a maximum matching, which is the largest possible one. A greedy pass over the edges finds a maximal matching in linear time. A maximum matching needs a more involved algorithm.
The algorithm:
- Start with an empty cover C.
- Scan the edges in any order. If an edge (u, v) has neither endpoint in C, add both u and v to C.
- Return C.
The edges that trigger step 2 form a maximal matching M. Each one is disjoint from the earlier ones, because its endpoints were not yet in C. Every other edge was skipped because it already touched C.
def vertex_cover_2approx(edges):
cover = set()
for u, v in edges:
if u not in cover and v not in cover:
cover.add(u)
cover.add(v)
return cover
print(vertex_cover_2approx([(0, 1), (1, 2), (2, 3), (3, 0)]))On the 4-cycle this prints {0, 1, 2, 3}. Edge (0, 1) is matched. Edges (1, 2) and (3, 0) are skipped because they touch the cover. Edge (2, 3) is matched. The optimal cover is {0, 2} (or {1, 3}), of size 2. On this input the algorithm uses exactly twice the optimum.
The Proof
Validity. Take any edge e. If e was matched, both its endpoints are in C. If it was skipped, then at the moment it was scanned, at least one endpoint was already in C, and vertices are never removed. So every edge is covered.
Ratio. Let M be the set of matched edges. C contains exactly the 2|M| endpoints of M, and those endpoints are distinct because M is a matching. So |C| = 2|M| ≤ 2 · OPT.
Running time. Each edge is examined once, with constant-time set lookups on average. That gives O(|V| + |E|) total.
The proof never uses the value of OPT. It uses only a quantity, |M|, that the algorithm produced itself and that is provably no larger than OPT. This pattern of building a certificate that bounds the optimum while building the solution returns later in LP rounding and primal-dual methods.
The Bound Is Tight
Can a better analysis improve the factor 2? No. Some inputs force exactly 2 · OPT.
- The 4-cycle, as shown above: output 4, OPT 2.
- The complete bipartite graph K_{n,n}. The algorithm finds a perfect matching of n edges and returns all 2n vertices. One side alone, n vertices, covers every edge.
- A perfect matching graph (n disjoint edges) is not tight. The algorithm returns 2n vertices, but OPT is n, which is the same ratio of 2. Any family of graphs where OPT equals the maximal matching size reaches the bound.
So 2 is the true worst-case ratio of this algorithm, not an artifact of a loose proof.
Checking the Guarantee Empirically
A brute-force check on small random graphs confirms both validity and the ratio. It is also a useful habit for catching implementation bugs in approximation code.
import itertools
import random
def vertex_cover_2approx(edges):
cover = set()
for u, v in edges:
if u not in cover and v not in cover:
cover.add(u)
cover.add(v)
return cover
def is_cover(cover, edges):
return all(u in cover or v in cover for u, v in edges)
def optimal_cover_size(n, edges):
for k in range(n + 1):
for subset in itertools.combinations(range(n), k):
if is_cover(set(subset), edges):
return k
return n
rng = random.Random(1)
worst = 0.0
for _ in range(300):
n = rng.randint(2, 9)
edges = [(u, v) for u in range(n) for v in range(u + 1, n)
if rng.random() < 0.4]
if not edges:
continue
alg = vertex_cover_2approx(edges)
assert is_cover(alg, edges)
opt = optimal_cover_size(n, edges)
assert len(alg) <= 2 * opt
worst = max(worst, len(alg) / opt)
print(f"worst observed ratio: {worst:.2f}")The assertions never fail, and the printed worst ratio is at most 2.00. On random graphs the typical ratio is well below 2. Worst-case guarantees are often pessimistic in practice.
Common Errors
Adding Only One Endpoint
A tempting change is to add just one endpoint of each uncovered edge, because it seems to waste fewer vertices. Without a rule for choosing, this has no constant guarantee. On a star with center c and leaves ℓ₁, …, ℓₙ, the edge (ℓᵢ, c) might always contribute the leaf. The algorithm then returns n leaves, while OPT = {c} has size 1. Adding both endpoints is what makes the matching argument work: one of the two endpoints is guaranteed to be "paid for" by OPT.
Greedy by Highest Degree
Repeatedly picking the vertex of highest remaining degree sounds smarter and often performs well. Its worst-case ratio, however, grows like Θ(log n). There are layered bipartite graphs that trick it into taking a large side when a small one suffices. Intuition about "good choices" is not a proof, and this heuristic has none for a constant factor.
Confusing Maximal with Maximum
The algorithm needs only a maximal matching. Computing a maximum matching costs more and improves nothing in the worst case: OPT ≥ |M| holds for any matching, and the cover is still 2|M|. In bipartite graphs, König's theorem says the minimum vertex cover size equals the maximum matching size. There a maximum matching gives an exact answer through a different construction, not by taking both endpoints.
Applying It to Weighted Vertex Cover
If vertices have weights and the goal is minimum total weight, this algorithm fails badly. A single matched edge might join a cheap vertex to an extremely expensive one, and the algorithm takes both. The matching bound counts vertices, not weight. Weighted vertex cover needs a different lower bound. A later chapter gets it from an LP relaxation.
Edge Cases in the Implementation
| Input | Behavior | Notes | Column | Column |
|---|---|---|---|---|
| No edges | Returns the empty set | Correct: OPT = 0 | ||
| Isolated vertices | Never added | They do not appear in the edge list | ||
| Self-loop (u, u) | Adds u once | u must be in every cover, so this is safe; the "2 | M | " count becomes loose but the bound still holds |
| Parallel edges | Second copy is skipped | Already covered | ||
| Different edge order | Different cover, same bound | The output depends on order; the guarantee does not |
How Good Is 2?
Despite decades of work, no polynomial-time algorithm achieves 2 − ε for a constant ε > 0. The best known ratio is 2 − Θ(1/√log n). On the hardness side, approximating vertex cover within a factor below √2 is NP-hard. Under the Unique Games Conjecture, a factor of 2 − ε is hard for every ε > 0. If that conjecture holds, this simple matching algorithm is essentially the best possible. Later chapters on hardness of approximation explain what these results mean.
Exercise
Run vertexcover2approx on the path 0–1–2–3–4 with edges given in the order (1, 2), (3, 4), (0, 1), (2, 3). What cover does it return, what is OPT, and what is the ratio?
Show answer
Edge (1, 2) is matched, adding {1, 2}. Edge (3, 4) is matched, adding {3, 4}. Edge (0, 1) is skipped because 1 is in the cover, and (2, 3) is skipped because 2 is. The output is {1, 2, 3, 4}, of size 4. OPT is 2, for example {1, 3}. The ratio is exactly 2.
Exercise
Explain why the proof still works if the algorithm scans the edges in an adversarially chosen order, and identify the single fact about the matched edges that the proof depends on.
Show answer
The proof uses only two facts. The matched edges form a matching, meaning they are pairwise disjoint, and every unmatched edge touches the cover. Both hold for any scan order, because an edge is matched only when neither endpoint is in the cover yet. Disjointness gives OPT ≥ |M|, and the construction gives |C| = 2|M|. Order changes which matching is found, but never these two facts.
Exercise
Give a weighted graph with two vertices where the matching algorithm returns a cover whose weight is more than 100 times the minimum weight.
Show answer
Take one edge (u, v) with w(u) = 1 and w(v) = 1000. The algorithm matches the edge and returns {u, v} with weight 1001. The minimum-weight cover is {u}, of weight 1. The ratio is 1001, which shows the unweighted matching bound does not extend to weights.