TExcelTAD
Home › Approximation Algorithms › The Primal-Dual Method for Vertex Cover

The Primal-Dual Method for Vertex Cover

LP rounding solves an LP first and then looks at it. The primal-dual method never solves the LP. It grows a feasible solution of the dual LP step by step and, guided by which dual constraints become tight, builds an integral primal solution at the same time. The dual solution is a lower bound on OPT that comes for free, so the algorithm certifies its own ratio. The method is fast (often linear time), purely combinatorial, and the source of the best algorithms for network design problems (Primal-Dual for Steiner Forest) and facility location (Uncapacitated Facility Location). Weighted vertex cover is the cleanest place to learn it.

The LP pair

For a graph with vertex weights wv≥0w_v \ge 0:

(P)min⁡∑vwvxvs.t.xu+xv≥1  ∀uv∈E,x≥0,(D)max⁡∑eyes.t.∑e∋vye≤wv  ∀v∈V,y≥0.\begin{aligned} (P) \quad & \min \sum_{v} w_v x_v && \text{s.t.} \quad x_u + x_v \ge 1 \ \ \forall uv \in E, \quad x \ge 0, \\ (D) \quad & \max \sum_{e} y_e && \text{s.t.} \quad \sum_{e \ni v} y_e \le w_v \ \ \forall v \in V, \quad y \ge 0 . \end{aligned}

Think of yey_e as money an edge offers to get covered, and of wvw_v as the price of a vertex: the dual constraint says no vertex receives more than its price. By weak duality (Weak and Strong Duality), ∑eye≤LP≤OPT\sum_e y_e \le \text{LP} \le \text{OPT} for every feasible yy.

The algorithm

text
PRIMAL-DUAL-VC(G, w)
1  y_e = 0 for every edge;  C = empty
2  while some edge uv has neither endpoint in C
3      raise y_uv as far as possible:  by  min( slack(u), slack(v) )
4             where slack(v) = w_v - (sum of y_e over edges e at v)
5      add every endpoint whose slack is now 0 (it is "tight") to C
6  return C and y

Each iteration makes at least one more vertex tight, so there are at most ∣V∣\lvert V \rvert iterations, and with the edges scanned once in any order the algorithm runs in O(∣V∣+∣E∣)O(\lvert V \rvert + \lvert E \rvert) time. It is due to Bar-Yehuda and Even (1981).

Theorem. The algorithm returns a vertex cover CC and a feasible dual yy with w(C)≤2∑eye≤2 OPTw(C) \le 2\sum_e y_e \le 2\,\text{OPT}.

Proof. The loop ends only when every edge has an endpoint in CC, so CC is a cover. Each raise is limited by the slacks of both endpoints, so no dual constraint is violated: yy is feasible. Every vertex in CC is tight, wv=∑e∋vyew_v = \sum_{e \ni v} y_e, so

w(C)=∑v∈C∑e∋vye=∑eye⋅∣e∩C∣≤2∑eye≤2 OPT,w(C) = \sum_{v \in C} \sum_{e \ni v} y_e = \sum_{e} y_e \cdot \lvert e \cap C \rvert \le 2 \sum_e y_e \le 2\,\text{OPT},

because an edge has only two endpoints. □\square

The view through complementary slackness

An optimal primal-dual pair satisfies complementary slackness (Complementary Slackness and Optimality Certificates): xv>0⇒x_v > 0 \Rightarrow the dual constraint of vv is tight, and ye>0⇒xu+xv=1y_e > 0 \Rightarrow x_u + x_v = 1. The algorithm keeps the primal conditions exactly (it only takes tight vertices) and relaxes the dual ones by a factor 2 (ye>0⇒xu+xv≤2y_e > 0 \Rightarrow x_u + x_v \le 2, trivially true). That is the general recipe:

