Algorithm Search⌘K

Proofs: RAM and Turing machine equivalence

These are the human proofs of RAM and Turing machine equivalence, at full length.

A multitape Turing machine simulates a logarithmic-cost RAM in time O(t^2) full

This proof shows: ram-by-tm.

Statement. Let PP be a RAM program with the instructions LOAD, STORE, ADD, SUB, READ, JUMP, JZERO, JEOF, HALT, as on the random-access machine page. Let PP decide L⊆{0,1}∗L \subseteq \{0,1\}^* in logarithmic-cost time t(n)t(n): on every input word ww of length nn, given as nn integers in {0,1}\{0,1\}, PP halts with logarithmic cost at most t(n)t(n), and with R0=1R_0 = 1 if w∈Lw \in L and R0=0R_0 = 0 if w∉Lw \notin L. Then there is a deterministic 5-tape Turing machine MPM_P that decides LL in O(t(n)2)O(t(n)^2) steps. The constant depends on PP only.

Setup. Fix an input ww of length nn and the run of PP on ww. Let the run execute NN instructions, numbered 1,…,N1, \ldots, N, and let csc_s be the logarithmic cost of instruction ss. Then C=∑scs≤t(n)C = \sum_s c_s \le t(n). Every cost csc_s is at least 1, so N≤CN \le C. We write ℓ(x)\ell(x) for the bit length, with ℓ(0)=1\ell(0) = 1. Let KPK_P be the largest length of a number that occurs in the text of PP (a constant, a register number or a jump target). Every constant KK below depends on PP only.

Encoding. Numbers are written in binary on the tapes with the least significant bit first and with no leading zeros, and 00 is the string 0. A number on a tape is followed by a blank or by a separator.

Tapes of MPM_P.

  • Tape 1 holds the input ww. Its head moves only to the right, and it is on the next unread symbol.
  • Tape 2, the memory tape, holds a list of records #a:v, where aa and vv are numbers. The list stands in the first cells, and the cells after it are blank. The addresses of different records are different. A register RaR_a with a≥1a \ge 1 that has no record has the value 00. We write mm for the length of the list.
  • Tape 3 holds the accumulator R0R_0.
  • Tapes 4 and 5 are work tapes. Tape 4 holds an address, and tape 5 holds a value or a copy.

The program counter is in the finite control. Because PP has finitely many instructions, the control has finitely many states.

Invariant JsJ_s. Before instruction ss of the run, the control is at the instruction that PP executes as instruction ss. Tape 3 holds the value of R0R_0. Tape 2 holds exactly one record #a:v for each register RaR_a with a≥1a \ge 1 that a STORE has written in the instructions 1,…,s−11, \ldots, s-1, with its current value vv. Tapes 4 and 5 are blank. Tape 1 has read exactly as many symbols as the instructions READ before ss. All heads except head 1 are on cell 0.

Step 1: the length of the memory tape. Let msm_s be the length of the list before instruction ss. We show ms≤2∑r<scrm_s \le 2 \sum_{r<s} c_r. A record #a:v has length ℓ(a)+ℓ(v)+2\ell(a) + \ell(v) + 2. It is in the list because of the last STORE r<sr < s that wrote the register RaR_a, and at that time RaR_a received the value vv from R0R_0. By the cost table, the cost of STORE i is ℓ(c(0))+ℓ(i)\ell(c(0)) + \ell(i), and the cost of STORE *i is ℓ(c(0))+ℓ(i)+ℓ(c(i))\ell(c(0)) + \ell(i) + \ell(c(i)). In both cases cr≥ℓ(v)+ℓ(a)c_r \ge \ell(v) + \ell(a), and also cr≥2c_r \ge 2. So the record has length at most cr+2≤2crc_r + 2 \le 2 c_r. Different records belong to different instructions STORE, since one STORE writes one register. So the lengths add up to at most 2∑r<scr≤2t(n)2 \sum_{r<s} c_r \le 2t(n).

