Approximation Algorithms: What This Course Covers
Most optimization problems that matter in practice are NP-hard: covering a set of demands with the fewest resources, routing a vehicle through all its stops, packing items into bins, scheduling jobs on machines. Part I of the track ended with the proofs (NP-Completeness and NP-Hardness) and a first look at the way out (Coping with NP-Hardness). This course takes that way out seriously. An approximation algorithm runs in polynomial time and returns a solution whose value is provably within a known factor of the optimum, on every input. The factor is a theorem, not an observation from experiments.
Three questions run through the whole course:
- How do we bound an optimum we cannot compute? Every guarantee compares the algorithm with a quantity that is provably on the right side of OPT: a matching for vertex cover, a minimum spanning tree for TSP, the average load for scheduling, and above all the value of a linear programming relaxation and its dual.
- Which design techniques give good factors? Greedy choices, LP rounding (deterministic and randomized), the primal-dual method, and approximation schemes that trade running time for accuracy.
- When is a factor the best possible? Hardness of approximation turns NP-hardness into statements such as "no polynomial algorithm satisfies more than of the clauses of a satisfiable 3-CNF formula unless P = NP". Its main tool is the PCP theorem.
The topics follow the core of a graduate course on the subject, in the order of Williamson and Shmoys' The Design of Approximation Algorithms (2011) and Vazirani's Approximation Algorithms (2001). The text, proofs as presented here and all programs are written for this site. The LP machinery comes from the previous course, Linear Programming & Duality for Algorithms; this course links to it instead of repeating it.
A first example: half of the best knapsack
The 0-1 knapsack problem: items with profits and sizes , a capacity ; choose items of total size at most with the largest total profit. It is NP-hard, although a pseudo-polynomial dynamic program solves it when numbers are small (0/1 Knapsack in the practice area).
The natural greedy algorithm sorts items by profit density and takes them while they fit. On its own it can be terrible: one tiny item of density 2 and one huge item of density 1 that fills the knapsack exactly make greedy take the tiny item and stop. The fix is one line: return the better of the greedy solution and the single most profitable item that fits.
Claim. If every item fits on its own (), the better of the two has profit at least .
Proof. Let greedy stop at the first item that does not fit. Items in density order overfill the knapsack, and filling a knapsack of size fractionally by density is optimal for the fractional problem, so . The greedy solution has profit at least , and the best single item has profit at least . Two numbers whose sum is at least OPT cannot both be below .
The program runs both on random instances and compares with the exact optimum from dynamic programming.
import random
def exact(items, B):
best = [0] * (B + 1) # best[c] = max profit with capacity c
for p, s in items:
for c in range(B, s - 1, -1):
best[c] = max(best[c], best[c - s] + p)
return best[B]
def greedy(items, B):
total, room = 0, B
for p, s in sorted(items, key=lambda t: -t[0] / t[1]):
if s <= room:
total, room = total + p, room - s
return total
def better_of_two(items, B):
return max(greedy(items, B), max(p for p, s in items if s <= B))
rng = random.Random(7)
print("trial OPT greedy better-of-two ratio")
worst = 1.0
for trial in range(6):
B = 50
items = [(rng.randint(1, 30), rng.randint(1, 40)) for _ in range(8)]
o, g, b = exact(items, B), greedy(items, B), better_of_two(items, B)
worst = min(worst, b / o)
print(f"{trial:5} {o:5} {g:7} {b:14} {b / o:6.3f}")
trap = [(2, 1), (50, 50)] # density 2 and density 1
print("trap instance: OPT", exact(trap, 50), " greedy", greedy(trap, 50), " better-of-two", better_of_two(trap, 50))
print("worst random ratio", round(worst, 3), "(guaranteed at least 0.5)")trial OPT greedy better-of-two ratio
0 69 54 54 0.783
1 43 41 41 0.953
2 65 65 65 1.000
3 88 88 88 1.000
4 53 53 53 1.000
5 63 63 63 1.000
trap instance: OPT 50 greedy 2 better-of-two 50
worst random ratio 0.783 (guaranteed at least 0.5)
The last lines show both halves of the story. On random inputs greedy is usually close to optimal, but a two-item instance makes plain greedy lose almost everything, and the guarantee is a statement about every instance. The better-of-two rule is a -approximation; the lesson A Knapsack FPTAS by Profit Scaling gets within for any .
Exact search does not scale
Brute force is still the referee in this course: every lesson checks its algorithm against the true optimum on small instances. That only works because the instances are small. The program counts how many subsets an exhaustive vertex cover search examines on a cycle as the number of vertices grows, against the number of edge scans of the matching-based 2-approximation from Vertex Cover from a Maximal Matching.
from itertools import combinations
def exhaustive(n, edges):
tried = 0
for k in range(n + 1):
for C in combinations(range(n), k):
tried += 1
s = set(C)
if all(u in s or v in s for u, v in edges):
return k, tried
def matching_cover(edges):
cover, scans = set(), 0
for u, v in edges:
scans += 1
if u not in cover and v not in cover:
cover |= {u, v}
return len(cover), scans
for n in [8, 12, 16, 18]:
edges = [(i, (i + 1) % n) for i in range(n)]
k, tried = exhaustive(n, edges)
a, scans = matching_cover(edges)
print(f"cycle C_{n:<2}: OPT {k:2} after {tried:7} subsets; approx {a:2} after {scans:2} edge scans")cycle C_8 : OPT 4 after 114 subsets; approx 8 after 8 edge scans
cycle C_12: OPT 6 after 1873 subsets; approx 12 after 12 edge scans
cycle C_16: OPT 8 after 30415 subsets; approx 16 after 16 edge scans
cycle C_18: OPT 9 after 122284 subsets; approx 18 after 18 edge scans
The subset count grows roughly like , about doubling with every extra vertex, while the approximation stays linear. Here it also shows its worst case: scanning the cycle in order, it matches every edge and takes all vertices, exactly twice the optimum . The guarantee says it is never worse than that.
The map of the course
| Section | Main results | Technique |
|---|---|---|
| Foundations | approximation ratios for minimization and maximization; PTAS, FPTAS, APX | lower and upper bounds on OPT |
| Combinatorial algorithms | vertex cover 2; greedy set cover ; metric TSP 2 and (Christofides, 1976); Steiner tree 2; -center 2; list scheduling and LPT (Graham, 1966 and 1969) | greedy, matchings, spanning trees |
| LP rounding | weighted vertex cover 2; set cover by randomized rounding; MAX-SAT and | solve the relaxation, then round |
| Primal-dual | vertex cover 2; Steiner forest 2 (Agrawal, Klein and Ravi, 1995; Goemans and Williamson, 1995); metric facility location 3 (Jain and Vazirani, 2001) | grow a dual, buy what becomes tight |
| Approximation schemes | knapsack FPTAS; bin packing (first fit, FFD, an asymptotic PTAS); Euclidean TSP PTAS (Arora, 1998; Mitchell, 1999) | rounding the input, dynamic programming |
| Hardness | no constant factor for general TSP; the PCP theorem; MAX-3SAT (Håstad, 2001); clique ; set cover (Dinur and Steurer, 2014); the Unique Games Conjecture | gap-introducing and gap-preserving reductions |
Conventions
- An instance is ; or just OPT is its optimal value and the value of the algorithm's solution.
- For a minimization problem the ratio means ; for a maximization problem means . This is the convention of Williamson and Shmoys. Some books state maximization ratios as ; a "2-approximation for max-cut" there is a -approximation here.
- Every hardness result is stated with its assumption: P NP, a stronger assumption such as NP DTIME, or the Unique Games Conjecture. Asymptotic guarantees (for large OPT) are marked as such.
- Proofs given in full are marked Proof; results only sketched say so, and long sketches sit in collapsible boxes.
- Programs are seeded and deterministic. They print the algorithm's value, the exact optimum (brute force, dynamic programming or SciPy's
milp) and the ratio. C++ versions of the core algorithms use C++20 and the standard library only.
Test yourself
What distinguishes an approximation algorithm from a heuristic?
- It is always faster
- It runs in polynomial time and has a proven bound on the ratio between its value and the optimum, valid for every instance
- It always finds the optimum on random inputs
- It uses linear programming
In this course's convention, what does a -approximation for a maximization problem guarantee?
- on every instance
- The algorithm is optimal on of the instances
Why does the better of greedy-by-density and the best single item reach at least half of OPT for knapsack?
- Greedy alone is always optimal
- The greedy items up to and including the first one that does not fit have total profit at least OPT, and the two candidates together cover that total
- The best single item is always at least OPT/2
- Because profits are integers