If xx is integral and primal feasible, yy is dual feasible, primal slackness holds exactly and dual slackness holds up to a factor β\beta (every ye>0y_e > 0 has its primal row satisfied at most β\beta times over), then ALG≤β∑eye≤β OPT\text{ALG} \le \beta \sum_e y_e \le \beta\,\text{OPT}.

For set cover, the same algorithm (raise the dual variable of an uncovered element until some set containing it becomes tight, take that set) gives β=f\beta = f, the maximum number of sets containing an element, matching the LP rounding bound of Rounding the Vertex Cover LP without solving any LP.

python · try it yourself
import random
from itertools import combinations
from scipy.optimize import linprog

def primal_dual(n, E, w):
    slack, y, C = list(w), {}, set()
    for u, v in E:
        if u in C or v in C:
            continue
        d = min(slack[u], slack[v])
        y[(u, v)] = d
        slack[u] -= d
        slack[v] -= d
        C |= {x for x in (u, v) if slack[x] == 0}
    return C, y

def opt_cover(n, E, w):
    return min(sum(w[v] for v in S) for k in range(n + 1) for S in combinations(range(n), k)
               if all(u in S or v in S for u, v in E))

rng = random.Random(10)
print(" n  |E|  dual sum   LP   ALG   OPT  ALG/OPT  ALG/dual")
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.4]
    w = [rng.randint(1, 12) for _ in range(n)]
    C, y = primal_dual(n, E, w)
    assert all(sum(y.get(e, 0) for e in E if v in e) <= w[v] for v in range(n))   # dual feasible
    lp = linprog(w, A_ub=[[-1 if v in e else 0 for v in range(n)] for e in E], b_ub=[-1] * len(E), method="highs").fun
    alg, opt, dual = sum(w[v] for v in C), opt_cover(n, E, w), sum(y.values())
    print(f"{n:2} {len(E):4} {dual:9} {float(lp):5.1f} {alg:5} {opt:5} {alg / opt:8.3f} {alg / dual:9.3f}")
 n  |E|  dual sum   LP   ALG   OPT  ALG/OPT  ALG/dual
 8   10        19  23.0    31    23    1.348     1.632
11   22        37  42.0    66    42    1.571     1.784
 9   16        24  25.5    43    27    1.593     1.792
 9   11        30  31.0    51    32    1.594     1.700
10   14        25  26.0    42    26    1.615     1.680
 9   10        18  18.0    31    18    1.722     1.722

The last column is the ratio the algorithm can prove about its own output without knowing OPT: it never exceeds 2, and it is usually larger than the true ratio in the column before, since the dual sum is a weaker lower bound than OPT.

In C++

The same algorithm, printing the dual values it raises and the final certificate.

cpp · try it yourself
#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

int main() {
    std::vector<int> w = {4, 3, 5, 2, 6, 3};
    std::vector<std::pair<int, int>> E = {{0, 1}, {0, 2}, {1, 2}, {1, 3}, {2, 4}, {3, 4}, {3, 5}, {4, 5}};
    std::vector<int> slack = w;
    std::vector<bool> inC(w.size(), false);
    int dual = 0;
    for (auto [u, v] : E) {
        if (inC[u] || inC[v]) continue;
        int d = std::min(slack[u], slack[v]);
        slack[u] -= d;
        slack[v] -= d;
        dual += d;
        std::printf("y(%d,%d) = %d\n", u, v, d);
        if (slack[u] == 0) inC[u] = true;
        if (slack[v] == 0) inC[v] = true;
    }
    int cost = 0;
    std::printf("cover:");
    for (size_t v = 0; v < w.size(); v++) if (inC[v]) { std::printf(" %zu", v); cost += w[v]; }
    std::printf("\ncost %d <= 2 * dual = %d, so cost <= 2 OPT\n", cost, 2 * dual);
}
y(0,1) = 3
y(0,2) = 1
y(2,4) = 4
y(3,4) = 2
cover: 0 1 2 3 4
cost 20 <= 2 * dual = 20, so cost <= 2 OPT

