Sub topic 12
LP Relaxation and Rounding for Weighted Vertex Cover
This chapter assumes you know what a linear program (LP) is: you minimize a linear objective subject to linear inequalities, and LPs can be solved in polynomial time. It also assumes you have read the earlier chapter on the matching-based 2-approximation for vertex cover, and that you can run Python with SciPy installed.
Why Weights Break the Matching Algorithm
In weighted vertex cover, each vertex i has a weight w_i ≥ 0. You want a set of vertices that touches every edge and has the smallest possible total weight. The maximal-matching algorithm ignores weights. It adds both endpoints of each matched edge, so a single edge (u, v) with w_u = 1 and w_v = 1000 gives a cover of weight 1001 when the optimum is 1. A maximal matching gives a lower bound on the number of vertices in a cover, but it says nothing about their total weight.
To handle weights, we need a lower bound on the optimum that accounts for them. Linear programming provides one.
The Integer Program and Its Relaxation
Write one variable x_i per vertex, with x_i = 1 meaning "vertex i is in the cover." Weighted vertex cover is then this integer linear program (ILP):
| Part | ILP (exact problem) | LP relaxation |
|---|---|---|
| Objective | minimize Σ w_i x_i | minimize Σ w_i x_i |
| Edge constraint | x_u + x_v ≥ 1 for every edge (u, v) | x_u + x_v ≥ 1 for every edge (u, v) |
| Variable domain | x_i ∈ {0, 1} | 0 ≤ x_i ≤ 1 |
| Solvable in polynomial time? | No (NP-hard) | Yes |
The relaxation keeps the constraints and drops integrality. Every feasible integer solution is also feasible for the LP, so the LP optimum LP* satisfies:
LP* ≤ OPT
This inequality is the lower bound on the optimum that the whole analysis rests on. The LP may find a cheaper fractional "cover," for example with half of a vertex, but it can never do worse than the best integer cover.
The Rounding Rule
Solve the LP to get an optimal fractional solution x*. Then round each coordinate with a threshold:
- If
x*_i ≥ 1/2, put vertexiin the cover. - Otherwise, leave it out.
Proof of the 2-Approximation
The proof has two parts, feasibility and cost.
Feasibility. Take any edge (u, v). The LP guarantees x*_u + x*_v ≥ 1. Two numbers that sum to at least 1 cannot both be below 1/2, so at least one endpoint has value ≥ 1/2 and gets rounded up. Every edge is covered.
Cost. Let x̂ be the rounded 0/1 vector. Compare coordinate by coordinate:
- If
x*_i ≥ 1/2, thenx̂_i = 1 ≤ 2 x*_i. - If
x*_i < 1/2, thenx̂_i = 0 ≤ 2 x*_i.
So x̂_i ≤ 2 x*_i for every i. Because weights are nonnegative, summing gives:
w(C) = Σ w_i x̂_i ≤ 2 Σ w_i x*_i = 2·LP* ≤ 2·OPT
The proof never uses the structure of the optimal integer solution. It only compares the output to LP*, which we can compute. The same pattern shows up again and again: relax, solve, round, and charge the rounding loss against the LP value.
Implementation
SciPy's linprog accepts only ≤ constraints, so each edge constraint x_u + x_v ≥ 1 is negated into -x_u - x_v ≤ -1.
from scipy.optimize import linprog
def lp_vertex_cover(n, edges, weights):
A_ub = []
for u, v in edges:
row = [0] * n
row[u] = -1
row[v] = -1
A_ub.append(row)
b_ub = [-1] * len(edges)
res = linprog(c=weights, A_ub=A_ub, b_ub=b_ub,
bounds=[(0, 1)] * n, method="highs")
return [i for i in range(n) if res.x[i] >= 0.5 - 1e-9]
print(lp_vertex_cover(4, [(0, 1), (1, 2), (2, 3)], [1, 3, 1, 2]))On the path 0–1–2–3 with weights [1, 3, 1, 2], this prints [0, 2], a cover of weight 2, which is optimal. The matching algorithm could have returned {0, 1, 2, 3} with weight 7.
Common mistakes
- Forgetting the sign flip. If you pass
x_u + x_v ≤ 1by mistake, the all-zeros vector is optimal and the "cover" is empty. - Omitting the bounds. Without
x_i ≤ 1the LP optimum stays the same for vertex cover with nonnegative weights. Withoutx_i ≥ 0, however, negative values can make the LP unbounded.linprogdefaults to(0, None), but state the bounds explicitly. - Not checking
res.status. In production code, confirmres.successbefore readingres.x. - Negative weights. The cost argument needs
w_i ≥ 0. A vertex with negative weight should simply be included, since it can only lower the cost, and then removed from the problem.
Where the Factor 2 Comes From: The Integrality Gap
How much can rounding lose? The integrality gap of a relaxation is the worst-case ratio OPT / LP* over all instances. Any algorithm whose analysis compares only against LP* cannot prove a ratio better than this gap.
The triangle shows the gap concretely:
from scipy.optimize import linprog
edges = [(0, 1), (1, 2), (0, 2)]
A_ub = []
for u, v in edges:
row = [0, 0, 0]
row[u] = -1
row[v] = -1
A_ub.append(row)
res = linprog(c=[1, 1, 1], A_ub=A_ub, b_ub=[-1, -1, -1],
bounds=[(0, 1)] * 3, method="highs")
print(round(res.fun, 6), [round(x, 6) for x in res.x])This prints 1.5 [0.5, 0.5, 0.5]. The integer optimum is 2, since any two vertices cover the triangle. Rounding takes all three vertices, for weight 3, which is exactly 2·LP*. On the complete graph K_n with unit weights, LP* = n/2 and OPT = n − 1:
| n | LP* | OPT | Gap OPT / LP* |
|---|---|---|---|
| 2 | 1 | 1 | 1.00 |
| 3 | 1.5 | 2 | 1.33 |
| 5 | 2.5 | 4 | 1.60 |
| 10 | 5 | 9 | 1.80 |
| n | n/2 | n − 1 | 2 − 2/n |
The gap tends to 2. Threshold rounding therefore gets everything this LP can give: no rounding scheme on this relaxation can guarantee better than 2 − 2/n. Beating 2 would need a stronger relaxation or a different technique. It is widely believed impossible. Vertex cover is NP-hard to approximate within about 1.36, and it is hard to approximate within 2 − ε if the Unique Games Conjecture holds. A later chapter covers these limits.
Half-Integrality and Bipartite Graphs
The vertex cover LP has a special structure. Every extreme-point optimal solution takes values only in {0, 1/2, 1} (Nemhauser and Trotter). Simplex-type solvers such as HiGHS usually return extreme points, so in practice the threshold test mostly separates clean 0s, 1/2s, and 1s. This structure has two consequences:
- Vertices with
x*_i = 1belong to some optimal cover, and vertices withx*_i = 0can be excluded safely. Only the half-valued vertices are uncertain. This idea underlies kernelization in parameterized algorithms. - On bipartite graphs the constraint matrix is totally unimodular, so the LP has an integral optimal extreme point and
LP* = OPT. The path example above is bipartite, which is why the result was exact. This matches König's theorem.
Trade-offs Compared With Other Methods
| Method | Handles weights | Ratio | Cost |
|---|---|---|---|
| Maximal matching | No | 2 (unweighted only) | O(V + E), no solver |
| LP + threshold rounding | Yes | 2 | One LP solve |
| Primal-dual (later chapter) | Yes | 2 | O(V + E), no solver |
LP rounding is the simplest to prove correct and it generalizes. For set cover where each element lies in at most f sets, the same threshold at 1/f gives an f-approximation by the same argument. Its drawback is that it needs an LP solver, which is slow on very large graphs. The primal-dual method achieves the same guarantee without one.
Exercise
Generalize the rounding to set cover in which every element appears in at most f sets. The LP has one constraint per element e: the sum of x_S over sets S containing e is at least 1. Which threshold should you use, and what approximation ratio does it give?
Show answer
Round xS up to 1 when x*S ≥ 1/f. An element's constraint has at most f terms that sum to at least 1, so at least one term is ≥ 1/f, and every element is covered. Each rounded variable satisfies x̂S ≤ f·x*S, so the cost is at most f·LP* ≤ f·OPT, an f-approximation. Vertex cover is the case f = 2, where each edge is an element contained in the sets of its two endpoints.
Exercise
For the star graph with center c and leaves l1, ..., lk (k ≥ 2), with w_c = k and every leaf weight 1, give an optimal LP solution and say what threshold rounding returns. Is the result optimal?
Show answer
Every edge (c, li) needs xc + xli ≥ 1. Setting xc = 1 costs k, and setting every leaf to 1 also costs k. Any mix xc = t, x_li = 1 − t costs kt + k(1 − t) = k, so LP* = k and many optimal LP solutions exist, including x = 1/2 everywhere. If the solver returns the all-1/2 point, rounding picks every vertex, for cost 2k, exactly twice the optimum of k. If it returns an integral extreme point, the cover is optimal. The guarantee holds in both cases, but the result depends on which optimal LP point the solver returns.
Exercise
Why does the inequality x̂i ≤ 2x*i fail as a proof step if some weight w_i is negative, and how do you fix the algorithm?
Show answer
Summing wi·x̂i ≤ 2·wi·x*i term by term requires multiplying by w_i ≥ 0. A negative weight reverses the inequality. Fix it by preprocessing: always include negative-weight vertices, since they lower the cost, delete the edges they cover, and run the LP rounding on what remains with nonnegative weights.