Sub topic 4
A 2-Approximation for Vertex Cover
This chapter assumes you know what a matching is, can read short Python functions, and remember the approximation ratio from the previous chapter. It applies the lower-bounding idea from the last chapter to its first concrete problem.
The Problem
A vertex cover of a graph is a set of vertices that touches every edge. In other words, each edge has at least one endpoint in the set. The goal is to find a smallest such set. Deciding whether a cover of size k exists is NP-complete, so no polynomial-time exact algorithm is expected.
A matching is a set of edges with no shared endpoints. It is maximal if you cannot add another edge without breaking that rule. A maximal matching is not the same as a maximum matching, and the algorithm only needs a maximal one.
The Algorithm
Scan the edges. When an edge has neither endpoint in the cover yet, add both endpoints.
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}. Edges (0,1) and (2,3) trigger additions. Edges (1,2) and (3,0) are already covered. The algorithm runs in O(|E|) time.
Why the Ratio Is 2
Let M be the set of edges that caused additions. The algorithm returns exactly 2|M| vertices.
- The output is a valid cover. Every edge either joined M, or it was skipped because one of its endpoints was already in the cover.
- M is a matching. An edge joins M only when both of its endpoints are new, so no two edges in M share a vertex.
- The optimum is at least |M|. Any cover C must contain an endpoint of each edge in M. These edges are disjoint, so those endpoints are all different, which gives |C| ≥ |M|.
- Conclusion. |output| = 2|M| ≤ 2|C*|.
The key step is the lower bound in point 3. You never compute the optimum. You only show that the optimum must be at least |M|.
The bound is tight. On the 4-cycle, and on any graph that has a perfect matching but a cover only half as big, the algorithm returns exactly twice the optimum.
Common Errors
| Mistake | What goes wrong |
|---|---|
| Adding only one endpoint per uncovered edge | The lower-bound argument no longer applies, and an unlucky choice of endpoint can give a much larger cover |
| Greedily picking the highest-degree vertex | Seems smarter but has only an O(log n) guarantee, and some graphs force that much |
| Using this on weighted graphs | Two cheap-looking endpoints may include a very expensive vertex, so the ratio fails |
| Computing a maximum matching | Unnecessary work. Any maximal matching is enough for the proof |
Exercise
Run the algorithm on the star graph with center 0 and leaves 1 through 5, given as edges [(0,1),(0,2),(0,3),(0,4),(0,5)]. What does it return, what is the optimum, and what is the ratio?
Show answer
The first edge adds {0, 1}. Every other edge then contains 0, so it is skipped. The output has 2 vertices. The optimum is {0} with 1 vertex, so the ratio is 2. This matches the bound because |M| = 1.
Exercise
Why does the proof need the edges in M to be pairwise disjoint?
Show answer
Disjointness means that the endpoints the optimal cover uses for the edges in M are all different vertices. That gives |C*| ≥ |M|. If two edges in M shared a vertex, one vertex could cover both, and the lower bound would fail.