Step 2: the subroutines. All subroutines start and end with the heads of tapes 2, 4 and 5 on cell 0.

  • FIND. Tape 4 holds a number aa. The machine moves head 2 over the list. For each record it compares aa with the address, symbol by symbol, with head 4 and head 2 moving together, until the symbols differ or one string ends. If both strings end together, the record matches. Then it moves head 4 back to cell 0 and head 2 to the end of the record. For one record this takes O(the length of the record+1)O(\text{the length of the record}+1) steps, since the comparison makes at most as many steps as the shorter address has symbols plus one, and the rewind of head 4 takes as many. So FIND takes O(m+1)O(m + 1) steps. It stops at the first match. It ends in one of two states: found, with head 2 on the : of the record, or not found, with head 2 on the first blank after the list.
  • LOOKUP. FIND, then copy the value after the : to tape 5 if found, or write 0 on tape 5 if not found. The result on tape 5 is the value of RaR_a. LOOKUP takes O(m+ℓ(v))O(m + \ell(v)) steps where vv is the value, and ℓ(v)≤m\ell(v) \le m if found. So it takes O(m+1)O(m+1) steps. If a=0a = 0, the address names the accumulator (a program can reach R0R_0 only through an indirect operand whose register holds 00): LOOKUP copies tape 3 to tape 5 and does not use the list.
  • UPDATE. Tape 4 holds aa and tape 3 holds vv. If a=0a = 0, do nothing, since STORE then copies R0R_0 to R0R_0. Else FIND. If found, copy the part of the list after the old value to tape 5 and blank it on tape 2, write vv after the :, copy the part back from tape 5 after vv, and blank tape 5. This takes O(m+ℓ(v))O(m + \ell(v)) steps. If not found, write #, aa, : and vv at the end of the list, which takes O(ℓ(a)+ℓ(v))O(\ell(a) + \ell(v)) steps. Then rewind. The total is O(m+ℓ(a)+ℓ(v)+1)O(m + \ell(a) + \ell(v) + 1) steps.
  • ARITH. Tapes 3 and 5 hold xx and vv. Add: scan both from the left with a carry, and write the sum on tape 3. Subtract: scan with a borrow; if a borrow remains at the end, write 0, else erase the leading zeros of the result. Each takes O(ℓ(x)+ℓ(v)+1)O(\ell(x) + \ell(v) + 1) steps.

Step 3: one instruction. Assume JsJ_s. Let λs\lambda_s be the length of R0R_0 before instruction ss. We show that the machine executes instruction ss in at most K(ms+cs+λs)K(m_s + c_s + \lambda_s) steps if ss is a LOAD or a READ, in at most K(ms+cs)K(m_s + c_s) steps if ss is a STORE, ADD, SUB or JZERO, and in O(1)O(1) steps if ss is a JUMP, JEOF or HALT, and that Js+1J_{s+1} holds after it. Only LOAD and READ pay λs\lambda_s outside csc_s. In the other instructions that read R0R_0, λs≤cs\lambda_s \le c_s by the cost table. Operands: for the operand =c=c the machine writes cc on tape 5 in O(KP)O(K_P) steps. For the operand ii it writes ii on tape 4, which takes O(KP)O(K_P) steps, and it does LOOKUP, which leaves c(i)c(i) on tape 5. For the operand ∗i*i it writes ii on tape 4 and does LOOKUP, so tape 5 holds c(i)c(i). It then copies this number to tape 4, which takes O(ℓ(c(i)))O(\ell(c(i))) steps, blanks tape 5 in O(ℓ(c(i)))O(\ell(c(i))) steps, and does LOOKUP again, so tape 5 holds c(c(i))c(c(i)). Each of these takes O(ms+KP+ℓ(c(i)))O(m_s + K_P + \ell(c(i))) steps, and each of the lengths that occur is part of the cost csc_s of the instruction.

  • LOAD X: get the operand on tape 5 as above, and copy tape 5 to tape 3, after erasing the old content. This takes O(λs+ℓ(v))O(\lambda_s + \ell(v)) more steps, and ℓ(v)≤cs\ell(v) \le c_s.
  • STORE i, STORE *i: put the address on tape 4 (for *i, by LOOKUP of ii, a copy to tape 4 and the blanking of tape 5, which cost O(ℓ(a))O(\ell(a)) steps), so that tape 5 is blank, and do UPDATE with vv on tape 3. The register RaR_a gets R0R_0. The records of the other registers do not change. The cost is O(ms+ℓ(a)+ℓ(v)+KP)O(m_s + \ell(a) + \ell(v) + K_P), and ℓ(v)+ℓ(a)≤cs\ell(v) + \ell(a) \le c_s.
  • ADD X, SUB X: get the operand on tape 5, and do ARITH. The cost is O(ms+ℓ(R0)+ℓ(v))O(m_s + \ell(R_0) + \ell(v)), and cs≥ℓ(R0)+ℓ(v)c_s \ge \ell(R_0) + \ell(v). The result is R0+vR_0 + v or max⁡(R0−v,0)\max(R_0 - v, 0), as ARITH computes.
  • READ: erase tape 3, and write the symbol of tape 1 under head 1 on tape 3 (the number is 0 or 1), and move head 1 right. If head 1 reads a blank, no input is left, and the machine writes 0 and does not move head 1. This takes O(λs+1)O(\lambda_s + 1) steps.
  • JUMP k: set the control to kk. JZERO k: read tape 3 and jump if it is 0. JEOF k: jump if head 1 reads a blank. HALT: accept if tape 3 holds 1, else reject. Each takes O(1)O(1) steps.

