Algorithm Search⌘K

Modelcore

Nondeterministic Turing machine

Also called NTM.

Turing machine with multiple allowed transitions; accepts if some computation path accepts.

A nondeterministic Turing machine is a Turing machine whose transition function gives a set of possible moves, so a configuration can have several successors. It accepts an input when at least one of its computation paths reaches the accept state. Its running time is the length of its longest path. The class NP is the set of languages that such a machine decides in polynomial time, and the best known deterministic simulation takes time exponential in the running time. Whether this loss can be avoided is open, and it is the P versus NP question.

The machine

A nondeterministic machine NN has the same parts as the one-tape machine of the Turing machine page, except for the transition function. Its states QQ include a start state q0q_0, accept and reject. Its tape alphabet is Γ∋⊔\Gamma \ni \sqcup, its input alphabet is Σ\Sigma, and

δ:(Q∖{accept,reject})×Γ→P(Q×Γ×{L,R}),\delta : (Q \setminus \{\text{accept}, \text{reject}\}) \times \Gamma \to \mathcal{P}(Q \times \Gamma \times \{L, R\}),

where P\mathcal{P} is the power set. The set δ(q,a)\delta(q, a) is finite and can be empty. Let b=max⁡q,a∣δ(q,a)∣b = \max_{q,a} |\delta(q,a)|, and assume b≥1b \ge 1. Configurations are as for the deterministic machine. We write C⊢C′C \vdash C' when C′C' follows from CC by one move in δ(q,a)\delta(q, a). A configuration can have zero, one or up to bb successors.

A computation path on an input ww is a sequence C0⊢C1⊢⋯C_0 \vdash C_1 \vdash \cdots with C0=q0 wC_0 = q_0\,w that stops at a halting configuration (state accept or reject) or at a configuration with no successor, or else goes on without end. The paths form a tree, the computation tree, whose root is C0C_0 and whose nodes have at most bb children.

Acceptance. NN accepts ww if some computation path on ww ends in a configuration with state accept. The language of NN is L(N)={w∈Σ∗:N accepts w}L(N) = \{ w \in \Sigma^* : N \text{ accepts } w\}. The word ww is not in L(N)L(N) when no path accepts: every path then rejects, stops without a successor, or runs forever. NN decides LL if L(N)=LL(N) = L and every path on every input is finite. The rule is one-sided. A single accepting path is enough to accept, but all paths must fail to reject. The machine does not guess "correctly" in any physical sense. The definition says only that a good sequence of choices exists.

Example. Let L3L_3 be the set of words over {0,1}\{0,1\} whose third symbol from the right is 11. The machine N3N_3 below guesses the position of that 11. In state q0q_0 it scans right, and when it reads a 11 it may either go on in q0q_0 or commit to this 11 by entering q1q_1. After the commit it reads two more symbols, then needs a blank. A pair that the table does not list has δ(q,a)=∅\delta(q, a) = \emptyset.

State Reads Choices (next state,write,move)(\text{next state}, \text{write}, \text{move})
q0q_0 00 (q0,0,R)(q_0, 0, R)
q0q_0 11 (q0,1,R)(q_0, 1, R) or (q1,1,R)(q_1, 1, R)
q1q_1 00 or 11 (q2,the same symbol,R)(q_2, \text{the same symbol}, R)
q2q_2 00 or 11 (q3,the same symbol,R)(q_3, \text{the same symbol}, R)
q3q_3 ⊔\sqcup (accept, ⊔\sqcup, RR)

On 01100110 the computation tree has three paths. The path that commits at the second symbol accepts after 5 steps. The two paths that stay in q0q_0 at the second symbol both stop without a successor after 4 steps. So N3N_3 accepts 01100110, and indeed the third symbol from the right is 11. On 00000000 the only path stops after 4 steps, so N3N_3 rejects. Every path on a word of length nn has at most n+1n + 1 steps. So N3N_3 decides L3L_3 in time n+1n + 1.

