Approximation Algorithms
Provably near-optimal answers when optimal is out of reach.
Finding a smallest vertex cover is NP-hard, yet a ten-line loop always returns a cover at most twice as large as the smallest one, without ever learning how large the smallest one is. Every approximation algorithm runs on the same trick: compare your answer with a bound on the optimum that you can compute.
What you’ll learn
- Define the approximation ratio for minimization and maximization problems, and prove a ratio by comparing the answer with a computable lower bound on the optimum.
- Prove that a maximal matching gives a 2-approximation for vertex cover, and that greedy set cover is within of optimal.
- Prove the 2-approximation for metric TSP by tree doubling, state Christofides' , and show that TSP without the triangle inequality has no constant-factor approximation unless P = NP.
- Analyze greedy load balancing () and LPT, and place problems on the map of FPTAS, PTAS, APX-hard and inapproximable.
- Recognize when a problem calls for an approximation algorithm, a heuristic, or an exact algorithm after all.
Ratios and the lower-bound trick
Many optimization problems in this course have fast exact algorithms: shortest paths, minimum spanning trees, maximum flows. For NP-hard ones such as vertex cover, set cover and the traveling salesman problem, no polynomial-time algorithm is known, and the lesson P, NP & NP-Completeness explains why none is expected. Brute force still works, but trying all subsets of vertices is hopeless beyond a few dozen vertices.
An approximation algorithm gives up exactness, not speed and not rigor: it runs in polynomial time and comes with a proof that its answer is never far from optimal.
The catch: you cannot compute , so how can you prove anything about it? Find a quantity that you can compute in polynomial time and that never exceeds the optimum, then show that your answer is at most times that bound:
| Problem | Lower bound on OPT | Why it is a lower bound | Ratio it yields |
|---|---|---|---|
| Vertex cover | the size of a maximal matching | a cover needs a different endpoint for each edge of | 2 |
| Metric TSP | the weight of a minimum spanning tree | deleting one edge of a tour leaves a spanning tree | 2 (tree doubling), (Christofides) |
| Load balancing on machines | some machine runs the longest job, and some machine gets at least the average load | ||
| Weighted covering problems | the optimum of a linear-programming relaxation | relaxing the constraints can only lower a minimum | 2 for vertex cover, for set cover |
Your algorithm returns a vertex cover of 90 vertices on a graph in which you have also found a matching of 50 edges. What do you know about the size of a minimum cover?
Show the answer
B.
Each of the 50 matching edges needs its own endpoint in every cover, so ; your answer is a cover, so . On this instance your cover is within of optimal.
Vertex cover: twice a maximal matching
A vertex cover of a graph is a set that touches every edge. Finding a smallest one is NP-hard, and brute force tries up to subsets. The approximation is almost embarrassingly simple: while some edge is uncovered, put both and into the cover.
def vertex_cover_2approx(edges):
cover = set()
for u, v in edges: # scan the edges once, in any order
if u not in cover and v not in cover:
cover.add(u) # (u, v) joins the matching M:
cover.add(v) # take both of its endpoints
return cover
path = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
print(sorted(vertex_cover_2approx(path))) # [0, 1, 2, 3, 4, 5], while {1, 3, 5} sufficesOne pass, time. The edges that trigger an insertion form a matching , since no two of them share an endpoint, and it is maximal: every other edge touches one of them, or it would have been taken.
is a cover. An edge is skipped only when one of its endpoints is already in , and every edge that is not skipped puts both endpoints in.
Lower bound. The edges of share no endpoints, so every cover, an optimal one included, contains a different vertex for each of them:
Upper bound. The algorithm adds exactly two vertices per matching edge:
The analysis is tight: on disjoint edges, or on the complete bipartite graph , the algorithm takes all vertices while suffice. Nobody knows a polynomial-time algorithm with ratio for any constant , and the Optimal lesson Hardness of Approximation & the PCP Theorem explains why 2 may be the truth.
Why does the algorithm take both endpoints of an uncovered edge instead of just one?
- Taking one endpoint could leave the edge uncovered
- Every cover must contain or , and you do not know which, so taking both pays at most twice what the optimum pays for that edge
- Taking both endpoints is faster than deciding between them
- It does not matter: taking one endpoint, chosen arbitrarily, also guarantees ratio 2
Show the answer
B. Every cover must contain or , and you do not know which, so taking both pays at most twice what the optimum pays for that edge
The charging argument needs each chosen pair to contain a vertex that the optimum also pays for, and disjoint pairs to be charged to different optimal vertices. With one arbitrary endpoint the guarantee breaks: on a star, always picking the leaf takes all leaves, while the center alone is a cover.
Greedy set cover and the ln n barrier
In set cover you are given a universe of elements and a family of subsets whose union is , and you want the fewest subsets whose union is still . It models choosing sensors that watch every room, tests that exercise every feature, or fire stations that reach every street. Vertex cover is the special case in which the elements are edges and each vertex is the set of edges it touches.
The natural greedy rule: repeatedly take the set that covers the most elements not yet covered.
def greedy_set_cover(universe, sets):
uncovered, chosen = set(universe), []
while uncovered:
best = max(range(len(sets)), key=lambda i: len(sets[i] & uncovered))
if not sets[best] & uncovered:
return None # some element is in no set
chosen.append(best)
uncovered -= sets[best]
return chosenEach round scans all sets, and there are at most rounds. Keeping the sets in buckets by their number of uncovered elements brings the whole run down to .
Spread the cost. When greedy takes a set that covers new elements, charge each of them a price of . The prices add up to the number of sets chosen:
Price of one element. Number the elements in the order greedy covers them. Just before the round that covers , at least elements are uncovered. The sets of an optimal solution cover all of them, so one of those sets covers at least of them, and greedy's choice covers at least as many:
Sum over all elements:
Greedy really can be about times worse. Split elements into blocks with , offer each block as a set, and add two sets and holding the first and second halves of every block. , so . But covers elements and each only , so greedy takes first, then for the same reason, and so on: sets. With , greedy takes 4 sets to cover 30 elements that 2 sets cover.
Greedy set cover runs on a universe of 1,000 elements, so its guarantee is the factor . Estimate it, using .
Show the answer
7.49
, so : greedy never uses more than about 7.5 times the optimal number of sets. The factor grows only logarithmically; for a million elements it is about 14.4.
Metric TSP: doubling a spanning tree
The traveling salesman problem (TSP) asks for the shortest tour that visits every city once and returns to the start. Here the distances matter. In metric TSP they are symmetric and obey the triangle inequality , as straight-line and shortest road distances do: a detour never makes a trip shorter.
The lower bound is a minimum spanning tree . Delete any edge of an optimal tour and you are left with a path through all the cities, which is a spanning tree, so .
Walk around the tree. A depth-first walk from any city traverses each edge of twice, once down and once back, and ends where it started:
Shortcut. The walk revisits cities. Skip every city already seen and jump straight to the next new one. By the triangle inequality, applied repeatedly, a jump from to costs no more than the stretch of walk that it replaces:
Result. The shortcut walk visits the cities in depth-first preorder, each once: a tour.
Christofides' algorithm (1976; found independently by Serdyukov, 1978) repairs the tree more cheaply. What stops a tree from being walked as a closed trail that uses each edge once is its odd-degree vertices. Instead of doubling every edge, add a minimum-weight perfect matching on the odd-degree vertices only. That matching costs at most , so the final tour costs at most . The full proof, and the 2021 improvement to for some , are in the Optimal lesson Approximating the Traveling Salesman.
Take any graph on vertices, an instance of the NP-complete Hamiltonian cycle problem, and build a TSP instance on the same vertices:
If has a Hamiltonian cycle, that cycle is a tour of length . If not, every tour uses at least one non-edge of :
An -approximation returns at most in the first case and more than in the second, so comparing its answer with decides Hamiltonicity in polynomial time, and P = NP.
These distances violate the triangle inequality wildly: a non-edge costs far more than a two-step detour through a neighbor, and a detour is exactly what shortcutting refuses to pay for.
Where exactly does the tree-doubling proof use the triangle inequality?
- In , since deleting a tour edge gives a spanning tree
- In the doubled walk costing exactly
- In the shortcut step: jumping past cities already visited must not lengthen the walk
- Nowhere: the proof works for any distances
Show the answer
C. In the shortcut step: jumping past cities already visited must not lengthen the walk
A tour minus an edge is a spanning path for any non-negative distances, and the doubled walk costs in any graph. Only replacing a stretch of the walk by one direct jump needs .
Load balancing, and the landscape of ratios
You have identical machines and jobs with processing times , and you want to minimize the makespan, the time at which the last machine finishes. The problem is NP-hard already for , where it contains the partition problem. Graham's list scheduling (1966) places each job, in the given order, on the machine with the least load so far.
import heapq
def list_schedule(times, m):
loads = [(0, i) for i in range(m)] # (load, machine): a min-heap
for t in times:
load, i = heapq.heappop(loads) # the least-loaded machine
heapq.heappush(loads, (load + t, i))
return max(load for load, _ in loads) # the makespan
m = 4
print(list_schedule([1] * (m * (m - 1)) + [m], m)) # 7, while OPT = 4
print(list_schedule(sorted([3, 3, 2, 2, 2], reverse=True), 2)) # LPT: 7, while OPT = 6with a heap. In the first call, twelve unit jobs fill the four machines to load 3, and the long job lands on top: . The optimum puts the long job alone on one machine and four unit jobs on each of the others: makespan 4.
Two lower bounds. Some machine runs the longest job, and the total work , spread over machines, leaves some machine with at least the average:
The last job. Let machine finish last, with load , and let be the last job placed on it. When was placed, machine had the smallest load, , so every machine had at least that much, and only jobs other than had been placed:
Combine the two:
The first example in the code meets this bound exactly, : the long job arrives last. Sorting the jobs longest first, the LPT rule, fixes exactly that. Graham (1969) proved that LPT's makespan is at most , again tight: the second example gives .
| Problem | Best ratio known | Known limit (unless P = NP) |
|---|---|---|
| 0/1 knapsack | FPTAS | only exactness is NP-hard |
| Makespan on identical machines | PTAS (Hochbaum & Shmoys, 1987) | no FPTAS: strongly NP-hard |
| Euclidean TSP in the plane | PTAS (Arora, 1998; Mitchell, 1999) | no FPTAS: strongly NP-hard |
| Vertex cover | 2 | (Khot, Minzer & Safra, 2018); if the Unique Games Conjecture holds |
| Metric TSP | , (Karlin, Klein & Oveis Gharan, 2021) | (Karpinski, Lampis & Schmied, 2015) |
| Set cover | (greedy) | (Dinur & Steurer, 2014) |
| Maximum clique, maximum independent set | little better than the trivial factor | (Håstad, 1999; Zuckerman, 2007) |
| TSP with arbitrary distances | none | every ratio (Sahni & Gonzalez, 1976) |
An algorithm for a minimization problem returns a -approximation in time, for every . What is it?
- An FPTAS
- A PTAS, but not an FPTAS
- Not a polynomial-time algorithm for any fixed
- Just a 2-approximation
Show the answer
B. A PTAS, but not an FPTAS
For each fixed the running time is a polynomial, for , so it is a PTAS. It is not an FPTAS, because sits in the exponent instead of in a polynomial factor.
Recognizing it
Reach for an approximation algorithm when these signals appear together:
- An NP-hard optimization core at a size exact search cannot touch: covering, packing, routing, scheduling or clustering with thousands of items. (With a few dozen items, exact methods from the lesson Coping with NP-Hardness may still work.)
- An objective that tolerates slack: a schedule 5% longer or a network 10% more expensive is acceptable. A yes/no question is not approximable in this sense.
- Metric structure: distances that obey the triangle inequality make TSP, facility location and clustering approximable within constants.
- A need for a guarantee, not just a good answer on average. Local search and other heuristics often do better in practice but promise nothing, so a common recipe runs both and uses the approximation's lower bound to certify the heuristic's answer.
| Problem, in brief | Key idea | Guarantee and cost |
|---|---|---|
| Place the fewest guards at street corners so that every street is watched | vertex cover: both endpoints of a maximal matching (LP rounding when corners have prices) | ratio 2, |
| Choose the fewest test cases that together exercise every feature | set cover: greedy by the most features not yet exercised | ratio , with buckets |
| Route a van through 2,000 addresses on a road network and back to the depot | metric TSP: Christofides for the guarantee, then 2-opt moves, which only shorten the tour | ratio ; typically within a few percent after polishing |
| Spread 10,000 render jobs over 64 identical servers to finish as early as possible | makespan: LPT, longest job first onto the least-loaded server | ratio , |
Common bugs.
- Proving a ratio against something that is not a lower bound: the spanning tree of only some of the cities, or a set of edges that is not a matching.
- Mixing conventions: for maximization, "ratio 2" means at least half the optimum; for minimization, at most twice it.
- Shortcutting a walk when distances are asymmetric or break the triangle inequality: the 2 and guarantees silently vanish.
- Edge cases: no jobs (makespan 0), one city (a tour of length 0), two cities (twice their distance), an element that no set contains (no cover exists).
- Trusting a worst-case ratio as a forecast: the matching algorithm is often beaten in practice by LP rounding plus a cleanup pass that drops redundant vertices.
A logistics team must route one truck through 3,000 stops on a road network whose travel times obey the triangle inequality. They want a route provably at most 50% longer than optimal, computed within minutes. What should they use?
- Held–Karp dynamic programming, to get the exact optimum
- Nearest neighbor alone, since it is the fastest
- Tree doubling, since it is the only algorithm here with a guarantee
- Christofides' algorithm, then 2-opt moves that can only shorten the tour
Show the answer
D. Christofides' algorithm, then 2-opt moves that can only shorten the tour
Christofides guarantees at most 1.5 times the optimum on metric instances in polynomial time, and every 2-opt move shortens the tour, so the guarantee survives the polishing.
Key takeaways
- An -approximation runs in polynomial time and is within a factor of optimal on every instance. Its proof compares the answer with a computable lower bound on : a matching, a spanning tree, an average load or an LP optimum.
- Both endpoints of a maximal matching form a vertex cover at most twice the minimum; greedy set cover is within , and beating is NP-hard.
- For metric TSP, tree doubling gives 2 and Christofides ; without the triangle inequality, no ratio at all is achievable unless P = NP.
- List scheduling gives for makespan and LPT . Problems range from FPTAS (knapsack) and PTAS (Euclidean TSP) to APX-hard (vertex cover, metric TSP) and inapproximable (clique, general TSP).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
The matching algorithm returns a vertex cover of 40 vertices. Which statement is guaranteed?
- The minimum cover has exactly 20 vertices
- The minimum cover has at least 20 and at most 40 vertices
- The minimum cover has at most 20 vertices
- The minimum cover has at least 40 vertices
Show the answer
B. The minimum cover has at least 20 and at most 40 vertices
The algorithm took 20 matching edges, and every cover needs a different vertex for each of them, so . The returned set is itself a cover, so .
Why is the weight of a minimum spanning tree a lower bound on the length of an optimal tour, for any non-negative distances?
- Every tour contains a minimum spanning tree
- Because of the triangle inequality
- Deleting one edge from an optimal tour leaves a spanning path, which is a spanning tree and so weighs at least as much as a minimum one
- A tour and a tree both visit every city exactly once
Show the answer
C. Deleting one edge from an optimal tour leaves a spanning path, which is a spanning tree and so weighs at least as much as a minimum one
An optimal tour minus any edge is a path through all the cities: one particular spanning tree. A minimum spanning tree weighs at most as much, so .
A TSP instance has arbitrary positive distances, not necessarily obeying the triangle inequality. Assuming P ≠ NP, what can a polynomial-time algorithm guarantee?
- A factor 2, by tree doubling
- A factor , by Christofides' algorithm
- A factor , by greedy
- No constant factor at all
Show the answer
D. No constant factor at all
Give the edges of a graph distance 1 and its non-edges distance . The optimal tour costs if has a Hamiltonian cycle and more than otherwise, so an -approximation would decide Hamiltonicity, an NP-complete problem.
Greedy set cover is within of optimal, and approximating set cover within is NP-hard. What do the two results say together?
- Greedy finds an optimal cover on every instance
- Unless P = NP, no polynomial-time algorithm has a worst-case ratio substantially better than greedy's
- Greedy's analysis is loose by a factor of
- Set cover can be solved exactly in polynomial time when is small
Show the answer
B. Unless P = NP, no polynomial-time algorithm has a worst-case ratio substantially better than greedy's
The upper bound and the lower bound meet up to lower-order terms, so greedy's worst-case guarantee is essentially the best possible for polynomial-time algorithms, assuming P ≠ NP.
Assuming P ≠ NP, which of these problems has a fully polynomial-time approximation scheme (FPTAS)?
- 0/1 knapsack
- Bin packing
- Metric TSP
- Vertex cover
Show the answer
A. 0/1 knapsack
Knapsack is only weakly NP-hard: its dynamic program is pseudo-polynomial, and rounding the values gives a -approximation in time polynomial in and . The other three are strongly NP-hard or APX-hard, which rules out an FPTAS.
Go deeper
- LP Relaxation & Rounding · Solve the relaxed problem, then round: vertex cover, set cover and integrality gaps.
- Approximating the Traveling Salesman · Tree doubling, Christofides, and the 2021 improvement.
- Hardness of Approximation & the PCP Theorem · Why some problems cannot even be approximated well.
Further reading
- D. P. Williamson & D. B. Shmoys, The Design of Approximation Algorithms (Cambridge University Press, 2011), chapters 1–3.
- V. V. Vazirani, Approximation Algorithms (Springer, 2001).
- J. Kleinberg & É. Tardos, Algorithm Design (Addison-Wesley, 2005), chapter 11.
- T. H. Cormen, C. E. Leiserson, R. L. Rivest & C. Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 35.