TExcelTAD
Home › Approximation Algorithms › Approximation Algorithms: What This Course Covers

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:

  1. 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.
  2. 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.
  3. When is a factor the best possible? Hardness of approximation turns NP-hardness into statements such as "no polynomial algorithm satisfies more than 78+ε\frac{7}{8} + \varepsilon 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 pip_i and sizes sis_i, a capacity BB; choose items of total size at most BB 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 pi/sip_i / s_i 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 (si≤Bs_i \le B), the better of the two has profit at least 12 OPT\frac{1}{2}\,\text{OPT}.

Proof. Let greedy stop at the first item kk that does not fit. Items 1,…,k1, \ldots, k in density order overfill the knapsack, and filling a knapsack of size BB fractionally by density is optimal for the fractional problem, so p1+⋯+pk≥OPTfrac≥OPTp_1 + \cdots + p_k \ge \text{OPT}_{\text{frac}} \ge \text{OPT}. The greedy solution has profit at least p1+⋯+pk−1p_1 + \cdots + p_{k-1}, and the best single item has profit at least pkp_k. Two numbers whose sum is at least OPT cannot both be below 12OPT\frac{1}{2}\text{OPT}. □\square

The program runs both on random instances and compares with the exact optimum from dynamic programming.

python · try it yourself
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 12\frac{1}{2}-approximation; the lesson A Knapsack FPTAS by Profit Scaling gets within 1−ε1 - \varepsilon for any ε>0\varepsilon > 0.

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.

python · try it yourself
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 (nn/2)\binom{n}{n/2}, 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 (0,1),(2,3),…(0,1), (2,3), \ldots and takes all nn vertices, exactly twice the optimum n2\frac{n}{2}. 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 HnH_n; metric TSP 2 and 32\frac{3}{2} (Christofides, 1976); Steiner tree 2; kk-center 2; list scheduling 2−1m2 - \frac{1}{m} and LPT 43−13m\frac{4}{3} - \frac{1}{3m} (Graham, 1966 and 1969) greedy, matchings, spanning trees
LP rounding weighted vertex cover 2; set cover O(ln⁡n)O(\ln n) by randomized rounding; MAX-SAT 1−1e1 - \frac{1}{e} and 34\frac{3}{4} 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 78\frac{7}{8} (Håstad, 2001); clique n1−εn^{1-\varepsilon}; set cover (1−ε)ln⁡n(1-\varepsilon)\ln n (Dinur and Steurer, 2014); the Unique Games Conjecture gap-introducing and gap-preserving reductions

Conventions

  • An instance is II; OPT(I)\text{OPT}(I) or just OPT is its optimal value and ALG(I)\text{ALG}(I) the value of the algorithm's solution.
  • For a minimization problem the ratio ρ≥1\rho \ge 1 means ALG≤ρ⋅OPT\text{ALG} \le \rho \cdot \text{OPT}; for a maximization problem ρ≤1\rho \le 1 means ALG≥ρ⋅OPT\text{ALG} \ge \rho \cdot \text{OPT}. This is the convention of Williamson and Shmoys. Some books state maximization ratios as 1ρ≥1\frac{1}{\rho} \ge 1; a "2-approximation for max-cut" there is a 12\frac{1}{2}-approximation here.
  • Every hardness result is stated with its assumption: P ≠\ne NP, a stronger assumption such as NP ⊈\not\subseteq DTIME(nO(log⁡log⁡n))(n^{O(\log \log n)}), 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?

  1. It is always faster
  2. It runs in polynomial time and has a proven bound on the ratio between its value and the optimum, valid for every instance
  3. It always finds the optimum on random inputs
  4. It uses linear programming

In this course's convention, what does a 78\frac{7}{8}-approximation for a maximization problem guarantee?

  1. ALG≤78 OPT\text{ALG} \le \frac{7}{8}\,\text{OPT}
  2. ALG≥78 OPT\text{ALG} \ge \frac{7}{8}\,\text{OPT} on every instance
  3. ALG≥87 OPT\text{ALG} \ge \frac{8}{7}\,\text{OPT}
  4. The algorithm is optimal on 78\frac{7}{8} of the instances

Why does the better of greedy-by-density and the best single item reach at least half of OPT for knapsack?

  1. Greedy alone is always optimal
  2. 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
  3. The best single item is always at least OPT/2
  4. Because profits are integers