What it counts

The time of a path is its number of steps. NN runs in time t(n)t(n) if, for every input of length nn, every computation path has at most t(n)t(n) steps. In particular NN halts on every path. This is the measure of Sipser: the longest path of the tree, not the shortest accepting path. It is the measure of this page. For a function tt, NTIME(t(n))\mathrm{NTIME}(t(n)) is the set of languages that some nondeterministic multitape machine decides in time O(t(n))O(t(n)). The class NP\mathrm{NP} is

NP=⋃k≥1NTIME(nk).\mathrm{NP} = \bigcup_{k \ge 1} \mathrm{NTIME}(n^k).

The space of a path is the number of distinct cells that the head visits, and the space of NN is the largest over all paths. The cost of a run does not add the costs of the branches: the paths are alternative runs, not parallel work that you pay for.

A nondeterministic machine is not a probabilistic machine. It assigns no probability to its paths, and it has no error. It also does not model a real parallel computer, because the number of branches can be exponential in the time.

Relation to other models

Every deterministic machine is a nondeterministic machine with ∣δ(q,a)∣=1|\delta(q,a)| = 1 for all q,aq, a, so DTIME(t)⊆NTIME(t)\mathrm{DTIME}(t) \subseteq \mathrm{NTIME}(t). The multitape version has the transition function δ:(Q∖{accept,reject})×Γk→P(Q×Γk×{L,R,S}k)\delta : (Q \setminus \{\text{accept}, \text{reject}\}) \times \Gamma^k \to \mathcal{P}(Q \times \Gamma^k \times \{L,R,S\}^k). The one-tape simulation of the multitape Turing machine page carries over to the nondeterministic case: the machine makes its choice at the step where the deterministic simulation applies δ\delta. So the polynomial-time classes do not depend on the number of tapes.

Theorem (deterministic simulation). Let NN be a nondeterministic machine that runs in time t(n)t(n), where t(n)≥nt(n) \ge n for all nn, and let L=L(N)L = L(N). Then a deterministic multitape machine DD decides LL in time 2O(t(n))2^{O(t(n))}. A deterministic one-tape machine decides LL in time 2O(t(n))2^{O(t(n))} too. Hence NTIME(t(n))⊆⋃c≥1DTIME(2c t(n))\mathrm{NTIME}(t(n)) \subseteq \bigcup_{c \ge 1} \mathrm{DTIME}(2^{c\,t(n)}).

Proof. Take NN with one tape and bb as above. Number the elements of each set δ(q,a)\delta(q,a) as 1,…,∣δ(q,a)∣1, \ldots, |\delta(q,a)|. A string c=c1⋯cℓ∈{1,…,b}ℓc = c_1 \cdots c_\ell \in \{1, \ldots, b\}^\ell is a list of choices. The run along cc starts in C0C_0 and does this for i=1,…,ℓi = 1, \ldots, \ell. If the configuration is halting or has ∣δ(q,a)∣<ci|\delta(q,a)| < c_i (this includes δ(q,a)=∅\delta(q,a) = \emptyset), the run stops. Else it makes move number cic_i of δ(q,a)\delta(q,a). The status of the run is accept if it stops in, or ends in, a configuration with state accept. It is alive if it makes all ℓ\ell moves and the last configuration is not halting. Else it is dead.

The machine DD has three tapes: the input, a work tape on which it simulates NN, and a tape that holds the string cc. For ℓ=0,1,2,…\ell = 0, 1, 2, \ldots in turn, and for each c∈{1,…,b}ℓc \in \{1,\ldots,b\}^\ell in lexicographic order, DD performs the run along cc. It copies the input to the work tape, then simulates the moves with the help of cc. Then it erases the work tape. If a run has status accept, DD accepts. After all strings of length ℓ\ell, if no run was alive, DD rejects. Else it goes on with ℓ+1\ell + 1.

