TExcelTAD

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 P=NP\mathrm{P} = \mathrm{NP}. 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 LL is NP-hard if L′≤PLL' \le_P L for every L′∈NPL' \in \mathrm{NP}. It is NP-complete if moreover L∈NPL \in \mathrm{NP}. The class of NP-complete languages is written NPC:

L∈NPC  ⟺  L∈NP  and  L′≤PL for every L′∈NP.L \in \mathrm{NPC} \iff L \in \mathrm{NP} \ \text{ and } \ L' \le_P L \text{ for every } L' \in \mathrm{NP}.

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 P=NP\mathrm{P} = \mathrm{NP}. Equivalently, if some problem in NP is not in P, then no NP-complete problem is in P.

Proof. Let L∈NPC∩PL \in \mathrm{NPC} \cap \mathrm{P} and let L′L' be any language in NP. Then L′≤PLL' \le_P L by NP-hardness, and Lemma 1 of Polynomial-Time Reductions gives L′∈PL' \in \mathrm{P}. So NP⊆P\mathrm{NP} \subseteq \mathrm{P}, and with P⊆NP\mathrm{P} \subseteq \mathrm{NP} the classes are equal. The second form is the contrapositive.

So the NP-complete problems stand or fall together. If P≠NP\mathrm{P} \ne \mathrm{NP}, 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.

text
              if P != NP                          if P = NP
   +-------------------------------+      +----------------------+
   | NP                            |      |                      |
   |   +-----+          +-------+  |      |   P = NP = NPC       |
   |   |  P  | (inter-) |  NPC  |  |      |   (all but the two   |
   |   +-----+  mediate +-------+  |      |    trivial languages)|
   +-------------------------------+      +----------------------+

(If P=NP\mathrm{P} = \mathrm{NP}, every language in P except ∅\emptyset and {0,1}∗\{0,1\}^* 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 LL, which is what the Cook-Levin theorem does once, for circuit satisfiability (Circuit Satisfiability and the Cook-Levin Idea). After that, transitivity of ≤P\le_P lets every new proof use a single reduction:

Lemma. If L′L' is NP-complete and L′≤PLL' \le_P L, then LL is NP-hard. If in addition L∈NPL \in \mathrm{NP}, then LL is NP-complete.

Proof. Every L′′∈NPL'' \in \mathrm{NP} satisfies L′′≤PL′L'' \le_P L', and composing with L′≤PLL' \le_P L gives L′′≤PLL'' \le_P L.

The recipe for showing a problem LL NP-complete:

  1. Show L∈NPL \in \mathrm{NP}: describe a certificate and a polynomial-time verifier.
  2. Pick a known NP-complete problem L′L'.
  3. Describe a function ff mapping instances of L′L' to instances of LL.
  4. Prove x∈L′  ⟺  f(x)∈Lx \in L' \iff f(x) \in L, both directions.
  5. Prove ff 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 x1=truex_1 = \text{true}, ask whether the formula is still satisfiable, keep the value if yes and flip it otherwise. After nn 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.

python · try it yourself
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 kk?"), 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.

python · try it yourself
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?

  1. There is none
  2. An NP-complete problem is NP-hard and also in NP
  3. NP-hard problems are easier
  4. NP-complete problems need not be decision problems

Suppose someone finds an O(n10)O(n^{10}) algorithm for an NP-complete problem. What follows?

  1. Only that problem becomes easy
  2. P=NP\mathrm{P} = \mathrm{NP}
  3. NP=co-NP\mathrm{NP} = \mathrm{co\text{-}NP} but P≠NP\mathrm{P} \ne \mathrm{NP}
  4. Nothing, because n10n^{10} is too slow

To show that a problem LL in NP is NP-complete, it suffices to show

  1. L≤PL′L \le_P L' for some NP-complete L′L'
  2. L′≤PLL' \le_P L for some NP-complete L′L'
  3. That brute force takes exponential time
  4. That LL has no known polynomial algorithm

With a decision procedure for SAT, how many calls find a satisfying assignment of a satisfiable formula with nn variables, by self-reduction?

  1. 1
  2. About nn
  3. About n2n^2
  4. About 2n2^n