After each instruction, the machine rewinds the heads to cell 0 (not head 1). This costs at most the lengths already counted. The effects are those of the RAM instruction, so Js+1J_{s+1} holds. For HALT the answer is the value of R0R_0, so MPM_P accepts if and only if PP halts with R0=1R_0 = 1.

Step 4: the length of the accumulator. In the list of Step 3, only LOAD and READ pay for the old length λs\lambda_s outside csc_s. In ADD and SUB the length λs=ℓ(R0)\lambda_s = \ell(R_0) is a part of csc_s. Let rr be the last instruction before ss that set R0R_0 (LOAD, ADD, SUB or READ). Then λs≤cr\lambda_s \le c_r: for LOAD X the new value is the value of XX, and its length is a part of crc_r; for ADD the new length is at most max⁡(ℓ(R0),ℓ(v))+1≤ℓ(R0)+ℓ(v)≤cr\max(\ell(R_0), \ell(v)) + 1 \le \ell(R_0) + \ell(v) \le c_r, where R0R_0 is the old value; for SUB it is at most the length of the old R0R_0, which is a part of crc_r; for READ it is 1≤cr1 \le c_r. If no instruction has set R0R_0, then λs=1\lambda_s = 1. A setter rr is the last setter before at most one later instruction ss, namely the next setter. So ∑sλs≤1+∑rcr≤t(n)+1\sum_s \lambda_s \le 1 + \sum_r c_r \le t(n) + 1, where the sum is over the instructions LOAD and READ.

Step 5: the total. The first step of MPM_P writes 0 on tape 3, and this makes J1J_1 true. By Step 3, Step 1 and Step 4, the total time of the simulation is at most

$$ K \sum_{s=1}^{N} (m_s + c_s + \lambda_s) + K' \le K\bigl( N \cdot 2t(n) + t(n) + t(n) + 1 \bigr) + K'. $$

Here the sum of the λs\lambda_s is over the instructions LOAD and READ, and the other terms are bounded by Step 1 (so ms≤2t(n)m_s \le 2t(n)) and by ∑scs≤t(n)\sum_s c_s \le t(n). Since N≤t(n)N \le t(n) and t(n)≥1t(n) \ge 1, this is O(t(n)2)O(t(n)^2). By the invariant at the halting instruction, MPM_P decides LL. ■\blacksquare

Remark. The bound uses that every instruction costs at least 1, so that the number of instructions is at most the time. It also uses that a STORE costs at least the length of its record, so that the list is as short as the time. These two facts are the reason for the logarithmic cost rule. With the unit rule and an instruction MULT, repeated squaring and a STORE make a record of length 2Ω(t)2^{\Omega(t)}, and the proof fails.

A RAM simulates a multitape Turing machine in logarithmic-cost time O(T log T) full

This proof shows: tm-by-ram.

