Sub topic 9
NP-Complete: The Hardest Problems in NP
This chapter assumes you know the definitions of P and NP from earlier chapters. It also assumes you know what a polynomial-time many-one reduction is and what NP-hard means. If you write A ≤p B, that means A reduces to B in polynomial time.
The definition
A decision problem L is NP-complete when both of these conditions hold:
- L is in NP. A polynomial-time verifier exists for L's yes-instances.
- L is NP-hard. Every problem in NP reduces to L in polynomial time.
Condition 2 says L is at least as hard as anything in NP. Condition 1 says L is no harder than NP itself. Together they place L at the top of NP.
| Property | NP-hard | NP-complete |
|---|---|---|
| Every NP problem reduces to it | Yes | Yes |
| Must be in NP | No | Yes |
| Must be a decision problem | No | Yes |
| Example | Halting problem, TSP optimization | SAT, 3-SAT, Vertex Cover |
Why NP-complete problems matter
NP-complete problems are all equivalent under polynomial-time reductions. This has a sharp consequence. If any single NP-complete problem has a polynomial-time algorithm, then every problem in NP does, and P = NP. The argument is direct. Take any problem A in NP. Reduce it to the NP-complete problem L in polynomial time, then run L's fast algorithm. Composing two polynomials gives a polynomial.
The reverse also holds. If P ≠ NP, then no NP-complete problem is in P.
Proving a new problem NP-complete
You never reduce all of NP directly to a new problem. Hardness is transitive, so one reduction from a known NP-complete problem is enough. The recipe has two steps:
- Show B is in NP. Write a polynomial-time verifier that checks a certificate.
- Pick a known NP-complete problem A and show A ≤p B. Transform any instance of A into an instance of B in polynomial time. The transformed instance must be a yes-instance of B exactly when the original was a yes-instance of A.
Here is step 1 for Vertex Cover, where the certificate is the proposed cover:
def verify_vertex_cover(edges, k, certificate):
cover = set(certificate)
if len(cover) > k:
return False
return all(u in cover or v in cover for u, v in edges)
edges = [(1, 2), (2, 3), (3, 4)]
print(verify_vertex_cover(edges, 2, [2, 3])) # True
print(verify_vertex_cover(edges, 1, [2])) # FalseCommon errors
- Reducing in the wrong direction. Showing B ≤p A, with A known hard, proves nothing about B. The known-hard problem must reduce to the new one.
- Skipping the NP membership step. Without a verifier you have only shown NP-hardness.
- Proving only one direction of correctness. The reduction must keep yes-instances as yes and no-instances as no.
- Using a reduction that is not polynomial. An exponential-size transformation is invalid.
Exercise
You show that your new problem B reduces to 3-SAT in polynomial time, and that B has a polynomial-time verifier. Have you proven B is NP-complete? Explain.
Show answer
No. Reducing B to 3-SAT shows only that B is no harder than 3-SAT, which the verifier already establishes by placing B in NP. To show NP-hardness you must reduce a known NP-complete problem such as 3-SAT to B.
Exercise
Suppose someone finds an O(n^5) algorithm for Vertex Cover. What follows for SAT, and why?
Show answer
SAT would be solvable in polynomial time. SAT is in NP, and Vertex Cover is NP-hard, so SAT reduces to Vertex Cover in polynomial time. Running the reduction and then the O(n^5) algorithm takes polynomial time overall, which would also prove P = NP.