Sub topic 2
Approximation Ratios: What a Guarantee Means
This chapter assumes you know Big-O notation, what it means for an algorithm to run in polynomial time, and the basic idea of NP-hardness. The previous chapter explained why exact algorithms for NP-hard optimization problems are impractical. Here we make "close to optimal" precise, so that later chapters can prove statements such as "this algorithm is never worse than twice the best possible."
The Setting: Instances, Solutions, Costs
An optimization problem has a set of instances. Each instance I has a set of feasible solutions, and each solution has a numeric value, called its cost for minimization or its profit for maximization. Two quantities matter:
OPT(I)is the value of the best feasible solution forI.ALG(I)is the value of the solution that algorithmAreturns onI.
An approximation algorithm runs in polynomial time and always returns a feasible solution. Its quality is measured by how ALG(I) compares with OPT(I) on every instance, including the worst one.
The Approximation Ratio
Minimization
For a minimization problem, A is a ρ-approximation (with ρ ≥ 1) if for every instance I:
OPT(I) <= ALG(I) <= rho * OPT(I)The left inequality always holds, because OPT is the minimum. The right inequality is the guarantee. A 2-approximation for vertex cover never returns a cover more than twice as large as the smallest one.
Maximization
For maximization the literature uses two conventions, and mixing them up is a common source of errors.
| Convention | Guarantee | Range of ρ | Example statement |
|---|---|---|---|
| Ratio at most 1 | `ALG(I) >= rho * OPT(I)` | 0 < ρ ≤ 1 | "1/2-approximation for Max-Cut" |
| Ratio at least 1 | `OPT(I) <= rho * ALG(I)` | ρ ≥ 1 | "2-approximation for Max-Cut" |
These two lines describe the same algorithm: one ρ is the reciprocal of the other. This tutorial uses the first convention for maximization, which is why later chapters speak of a 1/2-approximation for Max-Cut and a 7/8-approximation for MAX-3SAT. Under both conventions, a ratio closer to 1 means a better algorithm.
What "For Every Instance" Really Means
The ratio is a worst-case bound taken over all instances. Formally, the ratio of A for minimization is:
rho_A = sup over instances I of ALG(I) / OPT(I)An algorithm that is optimal on almost every input but terrible on one family of inputs has a terrible approximation ratio. The code below makes this concrete. It compares a naive vertex cover heuristic, "take every endpoint of every edge," with the true optimum found by brute force.
from itertools import combinations
def is_cover(edges, s):
return all(u in s or v in s for u, v in edges)
def opt_vertex_cover(n, edges):
for k in range(n + 1):
for s in combinations(range(n), k):
if is_cover(edges, set(s)):
return k
def naive_cover(edges):
return {x for e in edges for x in e}
def ratio(n, edges):
return len(naive_cover(edges)) / opt_vertex_cover(n, edges)
path = [(0, 1), (1, 2), (2, 3)]
print("path", ratio(4, path))
for leaves in [2, 4, 8, 12]:
star = [(0, i) for i in range(1, leaves + 1)]
print("star", leaves, ratio(leaves + 1, star))Output:
path 2.0
star 2 3.0
star 4 5.0
star 8 9.0
star 12 13.0On the path the heuristic looks like a 2-approximation. On a star with k leaves, the optimum is the single center vertex, but the heuristic takes all k + 1 vertices. The ratio grows without bound, so this heuristic has no constant approximation ratio. Testing on a few friendly instances would never have revealed this. That is why guarantees are proved and not measured.
Variations on the Basic Definition
Ratios that depend on input size
Sometimes no constant ρ is achievable, and the ratio is a function ρ(n) of the input size. Greedy set cover, covered later, guarantees a cover at most H_n ≈ ln n + 1 times optimal, where n is the number of elements. A ρ(n)-approximation still says something useful when ρ(n) grows slowly.
Expected ratios for randomized algorithms
A randomized algorithm's output is a random variable. The usual guarantee bounds its expectation: E[ALG(I)] >= rho * OPT(I) for maximization. This is a statement about the average over the algorithm's coin flips on a fixed instance. It is not an average over instances, which are still chosen adversarially. Repeating the algorithm and keeping the best result turns an expected guarantee into one that holds with high probability.
Tightness
A ratio ρ is tight for an algorithm if there are instances where ALG/OPT equals ρ or comes arbitrarily close to it. Tightness is about the analysis, not the problem. A tight example shows you cannot prove a better bound for this algorithm. A different algorithm might still do better. When you see a new algorithm, look for its tight example. It usually shows exactly where the proof loses ground.
Asymptotic ratio
For some problems the worst ratio occurs only on tiny instances. The asymptotic ratio ignores them: ALG(I) <= rho * OPT(I) + c for a constant c. Bin packing is the classic case. Deciding whether items fit in 2 bins is NP-hard, so no algorithm achieves a ratio below 3/2. Asymptotically, though, much better ratios are possible.
Multiplicative vs Additive Guarantees
An additive guarantee ALG(I) <= OPT(I) + c is stronger than any multiplicative one when it exists, but for most NP-hard problems it is impossible. The scaling argument shows why. Multiply every weight in a vertex cover instance by a large factor M. An additive error of c on the scaled instance becomes an error of c/M on the original. When M is large enough and the weights are integers, that error is below 1, which means the original instance has been solved exactly. Multiplicative ratios survive scaling, since ALG/OPT stays the same when both sides are multiplied by M. This is one reason the ratio is the standard measure.
A Pitfall: Equivalent Problems Approximate Differently
In a graph with n vertices, a set S is a vertex cover exactly when its complement is an independent set. So:
max independent set = n - min vertex coverFor exact solving, the two problems are the same problem. For approximation, they are very different. Take a graph with n = 100 vertices that forms a perfect matching of 50 disjoint edges. The minimum vertex cover has size 50. A 2-approximation may return all 100 vertices, which is allowed because 100 ≤ 2 · 50. The complement is the empty set, an independent set of size 0, while the optimum is 50. A good ratio for one problem gave a useless ratio for the other.
The reason is that ratios do not survive subtracting from a constant. In general, maximum independent set cannot be approximated within n^(1-ε) for any ε > 0 unless P = NP, while vertex cover has a simple 2-approximation.
Common Errors
- Reversing the inequality. For minimization, ρ multiplies OPT on the upper side. Writing
ALG >= rho * OPTwith ρ ≥ 1 for a minimization problem says nothing useful. - Mixing maximization conventions. "2-approximation for Max-Cut" and "1/2-approximation for Max-Cut" mean the same thing. Check which convention a source uses before comparing results.
- Forgetting that OPT may be zero. If
OPT(I) = 0, any multiplicative guarantee for minimization forcesALG(I) = 0. Either handle that case exactly or state the guarantee only forOPT > 0. - Treating the ratio as typical performance. A 2-approximation often lands within a few percent of optimal in practice. The ratio is a ceiling on how bad it can get, not a prediction.
- Confusing tightness of an analysis with a lower bound for the problem. A tight example rules out a better proof for one algorithm. Ruling out every algorithm requires hardness-of-approximation results, which come at the end of this tutorial.
Exercise
A minimization algorithm returns solutions of cost 30, 12, and 45 on three instances whose optimal costs are 20, 12, and 25. Based only on these instances, what is the smallest ρ consistent with the claim that it is a ρ-approximation, and can you conclude that it is a 1.8-approximation?
Show answer
The ratios are 1.5, 1.0, and 1.8, so any valid ρ must be at least 1.8. You cannot conclude that it is a 1.8-approximation. These three instances only give a lower bound on ρ. Another instance could have a larger ratio, so proving the guarantee requires an argument that covers every instance.
Exercise
A randomized algorithm for a maximization problem satisfies E[ALG(I)] >= 0.75 OPT(I) on every instance. Express this guarantee under the "ratio at least 1" convention, and explain whether a single run is guaranteed to reach 0.75 OPT(I).
Show answer
Under the other convention it is a 4/3-approximation in expectation, because OPT(I) <= (4/3) E[ALG(I)]. A single run is not guaranteed to reach 0.75 OPT(I). The bound applies to the average over the algorithm's random choices, so some runs may fall below it. Running the algorithm several times and keeping the best result makes success likely, but the guarantee itself only concerns the expectation.
Exercise
Explain why a vertex cover algorithm with guarantee ALG <= OPT + 1 on all graphs would imply P = NP. Hint: copy the graph.
Show answer
Take k + 2 disjoint copies of the input graph G, for example 3 copies (so k = 1). The optimum on the combined graph is 3 OPT(G). Run the algorithm to get a cover of size at most 3 OPT(G) + 1. Its restriction to the smallest of the three copies has size at most OPT(G) + 1/3, and since sizes are integers this is exactly OPT(G). That gives an exact polynomial-time algorithm for an NP-hard problem. Copying the graph plays the same role as scaling the weights.