Statement. Let MM be a deterministic kk-tape Turing machine, with the tape alphabet Γ\Gamma of gg symbols and the input alphabet {0,1}\{0,1\}, that decides L⊆{0,1}∗L \subseteq \{0,1\}^* and halts within T(n)T(n) steps on every input of length nn, where T(n)≥max⁡(n,2)T(n) \ge \max(n, 2). Then there is a RAM program PMP_M (with the instructions of the random-access machine page, no multiplication) that decides LL in unit-cost time O(T(n))O(T(n)) and in logarithmic-cost time O(T(n)log⁡T(n))O(T(n) \log T(n)). The constants depend on MM only.

Setup. Write Γ={γ0,…,γg−1}\Gamma = \{\gamma_0, \ldots, \gamma_{g-1}\} with γ0=⊔\gamma_0 = \sqcup, γ1=0\gamma_1 = 0 and γ2=1\gamma_2 = 1, and use the index jj as the code of γj\gamma_j. A blank cell has code 00, which is the value of a register that was never written. Let B=k+2B = k + 2. The registers are used like this.

  • RiR_i for 1≤i≤k1 \le i \le k holds the address of the register that stores the cell scanned by head ii.
  • Rk+1R_{k+1} is a pointer pp for the copy of the input.
  • The cell c≥0c \ge 0 of tape ii is stored in the register with the address B+kc+(i−1)B + kc + (i-1). These addresses are at least B>k+1B > k + 1 and are all different, for different pairs (i,c)(i, c), since (i−1)(i - 1) is the remainder of the offset modulo kk and cc is the quotient.
  • R0R_0 is the accumulator.

The program has two parts. Part I copies the input into the cells of tape 1.

      LOAD =B
      STORE k+1               ; p = B
loop  JEOF done
      READ                    ; acc = w_c, 0 or 1
      ADD =1                  ; code of the bit: 1 for 0, 2 for 1
      STORE *(k+1)            ; the cell gets the code
      LOAD k+1
      ADD =k
      STORE k+1               ; p = p + k
      JUMP loop
done  LOAD =B+i-1             ; for each i = 1..k
      STORE i                 ; head i on cell 0
      JUMP S(q0)

Part II has a block S(q)S(q) for each non-halting state qq of MM and a block for each of accept and reject. The block S(accept)S(\text{accept}) is LOAD =1, HALT. The block S(reject)S(\text{reject}) is LOAD =0, HALT. The block S(q)S(q) is a decision tree of depth kk that finds the scanned symbols. At a node for tape ii, with the symbols of the tapes 1,…,i−11, \ldots, i-1 already fixed by the path, it runs the tests for a=0,1,…,g−2a = 0, 1, \ldots, g-2 in this order. The test for aa is

      LOAD *i
      SUB =a
      JZERO (the node for tape i+1 in which the symbol of tape i is a)

