Sub topic 6
The Nondeterministic Definition of NP
This chapter assumes you already know the verifier definition of NP from the previous chapter, and that you have a rough idea of what a Turing machine is. Here we look at the original definition of NP, the one its name comes from, and show that it describes the same class.
Nondeterministic Turing machines
A deterministic Turing machine has exactly one next move in each configuration. A nondeterministic Turing machine (NTM) may have several. It works from a transition relation, not a transition function. At each step the computation can split into several branches, so a single run forms a tree of possible computations.
These are the acceptance rules:
- The NTM accepts an input if at least one branch reaches an accepting state.
- The NTM rejects an input only if every branch rejects.
- Its running time on an input is the length of the longest branch.
NP is the class of decision problems that some NTM decides in time polynomial in the input size. The "N" stands for nondeterministic. It does not stand for "non-".
Why this matches the verifier definition
The two definitions are equivalent, and the proof runs in both directions.
- From a verifier to an NTM. Suppose a polynomial-time verifier V checks certificates of length at most p(n). Build an NTM that first nondeterministically writes down a certificate, one symbol per step, and then runs V deterministically. Some branch guesses a correct certificate exactly when the answer is yes.
- From an NTM to a verifier. Suppose an NTM runs in polynomial time. Use the sequence of choices along an accepting branch as the certificate. The verifier replays the machine, following those choices, and checks that the branch ends in acceptance. The sequence has polynomial length, so the replay takes polynomial time.
You can therefore read the definition as "guess, then check." The guess is free, and the check must be efficient.
Simulating nondeterminism deterministically
A real computer can only explore the branches one at a time. If each run makes m binary choices, there are 2^m branches. The code below shows this for Subset Sum.
from itertools import product
def nondeterministic_accepts(verify, instance, num_choices):
"""Deterministically try every branch of a guess-then-check computation."""
for choices in product([0, 1], repeat=num_choices):
if verify(instance, choices):
return True
return False
def subset_sum_check(instance, bits):
numbers, target = instance
return sum(x for x, b in zip(numbers, bits) if b) == target
numbers = [3, 34, 4, 12, 5, 2]
print(nondeterministic_accepts(subset_sum_check, (numbers, 9), len(numbers))) # True
print(nondeterministic_accepts(subset_sum_check, (numbers, 30), len(numbers))) # FalseEach branch takes polynomial time, but the loop can run 2^n times. Nobody knows a general simulation that avoids this blow-up. Finding one for every NTM would prove P = NP.
Common errors
| Misconception | Correction |
|---|---|
| NP means "not polynomial" | NP means nondeterministic polynomial time, and P ⊆ NP. |
| An NTM is a randomized machine | Randomized machines accept with some probability. An NTM accepts if any branch accepts, however few there are. |
| NTMs are a model of real hardware | NTMs are a mathematical device for defining a class. Quantum computers are not NTMs either. |
| Rejection works the same way as acceptance | Rejection requires every branch to reject. This asymmetry is the source of co-NP. |
Exercise
An NTM for 3-coloring guesses a color for each of the n vertices and then checks every edge. How many branches does the guessing phase create, and why is the problem still in NP?
Show answer
The guessing phase creates 3^n branches. The problem is still in NP because the running time counts the length of one branch, not the number of branches. Each branch makes n guesses and then checks the edges in O(V + E) time, which is polynomial.
Exercise
For a "no" instance of an NP problem, what must be true of the NTM's computation tree?
Show answer
Every branch must reject. A single accepting branch would make the machine accept.