TExcelTAD
Home › Approximation Algorithms › Vertex Cover from a Maximal Matching

Vertex Cover from a Maximal Matching

A vertex cover of a graph G=(V,E)G = (V, E) is a set C⊆VC \subseteq V 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

text
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 C

The edges added in line 4 form a maximal matching MM: no two share an endpoint, and every other edge touches one of them (otherwise it would have been added). The algorithm runs in O(∣V∣+∣E∣)O(\lvert V \rvert + \lvert E \rvert) time.

Theorem. CC is a vertex cover with ∣C∣=2∣M∣≤2 OPT\lvert C \rvert = 2\lvert M \rvert \le 2\,\text{OPT}.

Proof. An edge that was not added has an endpoint already in CC, so CC covers every edge. An optimal cover C∗C^* must contain an endpoint of every edge of MM; those endpoints are distinct because MM is a matching, so OPT=∣C∗∣≥∣M∣\text{OPT} = \lvert C^* \rvert \ge \lvert M \rvert. □\square

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 Kn,nK_{n,n} every maximal matching is perfect, so the algorithm takes all 2n2n vertices, while one side of nn vertices is a cover. On a disjoint union of nn edges the same happens with OPT =n= n. Even the lower bound itself is off by a factor of 2: on the complete graph K2k+1K_{2k+1} a maximum matching has kk edges but every cover needs 2k2k vertices.

python · try it yourself
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.

cpp · try it yourself
#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 HΔ=O(log⁡Δ)H_{\Delta} = O(\log \Delta), and that is tight. Take kk "left" vertices; for each i=2,…,ki = 2, \ldots, k add ⌊k/i⌋\lfloor k/i \rfloor "right" vertices, each joined to its own block of ii left vertices. Left vertices have degree at most k−1k - 1, 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 ∑i=2k⌊k/i⌋≈kln⁡k\sum_{i=2}^{k} \lfloor k/i \rfloor \approx k \ln k right vertices, while the kk left vertices are a cover.

python · try it yourself
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 2k2k here), while the "smarter" greedy drifts upward like ln⁡k\ln k. 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 min⁡∑vxv\min \sum_v x_v subject to xu+xv≥1x_u + x_v \ge 1 for each edge, x≥0x \ge 0. Its dual is the fractional matching LP: max⁡∑eye\max \sum_e y_e subject to ∑e∋vye≤1\sum_{e \ni v} y_e \le 1 for each vertex, y≥0y \ge 0. A matching MM is an integral dual solution (ye=1y_e = 1 on MM), so ∣M∣≤LP≤OPT\lvert M \rvert \le \text{LP} \le \text{OPT}, and the algorithm's cover CC satisfies ∣C∣=2∑eye\lvert C \rvert = 2 \sum_e y_e. 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 2−ε2 - \varepsilon for a constant ε>0\varepsilon > 0 is known; the best known ratio is 2−Θ(1/log⁡n)2 - \Theta(1/\sqrt{\log n}) (Karakostas, 2009). On the hardness side:

  • Unless P = NP, there is no 1.36061.3606-approximation (Dinur and Safra, 2005), and no (2−ε)(\sqrt{2} - \varepsilon)-approximation for any ε>0\varepsilon > 0 (Khot, Minzer and Safra, 2018, building on the 2-to-2 games theorem).
  • Assuming the Unique Games Conjecture, no (2−ε)(2 - \varepsilon)-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?

  1. Every vertex has degree at most n−1n - 1
  2. Every cover contains a distinct endpoint of each edge of a matching, so OPT≥∣M∣\text{OPT} \ge \lvert M \rvert
  3. The algorithm picks vertices of maximum degree
  4. The optimal cover is a matching

On which graph does the matching algorithm return exactly twice the optimum, whatever the edge order?

  1. A star K1,nK_{1,n}
  2. The complete bipartite graph Kn,nK_{n,n}
  3. A path with three vertices
  4. A triangle

What is the worst-case ratio of the greedy rule 'take a vertex of maximum remaining degree'?

  1. 2
  2. Θ(log⁡Δ)\Theta(\log \Delta): it is greedy set cover, and bipartite examples force a ratio near ln⁡Δ\ln \Delta
  3. 1, it is optimal
  4. 32\frac{3}{2}

Under which assumption is it known that vertex cover has no (2−ε)(2 - \varepsilon)-approximation?

  1. P ≠\ne NP alone
  2. The Unique Games Conjecture (Khot and Regev, 2008)
  3. The exponential time hypothesis
  4. It is known unconditionally