Sub topic 8
What It Means to Be NP-Hard
This chapter assumes you already know three things from earlier chapters. You should know the definition of NP through polynomial-time verifiers. You should know what a polynomial-time many-one reduction is, written A ≤p B. You should also be comfortable reading short Python code that uses itertools. The aim here is to pin down the precise meaning of "NP-hard," show how hardness is proved in practice, and clear up the mistakes people most often make with the term.
The Definition
A problem L is NP-hard if every problem A in NP satisfies A ≤p L. In words, any problem in NP can be translated into L in polynomial time, and the translation preserves yes and no answers.
The definition makes no claim that L is in NP. L does not have to be a decision problem with short certificates. It does not even have to be decidable. NP-hardness is a lower bound on difficulty: L is at least as hard as everything in NP. It sets no upper bound.
Why Hardness Flows Along Reductions
Recall what A ≤p B buys you. Suppose there is a polynomial-time function f with x ∈ A ⟺ f(x) ∈ B, and suppose B can be solved in polynomial time. Then A can be solved in polynomial time too: compute f(x) and run B's algorithm on it. The composition is still polynomial, because f(x) has size polynomial in |x|, and a polynomial applied to a polynomial is a polynomial.
Apply this to an NP-hard L. If anyone found a polynomial-time algorithm for L, every problem in NP would get one through its reduction to L. That would mean P = NP. So a proof that L is NP-hard is strong evidence that no efficient algorithm for L exists. The evidence is conditional, since it rests on the widely believed but unproven assumption that P ≠ NP.
Proving NP-Hardness Without Reducing Everything
Taken literally, the definition asks you to reduce infinitely many problems to L. In practice you never do that, because reductions compose:
- If A ≤p B and B ≤p C, then A ≤p C. Compose the two mapping functions. The result runs in polynomial time.
- So if K is already known to be NP-hard and K ≤p L, then every A in NP satisfies A ≤p K ≤p L. That makes L NP-hard.
The standard recipe has three steps:
- Pick a problem K that is already known to be NP-hard, such as SAT, 3-SAT, Hamiltonian Cycle, or Vertex Cover.
- Build a polynomial-time function f that maps instances of K to instances of L.
- Prove both directions of the correctness claim: x is a yes-instance of K if and only if f(x) is a yes-instance of L.
You need one problem to start the chain, and some problem has to be proved NP-hard from first principles. The Cook-Levin theorem, covered two chapters ahead, does this for SAT.
A Worked Example: Hamiltonian Cycle to TSP
A Hamiltonian cycle in a graph visits every vertex exactly once and returns to the start. Deciding whether one exists is known to be NP-hard. The decision version of the Traveling Salesperson Problem (TSP) takes complete-graph edge weights and a budget B, and asks whether some tour has total weight at most B.
The reduction works as follows. Take a graph G with n vertices. Give each pair of vertices weight 1 if the pair is an edge in G and weight 2 if it is not. Set the budget to n. A tour always uses exactly n pairs. It therefore costs exactly n when it uses only real edges, and at least n + 1 when it uses even one non-edge. So G has a Hamiltonian cycle if and only if some tour costs at most n. The construction builds an n × n matrix, which takes O(n²) time.
from itertools import permutations
def tsp_decision(n, weight, budget):
"""Brute force: is there a tour through all n cities (n >= 3) with total weight <= budget?"""
for perm in permutations(range(1, n)):
tour = (0,) + perm
cost = sum(weight[tour[i]][tour[(i + 1) % n]] for i in range(n))
if cost <= budget:
return True
return False
def ham_cycle_to_tsp(n, edges):
"""Polynomial-time reduction: Hamiltonian Cycle instance -> TSP decision instance."""
edge_set = {frozenset(e) for e in edges}
weight = [[0 if i == j else (1 if frozenset((i, j)) in edge_set else 2)
for j in range(n)] for i in range(n)]
return n, weight, n
def has_ham_cycle(n, edges):
return tsp_decision(*ham_cycle_to_tsp(n, edges))
square = [(0, 1), (1, 2), (2, 3), (3, 0)]
path = [(0, 1), (1, 2), (2, 3)]
print(has_ham_cycle(4, square)) # True
print(has_ham_cycle(4, path)) # FalseThe brute-force tsp_decision takes exponential time. That does not weaken the argument. The reduction ham_cycle_to_tsp is the part that must be fast, and it is. If someone replaced tsp_decision with a polynomial-time algorithm, has_ham_cycle would immediately run in polynomial time. That is exactly why TSP inherits the hardness.
NP-Hard Problems That Are Not in NP
Some NP-hard problems lie outside NP, and these examples show that NP-hardness only sets a lower bound.
| Problem | NP-hard? | In NP? | Reason |
|---|---|---|---|
| 3-SAT | Yes | Yes | A satisfying assignment is a short, checkable certificate |
| TSP decision (tour ≤ B?) | Yes | Yes | The tour itself is the certificate |
| TSP optimization (find the cheapest tour) | Yes, under Cook reductions | No | It is a search problem, not a yes/no language |
| "Is the minimum tour cost exactly B?" | Yes | Not known to be | Proving that no cheaper tour exists has no obvious short certificate |
| Halting Problem | Yes | No | It is undecidable, so it lies outside every time-bounded class |
The Halting Problem is NP-hard for a simple reason. Given a SAT formula φ, build a program that tries every assignment, halts if one satisfies φ, and loops forever otherwise. That program halts exactly when φ is satisfiable, and writing it down takes polynomial time.
Common Misconceptions
- "NP-hard means it needs exponential time." Nobody has proved that. If P = NP, many NP-hard problems would have polynomial-time algorithms. NP-hardness is a relative statement about difficulty.
- "NP stands for non-polynomial." It stands for nondeterministic polynomial time. Every problem in P is also in NP.
- "Every instance of an NP-hard problem is hard." Hardness is a worst-case statement about the problem as a whole. Many real instances, and some restricted families such as trees or graphs with bounded treewidth, are easy.
- "NP-hard and NP-complete mean the same thing." NP-complete problems are the NP-hard problems that also belong to NP. The next chapter treats that intersection.
- "Any reduction will do." The reduction must run in polynomial time, and its output size must be polynomial in the input size. An exponential-size reduction proves nothing.
Exercise
You prove that your scheduling problem S satisfies S ≤p 3-SAT. A colleague says this shows S is NP-hard. Are they right? What would you need to show instead?
Show answer
No. S ≤p 3-SAT only shows S is no harder than 3-SAT (and, since the reduction maps into an NP problem, that S is in NP). To show S is NP-hard you must reduce a known NP-hard problem to S, for example 3-SAT ≤p S, with a polynomial-time mapping and a proof that yes-instances map to yes-instances and no-instances map to no-instances.
Exercise
In the Hamiltonian Cycle to TSP reduction, why do non-edges get weight 2 rather than, say, 1000? Would 1000 still work, and would weight 1 for non-edges work?
Show answer
Any non-edge weight greater than 1 works, because a single non-edge already pushes the tour cost above the budget n. 1000 would work as well, and the numbers stay polynomial-size. Weight 1 for non-edges fails: every tour would cost exactly n, so the TSP answer would be yes even for graphs that have no Hamiltonian cycle.
Exercise
Problem L is NP-hard, and someone shows L is undecidable. Does this contradict anything, and does it tell us anything about P versus NP?
Show answer
There is no contradiction. NP-hardness puts no upper bound on difficulty, and the Halting Problem is an example of an undecidable NP-hard problem. It tells us nothing new about P versus NP, because the reduction argument only gives P = NP if L has a polynomial-time algorithm, and an undecidable L has no algorithm at all.