Sub topic 6
The Nondeterministic Definition of NP
The previous chapter defined NP with verifiers. A problem is in NP when a polynomial-time algorithm can check a short certificate for every yes-instance. This chapter covers the original definition, which uses nondeterministic Turing machines, and shows that the two definitions describe the same class. Many textbooks, papers, and the Cook-Levin proof use the machine-based wording, so you need to be able to read it.
This chapter assumes you know the verifier definition of NP, Big-O notation, and the basic idea of a Turing machine: a finite set of states, a tape, and a transition rule applied one step at a time.
Deterministic Versus Nondeterministic Machines
A deterministic Turing machine (DTM) has a transition function. For each pair of current state and symbol under the head, there is exactly one next move. Running a DTM on an input produces a single sequence of configurations, which ends in accept, ends in reject, or never ends.
A nondeterministic Turing machine (NTM) has a transition relation instead. For a given state and symbol it may allow zero, one, or several next moves. A run on an input therefore branches, and the possible runs form a computation tree. Each path from the root to a leaf is one possible computation.
An NTM's acceptance rule is deliberately lopsided:
- The NTM accepts input x if at least one branch reaches the accept state.
- The NTM rejects x only if no branch accepts.
An NTM runs in time t(n) if, for every input of length n, every branch halts within t(n) steps. The class NTIME(t(n)) contains the languages decided by such machines. The classical definition is:
NP = the union of NTIME(n^k) over all constants k ≥ 1.
The name stands for nondeterministic polynomial time. It does not mean "non-polynomial."
The input is accepted because one leaf accepts. Each branch is short: three choices followed by a sum. The tree as a whole, however, has 2^n leaves.
Guess, Then Check
Almost every NTM you meet in practice has two phases:
- Guess. Nondeterministically write down a candidate solution, one choice per step. The candidate could be a subset, a truth assignment, or a vertex ordering.
- Check. Deterministically verify the candidate and accept if it passes.
This is the verifier definition with the certificate supplied by branching instead of handed in as a second input. The code below simulates that NTM deterministically. Each recursive call is one nondeterministic choice, and the or carries out the "some branch accepts" rule.
def ntm_subset_sum(numbers, target):
"""Deterministic simulation of a guess-then-check NTM for Subset Sum.
Returns (accepted, number_of_leaves_explored)."""
leaves = 0
def branch(i, chosen):
nonlocal leaves
if i == len(numbers):
# Check phase: a deterministic, polynomial-time verification.
leaves += 1
return sum(numbers[j] for j in chosen) == target
# Guess phase: two possible moves at this step.
return branch(i + 1, chosen) or branch(i + 1, chosen + [i])
accepted = branch(0, [])
return accepted, leaves
print(ntm_subset_sum([3, 5, 2], 7)) # (True, 4): stops at the first accepting leaf
print(ntm_subset_sum([3, 5, 2], 11)) # (False, 8): must visit every leaf to reject
print(ntm_subset_sum(list(range(1, 16)), 1000)[1]) # 32768 = 2**15 leavesThe output shows the cost. A single branch takes O(n) choices plus an O(n) check. A deterministic machine that wants to reject has to rule out every branch, which here means 2^n leaves.
Why the Two Definitions Are Equivalent
| Aspect | Verifier definition | NTM definition | Column | Column | Column | Column | Column | Column |
|---|---|---|---|---|---|---|---|---|
| Object | Deterministic verifier V(x, c) | Nondeterministic machine N(x) | ||||||
| Yes-instance | Some certificate c with V(x, c) = accept | Some branch of N on x accepts | ||||||
| Length bound | \ | c\ | ≤ q(\ | x\ | ) for a polynomial q | Every branch has length ≤ p(\ | x\ | ) |
| Witness | The certificate | The sequence of choices on an accepting branch |
Verifier to NTM. Suppose V runs in time p(n + m), where m is the certificate length, and certificates have length at most q(n). Build an NTM N that first writes q(n) symbols nondeterministically, one per step, and then runs V on (x, guessed string). Every branch halts within q(n) + p(n + q(n)) steps, which is a polynomial. N has an accepting branch exactly when some certificate makes V accept.
NTM to verifier. Suppose N runs in time p(n), and at each step it chooses among at most b moves, where b is a constant fixed by N's transition relation. Use the sequence of choices along an accepting branch as the certificate. It has p(n) entries, each from a set of size b, so its length is O(p(n)). The verifier simulates N deterministically, follows the listed choice at every step, and accepts if that path accepts. This takes polynomial time. If x is a yes-instance, some choice sequence works. If x is a no-instance, no choice sequence leads to acceptance, so the verifier rejects every certificate.
Both definitions therefore pick out the same set of languages.
What Simulation Costs
A DTM can simulate an NTM by walking its whole computation tree. With branching factor b and depth p(n), the tree has at most b^p(n) leaves. That gives NP ⊆ EXP, where EXP is the class of problems solvable in time 2^poly(n). A depth-first walk only needs to store the current path, which shows NP ⊆ PSPACE as well. The branch recursion above is that depth-first walk.
No one knows a simulation that avoids exponential time in general. Asking whether one exists is the P vs NP question in machine form. Every DTM is an NTM that happens never to branch, so P ⊆ NP holds trivially.
Common Misreadings
- "An NTM is a parallel computer." It would need exponentially many processors in the worst case. No physical machine works this way, and quantum computers are not believed to solve NP-complete problems efficiently.
- "The NTM magically finds the answer." The model only defines which inputs count as accepted. It is a mathematical description of a language, not an algorithm you can run.
- "Rejection is symmetric." It is not. Accepting needs one good branch, while rejecting requires every branch to fail. If you swap the accept and reject states of an NTM, you do not get the complement language. That asymmetry is why co-NP is studied as a separate class, covered in a later chapter.
- "Branches may run forever on no-instances." For NP, every branch must halt within the polynomial bound on every input, not only on yes-instances.
Exercise
Describe a guess-then-check NTM for 3-coloring a graph with n vertices and m edges, and bound its running time.
Show answer
Guess phase: for each vertex, nondeterministically pick one of three colors, which takes n steps with branching factor 3. Check phase: for each edge (u, v), reject if u and v got the same color, which takes O(m) steps. Accept if every edge passes. Every branch runs in O(n + m) time, so 3-coloring is in NP. The certificate is the list of n colors.
Exercise
An NTM N decides L in time n^2 with at most 4 choices per step. What is the certificate length in the NTM-to-verifier conversion, and how many leaves can a brute-force deterministic simulation visit?
Show answer
The certificate is one choice per step, so n^2 choices of 2 bits each, or 2n^2 bits. The computation tree has at most 4^(n^2) = 2^(2n^2) leaves, so brute-force simulation can take time exponential in n^2.
Exercise
Why does swapping the accept and reject states of an NTM for L generally fail to give an NTM for the complement of L?
Show answer
After the swap, the new machine accepts x if some branch of the original machine rejected. On a yes-instance of L, there is usually at least one rejecting branch alongside the accepting one, so the new machine also accepts that x. Deciding the complement correctly would require accepting only when every original branch rejects, and the NTM acceptance rule cannot express that condition.