Sub topic 14
The Primal-Dual Method
This chapter assumes you know the LP relaxation of weighted vertex cover from the chapter on LP rounding, and that you can read a linear program and form its dual. Linear programming duality is the main tool here. You will also need the greedy set cover algorithm from earlier in the tutorial.
LP rounding solves a linear program and then converts the fractional answer into an integer one. The primal-dual method gets the same kind of guarantee without solving any LP. It builds an integer primal solution and a feasible dual solution together, using simple combinatorial steps. The dual solution is a lower bound on the optimum, so it certifies how good the primal solution is.
LP Duality as a Source of Lower Bounds
For a minimization LP, every feasible dual solution has an objective value at most the LP optimum. This fact is called weak duality. The LP optimum is in turn at most the integer optimum, because the LP is a relaxation. Together these give a chain:
dual value ≤ OPT_LP ≤ OPT
To prove that an algorithm's cost is within a factor α of OPT, it is enough to show that the cost is at most α times the value of some feasible dual solution. The algorithm never needs to know OPT_LP. It only needs to construct the dual solution as it goes.
The Vertex Cover Primal and Dual
For a graph G = (V, E) with vertex weights w_v ≥ 0, the primal relaxation and its dual are:
| Row | Primal (cover) | Dual (packing) |
|---|---|---|
| Variables | x_v ≥ 0 for each vertex | y_e ≥ 0 for each edge |
| Objective | minimize Σ w_v x_v | maximize Σ y_e |
| Constraints | x_u + x_v ≥ 1 for each edge (u, v) | Σ_{e ∋ v} y_e ≤ w_v for each vertex v |
One way to read the dual: each edge pays some amount y_e, and no vertex may be charged more than its weight by the edges that touch it. A vertex is tight when the edges touching it have paid exactly its weight.
The Algorithm: Raise Duals Until Something Is Tight
The primal-dual algorithm for vertex cover, due to Bar-Yehuda and Even, works like this:
- Start with y_e = 0 for every edge and an empty cover C.
- Pick an edge (u, v) with neither endpoint in C.
- Raise y_e as far as possible without breaking dual feasibility. That amount is the smaller remaining slack of u and v.
- Add every endpoint that became tight to C.
- Repeat until every edge has an endpoint in C.
The algorithm never calls an LP solver. Each edge is handled once, so the running time is O(|V| + |E|).
def primal_dual_vertex_cover(n, edges, weights):
slack = list(weights) # w_v minus the dual paid so far
y = {}
cover = set()
for u, v in edges:
if u in cover or v in cover:
continue
delta = min(slack[u], slack[v])
y[(u, v)] = delta
slack[u] -= delta
slack[v] -= delta
if slack[u] == 0:
cover.add(u)
if slack[v] == 0:
cover.add(v)
return cover, y
def report(n, edges, weights):
cover, y = primal_dual_vertex_cover(n, edges, weights)
for v in range(n): # check dual feasibility
paid = sum(val for e, val in y.items() if v in e)
assert paid <= weights[v]
for u, v in edges: # check primal feasibility
assert u in cover or v in cover
cost = sum(weights[v] for v in cover)
dual = sum(y.values())
print("cover:", sorted(cover), "cost:", cost, "dual:", dual,
"certified ratio <=", cost / dual if dual else 1.0)
report(4, [(0, 1), (1, 2), (2, 3)], [1, 3, 1, 2])
report(2, [(0, 1)], [1, 1])Running this prints:
cover: [0, 2] cost: 2 dual: 2 certified ratio <= 1.0
cover: [0, 1] cost: 2 dual: 1 certified ratio <= 2.0On the path graph, edge (0, 1) raises its dual by 1, which makes vertex 0 tight. Edge (1, 2) raises its dual by 1, which makes vertex 2 tight. Edge (2, 3) is already covered. The cover costs 2 and the dual is worth 2, so the run shows that this cover is optimal. On the single edge with equal weights, both endpoints become tight at the same moment and both are added. That gives cost 2 against an optimum of 1, which shows that the factor 2 is tight.
Why the Cover Costs at Most Twice the Optimum
Two facts give the proof.
- Every vertex in C is tight. A vertex enters C only when its slack reaches zero, so wv = Σ{e ∋ v} y_e for every v ∈ C.
- Each edge has two endpoints, so each y_e is counted at most twice when you sum over the vertices of C.
So:
cost(C) = Σ{v ∈ C} wv = Σ{v ∈ C} Σ{e ∋ v} ye ≤ 2 Σ{e ∈ E} ye ≤ 2 · OPTLP ≤ 2 · OPT
The algorithm keeps y feasible at every step, because no vertex is ever charged beyond its weight. That is what allows the weak duality step. The output C is a valid cover because the loop runs until no uncovered edge remains.
The Bound Depends on the Instance
The ratio cost(C) / Σ y_e is a guarantee for each instance, and you can compute it after the run, as the report function does. It is often much better than 2. In practice this certificate is a strong reason to prefer primal-dual algorithms, because each answer comes with its own quality bound.
Complementary Slackness, Relaxed
For an optimal primal-dual pair, the complementary slackness conditions hold:
- Primal condition: if x_v > 0, then vertex v is tight in the dual.
- Dual condition: if ye > 0, then the primal constraint for e is tight, so xu + x_v = 1.
An integer solution usually cannot satisfy both. The primal-dual method keeps one condition exactly and relaxes the other. In the vertex cover algorithm, the primal condition holds exactly, since only tight vertices are chosen. The dual condition is relaxed to xu + xv ≤ 2, which holds trivially for 0/1 values. The relaxation factor in the dual condition becomes the approximation ratio. This is the general recipe:
- Write the primal relaxation and its dual.
- Grow dual variables, keeping the dual feasible.
- Add primal elements only when their dual constraints become tight.
- Bound how far the relaxed dual condition can be violated. That bound is α.
The same recipe applied to weighted set cover gives an f-approximation, where f is the largest number of sets that any single element appears in. Vertex cover is the special case f = 2, with vertices as sets and edges as elements. More refined versions, such as Goemans and Williamson's algorithm for Steiner forest, raise many dual variables at the same time and then delete unneeded primal elements afterward.
Dual Fitting: Analyzing an Algorithm You Already Have
Dual fitting runs the argument in the other direction. You start with an existing algorithm, often a greedy one, and afterward build a dual solution that "fits" its cost.
For greedy set cover, let each newly covered element e pay a price pe equal to the cost of the chosen set divided by the number of new elements it covers. The prices add up exactly to the greedy cost. These prices are generally not dual-feasible, because a single set can be overcharged. One can show that every set S is overcharged by at most a factor H{|S|} ≤ Hn. Dividing all prices by Hn therefore gives a feasible dual, so:
greedy cost = Σ pe = Hn · Σ (pe / Hn) ≤ Hn · OPTLP ≤ H_n · OPT
This gives the Hn bound from the greedy set cover chapter in a new form. It also proves something stronger: greedy is within Hn of the LP optimum, not just of the integer optimum.
Choosing Between Rounding and Primal-Dual
| Aspect | LP rounding | Primal-dual |
|---|---|---|
| Needs an LP solver | Yes | No |
| Running time | That of the LP solver | Usually near-linear, combinatorial |
| Per-instance certificate | The LP value | The dual value, built along the way |
| Ratio for weighted vertex cover | 2 | 2 |
| Sensitivity | Depends on solver accuracy | Depends on processing order and tie handling |
Use primal-dual when instances are large, an LP solver is unavailable, or you want a fast algorithm with its own certificate. Use rounding when the LP structure is complex and a combinatorial dual-growing rule is hard to design.
Common Mistakes
- Raising a dual beyond the smaller slack. This breaks dual feasibility, and the lower bound no longer holds.
- Adding only one endpoint when both become tight. Adding only one is still correct, but the analysis must then avoid assuming both were added.
- Treating the dual value as OPT. It is a lower bound, and the gap between it and OPT can be as large as the integrality gap.
Exercise
Run primaldualvertex_cover on the triangle with edges [(0, 1), (1, 2), (0, 2)] and weights [2, 2, 2]. What cover, cost, and dual value result, and what is the true optimum?
Show answer
Edge (0, 1) raises y by 2, so vertices 0 and 1 become tight and both join the cover. The other two edges are already covered. The cover is {0, 1} with cost 4 and dual value 2, so the certified ratio is at most 2. The true optimum is 4, because any vertex cover of a triangle needs two vertices. The cover is optimal, but the dual certificate only proves a factor of 2. This gap comes from the LP: setting xv = 1/2 everywhere gives OPTLP = 3.
Exercise
In the analysis, which complementary slackness condition holds exactly and which is relaxed, and by what factor?
Show answer
The primal condition holds exactly, because a vertex is chosen only when its dual constraint is tight. The dual condition is relaxed: when ye > 0 the algorithm guarantees only xu + x_v ≤ 2 instead of = 1. The factor 2 is the approximation ratio.
Exercise
Generalize the algorithm to weighted set cover where each element is in at most f sets. Describe the dual-raising step and state the ratio.
Show answer
For each element e not yet covered, raise ye by the smallest remaining slack among the sets that contain e, and add every set that becomes tight. Every chosen set is tight, and each ye is counted at most f times when summing over chosen sets. So the cost is at most f · Σ y_e ≤ f · OPT, which makes the algorithm an f-approximation.