Sub topic 13
From Optimization Problems to Decision Problems
Most problems engineers care about ask for the best answer: the shortest route, the smallest set of servers, the cheapest schedule. The theory of P and NP, as defined in earlier chapters, covers only yes/no questions. This chapter connects the two. It shows how an optimization problem turns into a decision problem, why that conversion loses very little, and where it fails.
This chapter assumes you know the definitions of P, NP, polynomial-time reductions, and NP-hardness from earlier chapters. You should also be comfortable with binary search and with reading Python.
Three Versions of the Same Problem
Each combinatorial problem usually comes in three forms:
- The optimization version asks for the best value, such as "What is the size of the smallest vertex cover?"
- The search version asks for a witness that achieves it, such as "Give me a smallest vertex cover."
- The decision version adds a threshold k and asks yes or no, such as "Is there a vertex cover of size at most k?"
| Problem | Optimization | Search | Decision |
|---|---|---|---|
| Vertex Cover | Minimum cover size | A minimum cover | Is there a cover of size ≤ k? |
| Traveling Salesperson | Length of shortest tour | A shortest tour | Is there a tour of length ≤ L? |
| Clique | Size of largest clique | A largest clique | Is there a clique of size ≥ k? |
| Graph Coloring | Chromatic number | An optimal coloring | Can G be colored with ≤ k colors? |
The threshold points in the direction of "good". For minimization the question is "at most k". For maximization it is "at least k". If you get this direction wrong, the problem becomes trivial: every graph has a vertex cover of size at least 1 when it has a vertex, because the whole vertex set is a cover.
The Easy Direction: Decision Is No Harder
If you can solve the optimization version, the decision version follows at once. Compute the minimum cover size m and answer "yes" exactly when m ≤ k. If you can solve the search version, take the witness and measure it. So any lower bound on the decision version also holds for the other two. This is the reason NP-hardness results are stated for decision problems. If the decision version is NP-hard, the optimization and search versions are at least as hard.
The optimization and search versions are not decision problems, so they cannot belong to NP. Calling them "NP-complete" is technically wrong. The correct statement is that they are NP-hard. People often blur this in conversation, but the distinction matters in proofs.
The Hard Direction, Part 1: Binary Search on the Threshold
Suppose you have a black box that answers the decision version. You can recover the optimal value by binary search on k. For Vertex Cover the answer lies between 0 and n, so about log₂(n) + 1 oracle calls are enough.
Weighted problems need more care. In TSP with integer edge weights, the tour length lies between 0 and the sum S of all weights. Binary search over [0, S] uses O(log S) calls. The value log S is at most the number of bits in the input, so the number of calls is polynomial in the input length. A linear scan over k = 0, 1, 2, ... would use up to S calls. That count is exponential in the bit length, which is the pseudo-polynomial trap covered in a later chapter.
The Hard Direction, Part 2: Self-Reduction to Find the Witness
Knowing that the best cover has size 3 does not tell you which 3 vertices to pick. A self-reduction solves this. It uses the decision oracle on smaller instances of the same problem to build a witness one piece at a time.
For Vertex Cover, let k be the minimum size. If you delete a vertex v and all its edges, the remaining graph has a cover of size k − 1 exactly when some minimum cover contains v. Find such a v, commit it to the answer, and repeat on the smaller graph until k reaches 0. Each round makes at most n oracle calls, and there are at most n rounds, so the whole search uses O(n²) calls.
The program below implements all three steps. The oracle is brute force so that the example runs on its own. In a real reduction argument you would treat has_vertex_cover as a black box.
from itertools import combinations
def has_vertex_cover(vertices, edges, k):
"""Decision oracle: does the graph have a vertex cover of size at most k?
Brute force, for demonstration only."""
if k < 0:
return False
k = min(k, len(vertices))
# Any superset of a cover is a cover, so checking size exactly k suffices.
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 remove_vertex(vertices, edges, v):
new_vertices = [u for u in vertices if u != v]
new_edges = [(a, b) for a, b in edges if a != v and b != v]
return new_vertices, new_edges
def min_cover_size(vertices, edges):
"""Optimization via binary search: O(log n) oracle calls."""
lo, hi = 0, len(vertices) # a cover of size n always exists
while lo < hi:
mid = (lo + hi) // 2
if has_vertex_cover(vertices, edges, mid):
hi = mid
else:
lo = mid + 1
return lo
def find_min_cover(vertices, edges):
"""Search via self-reduction: O(n^2) oracle calls."""
k = min_cover_size(vertices, edges)
cover = []
while k > 0:
for v in vertices:
vs, es = remove_vertex(vertices, edges, v)
if has_vertex_cover(vs, es, k - 1):
cover.append(v)
vertices, edges = vs, es
k -= 1
break
return cover
vertices = [1, 2, 3, 4, 5]
edges = [(1, 2), (1, 3), (2, 3), (3, 4), (4, 5)]
print(min_cover_size(vertices, edges)) # 3
print(find_min_cover(vertices, edges)) # [1, 2, 4]The inner loop always finds a vertex. While k > 0 the current graph still has edges, and every minimum cover of it is nonempty. Any vertex in some minimum cover passes the test. When k hits 0, the remaining graph has a cover of size 0, so no edges remain and the committed vertices cover the original graph.
These two arguments together show that, for Vertex Cover, all three versions are polynomial-time equivalent. A polynomial-time algorithm for any one of them gives polynomial-time algorithms for the other two.
Gotchas and Edge Cases
Self-reducibility holds for every NP-complete problem, because you can reduce the question "does a witness extending this partial choice exist?" to SAT. It is not automatic for every problem, though. Several pitfalls are common:
- Easy decision, unclear search. "Is n composite?" is in P (the AKS primality test). Finding a nontrivial factor of n has no known polynomial-time algorithm. The decision answer is "yes", but it gives no hint about the factor. Equivalence between versions has to be proved for each problem. You cannot assume it.
- Wrong threshold direction. As noted above, flipping "≤" and "≥" turns a hard problem into a trivial one.
- Approximation does not transfer. The reductions in this chapter preserve exact answers. They say nothing about how well a problem can be approximated. Vertex Cover has a simple 2-approximation, while general TSP has no constant-factor approximation unless P = NP. Both have NP-complete decision versions.
- Encoding the threshold. k is part of the input and is written in binary. Running time is measured against that encoding, so binary search is polynomial and scanning every value of k may not be.
Exercise
Write the decision version of Maximum Independent Set, and say whether the optimization version is NP-complete.
Show answer
Given a graph G and integer k, does G have an independent set of size at least k? The decision version is NP-complete. The optimization version is NP-hard but not NP-complete, because it is not a decision problem and so cannot be in NP.
Exercise
A TSP instance has n cities and integer edge weights, each at most W. How many decision-oracle calls does binary search need to find the optimal tour length, and why is this polynomial in the input size?
Show answer
The tour length lies in [0, nW], so binary search needs O(log(nW)) = O(log n + log W) calls. Writing the weights takes at least log W bits, and the input lists about n² weights, so the number of calls is polynomial in the input length.
Exercise
Why does the self-reduction in findmincover fail to suggest a fast factoring algorithm from a fast compositeness test?
Show answer
The Vertex Cover self-reduction asks the oracle about smaller instances whose answers show which piece of the witness to keep. A compositeness test answers only "is this number composite?", and fixing part of a factor does not give a smaller compositeness question. To find factors with a decision oracle you need a different decision problem, such as "does n have a factor at most m?", and nobody knows how to decide that one in polynomial time.