Sub topic 13
From Optimization Problems to Decision Problems
This chapter assumes you know Big-O notation, can read short Python functions, and remember the Vertex Cover problem from the earlier tour of classic NP-complete problems.
Most problems people care about in practice ask for the best answer: the shortest route, the smallest cover, the cheapest schedule. P and NP, however, are classes of decision problems, which only ask for yes or no. To use NP-hardness on real problems, you need to see how the three versions of a problem relate to each other.
Three Versions of One Problem
| Version | Vertex Cover question | Output |
|---|---|---|
| Decision | Does G have a vertex cover of size at most k? | Yes or no |
| Optimization | What is the size of the smallest vertex cover of G? | A number |
| Search | Find a smallest vertex cover of G. | A set of vertices |
Each version is at least as hard as the one above it. If you can find a smallest cover, you can count its vertices. If you know the minimum size, you can compare it to k. So when the decision version is NP-complete, the optimization and search versions are NP-hard.
Going the Other Way
The reverse direction matters just as much. With a fast algorithm for the decision version, you could solve the optimization version fast too. Binary search over k does it with only O(log n) calls to the decision procedure.
from itertools import combinations
def has_vertex_cover(vertices, edges, k):
"""Decision version (brute force, exponential time)."""
for combo in combinations(vertices, k):
chosen = set(combo)
if all(u in chosen or v in chosen for u, v in edges):
return True
return False
def min_vertex_cover_size(vertices, edges):
"""Optimization version via binary search on the decision version."""
lo, hi = 0, len(vertices)
while lo < hi:
mid = (lo + hi) // 2
if has_vertex_cover(vertices, edges, mid):
hi = mid
else:
lo = mid + 1
return lo
print(min_vertex_cover_size(["a", "b", "c"], [("a", "b"), ("b", "c")])) # 1Binary search works because the answers are monotone. If a cover of size k exists, you can add any vertices to it and get a cover of size k + 1.
The search version also reduces to the decision version, through a property called self-reducibility. Let k be the minimum. Try deleting a vertex v together with its edges. If the smaller graph still has a cover of size k − 1, then v belongs to some optimal cover. Keep v, then repeat on the smaller graph. This takes polynomially many decision calls.
Common Errors
- Calling an optimization problem "NP-complete." NP contains only decision problems, so the optimization version is NP-hard, not NP-complete.
- Forgetting the budget k. A decision version without a threshold ("Is this cover minimal?") is a different problem, and it may not lie in NP at all.
- Assuming binary search is always cheap. The number of calls is the logarithm of the range of possible answers. That stays polynomial when the answers are integers whose size is polynomial in the number of input bits.
Exercise
The optimization version of Traveling Salesperson asks for the length of the shortest tour, where edge weights are integers of at most b bits on n cities. Using an oracle for "Is there a tour of length at most L?", how many oracle calls does binary search need, and is that polynomial?
Show answer
Every tour length lies between 0 and n·2^b, so binary search needs about log2(n·2^b) = b + log2 n calls. That number is polynomial in the input size, so the optimization version is solvable in polynomial time if and only if the decision version is.