Correctness. Suppose NN accepts ww. Some accepting path has ss steps and a list of choices c∈{1,…,b}sc \in \{1,\ldots,b\}^s, and the run along cc has status accept. DD reaches level ss unless it accepts earlier. At each level it can reject only if no run is alive. A reject at level ℓ<s\ell < s would mean that no run along a string of length ℓ\ell is alive. But the first ℓ\ell choices of the accepting path give a run of that length that makes all ℓ\ell moves and is not halting, because ℓ<s\ell < s. So that run is alive. Hence DD does not reject before level ss, and it accepts at level ss at the latest. Conversely, DD accepts only after a run of status accept, and that run is a real accepting path of NN. So DD accepts ww if and only if NN accepts ww, as long as DD halts.

Suppose NN does not accept ww. At level ℓ=t(n)+1\ell = t(n) + 1 no run is alive: an alive run is a path of NN with t(n)+1t(n) + 1 steps whose last configuration is not halting, and no path has more than t(n)t(n) steps. So DD halts at level t(n)+1t(n)+1 at the latest. It does not accept, because no run has status accept. It rejects.

Time. DD stops by level t(n)+1t(n) + 1. The number of strings it tries is at most ∑ℓ=0t(n)+1bℓ≤(t(n)+2) bt(n)+1\sum_{\ell=0}^{t(n)+1} b^\ell \le (t(n)+2)\, b^{t(n)+1}. For each string, DD spends O(n)O(n) steps to copy the input, O(ℓ)O(\ell) steps to simulate the moves (each move is a constant number of steps), O(n+ℓ)O(n + \ell) steps to erase the work tape, and O(ℓ)O(\ell) steps to produce the next string. This is O(t(n))O(t(n)) steps in all, since n≤t(n)n \le t(n) and ℓ≤t(n)+1\ell \le t(n)+1. The total is O(t(n)2 bt(n)+1)=2O(t(n))O(t(n)^2\, b^{t(n)+1}) = 2^{O(t(n))}, because bb is a constant and t(n)2≤22t(n)t(n)^2 \le 2^{2 t(n)} for t(n)≥1t(n) \ge 1. A one-tape machine simulates DD with a square of this time (the claim of the multitape page), and (2O(t))2=2O(t)(2^{O(t)})^2 = 2^{O(t)}. A machine with more tapes is simulated in the same way, with O(1)O(1) steps for each of its moves. ■\blacksquare

Theorem (recognizers). For every nondeterministic machine NN there is a deterministic machine DD with L(D)=L(N)L(D) = L(N). If every path of NN is finite on every input, DD can be made a decider.

Proof. Let DD be the machine of the proof above, with this change: it stops with accept when a run has status accept, and otherwise it goes on to level ℓ+1\ell + 1 while some run is alive, and it rejects when a level has no alive run. If NN accepts ww, the argument above shows that DD accepts ww at level ss at the latest, even when other paths of NN run forever, because the search is by level and so is breadth first. If NN does not accept ww, then DD never accepts, and it either rejects or runs forever, so L(D)=L(N)L(D) = L(N). Now suppose that every path of NN on ww is finite. The computation tree is finitely branching with no infinite path, so by König's lemma it is finite. Then it has some depth hh, and no run is alive at level h+1h + 1, so DD halts. Hence DD decides L(N)L(N). ■\blacksquare

The same simulation shows NTIME(t(n))⊆DSPACE(t(n))\mathrm{NTIME}(t(n)) \subseteq \mathrm{DSPACE}(t(n)) for t(n)≥nt(n) \ge n, when the space counts the cells of all tapes. The work tape and the string cc have O(t(n))O(t(n)) cells, and DD reuses them for each run. The Savitch theorem is a separate result about nondeterministic space.

