Sub topic 9
NP-Hardness and NP-Completeness
This chapter assumes you already know three things from earlier chapters. A polynomial-time (Karp) reduction A ≤p B is a function f, computable in polynomial time, with x ∈ A exactly when f(x) ∈ B. Reductions compose: if A ≤p B and B ≤p C, then A ≤p C. NP is the class of decision problems whose yes-instances have certificates that a polynomial-time verifier can check. This chapter combines those ideas into the two definitions used to classify hard problems, then gives the standard recipe for proving a problem NP-complete.
The Definitions
A decision problem B is NP-hard if every problem A in NP satisfies A ≤p B. Being NP-hard means B is at least as hard as everything in NP: an efficient algorithm for B, composed with the reduction, would solve any NP problem efficiently.
A decision problem B is NP-complete if two conditions hold:
- B is in NP.
- B is NP-hard.
The NP-complete problems are therefore the hardest problems inside NP. NP-hardness alone places no upper bound on difficulty.
| Property | Upper bound (B is in NP) | Lower bound (B is NP-hard) | Example |
|---|---|---|---|
| In P | yes | unknown (only if P = NP) | 2-coloring a graph |
| NP-complete | yes | yes | SAT, Vertex Cover, Clique |
| NP-hard, not known to be in NP | no | yes | Optimal-move problems for generalized chess |
| NP-hard, provably outside NP | no | yes | The halting problem |
Why NP-Completeness Matters: The Collapse Theorem
Theorem. If any NP-complete problem B is in P, then P = NP.
Proof. Take any A in NP. Since B is NP-hard, A ≤p B through some f that runs in time O(n^a). The output f(x) has length at most O(n^a), because a machine cannot write more symbols than it has steps. B has an algorithm running in time O(m^b). Running f and then the algorithm for B decides A in time O(n^a + (n^a)^b), which is polynomial. So A is in P. Since A was arbitrary, NP ⊆ P, and P ⊆ NP always holds. ∎
The step that bounds the output length is easy to forget. If f could blow up its output exponentially, a polynomial algorithm for B would give an exponential algorithm for A, and the argument would fail.
The contrapositive is how the result gets used. If P ≠ NP, which most researchers believe, then no NP-complete problem has a polynomial-time algorithm. Proving a problem NP-complete is strong evidence that you should stop looking for an exact, general, worst-case efficient algorithm.
The Recipe for Proving NP-Completeness
Showing directly that every NP problem reduces to B is hard. The Cook-Levin theorem in the next chapter does it once, for SAT. After that, transitivity lets you reuse it:
Lemma. If A is NP-hard and A ≤p B, then B is NP-hard.
Proof. For any L in NP, L ≤p A because A is NP-hard. Composing with A ≤p B gives L ≤p B. ∎
That lemma gives the standard five-step recipe.
Step 5 is usually the hardest. It is not enough to show that solutions of A become solutions of B. You also have to show that every yes-instance f(x) of B, including instances that look strange, came from a yes-instance x. Proving the contrapositive often helps: if x is a no-instance, then f(x) is a no-instance.
Worked Example: Vertex Cover Is NP-Complete
Assume Independent Set is NP-complete. An earlier chapter derived it from Clique, and a later chapter proves it from 3-SAT. Here is the full proof for Vertex Cover: given a graph G = (V, E) and an integer k, does G have a vertex cover of size at most k?
Step 1: membership in NP. The certificate is a set C of vertices. The verifier checks that C ⊆ V, that |C| ≤ k, and that every edge has an endpoint in C. With a hash set this takes O(|V| + |E|) time.
Steps 2–3: the reduction. From Independent Set, map (G, k) to (G, |V| − k). Computing this takes linear time.
Step 4 (forward). If S is an independent set with |S| ≥ k, then V \ S is a vertex cover. No edge has both endpoints in S, so every edge has at least one endpoint outside S. Its size is |V| − |S| ≤ |V| − k.
Step 5 (backward). If C is a vertex cover with |C| ≤ |V| − k, then V \ C is independent. An edge with both endpoints outside C would be uncovered. Its size is at least k. ∎
The code below implements the verifier and the reduction. It then checks both directions by brute force on random small graphs. A brute-force test does not prove anything, but it catches most broken reductions quickly.
import random
from itertools import combinations
def verify_vertex_cover(vertices, edges, k, certificate):
"""Polynomial-time verifier: membership of Vertex Cover in NP."""
c = set(certificate)
vs = set(vertices)
return c <= vs and len(c) <= k and all(u in c or v in c for u, v in edges)
def independent_set_to_vertex_cover(vertices, edges, k):
"""Karp reduction: (G, k) -> (G, |V| - k)."""
return vertices, edges, len(vertices) - k
def has_independent_set(vertices, edges, k):
if k <= 0:
return True
return any(all(not (u in s and v in s) for u, v in edges)
for s in map(set, combinations(vertices, k)))
def has_vertex_cover(vertices, edges, k):
if k < 0:
return False
return any(verify_vertex_cover(vertices, edges, k, s)
for size in range(min(k, len(vertices)) + 1)
for s in combinations(vertices, size))
random.seed(0)
for trial in range(300):
n = random.randint(1, 6)
vertices = list(range(n))
edges = [e for e in combinations(vertices, 2) if random.random() < 0.4]
for k in range(n + 2):
a = has_independent_set(vertices, edges, k)
b = has_vertex_cover(*independent_set_to_vertex_cover(vertices, edges, k))
assert a == b, (vertices, edges, k)
print("reduction agrees on all tested instances")The test includes k = n + 1. There the reduction produces a negative bound, and both sides must answer no. Edge cases like this are where hand-written reductions break most often.
Common Misreadings
| Misreading | Correction |
|---|---|
| "NP means not polynomial." | NP means nondeterministic polynomial time. Every problem in P is also in NP. |
| "NP-hard problems are always slow in practice." | NP-hardness is a worst-case statement. SAT solvers routinely handle industrial instances with millions of variables. |
| "An optimization problem is NP-complete." | NP-completeness is defined for decision problems. Call the optimization version NP-hard and its threshold version NP-complete. |
| "Knapsack is NP-complete, so no fast algorithm exists for any input." | Knapsack has an O(nW) dynamic program. That is polynomial in the numeric value W but exponential in the number of bits needed to write W. Problems like this are called weakly NP-hard. |
| "My special case is NP-complete because the general problem is." | Restrictions can be easier. Independent Set on bipartite graphs is in P. The hardness proof has to produce instances inside your restricted class. |
What a Proof Buys You
Once B is known to be NP-complete, the useful options are well understood. You can restrict to a tractable special case. You can accept an approximation, and later chapters show that NP-hard problems differ a lot in how well they can be approximated. You can parameterize, for example with a 2^k·poly(n) algorithm for Vertex Cover. Or you can hand the instance to a SAT or ILP solver and rely on good typical-case behavior. Every new NP-complete problem also becomes a possible source problem A for later reductions.
Exercise
A student proves "Problem B is NP-complete" by giving a polynomial-time reduction from B to 3-SAT and a polynomial-time verifier for B. What has the student actually shown?
Show answer
Only that B is in NP. The verifier shows this directly, and a reduction to 3-SAT also implies it, since 3-SAT is in NP. Hardness needs a reduction in the opposite direction, from a known NP-complete problem such as 3-SAT to B.
Exercise
Prove that Clique is NP-complete, assuming Independent Set is NP-complete. Give the certificate, the reduction, and both directions.
Show answer
Certificate: a set K of k vertices. The verifier checks that K ⊆ V, |K| ≥ k, and that every pair in K is an edge, which takes O(k²) checks. Reduction: map (G, k) to (Ḡ, k), where Ḡ has an edge exactly where G does not. Building Ḡ takes O(|V|²) time. Forward: an independent set in G has no edges between its vertices, so in Ḡ every pair is joined and the set is a clique. Backward: a clique in Ḡ has every pair joined in Ḡ, so no pair is joined in G and the set is independent in G. Sizes are unchanged.
Exercise
Suppose someone shows Problem X is NP-hard, and separately gives an O(n^3) algorithm for X. What follows, and why would you be suspicious?
Show answer
By the collapse theorem, NP-hardness plus a polynomial algorithm gives P = NP, which would settle a major open problem. More likely there is an error. The reduction may not run in polynomial time, the algorithm may be pseudo-polynomial (polynomial in numeric values rather than input length), or the two results may be about different versions of X.