Algorithm Search⌘K

Theoremcore

RAM and Turing machine equivalence

Also called RAM simulates Turing machine, Turing machine simulates RAM.

Polynomial-time simulations exist in both directions, so polynomial-time classes are robust to the choice of model.

A random-access machine with the logarithmic cost rule and a multitape Turing machine can simulate each other with a polynomial loss of time. A RAM program of logarithmic-cost time t(n)t(n) runs on a multitape machine in O(t(n)2)O(t(n)^2) steps, and a multitape machine of time T(n)T(n) runs on a RAM in O(T(n)log⁡T(n))O(T(n) \log T(n)) logarithmic-cost time. So the class P is the same on both models. The result fails for a RAM with unit-cost multiplication, where repeated squaring builds a number with exponentially many bits in a linear number of steps.

Statement

The RAM is the machine of the random-access machine page, with the instructions LOAD, STORE, ADD, SUB (with the operands =c=c, ii and ∗i*i), READ, JUMP, JZERO, JEOF and HALT. It has no multiplication. A RAM decides a language L⊆{0,1}∗L \subseteq \{0,1\}^* if, on every input word w∈{0,1}∗w \in \{0,1\}^*, given as the ∣w∣|w| integers w1,…,w∣w∣w_1, \ldots, w_{|w|} in order, it halts, and it halts with R0=1R_0 = 1 if w∈Lw \in L and with R0=0R_0 = 0 if w∉Lw \notin L. Its time is the logarithmic cost of the run (the table of the RAM page), and the time t(n)t(n) is the largest time over the words of length nn. The Turing machine is the deterministic kk-tape machine of the multitape Turing machine page, and its time is the number of steps.

Theorem.

  1. RAM by Turing machine. Let a RAM program PP decide LL in time t(n)t(n). Then a deterministic 5-tape Turing machine decides LL in time O(t(n)2)O(t(n)^2). The constant depends on PP only.
  2. Turing machine by RAM. Let a deterministic multitape machine MM decide LL in time T(n)T(n) with T(n)≥max⁡(n,2)T(n) \ge \max(n, 2). Then a RAM program decides LL in logarithmic-cost time O(T(n)log⁡T(n))O(T(n) \log T(n)) and in unit-cost time O(T(n))O(T(n)). The constants depend on MM only.
  3. Unit cost without multiplication. Let a RAM program decide LL in unit-cost time t(n)t(n). Then it decides LL in logarithmic-cost time O(t(n)2)O(t(n)^2), and a multitape machine decides LL in time O(t(n)4)O(t(n)^4).
  4. Corollary. The languages decided in polynomial time are the same for the multitape machine, for the logarithmic-cost RAM and for the unit-cost RAM without multiplication.

The statement is about decision problems with the input read in order, as the RAM page defines it.

In plain words

A real computer reaches any memory cell in one step. A Turing machine must walk along its tape. The theorem says the computer is not much faster, in the sense that matters for polynomial time.

To copy a computer on a Turing machine, keep a list of every cell written, with its number and value. To read a cell, scan the list. The list is no longer than the work done, so the run costs about the square.

To copy a Turing machine on a computer, keep the tape in memory and the head position in a register. Each tape step takes a fixed number of instructions.

Two conditions are needed. Instruction cost must count the digits of numbers, and multiplication must not be a single fast step, because a few multiplications build a number too long to write.

Proof

The two parts have complete proofs in separate files.

  • Part 1 is proved in the proof of the simulation of a RAM. The Turing machine stores the registers that the RAM has written as a list of pairs (address, value) on one tape. A step of the RAM scans this list. The list has length O(t)O(t) by the cost rule, there are at most tt steps, and every other operation costs at most the logarithmic cost of the instruction. This gives O(t2)O(t^2).
  • Part 2 is proved in the proof of the simulation of a Turing machine. The RAM stores the cell cc of tape ii in the register B+kc+(i−1)B + kc + (i-1) for a constant BB, and it keeps the head positions in kk registers. Each step of MM costs a constant number of instructions, each of cost O(log⁡T)O(\log T).

Proof of part 3. Let VV be the larger of 22 and the largest number that occurs in the text of the program (operand constants and register numbers). After ss executed instructions, every register and the accumulator hold a value of at most 2sV2^s V. This is true for s=0s = 0, since all values are 00. Each instruction creates a value that is an input bit (at most 11), a constant of the program (at most VV), a copy of a value that exists, or a sum of two values that exist, which is at most twice the larger one; a difference is not larger than the minuend. So the bound holds for s+1s + 1. Every number that an instruction uses (a constant, a register number or a register value) is a constant of the program or a value of a register or the accumulator. It has therefore at most t+lg⁡V+1t + \lg V + 1 bits. An instruction costs at most four such lengths, so it costs O(t)O(t) in the logarithmic-cost rule, and the run costs O(t2)O(t^2) in all. Part 1 then gives a multitape machine with O((t2)2)=O(t4)O((t^2)^2) = O(t^4) steps. ■\blacksquare

Proof of part 4. Write TM\mathrm{TM}, LOG\mathrm{LOG} and UNIT\mathrm{UNIT} for the three classes of languages decided in polynomial time. Part 1 gives LOG⊆TM\mathrm{LOG} \subseteq \mathrm{TM}, since a polynomial of a polynomial is a polynomial. A machine that halts within p(n)p(n) steps also halts within T(n)=p(n)+n+2T(n) = p(n) + n + 2 steps, which is a polynomial with T(n)≥max⁡(n,2)T(n) \ge \max(n, 2). So part 2 gives TM⊆LOG\mathrm{TM} \subseteq \mathrm{LOG} and TM⊆UNIT\mathrm{TM} \subseteq \mathrm{UNIT}. Part 3 gives UNIT⊆LOG\mathrm{UNIT} \subseteq \mathrm{LOG}. So TM⊆UNIT⊆LOG⊆TM\mathrm{TM} \subseteq \mathrm{UNIT} \subseteq \mathrm{LOG} \subseteq \mathrm{TM}, and the three classes are equal. ■\blacksquare

