Dual Fitting: Greedy Set Cover and the Harmonic Bound
Duality is not only for algorithms that solve LPs. Dual fitting analyses a combinatorial algorithm by reading a dual solution off its run: the algorithm "pays" for its choices by charging prices to the items it handles, the prices are shown to be (after scaling by some factor ) a feasible dual solution, and weak duality then gives . The classic example is the greedy algorithm for weighted set cover and its approximation ratio (Johnson, 1974, and Lovász, 1975, for the unweighted case; Chvátal, 1979, for weights). This lesson gives the proof, runs it on examples, and shows the instance where the bound is tight. The design of approximation algorithms with such techniques continues in Approximation Algorithms.
Set cover and its LP pair
A universe of elements, sets with weights ; choose sets of minimum total weight covering . The LP relaxation and its dual are
Think of as a price element pays for being covered. The dual constraint says that no set is overcharged: the elements of together pay at most . By weak duality, any such prices sum to at most .
The greedy algorithm and its prices
GREEDY-SET-COVER(U, sets, w)
1 C = empty, uncovered = U
2 while uncovered is not empty
3 pick S minimizing w_S / |S ∩ uncovered| -- cost per new element
4 for each e in S ∩ uncovered: price(e) = w_S / |S ∩ uncovered|
5 add S to C, remove S from uncovered
6 return C -- total weight = sum of pricesBy construction the total weight of the chosen sets equals the sum of the prices: each chosen set's weight is split evenly among the elements it newly covers. The prices are generally not dual feasible (a set can be overcharged), but they fail by at most a factor , where is the size of the largest set.
Lemma. For every set , .
Proof. Order the elements of as in the order greedy covers them (ties arbitrary). Just before is covered, at least elements of (namely ) are still uncovered, so itself offers cost per new element at most . Greedy picks the cheapest option, so . Summing over gives .
Theorem. Greedy returns a cover of weight at most .
Proof. By the lemma, is dual feasible. By weak duality . So .
The bound is against the LP, not just OPT, so it also shows that the integrality gap of set cover is at most . Since , the ratio is .
Running the proof
The program runs greedy on a small weighted instance, records the prices, computes the worst overcharge ratio , scales the prices into a feasible dual, and compares all the numbers with the LP optimum and the brute-force integer optimum. All arithmetic is exact.
from fractions import Fraction as F
from itertools import combinations
from scipy.optimize import linprog
U = set(range(1, 9))
sets = {"A": ({1, 2, 3, 4}, 6), "B": ({4, 5, 6}, 6), "C": ({6, 7, 8}, 3), "D": ({1, 5, 7}, 6),
"E": ({2, 3, 8}, 2), "F": ({5, 6, 7, 8}, 6)}
uncovered, chosen, price = set(U), [], {}
while uncovered:
name = min(sets, key=lambda s: (F(sets[s][1], len(sets[s][0] & uncovered)) if sets[s][0] & uncovered else F(10 ** 9), s))
new = sets[name][0] & uncovered
for e in new:
price[e] = F(sets[name][1], len(new))
chosen.append(name)
uncovered -= new
alg = sum(sets[s][1] for s in chosen)
print("greedy picks", chosen, "weight", alg, "= sum of prices", sum(price.values()))
ratio = max(sum(price[e] for e in S) / w for S, w in sets.values())
k = max(len(S) for S, _ in sets.values())
H = sum(F(1, i) for i in range(1, k + 1))
y = {e: price[e] / ratio for e in U}
print("worst overcharge ratio", ratio, "<= H_k =", H, "for k =", k)
print("scaled prices are dual feasible:", all(sum(y[e] for e in S) <= w for S, w in sets.values()), " dual value", sum(y.values()))
names = list(sets)
lp = linprog([sets[s][1] for s in names], A_ub=[[-1 if e in sets[s][0] else 0 for s in names] for e in sorted(U)],
b_ub=[-1] * len(U), bounds=[(0, None)] * len(names), method="highs")
opt = min(sum(sets[s][1] for s in C) for r in range(1, len(names) + 1) for C in combinations(names, r)
if set().union(*(sets[s][0] for s in C)) == U)
print("dual bound", round(float(sum(y.values())), 4), "<= LP", round(float(lp.fun), 4), "<= OPT", opt, "<= ALG", alg)greedy picks ['E', 'C', 'A', 'B'] weight 17 = sum of prices 17
worst overcharge ratio 7/4 <= H_k = 25/12 for k = 4
scaled prices are dual feasible: True dual value 68/7
dual bound 9.7143 <= LP 11.5 <= OPT 12 <= ALG 17
Greedy is fooled here: it starts with the cheap set and ends with weight 17, while OPT is 12 (the sets and ) and the LP optimum is 11.5. The chain of inequalities in the last line is the proof, instantiated: the scaled prices give the lower bound that no solver had to compute, and , so this run certifies its own ratio of at most , better than the worst-case guarantee .
In C++
Greedy with exact prices and the overcharge check, on the same instance. Sets are bit masks over the 8 elements.
#include <cstdio>
#include <numeric>
#include <vector>
struct Q {
long long p, q;
Q(long long a = 0, long long b = 1) : p(a), q(b) { long long g = std::gcd(p < 0 ? -p : p, q); if (g > 1) { p /= g; q /= g; } }
Q operator+(Q o) const { return Q(p * o.q + o.p * q, q * o.q); }
Q operator/(Q o) const { return Q(p * o.q, q * o.p); }
bool operator<(Q o) const { return p * o.q < o.p * q; }
};
int main() {
std::vector<int> mask = {0b00001111, 0b00111000, 0b11100000, 0b01010001, 0b10000110, 0b11110000};
std::vector<long long> w = {6, 6, 3, 6, 2, 6};
const char* names = "ABCDEF";
int uncovered = 0b11111111;
std::vector<Q> price(8);
long long total = 0;
std::printf("greedy picks");
while (uncovered) {
int best = -1;
for (int s = 0; s < (int)mask.size(); s++) {
int cnt = __builtin_popcount(mask[s] & uncovered);
if (cnt && (best < 0 || Q(w[s], cnt) < Q(w[best], __builtin_popcount(mask[best] & uncovered)))) best = s;
}
int fresh = mask[best] & uncovered, cnt = __builtin_popcount(fresh);
for (int e = 0; e < 8; e++) if (fresh >> e & 1) price[e] = Q(w[best], cnt);
uncovered &= ~mask[best];
total += w[best];
std::printf(" %c", names[best]);
}
Q worst(0);
for (size_t s = 0; s < mask.size(); s++) {
Q sum(0);
for (int e = 0; e < 8; e++) if (mask[s] >> e & 1) sum = sum + price[e];
Q r = sum / Q(w[s]);
if (worst < r) worst = r;
}
std::printf(", weight %lld\nworst overcharge ratio %lld/%lld\n", total, worst.p, worst.q);
}greedy picks E C A B, weight 17
worst overcharge ratio 7/4
The bound is tight for greedy
For elements , take the singletons with weight and the whole universe as one set of weight . OPT is the big set. Greedy compares the singleton (cost per element) with the big set (cost per element while elements are uncovered) and always takes the singleton of the largest remaining element, paying in total.
from fractions import Fraction as F
for n in [4, 8, 16, 32]:
eps = F(1, 100)
sets = {f"{{{i}}}": ({i}, F(1, i)) for i in range(1, n + 1)}
sets["all"] = (set(range(1, n + 1)), 1 + eps)
uncovered, cost = set(range(1, n + 1)), F(0)
while uncovered:
name = min(sets, key=lambda s: sets[s][1] / len(sets[s][0] & uncovered) if sets[s][0] & uncovered else F(10 ** 9))
cost += sets[name][1]
uncovered -= sets[name][0]
print(f"n = {n:2}: greedy {float(cost):.4f}, OPT {float(1 + eps):.4f}, ratio {float(cost / (1 + eps)):.4f}, H_n {float(sum(F(1, i) for i in range(1, n + 1))):.4f}")n = 4: greedy 2.0833, OPT 1.0100, ratio 2.0627, H_n 2.0833
n = 8: greedy 2.7179, OPT 1.0100, ratio 2.6909, H_n 2.7179
n = 16: greedy 3.3807, OPT 1.0100, ratio 3.3473, H_n 3.3807
n = 32: greedy 4.0585, OPT 1.0100, ratio 4.0183, H_n 4.0585
No polynomial-time algorithm does much better unless P = NP: Feige (1998) showed that a ratio of is impossible unless NP has -time algorithms, and Dinur and Steurer (2014) proved the same threshold under P NP. Greedy is essentially optimal.
Tip: The general recipe of dual fitting: (1) let the algorithm define prices whose sum is its cost; (2) find the factor by which any dual constraint can be violated; (3) divide by and invoke weak duality. The same recipe analyses greedy facility location (Jain, Mahdian, Markakis, Saberi and Vazirani, 2003) and many online algorithms.
Test yourself
In the dual fitting proof for greedy set cover, what are the dual variables?
- The sets greedy chose
- Prices of the elements: each newly covered element pays the chosen set's cost per new element, divided by
- The LP optimum
- Indicator variables of uncovered elements
Why is for the -th covered element of a set of size ?
- Because is always chosen
- Because just before is covered, still has at least uncovered elements, so greedy's choice costs at most per element
- Because prices decrease over time
- Because
What does the dual fitting argument prove about the integrality gap of the set cover LP?
- It is exactly 2
- It is at most , since greedy's integral cover costs at most times the LP value
- It is unbounded
- Nothing, the argument only concerns OPT