Sub topic 9
NP-Complete: The Hardest Problems in NP
This chapter assumes you already know three things from earlier chapters. NP is the class of decision problems whose "yes" answers can be checked in polynomial time using a certificate. A polynomial-time many-one reduction from A to B, written A ≤p B, turns every instance of A into an instance of B that has the same answer. A problem is NP-hard when every problem in NP reduces to it. NP-completeness puts the last two ideas together.
The Definition
A decision problem L is NP-complete when it meets both of these conditions:
- L is in NP. A polynomial-time verifier exists for its "yes" instances.
- L is NP-hard. For every problem A in NP, A ≤p L.
Each condition does a different job. The second one makes L at least as hard as anything in NP. The first one keeps L inside NP, so L is no harder than NP problems can be. NP-complete problems are therefore the hardest problems in NP. You can also see them as the problems where NP-hardness and NP membership overlap.
| Property | In NP? | NP-hard? | Example |
|---|---|---|---|
| In P | Yes | Only if P = NP | 2-coloring, shortest path (decision version) |
| NP-complete | Yes | Yes | SAT, 3-SAT, Vertex Cover, Subset Sum |
| NP-hard but not in NP | No | Yes | Halting problem, TSP optimization version |
| NP, status unknown | Yes | Unknown | Graph Isomorphism, Factoring (decision version) |
Why One Algorithm Would Settle Everything
The key fact about NP-complete problems is this:
> If any single NP-complete problem has a polynomial-time algorithm, then P = NP.
Here is the proof. Let L be NP-complete and suppose algorithm M decides L in time O(n^c). Take any problem A in NP. Because L is NP-hard, a reduction f turns A instances into L instances in time O(n^d). To decide an instance x of A, compute f(x) and run M on it. The output f(x) has length at most O(n^d), since a polynomial-time machine cannot write more symbols than it has steps. M therefore runs in O((n^d)^c) = O(n^(cd)) time. That bound is still polynomial, so A is in P. Since A was arbitrary, NP ⊆ P, and P ⊆ NP already holds.
The contrapositive matters just as much. If P ≠ NP, then no NP-complete problem has a polynomial-time algorithm. Thousands of NP-complete problems have resisted efficient algorithms for decades. That record is the main reason most researchers believe P ≠ NP, and it is why "this problem is NP-complete" counts as strong practical evidence that you should stop looking for an exact fast algorithm.
Proving a New Problem NP-Complete
Showing directly that every NP problem reduces to L is hard. The Cook-Levin theorem, covered in the next chapter, does this once for SAT. After that, you get NP-hardness from transitivity of reductions: if A ≤p B and B ≤p C, then A ≤p C, because the composition of two polynomial-time functions is also polynomial-time.
The standard recipe to show that a new problem L is NP-complete:
- Give a polynomial-time verifier for L. This shows L ∈ NP.
- Pick a known NP-complete problem K.
- Build a reduction f from K to L. The direction must be K ≤p L.
- Show that f runs in polynomial time.
- Show correctness in both directions: x is a yes-instance of K if and only if f(x) is a yes-instance of L.
Transitivity in Code
The program below composes two reductions. Clique reduces to Independent Set through the complement graph, because a clique in G is an independent set in the complement of G. Independent Set reduces to Vertex Cover by complementing the vertex set. Composing the two gives Clique ≤p Vertex Cover. The test checks by brute force that every 4-vertex graph and every k keeps the same answer.
from itertools import combinations
def complement_edges(vertices, edges):
present = {frozenset(e) for e in edges}
return [(u, v) for u, v in combinations(vertices, 2)
if frozenset((u, v)) not in present]
def clique_to_independent_set(vertices, edges, k):
return vertices, complement_edges(vertices, edges), k
def independent_set_to_vertex_cover(vertices, edges, k):
return vertices, edges, len(vertices) - k
def clique_to_vertex_cover(vertices, edges, k):
# Composition of two polynomial-time reductions.
return independent_set_to_vertex_cover(
*clique_to_independent_set(vertices, edges, k))
def has_clique(vertices, edges, k):
present = {frozenset(e) for e in edges}
return any(all(frozenset(p) in present for p in combinations(s, 2))
for s in combinations(vertices, k))
def has_vertex_cover(vertices, edges, k):
if k < 0:
return False
return any(all(u in s or v in s for u, v in edges)
for s in map(set, combinations(vertices, k)))
vertices = [0, 1, 2, 3]
all_pairs = list(combinations(vertices, 2))
checked = 0
for mask in range(1 << len(all_pairs)):
edges = [all_pairs[i] for i in range(len(all_pairs)) if mask >> i & 1]
for k in range(len(vertices) + 1):
expected = has_clique(vertices, edges, k)
got = has_vertex_cover(*clique_to_vertex_cover(vertices, edges, k))
assert expected == got, (edges, k)
checked += 1
print(f"All {checked} instances agree") # All 320 instances agreeThe brute-force checkers take exponential time. They only test the reduction. The reduction itself does O(n²) work to build the complement graph.
Common Errors
Other errors that show up often:
- Skipping NP membership. A reduction from 3-SAT shows only NP-hardness. Without a verifier, the problem might lie outside NP, as the optimization version of TSP does.
- Proving only one direction of correctness. If you show only that yes-instances map to yes-instances, a reduction that always outputs a fixed yes-instance would pass. You also need the converse: if f(x) is a yes-instance, then x is a yes-instance.
- Hidden exponential blowup. A construction that creates one gadget per subset, or that writes numbers in unary, may not run in polynomial time in the input length.
- Mixing up "NP-complete" with "requires exponential time." Nobody has proven that NP-complete problems need exponential time. That claim is equivalent to P ≠ NP, which is still open.
- Calling an optimization problem NP-complete. Strictly, NP-completeness applies to decision problems. "Find the minimum vertex cover" is NP-hard. "Is there a vertex cover of size at most k?" is NP-complete.
Edge Cases Worth Knowing
- Trivial problems cannot be NP-complete under many-one reductions. The empty language and the language containing every string have no instance with the opposite answer, so no reduction can map both yes and no instances onto them correctly.
- Restrictions can drop out of NP-complete. 3-SAT is NP-complete, but 2-SAT is in P. 3-coloring is NP-complete, but 2-coloring is in P. When a problem's parameters change, you have to check it again.
- Numeric problems need care. Subset Sum is NP-complete when numbers are written in binary. A later chapter explains why its dynamic-programming algorithm does not contradict this.
Exercise
A colleague shows that their scheduling problem S reduces to SAT in polynomial time, and a verifier for S exists. They conclude S is NP-complete. What is wrong, and what would fix the argument?
Show answer
S ≤p SAT together with the verifier shows only that S is in NP, so S could even be in P. To show NP-hardness, they need a reduction in the other direction, from a known NP-complete problem such as 3-SAT into S, with a proof that it runs in polynomial time and preserves answers in both directions.
Exercise
Suppose someone finds an O(n^3) algorithm for Vertex Cover, and the reduction from 3-SAT to Vertex Cover produces instances of size O(m^2) from a formula of size m. What running time does this give for 3-SAT, and what does it imply about P and NP?
Show answer
Reduce, then solve: O(m^2) to build the instance, plus O((m^2)^3) = O(m^6) to solve it, so 3-SAT runs in O(m^6) time. Since Vertex Cover is NP-complete, every NP problem would then be in P, so P = NP.
Exercise
Why does the halting problem count as NP-hard but not NP-complete?
Show answer
Every NP problem reduces to it: map an instance x to a program that halts exactly when a brute-force search finds a certificate for x. So it is NP-hard. It is undecidable, though, so it has no polynomial-time verifier and is not in NP, which NP-completeness requires.