TExcelTAD
Home › Linear Programming & Duality for Algorithms › Dual Fitting: Greedy Set Cover and the Harmonic Bound

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 α\alpha) a feasible dual solution, and weak duality then gives ALG≤α⋅LP≤α⋅OPT\text{ALG} \le \alpha \cdot \text{LP} \le \alpha \cdot \text{OPT}. The classic example is the greedy algorithm for weighted set cover and its HnH_n 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 UU of nn elements, sets S1,…,Sm⊆US_1, \ldots, S_m \subseteq U with weights wS>0w_S > 0; choose sets of minimum total weight covering UU. The LP relaxation and its dual are

(P)min⁡∑SwSxSs.t.∑S∋exS≥1  ∀e∈U,x≥0,(D)max⁡∑e∈Uyes.t.∑e∈Sye≤wS  ∀S,y≥0.\begin{aligned} (P) \quad & \min \sum_S w_S x_S \quad \text{s.t.} \quad \sum_{S \ni e} x_S \ge 1 \ \ \forall e \in U, \quad x \ge 0, \\ (D) \quad & \max \sum_{e \in U} y_e \quad \text{s.t.} \quad \sum_{e \in S} y_e \le w_S \ \ \forall S, \quad y \ge 0 . \end{aligned}

Think of yey_e as a price element ee pays for being covered. The dual constraint says that no set is overcharged: the elements of SS together pay at most wSw_S. By weak duality, any such prices sum to at most LP≤OPT\text{LP} \le \text{OPT}.

The greedy algorithm and its prices

text
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 prices

By 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 Hk=1+12+⋯+1kH_k = 1 + \frac{1}{2} + \cdots + \frac{1}{k}, where kk is the size of the largest set.

Lemma. For every set SS, ∑e∈Sprice(e)≤H∣S∣ wS\sum_{e \in S} \text{price}(e) \le H_{\lvert S \rvert} \, w_S.

Proof. Order the elements of SS as e1,…,eke_1, \ldots, e_k in the order greedy covers them (ties arbitrary). Just before eie_i is covered, at least k−i+1k - i + 1 elements of SS (namely ei,…,eke_i, \ldots, e_k) are still uncovered, so SS itself offers cost per new element at most wSk−i+1\frac{w_S}{k - i + 1}. Greedy picks the cheapest option, so price(ei)≤wSk−i+1\text{price}(e_i) \le \frac{w_S}{k - i + 1}. Summing over ii gives wS(1k+⋯+1)=HkwSw_S (\frac{1}{k} + \cdots + 1) = H_k w_S. □\square

Theorem. Greedy returns a cover of weight at most Hk⋅LP≤Hn⋅OPTH_k \cdot \text{LP} \le H_n \cdot \text{OPT}.

Proof. By the lemma, ye=price(e)/Hky_e = \text{price}(e) / H_k is dual feasible. By weak duality ∑eye≤LP\sum_e y_e \le \text{LP}. So ALG=∑eprice(e)=Hk∑eye≤Hk⋅LP\text{ALG} = \sum_e \text{price}(e) = H_k \sum_e y_e \le H_k \cdot \text{LP}. □\square

The bound is against the LP, not just OPT, so it also shows that the integrality gap of set cover is at most HnH_n. Since Hn≤1+ln⁡nH_n \le 1 + \ln n, the ratio is O(log⁡n)O(\log n).

Running the proof

The program runs greedy on a small weighted instance, records the prices, computes the worst overcharge ratio max⁡S1wS∑e∈Sprice(e)\max_S \frac{1}{w_S} \sum_{e \in S} \text{price}(e), 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.

python · try it yourself
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 EE and ends with weight 17, while OPT is 12 (the sets AA and FF) 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 687\frac{68}{7} that no solver had to compute, and 17=74⋅68717 = \frac{7}{4} \cdot \frac{68}{7}, so this run certifies its own ratio of at most 74\frac{7}{4}, better than the worst-case guarantee H4=2512H_4 = \frac{25}{12}.

In C++

Greedy with exact prices and the overcharge check, on the same instance. Sets are bit masks over the 8 elements.

cpp · try it yourself
#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 1,…,n1, \ldots, n, take the singletons {i}\{i\} with weight 1i\frac{1}{i} and the whole universe as one set of weight 1+ε1 + \varepsilon. OPT is the big set. Greedy compares the singleton {j}\{j\} (cost 1j\frac{1}{j} per element) with the big set (cost 1+εj\frac{1 + \varepsilon}{j} per element while jj elements are uncovered) and always takes the singleton of the largest remaining element, paying HnH_n in total.

python · try it yourself
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 (1−ε)ln⁡n(1 - \varepsilon) \ln n is impossible unless NP has nO(log⁡log⁡n)n^{O(\log \log n)}-time algorithms, and Dinur and Steurer (2014) proved the same threshold under P ≠\ne 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 α\alpha by which any dual constraint can be violated; (3) divide by α\alpha 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?

  1. The sets greedy chose
  2. Prices of the elements: each newly covered element pays the chosen set's cost per new element, divided by HkH_k
  3. The LP optimum xSx_S
  4. Indicator variables of uncovered elements

Why is price(ei)≤wSk−i+1\text{price}(e_i) \le \frac{w_S}{k - i + 1} for the ii-th covered element of a set SS of size kk?

  1. Because SS is always chosen
  2. Because just before eie_i is covered, SS still has at least k−i+1k - i + 1 uncovered elements, so greedy's choice costs at most wSk−i+1\frac{w_S}{k - i + 1} per element
  3. Because prices decrease over time
  4. Because wS≤1w_S \le 1

What does the dual fitting argument prove about the integrality gap of the set cover LP?

  1. It is exactly 2
  2. It is at most HnH_n, since greedy's integral cover costs at most HnH_n times the LP value
  3. It is unbounded
  4. Nothing, the argument only concerns OPT