Consequences

  • The class P does not depend on the choice among the multitape machine and the RAM with logarithmic cost. This is the reason that polynomial time is the usual meaning of "efficient" for both models.
  • An algorithm analysed on the unit-cost RAM with addition and subtraction, with an input of bits, runs on a Turing machine in a time polynomial in its unit-cost time, with the exponent 4 of part 3. So a polynomial bound on the RAM is a polynomial bound in the other models.
  • The two models compute the same functions, which supports the Church-Turing thesis.
  • The theorem does not transfer exact bounds. A linear-time RAM algorithm is not a linear-time Turing machine algorithm. The word RAM bounds in particular do not carry over by a constant factor.

Tightness

Unit-cost multiplication. Add the instruction MULT X (R0←R0⋅vR_0 \leftarrow R_0 \cdot v) at unit cost. The program below reads kk ones and halts with R0=22kR_0 = 2^{2^k}.

1   LOAD =2
2   STORE 1
3   JEOF 9
4   READ
5   LOAD 1
6   MULT 1
7   STORE 1
8   JUMP 3
9   LOAD 1
10  HALT

Each pass through lines 3 to 8 squares R1R_1, so after kk passes R1=22kR_1 = 2^{2^k}. The unit-cost time is 6k+56k + 5. The number 22k2^{2^k} has 2k+12^k + 1 bits, and a Turing machine that writes it makes at least 2k+12^k + 1 steps. So no simulation of the unit-cost RAM with multiplication can run in time polynomial in the unit-cost time, for the task of computing the output number. This is the reason for the logarithmic cost rule. For decision problems, Hartmanis and Simon (1974) showed that polynomial time on a unit-cost RAM with multiplication and bitwise Boolean instructions equals PSPACE. So the class P is not robust under that machine unless P equals PSPACE.

Logarithmic cost with multiplication. The same simulation, with the school method for the product, takes O(ab)O(a b) steps for numbers of aa and bb bits, which is at most the square of the cost of the instruction. Since the sum of the squares of costs csc_s with ∑cs≤t\sum c_s \le t is at most t2t^2, the bound O(t2)O(t^2) of part 1 still holds. The proof file does not treat this instruction.

Input in registers. Suppose that the input w1⋯wnw_1 \cdots w_n is in R1,…,RnR_1, \ldots, R_n and that R0=nR_0 = n. Then the RAM reads the last symbol wnw_n in O(log⁡n)O(\log n) cost, but a Turing machine needs at least n−1n - 1 steps to decide the language of the words with last symbol 11, since its head must reach the last cell. Part 1 is false in that convention. The sequential input removes this effect. If t(n)≥nlg⁡nt(n) \ge n \lg n, the Turing machine can write the nn input registers as a list in time O(nlog⁡n)O(n \log n), the list adds O(nlog⁡n)=O(t)O(n \log n) = O(t) to its length, and the bound of part 1 is O(t2)O(t^2) again.

Exponent. The exponent 2 of part 1 comes from one scan of the list for each instruction. It is not known to be optimal for this simulation.

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

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∑s=1N(ms+cs+λs)+K′≤K(N⋅2t(n)+t(n)+t(n)+1)+K′.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.

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

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

All proofs on one page

History

The RAM was studied by Shepherdson and Sturgis (1963) and by Elgot and Robinson (1964), and the time-bounded RAM with the unit and the logarithmic cost rules is in Cook and Reckhow (1973). Cook and Reckhow (1973) and Aho, Hopcroft and Ullman (1974, Chapter 1) give the simulations between the RAM and the Turing machine. The proofs on this page use the accumulator machine of Aho, Hopcroft and Ullman without MULT and DIV, with sequential input. The invariance thesis (Slot and van Emde Boas, 1984; see van Emde Boas 1990) says that reasonable machines simulate each other with a polynomial loss of time and a constant factor of space.

References

  1. Alfred V. Aho, John E. Hopcroft and Jeffrey D. Ullman, The Design and Analysis of Computer Algorithms, Addison-Wesley, 1974, Chapter 1, the sections on the cost of RAM programs and on the relation between the Turing machine and the RAM
  2. Stephen A. Cook and Robert A. Reckhow, Time-bounded random access machines, Journal of Computer and System Sciences 7(4), 1973, pp. 354 to 375
  3. Christos H. Papadimitriou, Computational Complexity, Addison-Wesley, 1994, Chapter 2, the section on random access machines
  4. J. Hartmanis and J. Simon, On the power of multiplication in random access machines, 15th Annual Symposium on Switching and Automata Theory, 1974, the theorem that polynomial time with multiplication and Boolean instructions equals PSPACE
  5. J. C. Shepherdson and H. E. Sturgis, Computability of recursive functions, Journal of the ACM 10(2), 1963, pp. 217 to 255
  6. C. C. Elgot and A. Robinson, Random-access stored-program machines, an approach to programming languages, Journal of the ACM 11(4), 1964, pp. 365 to 399
  7. Peter van Emde Boas, Machine models and simulations, in Handbook of Theoretical Computer Science, volume A, 1990, Chapter 1, the invariance thesis

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 proofH1 sketch
  • a full proof of correctness
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/ZBD1SF/text.yaml