NP-Completeness and NP-Hardness
With reductions in hand, we can single out the hardest problems in NP: those to which every problem in NP reduces. They are the NP-complete problems. One fact makes them central: if any single NP-complete problem has a polynomial-time algorithm, then every problem in NP does, and . Thousands of problems from scheduling, routing, biology, logic and games are known to be NP-complete, and none of them has a polynomial algorithm. This lesson gives the definitions, proves the key theorem, and lays out the recipe that the following lessons apply again and again.
Definitions
A language is NP-hard if for every . It is NP-complete if moreover . The class of NP-complete languages is written NPC:
NP-hard problems need not be in NP. The optimization version of the travelling salesman problem ("output a shortest tour") is NP-hard but not a decision problem at all; the halting problem is NP-hard (every NP language reduces to it) but undecidable. "NP-hard" is the right word for such problems; "NP-complete" is reserved for decision problems that are in NP as well.
The central theorem
Theorem. If some NP-complete problem is in P, then . Equivalently, if some problem in NP is not in P, then no NP-complete problem is in P.
Proof. Let and let be any language in NP. Then by NP-hardness, and Lemma 1 of Polynomial-Time Reductions gives . So , and with the classes are equal. The second form is the contrapositive.
So the NP-complete problems stand or fall together. If , as most researchers believe, then no NP-complete problem has a polynomial-time algorithm, and moreover (Ladner, 1975) there are problems in NP that are neither in P nor NP-complete. Candidates for such NP-intermediate problems are factoring and graph isomorphism, which is why neither is believed to be NP-complete.
if P != NP if P = NP
+-------------------------------+ +----------------------+
| NP | | |
| +-----+ +-------+ | | P = NP = NPC |
| | P | (inter-) | NPC | | | (all but the two |
| +-----+ mediate +-------+ | | trivial languages)|
+-------------------------------+ +----------------------+(If , every language in P except and is NP-complete, since a reduction may solve the instance itself and output a fixed yes- or no-instance.)
How to prove NP-completeness
Proving NP-hardness from the definition means reducing every NP language to , which is what the Cook-Levin theorem does once, for circuit satisfiability (Circuit Satisfiability and the Cook-Levin Idea). After that, transitivity of lets every new proof use a single reduction:
Lemma. If is NP-complete and , then is NP-hard. If in addition , then is NP-complete.
Proof. Every satisfies , and composing with gives .
The recipe for showing a problem NP-complete:
- Show : describe a certificate and a polynomial-time verifier.
- Pick a known NP-complete problem .
- Describe a function mapping instances of to instances of .
- Prove , both directions.
- Prove runs in polynomial time.
Step 4 is where the creativity goes; the usual technique is a gadget construction, small pieces of the target instance that simulate pieces of the source instance (a variable, a clause, an edge).
Search reduces to decision
A practical corollary: for NP-complete problems, finding a solution is not much harder than deciding whether one exists. Given a decision procedure for SAT, set the variables one at a time: fix , ask whether the formula is still satisfiable, keep the value if yes and flip it otherwise. After more questions every variable is fixed and the assignment satisfies the formula. This self-reducibility holds for all the classic NP-complete problems. The program uses a brute-force decider as the oracle and counts the calls.
from itertools import product
calls = 0
def satisfiable(clauses, n, fixed):
"""decision oracle (brute force here): can the remaining variables be set to satisfy all clauses?"""
global calls
calls += 1
free = [v for v in range(1, n + 1) if v not in fixed]
for values in product([False, True], repeat=len(free)):
a = dict(fixed) | dict(zip(free, values))
if all(any(a[abs(l)] == (l > 0) for l in c) for c in clauses):
return True
return False
def find_assignment(clauses, n):
fixed = {}
if not satisfiable(clauses, n, fixed):
return None
for v in range(1, n + 1):
fixed[v] = True
if not satisfiable(clauses, n, fixed):
fixed[v] = False # must be satisfiable this way
return fixed
# clauses as lists of literals: 3 means x3, -3 means not x3
clauses = [[1, 2, -3], [-1, 3], [-2, 3, 4], [-4, -1], [2, 4], [-3, -4, 1]]
a = find_assignment(clauses, 4)
print("assignment:", {f"x{v}": val for v, val in a.items()})
print("satisfies every clause:", all(any(a[abs(l)] == (l > 0) for l in c) for c in clauses))
print("oracle calls:", calls, "(n + 1 =", 4 + 1, ")")assignment: {'x1': True, 'x2': True, 'x3': True, 'x4': False}
satisfies every clause: True
oracle calls: 5 (n + 1 = 5 )
Five yes/no questions recover a whole satisfying assignment. So if a polynomial decision algorithm for SAT existed, satisfying assignments could be found in polynomial time too.
The same works for optimization problems. With a decision procedure for VERTEX-COVER ("is there a cover of size at most ?"), binary search finds the minimum size, and then vertices are removed one at a time: delete a vertex together with its edges and ask whether the rest has a cover one smaller; if so, the vertex belongs to an optimal cover.
from itertools import combinations
calls = 0
def has_cover(edges, k):
"""decision oracle (brute force here)"""
global calls
calls += 1
vertices = sorted({v for e in edges for v in e})
if not edges:
return k >= 0
return any(all(u in c or v in c for u, v in edges) for size in range(k + 1)
for c in map(set, combinations(vertices, size)))
edges = [(0, 1), (0, 2), (1, 2), (2, 3), (3, 4), (4, 5), (3, 5), (1, 6)]
lo, hi = 0, 7
while lo < hi: # minimum size by binary search
mid = (lo + hi) // 2
if has_cover(edges, mid): hi = mid
else: lo = mid + 1
k, cover, rest = lo, [], list(edges)
for v in sorted({v for e in edges for v in e}):
if k == 0:
break
without = [e for e in rest if v not in e]
if has_cover(without, k - 1): # v can be part of a minimum cover
cover.append(v); rest, k = without, k - 1
print("minimum vertex cover:", cover, "of size", len(cover), "found with", calls, "oracle calls")minimum vertex cover: [0, 1, 3, 4] of size 4 found with 8 oracle calls
NP-hard in practice
An NP-hardness proof is not a reason to give up; it is information. It says that an algorithm that is always correct, always fast and works for every input is (very probably) out of reach, so one of the three must give: restrict the inputs (trees, planar graphs, small parameters, small numbers), accept approximate answers, or accept exponential worst cases with good typical behaviour (SAT solvers routinely handle millions of clauses from industrial problems). Coping with NP-Hardness at the end of this section surveys these options.
Test yourself
What is the difference between NP-hard and NP-complete?
- There is none
- An NP-complete problem is NP-hard and also in NP
- NP-hard problems are easier
- NP-complete problems need not be decision problems
Suppose someone finds an algorithm for an NP-complete problem. What follows?
- Only that problem becomes easy
- but
- Nothing, because is too slow
To show that a problem in NP is NP-complete, it suffices to show
- for some NP-complete
- for some NP-complete
- That brute force takes exponential time
- That has no known polynomial algorithm
With a decision procedure for SAT, how many calls find a satisfying assignment of a satisfiable formula with variables, by self-reduction?
- 1
- About
- About
- About