and if all g−1g - 1 tests fail, the code goes on to the node in which the symbol of tape ii is g−1g - 1. The tree has gkg^k leaves, one for each tuple (a1,…,ak)(a_1, \ldots, a_k) of codes, and each leaf is a separate piece of code. In the leaf (q;a1,…,ak)(q; a_1, \ldots, a_k), let δ(q,γa1,…,γak)=(q′,γb1,…,γbk,D1,…,Dk)\delta(q, \gamma_{a_1}, \ldots, \gamma_{a_k}) = (q', \gamma_{b_1}, \ldots, \gamma_{b_k}, D_1, \ldots, D_k). The leaf has these instructions, for i=1,…,ki = 1, \ldots, k in turn.

  • LOAD =b_i, then STORE *i: write the symbol.
  • If Di=RD_i = R: LOAD i, ADD =k, STORE i.
  • If Di=LD_i = L: LOAD i, SUB =B+i-1, JZERO skip_i, LOAD i, SUB =k, STORE i, and then the label skip_i.
  • If Di=SD_i = S: nothing.

The leaf ends with JUMP S(q'). The program has O(gk)O(g^k) instructions, which is a constant.

Step 1: the test finds the symbol. Let x=RRix = R_{R_i}, the code of the scanned symbol, with 0≤x≤g−10 \le x \le g-1. The test for aa computes max⁡(x−a,0)\max(x - a, 0), and the jump is taken if and only if x≤ax \le a. The tests run for a=0,1,…a = 0, 1, \ldots in this order. If x=a0x = a_0, then the tests for a<a0a < a_0 fail, since x>ax > a. The test for a=a0a = a_0 jumps. So the first jump is at a=xa = x. If no test jumps, then x>g−2x > g - 2, so x=g−1x = g - 1. Hence the leaf that the block reaches is the leaf of the true symbols. (After each test the accumulator is overwritten by the next LOAD *i, so the tests do not interact.)

Step 2: the left move. The cell 00 of tape ii has the address B+i−1B + i - 1, and every cell has an address of at least this value. So SUB =B+i-1 gives 00 if and only if Ri≤B+i−1R_i \le B + i - 1, that is, if and only if head ii is on cell 00. In that case the move LL keeps the head in place, as in the machine, since JZERO skips the subtraction. Else SUB =k moves the address of head ii one cell to the left (the address of cell c−1c-1 is that of cell cc minus kk).

Invariant IjI_j. After jj steps of MM (and after Part I for j=0j = 0), the control is at the beginning of S(qj)S(q_j), where qjq_j is the state of MM after jj steps. For each ii the register RiR_i holds the address of the cell scanned by head ii. For each ii and cc the register with the address B+kc+(i−1)B + kc + (i-1) holds the code of the symbol in cell cc of tape ii.

Proof of I0I_0. The registers other than those of tape 1 are 00, which is the code of the blank, and tape i≥2i \ge 2 is blank at the start. Part I stores the codes of the input symbols at the addresses B+kcB + kc for c=0,1,…,n−1c = 0, 1, \ldots, n-1, since pp starts at BB and grows by kk at each pass, and JEOF done ends the loop after nn passes. The cells c≥nc \ge n of tape 1 are blank and the registers are 00. The heads start on cell 00, whose address is B+i−1B + i - 1.

Proof of Ij⇒Ij+1I_{j} \Rightarrow I_{j+1}. Assume IjI_j and qjq_j non-halting. By Step 1 the block reaches the leaf of the true tuple of symbols. In the leaf, for each ii the instruction pair LOAD =b_i, STORE *i writes the symbol bib_i into the register of the scanned cell, and then the move updates RiR_i as in Step 2. Different tapes use different registers, so the order in which the tapes are handled does not matter. The leaf jumps to S(qj+1)S(q_{j+1}). This is the configuration of MM after step j+1j+1. If qjq_j is accept, S(qj)S(q_j) halts with R0=1R_0 = 1, and if reject with R0=0R_0 = 0. ■\blacksquare (of the invariant)

Step 3: the cost. Let MM halt after T′≤T(n)T' \le T(n) steps on the input. A head moves at most one cell in a step, so during the run, every scanned cell has an index of at most max⁡(n,T′)≤T(n)\max(n, T')\le T(n) (the input has length n≤T(n)n \le T(n)). So every address and every register value that occurs is at most B+k(T(n)+1)+kB + k(T(n)+1) + k, and every constant in the program is bounded by a constant. An instruction costs at most four lengths (by the cost table), each at most lg⁡(B+k(T(n)+2))+1=O(log⁡T(n))\lg(B + k(T(n) + 2)) + 1 = O(\log T(n)), since T(n)≥2T(n) \ge 2. The number of instructions executed is as follows.

  • Part I executes 8n+O(1)8n + O(1) instructions with nn the length of the input, since each pass of the loop executes 8 instructions and the end has 2k+12k + 1.
  • Each step of MM executes at most 3k(g−1)+8k+13k(g-1) + 8k + 1 instructions: at most 3(g−1)3(g-1) for each tape in the tests, at most 88 for each tape in the leaf (the longest case is the move LL with 6 instructions plus 2 for the write), and 1 for the jump at the end of the leaf.
  • The last block has 2 instructions.

So the number of instructions is at most 8n+K1T′+K2≤KT(n)8n + K_1 T' + K_2 \le K T(n), using n≤T(n)n \le T(n). This is the unit-cost time O(T(n))O(T(n)). Multiplying by the cost per instruction, the logarithmic-cost time is O(T(n)log⁡T(n))O(T(n) \log T(n)). By the invariant, PMP_M halts with R0=1R_0 = 1 exactly when MM accepts. ■\blacksquare

Back to RAM and Turing machine equivalence