Sub topic 1
Why NP-Hard Optimization Calls for Approximation
Many problems you meet in practice ask for the best solution, not just a valid one. Examples include the smallest set of servers that monitors every network link, the shortest delivery route, and the most valuable load that fits in a truck. A large number of these problems are NP-hard. This chapter explains what that means for optimization and why approximation algorithms are a sensible response.
Prerequisites
This chapter assumes you know:
- Big-O notation and what makes an algorithm polynomial-time.
- Basic graph terms: vertices, edges, trees, matchings.
- NP-completeness and polynomial-time reductions, at the level of an undergraduate algorithms course.
- How to read and run short Python programs.
Decision Problems Versus Optimization Problems
NP-completeness is defined for decision problems, which have yes/no answers. "Does this graph have a vertex cover of size at most k?" is NP-complete. A vertex cover is a set of vertices that touches every edge.
The optimization version asks for the minimum vertex cover itself. If you could solve the optimization version in polynomial time, you could answer the decision version by comparing the optimum with k. So the optimization problem is at least as hard as the decision problem, which makes it NP-hard. Unless P = NP, no polynomial-time algorithm finds the optimum for every input.
What Exact Search Costs
The obvious exact method tries every subset of vertices, smallest first, and stops at the first one that covers all edges.
from itertools import combinations
def min_vertex_cover_bruteforce(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
print(min_vertex_cover_bruteforce(4, [(0, 1), (1, 2), (2, 3), (3, 0)]))On the 4-cycle it prints ({0, 2}, 7). In the worst case it checks all 2^n subsets. The table assumes one billion checks per second.
| Vertices n | Subsets 2^n | Time at 10^9 checks/s |
|---|---|---|
| 20 | about 1.0 × 10^6 | about 1 millisecond |
| 40 | about 1.1 × 10^12 | about 18 minutes |
| 60 | about 1.2 × 10^18 | about 36 years |
| 100 | about 1.3 × 10^30 | about 4 × 10^13 years |
Branch-and-bound and integer programming solvers do much better on many real instances. Their worst-case running time is still exponential, and you usually cannot predict in advance which instances will be slow.
Three Options
A heuristic such as "pick the highest-degree vertex repeatedly" often works well, but on some inputs it can be far from optimal, and you have no way of knowing how far. An approximation algorithm guarantees on every input that its answer is within a stated factor of the optimum, such as twice the optimum for vertex cover. The next chapter makes that factor precise.
Common Misconceptions
- "NP-hard means every instance is hard." It does not. Many instances are easy. Hardness is a statement about the worst case.
- "A good heuristic is as good as an approximation algorithm." Without a proof, good results on benchmarks say nothing about the next input.
- "Approximation is always possible." Some problems, such as general TSP, cannot be approximated within any constant factor unless P = NP. Later chapters show why.
Exercise
Suppose you have a polynomial-time algorithm that returns the size of a minimum vertex cover, but not the cover itself. Explain how it answers the decision question "Is there a vertex cover of size at most k?" and what this tells you about the optimization problem's hardness.
Show answer
Run the algorithm, then answer yes exactly when the returned size is at most k. This takes polynomial time, so a polynomial-time algorithm for the optimization problem would solve an NP-complete decision problem and imply P = NP. The optimization problem is therefore NP-hard.
Exercise
Using the table's rate of 10^9 checks per second, roughly how many vertices can brute-force vertex cover handle in one day (about 8.64 × 10^4 seconds) in the worst case?
Show answer
One day allows about 8.64 × 10^13 checks. Since 2^46 is about 7.0 × 10^13 and 2^47 is about 1.4 × 10^14, the limit is about 46 vertices.