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 :
Think of as money an edge offers to get covered, and of as the price of a vertex: the dual constraint says no vertex receives more than its price. By weak duality (Weak and Strong Duality), for every feasible .
The algorithm
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 yEach iteration makes at least one more vertex tight, so there are at most iterations, and with the edges scanned once in any order the algorithm runs in time. It is due to Bar-Yehuda and Even (1981).
Theorem. The algorithm returns a vertex cover and a feasible dual with .
Proof. The loop ends only when every edge has an endpoint in , so is a cover. Each raise is limited by the slacks of both endpoints, so no dual constraint is violated: is feasible. Every vertex in is tight, , so
because an edge has only two endpoints.
The view through complementary slackness
An optimal primal-dual pair satisfies complementary slackness (Complementary Slackness and Optimality Certificates): the dual constraint of is tight, and . The algorithm keeps the primal conditions exactly (it only takes tight vertices) and relaxes the dual ones by a factor 2 (, trivially true). That is the general recipe:
If is integral and primal feasible, is dual feasible, primal slackness holds exactly and dual slackness holds up to a factor (every has its primal row satisfied at most times over), then .
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 , the maximum number of sets containing an element, matching the LP rounding bound of Rounding the Vertex Cover LP without solving any LP.
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.
#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 with unit weights, raising the duals of a perfect matching (each to 1) makes every vertex tight, so the algorithm takes all vertices against , with dual sum : 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 , subtract from both weights, recurse, and take all vertices whose weight dropped to 0. The analysis: with respect to the weight function that is on and and 0 elsewhere, every cover costs at least and every minimal cover at most , and ratios of add up over the decomposition. The amounts are exactly the dual values .
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 above, and the same cover.
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?
- Because it is optimal
- Each raise of is at most the smaller slack of and , so no vertex receives more than its weight
- Because all weights are integers
- Because the graph is bipartite
Where does the factor 2 come from in the primal-dual analysis?
- From rounding at
- Each is counted once for every endpoint of in the cover, at most twice
- From the integrality gap of
- From the number of iterations
Which complementary slackness conditions does the algorithm keep exactly?
- None
- The primal ones: it only takes vertices whose dual constraint is tight
- The dual ones: every edge with has exactly one endpoint in the cover
- Both, so the output is optimal
What does the primal-dual method give for set cover when each element is in at most sets?
- An -approximation
- An -approximation, without solving an LP
- An optimal solution
- A 2-approximation for every