Sub topic 15
Greedy Approximation: Set Cover and Beyond
This chapter assumes you can read Python sets and dictionaries, know Big-O notation, and remember the coin-change counterexamples from earlier chapters. You will see how greedy can still be useful when it cannot be optimal.
Why Settle for Approximation
Some problems are NP-hard. For these problems, no known algorithm finds the optimum in polynomial time. For them, a fast greedy algorithm with a proven worst-case bound is often the best practical choice. An approximation ratio of ρ means the algorithm's answer is never worse than ρ times the optimum.
The Set Cover Problem
You are given a universe U of elements and a collection of subsets of U. You must pick the fewest subsets whose union is U. For example, the elements could be skills and the subsets could be job candidates. You want the smallest team that covers every skill.
The greedy rule is simple: always pick the subset that covers the most elements that are still uncovered.
def greedy_set_cover(universe, subsets):
uncovered = set(universe)
chosen = []
while uncovered:
name = max(subsets, key=lambda s: len(subsets[s] & uncovered))
if not subsets[name] & uncovered:
return None # some element appears in no subset
chosen.append(name)
uncovered -= subsets[name]
return chosen
subsets = {
"S1": {1, 2, 3},
"S2": {4, 5, 6},
"S3": {1, 2, 4, 5},
}
print(greedy_set_cover(range(1, 7), subsets)) # ['S3', 'S1', 'S2']Greedy takes S3 first because it covers four new elements. That choice leaves 3 and 6 uncovered, and they live in different sets, so greedy needs three sets in total. The optimum is two: S1 and S2. As with coin change, the locally best choice does not lead to the global optimum.
The ln n Guarantee
Suppose the optimal cover uses k sets, and u elements are still uncovered. Those k sets together cover all u elements, so at least one of them covers at least u/k of them. Greedy picks a set at least that good. Each step therefore removes at least a 1/k fraction of what remains, which leaves at most u(1 − 1/k) uncovered. After about k ln n steps, fewer than one element is left. Greedy therefore uses at most about k ln n sets. A tighter analysis gives the bound H(n) = 1 + 1/2 + … + 1/n ≤ ln n + 1.
Other Greedy Approximations
| Problem | Greedy rule | Guarantee |
|---|---|---|
| Vertex cover | Take an uncovered edge and add both of its endpoints | 2 × optimum |
| Load balancing on m machines | Assign each job to the least-loaded machine | 2 × optimum |
| Load balancing, sorted (LPT) | Same rule, with the largest jobs first | 4/3 × optimum |
| 0/1 knapsack | Fill by best value per weight, then take the better of that fill and the single most valuable item | ≥ 1/2 of optimum |
| Metric k-center | Repeatedly add the point farthest from the centers chosen so far | 2 × optimum |
Common Errors
- Claiming optimality. A greedy approximation needs a ratio proof, not an exchange argument. Report the bound alongside the answer.
- Confusing ratio with typical behavior. The bound is a worst case. On real data greedy often does much better, so measure before you replace it.
- Forgetting infeasibility. If some element is in no subset, the loop never terminates unless you check for it, as the code above does.
Exercise
In the load-balancing greedy, why can the final makespan never exceed 2 × OPT? Use two facts: OPT ≥ (total work)/m and OPT ≥ the largest single job.
Show answer
Let machine i finish last, and let its last job have length t. When that job was assigned, machine i was the least loaded, so its load before t was at most (total work)/m ≤ OPT. Adding t ≤ OPT gives a makespan of at most OPT + OPT = 2 × OPT.
Exercise
Run greedysetcover on universe {1..6} with subsets A={1,2,3,4}, B={1,2,5}, C={3,4,6}, D={5,6}. What does it return, and is it optimal?
Show answer
A covers four elements and is picked first. Elements 5 and 6 remain, and D covers both, so the result is ['A', 'D']. Two sets is optimal, because no single subset covers all six elements.