The cover meets the bound exactly. It is also wasteful: vertex 0 is redundant, since vertices 1 and 2 already cover both of its edges. A cleanup pass that drops vertices whose edges are all covered by others never hurts the analysis and often helps in practice; for Steiner forest this reverse delete step becomes essential to the guarantee.

Tightness and the local-ratio view

On Kn,nK_{n,n} with unit weights, raising the duals of a perfect matching (each to 1) makes every vertex tight, so the algorithm takes all 2n2n vertices against OPT=n\text{OPT} = n, with dual sum nn: the factor 2 is tight, for the algorithm and for the dual bound.

The local-ratio formulation of the same algorithm (Bar-Yehuda and Even, 1985) avoids LPs altogether: pick an edge uvuv, subtract ε=min⁡(wu,wv)\varepsilon = \min(w_u, w_v) from both weights, recurse, and take all vertices whose weight dropped to 0. The analysis: with respect to the weight function that is ε\varepsilon on uu and vv and 0 elsewhere, every cover costs at least ε\varepsilon and every minimal cover at most 2ε2\varepsilon, and ratios of 22 add up over the decomposition. The amounts ε\varepsilon are exactly the dual values yuvy_{uv}.

The program runs the local-ratio version on the graph of the C++ example and prints the amount subtracted at each edge: the same numbers as the dual values yey_e above, and the same cover.

python · try it yourself
w = [4, 3, 5, 2, 6, 3]
E = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 4), (3, 4), (3, 5), (4, 5)]

def local_ratio(w, E):
    w = list(w)
    for u, v in E:
        eps = min(w[u], w[v])
        if eps > 0:
            w[u] -= eps
            w[v] -= eps
            print(f"edge ({u},{v}): subtract {eps}, weights now {w}")
    return [v for v in range(len(w)) if w[v] == 0]

cover = local_ratio(w, E)
print("cover (weight reduced to 0):", cover, " cost", sum(w[v] for v in cover))
edge (0,1): subtract 3, weights now [1, 0, 5, 2, 6, 3]
edge (0,2): subtract 1, weights now [0, 0, 4, 2, 6, 3]
edge (2,4): subtract 4, weights now [0, 0, 0, 2, 2, 3]
edge (3,4): subtract 2, weights now [0, 0, 0, 0, 0, 3]
cover (weight reduced to 0): [0, 1, 2, 3, 4]  cost 20

Why primal-dual?

  • Speed: no LP solver, often near-linear time, and easy to implement in distributed or streaming settings.
  • A certificate: the dual solution proves the ratio for each output, which is useful for monitoring in practice.
  • Reach: for problems with exponentially many constraints, such as cut covering formulations of network design, the dual has one variable per cut, but the algorithm only ever raises polynomially many of them. That is how the next lesson handles Steiner forest.

Test yourself

Why is the dual solution built by the algorithm always feasible?

  1. Because it is optimal
  2. Each raise of yuvy_{uv} is at most the smaller slack of uu and vv, so no vertex receives more than its weight
  3. Because all weights are integers
  4. Because the graph is bipartite

Where does the factor 2 come from in the primal-dual analysis?

  1. From rounding at 12\frac{1}{2}
  2. Each yey_e is counted once for every endpoint of ee in the cover, at most twice
  3. From the integrality gap of KnK_n
  4. From the number of iterations

Which complementary slackness conditions does the algorithm keep exactly?

  1. None
  2. The primal ones: it only takes vertices whose dual constraint is tight
  3. The dual ones: every edge with ye>0y_e > 0 has exactly one endpoint in the cover
  4. Both, so the output is optimal

What does the primal-dual method give for set cover when each element is in at most ff sets?

  1. An HnH_n-approximation
  2. An ff-approximation, without solving an LP
  3. An optimal solution
  4. A 2-approximation for every ff