Sub topic 1
Why NP-Hard Optimization Calls for Approximation
Many practical problems ask for the best solution among a huge set of candidates. Examples include the fewest servers that monitor every network link, the shortest delivery route through 200 stops, and the most profitable set of items that fits within a budget. For many of these problems, no one knows a polynomial-time algorithm that always finds the best answer, and most researchers believe that none exists. This chapter explains why that is so and what we lose by trying to compute exact answers anyway. It also introduces the idea that the rest of the tutorial develops: algorithms that run fast and come with a proven bound on how far their answer can be from the best one.
Prerequisites
This chapter assumes you are comfortable with the following:
- Big-O notation, and the difference between polynomial running times such as O(n³) and exponential ones such as O(2ⁿ).
- Basic graph vocabulary: vertices, edges, trees, and matchings.
- The classes P and NP, NP-completeness, and polynomial-time reductions at the level of a first algorithms course.
- Basic discrete probability. Later chapters use expectation and linearity of expectation.
- Reading and running short Python programs.
Optimization Problems and Their Decision Versions
An optimization problem asks for a feasible solution that minimizes or maximizes an objective. Minimum vertex cover is a standard example. You are given a graph G = (V, E), and you must find a smallest set S ⊆ V such that every edge has at least one endpoint in S.
NP-completeness is defined for decision problems, which have yes/no answers. The decision version of vertex cover asks: "Does G have a vertex cover of size at most k?" This question is NP-complete.
The two versions are closely linked:
- If you could solve the optimization version in polynomial time, you could answer the decision version by computing the optimum and comparing it with k.
- If you could solve the decision version in polynomial time, you could find the optimal value by binary search on k. That takes O(log n) calls. Fixing vertices one at a time then recovers an optimal set.
An optimization problem is NP-hard when a polynomial-time algorithm for it would give polynomial-time algorithms for every problem in NP. Minimum vertex cover, set cover, the traveling salesman problem (TSP), makespan scheduling, 0/1 knapsack, and MAX-SAT are all NP-hard. Unless P = NP, none of them has an algorithm that is exact, runs in polynomial time, and works on every instance.
What Exact Search Costs
The direct exact method tries candidate solutions until it finds the best one. For vertex cover, that means testing subsets of vertices in order of increasing size. The program below does this and counts how many subsets it examines.
from itertools import combinations
def exact_vertex_cover(n, edges):
checked = 0
for k in range(n + 1):
for subset in combinations(range(n), k):
checked += 1
s = set(subset)
if all(u in s or v in s for u, v in edges):
return s, checked
return set(range(n)), checked
def cycle(n):
return [(i, (i + 1) % n) for i in range(n)]
for n in [8, 12, 16, 20]:
cover, checked = exact_vertex_cover(n, cycle(n))
print(f"n={n:2d} optimum={len(cover):2d} subsets checked={checked}")On a cycle with n vertices the optimum is ⌈n/2⌉. The search must reject every smaller subset before it finds one that works, so the count grows roughly like 2ⁿ. For n = 20 the program already checks more than 200,000 subsets. Smarter exact methods help a lot in practice. Branch-and-bound, integer programming solvers, and fixed-parameter algorithms that run in O(1.28ᵏ + kn) for a cover of size k all fall in this group. None of them escapes exponential worst-case growth.
The table shows what exponential growth means at a generous rate of one billion candidates per second.
| n | 2ⁿ candidates | Time at 10⁹ per second |
|---|---|---|
| 20 | about 1.0 × 10⁶ | about 1 millisecond |
| 40 | about 1.1 × 10¹² | about 18 minutes |
| 60 | about 1.2 × 10¹⁸ | about 36 years |
| 100 | about 1.3 × 10³⁰ | about 4 × 10¹³ years |
Faster hardware does not change this much. A machine 1000 times faster adds only about 10 to the largest n you can handle, because 2¹⁰ ≈ 1000.
The Options When a Problem Is NP-Hard
When you face an NP-hard optimization problem, you have four broad choices.
- Exact exponential algorithms. These give correct answers on small or well-structured instances, but their running time is unpredictable.
- Restricting the input. You solve a special case exactly, such as trees, planar graphs, or bounded parameters, if your inputs really have that structure.
- Heuristics. These are fast methods, such as simulated annealing or ad hoc greedy rules, that often work well in practice. They give no guarantee, so on some inputs they can be arbitrarily bad, and you cannot tell from the output alone.
- Approximation algorithms. These run in polynomial time on every input and have a proven bound on the quality of the solution.
These choices can be combined. A common engineering pattern runs an approximation algorithm to get a guaranteed solution and an upper bound on cost. Then it uses that bound to prune an exact search, or it hands the solution to a heuristic that tries to improve it.
What an Approximation Algorithm Promises
An approximation algorithm makes two promises, and both must hold on every input:
- Efficiency: it runs in time polynomial in the input size.
- Quality: its solution is within a known factor of the optimum. For a minimization problem with optimal cost OPT, a factor-ρ algorithm returns cost at most ρ · OPT.
Chapter 2 makes the definition precise, including the conventions for maximization problems. For now, the key point is that the guarantee is proven, not observed. A 2-approximation for vertex cover never returns a cover more than twice the minimum size, even on inputs nobody has tried.
How can you prove a solution is close to OPT when computing OPT is NP-hard? The answer is to compare the algorithm's output with a bound on OPT that can be computed cheaply. Examples include the size of a matching, the weight of a spanning tree, or the value of a linear program. Chapter 3 develops this technique, and the chapters after it apply it again and again.
Problems Differ in How Well They Can Be Approximated
All NP-complete decision problems are equally hard in the sense of exact solvability. Their optimization versions are not equally hard to approximate. The range is wide:
| Problem | Best polynomial-time guarantee (roughly) | Covered in |
|---|---|---|
| 0/1 knapsack | (1 − ε) for any ε > 0, in time polynomial in n and 1/ε | Chapters 10–11 |
| Makespan on identical machines | 4/3 by a simple rule; any 1 + ε with more time | Chapter 6 |
| Vertex cover | 2 | Chapters 4, 12 |
| Metric TSP | 2 by a simple method; 1.5 by Christofides | Chapter 7 |
| Set cover | about ln n, and essentially no better | Chapter 5 |
| General TSP | no constant factor unless P = NP | Chapter 8 |
This variation explains why the subject goes beyond "use a heuristic." Some problems can be approximated as closely as you like. For others, even a weak guarantee is provably impossible unless P = NP. Knowing which case you are in tells you what to aim for. The final chapters turn these differences into a classification.
Common Mistakes
- Treating a heuristic as an approximation algorithm. Good results on benchmarks are not a guarantee. An approximation ratio must hold on every input.
- Assuming NP-hard means every instance is intractable. Industrial solvers routinely solve instances with thousands of variables exactly. Hardness concerns worst-case growth.
- Mixing up NP-complete and NP-hard. Optimization problems are not in NP as stated, because they are not yes/no questions, so they are called NP-hard. Their decision versions are NP-complete.
- Assuming one problem's approximability carries over to another. Polynomial-time reductions preserve exact solvability but usually not approximation ratios. Vertex cover and independent set are complements, yet vertex cover has a 2-approximation, while independent set is hard to approximate within n^(1−ε).
Exercises
Exercise
Explain how a polynomial-time algorithm for the decision question "Does G have a vertex cover of size at most k?" yields a polynomial-time algorithm that outputs the size of a minimum vertex cover.
Show answer
The optimum lies between 0 and n. Binary search on k, calling the decision algorithm at each step, finds the smallest k with answer "yes" in O(log n) calls. Each call takes polynomial time, so the total time is polynomial.
Exercise
An exact algorithm runs in time 2ⁿ microseconds. You buy hardware that is 1,000,000 times faster. By about how much does the largest n you can solve in one hour increase?
Show answer
By about 20, because 2²⁰ ≈ 10⁶. A constant-factor speedup adds only a constant to n for exponential algorithms.
Exercise
A greedy routing heuristic beats every known method on 10,000 benchmark instances. Does that make it an approximation algorithm? Why or why not?
Show answer
No. An approximation algorithm needs a proof that its ratio to the optimum is bounded on every input. Benchmark results say nothing about worst-case inputs, where the heuristic might be arbitrarily bad.
Exercise
Vertex cover and maximum independent set are complements: S is a vertex cover exactly when V \ S is an independent set. Why does a 2-approximation for vertex cover not give a good approximation for independent set?
Show answer
Suppose n = 1000 and the minimum cover has size 499, so the maximum independent set has size 501. A 2-approximate cover could have size 998, which leaves an independent set of size 2. The additive relationship |S| + |V \ S| = n does not preserve multiplicative ratios.