Sub topic 5
Greedy Set Cover and the ln n Bound
This chapter assumes you know what NP-hardness means and have seen the approximation ratio defined. It also uses the main idea from the last two chapters: you prove a guarantee by comparing the algorithm's output with a bound on the unknown optimum. You should be comfortable with sums like 1 + 1/2 + ... + 1/n and with reading short Python programs.
Vertex cover had a constant-factor guarantee. Set cover does not. The simple greedy algorithm comes within a factor of about ln n of optimal, and under standard complexity assumptions no polynomial-time algorithm does meaningfully better. This makes set cover the standard example of a problem with a logarithmic approximation ratio.
The Problem
An instance of set cover has a universe U of n elements and a family of m subsets S1, ..., Sm of U. A cover is a choice of subsets whose union is U. The goal is to use as few subsets as possible. In the weighted version, each subset has a cost and you minimize total cost.
The problem appears in many forms:
| Application | Universe elements | Subsets |
|---|---|---|
| Sensor placement | Locations that need monitoring | Locations each sensor can see |
| Test suite reduction | Code branches | Branches each test exercises |
| Crew scheduling | Flights | Feasible crew rotations |
| Feature selection | Training examples | Examples each feature explains |
Vertex cover is a special case. The edges form the universe, and each vertex is the subset of edges touching it. Every element then lies in exactly two subsets. General set cover lets an element appear in many subsets, and that is where the problem gets harder.
The Greedy Algorithm
The rule is: while some element is uncovered, choose the subset that covers the most uncovered elements.
def greedy_set_cover(universe, subsets):
uncovered = set(universe)
chosen = []
while uncovered:
best = max(range(len(subsets)), key=lambda i: len(subsets[i] & uncovered))
if not subsets[best] & uncovered:
raise ValueError("universe cannot be covered by the given subsets")
chosen.append(best)
uncovered -= subsets[best]
return chosen
U = {1, 2, 3, 4, 5}
S = [{1, 2, 3}, {2, 4}, {3, 4}, {4, 5}]
print(greedy_set_cover(U, S))The output is [0, 3]. The trace goes like this:
- All five elements are uncovered. S0 covers 3 of them, and every other subset covers at most 2, so greedy picks S0. Elements {4, 5} remain.
- S1 and S2 each cover one new element ({4}). S3 covers two ({4, 5}). Greedy picks S3 and finishes.
Two subsets are clearly optimal here, since no single subset covers U.
Running time
Each iteration covers at least one new element and picks a subset not chosen before, so there are at most min(n, m) iterations. Each iteration intersects every subset with the uncovered set, which costs O(Σ|Si|). The total is O(min(n, m) · Σ|Si|), which is polynomial in the input size. For large inputs, use a max-heap keyed on stale counts. A subset's count can only go down, so when you pop a subset you recompute its count, and you accept it only if it still beats the next key in the heap. This "lazy greedy" version usually runs close to linear time in practice.
First Proof: Uncovered Elements Shrink Geometrically
Let OPT be the size of an optimal cover. Let ut be the number of uncovered elements after t greedy steps, so u0 = n.
Key observation. The OPT optimal subsets cover all of U, so they also cover the ut elements that are still uncovered. By the pigeonhole principle, one of those subsets contains at least ut / OPT of them. Greedy picks the subset with the most uncovered elements, so it covers at least that many:
u{t+1} ≤ ut − ut / OPT = ut (1 − 1/OPT).
Applying this repeatedly and using 1 − x < e^{−x} for x > 0:
u_t ≤ n (1 − 1/OPT)^t < n · e^{−t/OPT}.
At t = OPT · ln n the right side equals 1, so fewer than one element remains, which means none. Greedy therefore uses at most OPT · ln n + 1 subsets (the +1 absorbs rounding t up to an integer).
This argument has the shape you should expect. The optimum was never computed. The proof only used the fact that OPT subsets suffice to cover every remaining set of elements.
Second Proof: Charging Elements, and the H_n Bound
A second argument gives the sharper bound Hn = 1 + 1/2 + ... + 1/n, where ln n < Hn ≤ ln n + 1. It also extends directly to weighted set cover.
Pricing. Suppose greedy picks a subset that covers k new elements. Charge each of those k elements a price of 1/k. Each step charges a total of exactly 1, so
(number of subsets greedy picks) = Σ over all elements e of price(e).
Bounding the price inside one optimal subset. Take any subset S in an optimal cover, with |S| = s. List its elements in the order greedy covers them. Elements covered in the same step can be listed in any order. Look at the element that is j-th from the end of this list. Just before it is covered, at least j elements of S are still uncovered: this one and the j − 1 after it. So S itself was available and would have covered at least j new elements. Greedy chose a subset at least that good, so this element's price is at most 1/j. Summing over the elements of S:
Σ over e in S of price(e) ≤ 1/s + 1/(s−1) + ... + 1/1 = Hs ≤ Hn.
Combining. Every element belongs to at least one optimal subset, so
greedy = Σe price(e) ≤ Σ over S in OPT of Σ over e in S of price(e) ≤ OPT · Hn.
The proof actually gives Hd, where d is the size of the largest subset. When subsets are small this is much better than Hn. With d = 3, for example, the ratio is at most 11/6.
The Bound Is Tight for Greedy
The ln n factor is not an artifact of the analysis. The instance below forces greedy to use about log₂ n subsets when two suffice.
Arrange the elements in two rows, A and B, each of size 2^k − 1. Cut both rows into column blocks C1, ..., Ck, where Ci takes 2^{i−1} elements from each row, so |Ci| = 2^i. The rows A and B form a cover of size 2. At every step, the largest remaining block has exactly one more uncovered element than either row, so greedy always picks a block.
def greedy_set_cover(universe, subsets):
uncovered = set(universe)
chosen = []
while uncovered:
best = max(range(len(subsets)), key=lambda i: len(subsets[i] & uncovered))
if not subsets[best] & uncovered:
raise ValueError("universe cannot be covered by the given subsets")
chosen.append(best)
uncovered -= subsets[best]
return chosen
def tight_instance(k):
row_a, row_b, blocks = set(), set(), []
idx = 0
for i in range(1, k + 1):
block = set()
for _ in range(2 ** (i - 1)):
row_a.add(idx); block.add(idx); idx += 1
row_b.add(idx); block.add(idx); idx += 1
blocks.append(block)
return set(range(idx)), [row_a, row_b] + blocks
for k in range(2, 7):
U, S = tight_instance(k)
picks = greedy_set_cover(U, S)
print(f"n={len(U):4d} greedy={len(picks)} optimal=2")The output shows greedy using k subsets for n = 2^{k+1} − 2 elements, a ratio of k/2 = Θ(log n):
| k | n | Greedy | Optimal |
|---|---|---|---|
| 2 | 6 | 2 | 2 |
| 4 | 30 | 4 | 2 |
| 6 | 126 | 6 | 2 |
More careful constructions bring greedy's ratio up to (1 − o(1)) ln n, which matches the upper bound.
No Algorithm Does Much Better
Greedy's weakness is not just a flaw of greedy. Feige (1998) showed that set cover cannot be approximated within (1 − ε) ln n unless NP has quasi-polynomial-time algorithms. Dinur and Steurer (2014) strengthened this: getting within (1 − ε) ln n is NP-hard for every ε > 0. A simple greedy rule is therefore essentially the best possible polynomial-time algorithm. The chapter on Limits of Approximation explains how results like these are proved.
Common Mistakes
- Ranking by full subset size. The rule counts only uncovered elements. Ranking by |S_i| stays fixed after the first step and can end up choosing subsets that add nothing.
- Not checking feasibility. If some element is in no subset, the loop never ends, or
maxkeeps returning a subset that covers nothing. The explicit check in the code catches this. - Using greedy for vertex cover and expecting a factor of 2. On vertex cover instances, greedy only guarantees H_Δ, where Δ is the maximum degree. That can be worse than the matching-based 2-approximation. When every element appears in at most f subsets, LP rounding gives an f-approximation, as the LP Relaxation chapter shows.
- Reading "ln n" as "usually bad." The ratio is a worst-case guarantee. On real instances greedy is often within a few percent of optimal. The guarantee tells you how far off it can be, not how far off it usually is.
Exercise
Run the greedy algorithm by hand on U = {1,...,6} with subsets S0 = {1,2,3,4}, S1 = {1,2,5}, S2 = {3,4,6}, S3 = {5,6}. Which subsets does it choose, what is the optimum, and is the result consistent with the H_d bound?
Show answer
Greedy picks S0 first (4 new elements). The remaining elements are {5,6}, and S3 covers both while S1 and S2 cover one each, so greedy picks S3. The result is {S0, S3}, two subsets. The optimum is also 2 (for example S1 and S2), because no single subset covers U. The largest subset has d = 4, so the bound allows up to H4 · 2 ≈ 4.17 subsets. Greedy's 2 is well within it.
Exercise
In the charging proof, why does the element that is j-th from the end of an optimal subset S get a price of at most 1/j? What breaks if several elements of S are covered in the same greedy step?
Show answer
Just before that element is covered, it and the j − 1 elements of S listed after it are all uncovered, so S would cover at least j new elements. Greedy chose a subset covering at least as many, say k ≥ j, so the price is 1/k ≤ 1/j. Nothing breaks when several elements of S are covered together. They share the same price 1/k. Order them arbitrarily, and the earliest of them in the list has at least as many uncovered elements of S after it as any of the others, so each one's position-based bound still holds.
Exercise
Suppose every subset has at most 3 elements. Give the best ratio you can guarantee for greedy, and explain why the geometric-shrinkage proof cannot give it.
Show answer
The charging proof gives H_3 = 1 + 1/2 + 1/3 = 11/6 ≈ 1.83. The shrinkage proof only uses the fact that some optimal subset covers a 1/OPT fraction of what remains. That gives a bound in terms of ln n, and it never uses the subset size limit, so it cannot produce a constant independent of n.
Exercise
Change greedysetcover to handle weighted set cover given a list costs, and state the guarantee.
Show answer
Replace the selection with: among subsets i with a nonempty intersection with uncovered, pick the one minimizing costs[i] / len(subsets[i] & uncovered). If no subset has a nonempty intersection, raise the infeasibility error. Charge each newly covered element that ratio. The charging argument then gives total cost at most H_d times the optimal cost, where d is the largest subset size.