Known results in the model

  • NP. NP=⋃kNTIME(nk)\mathrm{NP} = \bigcup_k \mathrm{NTIME}(n^k). A language LL is in NP if and only if there are polynomials p,qp, q (with natural-number coefficients) and a deterministic machine VV such that VV runs in time q(∣x∣+∣y∣)q(|x| + |y|) and L={x:∃y, ∣y∣≤p(∣x∣) and V accepts (x,y)}L = \{ x : \exists y,\ |y| \le p(|x|) \text{ and } V \text{ accepts } (x, y) \}. The string yy is a certificate, and VV is a verifier. The proof is below.
  • Place of NP. P⊆NP\mathrm{P} \subseteq \mathrm{NP}, because a deterministic machine is a nondeterministic machine. By the theorem above, NP⊆EXP\mathrm{NP} \subseteq \mathrm{EXP} and NP⊆PSPACE\mathrm{NP} \subseteq \mathrm{PSPACE}. See P, PSPACE and EXPTIME. Whether P=NP\mathrm{P} = \mathrm{NP} is open.
  • Complete problems. The Cook-Levin theorem shows that SAT is NP-complete. SAT is in NP because a machine can guess an assignment of the variables and check it in polynomial time.
  • Acceptance is not symmetric. coNP is the class of complements of languages in NP. The definition gives no machine for it, because "no path accepts" is a universal statement. Whether NP=coNP\mathrm{NP} = \mathrm{coNP} is open.

Proof of the certificate characterization. Here pp is a polynomial with natural-number coefficients, and qq is a polynomial. Assume that L∈NPL \in \mathrm{NP}. Let NN decide LL with every path of at most r(n)r(n) steps, where rr is a polynomial. Let VV be the machine that, on (x,y)(x, y) with y∈{1,…,b}∗y \in \{1, \ldots, b\}^* coded in binary, runs NN on xx and uses yiy_i as the choice at step ii. VV rejects if a choice is out of range, if yy ends before NN halts, or if NN halts in reject. VV accepts if NN reaches accept. The run takes O(r(∣x∣))O(r(|x|)) steps on a multitape machine. If x∈Lx \in L, an accepting path has at most r(∣x∣)r(|x|) steps, and its list of choices is a certificate of length O(r(∣x∣))O(r(|x|)). If VV accepts (x,y)(x, y), the run is an accepting path of NN, so x∈Lx \in L.

Conversely, assume that VV, pp and qq are as stated. Let (x,y)(x, y) be coded as xx, a separator symbol, then yy, and measure the time of VV in ∣x∣+∣y∣|x| + |y|. Let NN work in three stages on an input xx of length nn. First it writes p(n)p(n) ones on a work tape. This is deterministic and takes time polynomial in nn, because pp is a fixed polynomial with natural-number coefficients. Second it writes a string yy on another tape, symbol by symbol. At each step it chooses among writing 00, writing 11 and stopping, and it counts its symbols against the ones so that it stops after p(n)p(n) symbols at the latest. Third it simulates VV on the coded pair (x,y)(x, y), which has length at most n+p(n)+1n + p(n) + 1, and accepts if and only if VV accepts. Each path has polynomially many steps, because ∣y∣≤p(n)|y| \le p(n) and the third stage takes at most q(n+p(n)+1)q(n + p(n) + 1) simulated steps. A path accepts if and only if its string yy makes VV accept. So NN accepts xx if and only if some yy with ∣y∣≤p(n)|y| \le p(n) makes VV accept, that is, if and only if x∈Lx \in L. Hence L∈NPL \in \mathrm{NP}. ■\blacksquare

References

  1. Michael Sipser, Introduction to the Theory of Computation, third edition, Section 3.2 (nondeterministic Turing machines) and Section 7.3 (the class NP)
  2. Sanjeev Arora and Boaz Barak, Computational Complexity: A Modern Approach, Chapter 2, Section 2.1 (the class NP), including Section 2.1.2 (nondeterministic Turing machines)
  3. Christos H. Papadimitriou, Computational Complexity, Addison-Wesley, 1994, Section 2.7 (nondeterministic machines)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/8WWSGW/text.yaml