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 runs on a multitape machine in steps, and a multitape machine of time runs on a RAM in 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 , and ), READ, JUMP, JZERO, JEOF and HALT. It has no multiplication. A RAM decides a language if, on every input word , given as the integers in order, it halts, and it halts with if and with if . Its time is the logarithmic cost of the run (the table of the RAM page), and the time is the largest time over the words of length . The Turing machine is the deterministic -tape machine of the multitape Turing machine page, and its time is the number of steps.
Theorem.
- RAM by Turing machine. Let a RAM program decide in time . Then a deterministic 5-tape Turing machine decides in time . The constant depends on only.
- Turing machine by RAM. Let a deterministic multitape machine decide in time with . Then a RAM program decides in logarithmic-cost time and in unit-cost time . The constants depend on only.
- Unit cost without multiplication. Let a RAM program decide in unit-cost time . Then it decides in logarithmic-cost time , and a multitape machine decides in time .
- 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 by the cost rule, there are at most steps, and every other operation costs at most the logarithmic cost of the instruction. This gives .
- Part 2 is proved in the proof of the simulation of a Turing machine. The RAM stores the cell of tape in the register for a constant , and it keeps the head positions in registers. Each step of costs a constant number of instructions, each of cost .
Proof of part 3. Let be the larger of and the largest number that occurs in the text of the program (operand constants and register numbers). After executed instructions, every register and the accumulator hold a value of at most . This is true for , since all values are . Each instruction creates a value that is an input bit (at most ), a constant of the program (at most ), 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 . 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 bits. An instruction costs at most four such lengths, so it costs in the logarithmic-cost rule, and the run costs in all. Part 1 then gives a multitape machine with steps.
Proof of part 4. Write , and for the three classes of languages decided in polynomial time. Part 1 gives , since a polynomial of a polynomial is a polynomial. A machine that halts within steps also halts within steps, which is a polynomial with . So part 2 gives and . Part 3 gives . So , and the three classes are equal.
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 () at unit cost. The program below reads ones and halts with .
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 , so after passes . The unit-cost time is . The number has bits, and a Turing machine that writes it makes at least 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 steps for numbers of and bits, which is at most the square of the cost of the instruction. Since the sum of the squares of costs with is at most , the bound of part 1 still holds. The proof file does not treat this instruction.
Input in registers. Suppose that the input is in and that . Then the RAM reads the last symbol in cost, but a Turing machine needs at least steps to decide the language of the words with last symbol , since its head must reach the last cell. Part 1 is false in that convention. The sequential input removes this effect. If , the Turing machine can write the input registers as a list in time , the list adds to its length, and the bound of part 1 is 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 be a RAM program with the instructions LOAD, STORE, ADD, SUB, READ, JUMP, JZERO, JEOF, HALT, as on the random-access machine page. Let decide in logarithmic-cost time : on every input word of length , given as integers in , halts with logarithmic cost at most , and with if and if . Then there is a deterministic 5-tape Turing machine that decides in steps. The constant depends on only.
Setup. Fix an input of length and the run of on . Let the run execute instructions, numbered , and let be the logarithmic cost of instruction . Then . Every cost is at least 1, so . We write for the bit length, with . Let be the largest length of a number that occurs in the text of (a constant, a register number or a jump target). Every constant below depends on only.
Encoding. Numbers are written in binary on the tapes with the least significant bit first and with no leading zeros, and is the string 0. A number on a tape is followed by a blank or by a separator.
Tapes of .
- Tape 1 holds the input . 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 and 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 with that has no record has the value . We write for the length of the list. - Tape 3 holds the accumulator .
- 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 has finitely many instructions, the control has finitely many states.
Invariant . Before instruction of the run, the control is at the instruction that executes as instruction . Tape 3 holds the value of . Tape 2 holds exactly one record #a:v for each register with that a STORE has written in the instructions , with its current value . Tapes 4 and 5 are blank. Tape 1 has read exactly as many symbols as the instructions READ before . All heads except head 1 are on cell 0.
Step 1: the length of the memory tape. Let be the length of the list before instruction . We show . A record #a:v has length . It is in the list because of the last STORE that wrote the register , and at that time received the value from . By the cost table, the cost of STORE i is , and the cost of STORE *i is . In both cases , and also . So the record has length at most . Different records belong to different instructions STORE, since one STORE writes one register. So the lengths add up to at most .
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 . The machine moves head 2 over the list. For each record it compares 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 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 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 write0on tape 5 if not found. The result on tape 5 is the value of . LOOKUP takes steps where is the value, and if found. So it takes steps. If , the address names the accumulator (a program can reach only through an indirect operand whose register holds ): LOOKUP copies tape 3 to tape 5 and does not use the list. - UPDATE. Tape 4 holds and tape 3 holds . If , do nothing, since
STOREthen copies to . Else FIND. If found, copy the part of the list after the old value to tape 5 and blank it on tape 2, write after the:, copy the part back from tape 5 after , and blank tape 5. This takes steps. If not found, write#, ,:and at the end of the list, which takes steps. Then rewind. The total is steps. - ARITH. Tapes 3 and 5 hold and . 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 steps.
Step 3: one instruction. Assume . Let be the length of before instruction . We show that the machine executes instruction in at most steps if is a LOAD or a READ, in at most steps if is a STORE, ADD, SUB or JZERO, and in steps if is a JUMP, JEOF or HALT, and that holds after it. Only LOAD and READ pay outside . In the other instructions that read , by the cost table. Operands: for the operand the machine writes on tape 5 in steps. For the operand it writes on tape 4, which takes steps, and it does LOOKUP, which leaves on tape 5. For the operand it writes on tape 4 and does LOOKUP, so tape 5 holds . It then copies this number to tape 4, which takes steps, blanks tape 5 in steps, and does LOOKUP again, so tape 5 holds . Each of these takes steps, and each of the lengths that occur is part of the cost 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 more steps, and .STORE i,STORE *i: put the address on tape 4 (for*i, by LOOKUP of , a copy to tape 4 and the blanking of tape 5, which cost steps), so that tape 5 is blank, and do UPDATE with on tape 3. The register gets . The records of the other registers do not change. The cost is , and .ADD X,SUB X: get the operand on tape 5, and do ARITH. The cost is , and . The result is or , 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 writes0and does not move head 1. This takes steps.JUMP k: set the control to .JZERO k: read tape 3 and jump if it is0.JEOF k: jump if head 1 reads a blank.HALT: accept if tape 3 holds1, else reject. Each takes 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 holds. For HALT the answer is the value of , so accepts if and only if halts with .
Step 4: the length of the accumulator. In the list of Step 3, only LOAD and READ pay for the old length outside . In ADD and SUB the length is a part of . Let be the last instruction before that set (LOAD, ADD, SUB or READ). Then : for LOAD X the new value is the value of , and its length is a part of ; for ADD the new length is at most , where is the old value; for SUB it is at most the length of the old , which is a part of ; for READ it is . If no instruction has set , then . A setter is the last setter before at most one later instruction , namely the next setter. So , where the sum is over the instructions LOAD and READ.
Step 5: the total. The first step of writes 0 on tape 3, and this makes true. By Step 3, Step 1 and Step 4, the total time of the simulation is at most
Here the sum of the is over the instructions LOAD and READ, and the other terms are bounded by Step 1 (so ) and by . Since and , this is . By the invariant at the halting instruction, decides .
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 , and the proof fails.
Proof: A RAM simulates a multitape Turing machine in logarithmic-cost time O(T log T)full
Statement. Let be a deterministic -tape Turing machine, with the tape alphabet of symbols and the input alphabet , that decides and halts within steps on every input of length , where . Then there is a RAM program (with the instructions of the random-access machine page, no multiplication) that decides in unit-cost time and in logarithmic-cost time . The constants depend on only.
Setup. Write with , and , and use the index as the code of . A blank cell has code , which is the value of a register that was never written. Let . The registers are used like this.
- for holds the address of the register that stores the cell scanned by head .
- is a pointer for the copy of the input.
- The cell of tape is stored in the register with the address . These addresses are at least and are all different, for different pairs , since is the remainder of the offset modulo and is the quotient.
- 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 for each non-halting state of and a block for each of accept and reject. The block is LOAD =1, HALT. The block is LOAD =0, HALT. The block is a decision tree of depth that finds the scanned symbols. At a node for tape , with the symbols of the tapes already fixed by the path, it runs the tests for in this order. The test for is
LOAD *i
SUB =a
JZERO (the node for tape i+1 in which the symbol of tape i is a)
and if all tests fail, the code goes on to the node in which the symbol of tape is . The tree has leaves, one for each tuple of codes, and each leaf is a separate piece of code. In the leaf , let . The leaf has these instructions, for in turn.
LOAD =b_i, thenSTORE *i: write the symbol.- If :
LOAD i,ADD =k,STORE i. - If :
LOAD i,SUB =B+i-1,JZERO skip_i,LOAD i,SUB =k,STORE i, and then the labelskip_i. - If : nothing.
The leaf ends with JUMP S(q'). The program has instructions, which is a constant.
Step 1: the test finds the symbol. Let , the code of the scanned symbol, with . The test for computes , and the jump is taken if and only if . The tests run for in this order. If , then the tests for fail, since . The test for jumps. So the first jump is at . If no test jumps, then , so . 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 of tape has the address , and every cell has an address of at least this value. So SUB =B+i-1 gives if and only if , that is, if and only if head is on cell . In that case the move keeps the head in place, as in the machine, since JZERO skips the subtraction. Else SUB =k moves the address of head one cell to the left (the address of cell is that of cell minus ).
Invariant . After steps of (and after Part I for ), the control is at the beginning of , where is the state of after steps. For each the register holds the address of the cell scanned by head . For each and the register with the address holds the code of the symbol in cell of tape .
Proof of . The registers other than those of tape 1 are , which is the code of the blank, and tape is blank at the start. Part I stores the codes of the input symbols at the addresses for , since starts at and grows by at each pass, and JEOF done ends the loop after passes. The cells of tape 1 are blank and the registers are . The heads start on cell , whose address is .
Proof of . Assume and non-halting. By Step 1 the block reaches the leaf of the true tuple of symbols. In the leaf, for each the instruction pair LOAD =b_i, STORE *i writes the symbol into the register of the scanned cell, and then the move updates 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 . This is the configuration of after step . If is accept, halts with , and if reject with . (of the invariant)
Step 3: the cost. Let halt after 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 (the input has length ). So every address and every register value that occurs is at most , 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 , since . The number of instructions executed is as follows.
- Part I executes instructions with the length of the input, since each pass of the loop executes 8 instructions and the end has .
- Each step of executes at most instructions: at most for each tape in the tests, at most for each tape in the leaf (the longest case is the move 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 , using . This is the unit-cost time . Multiplying by the cost per instruction, the logarithmic-cost time is . By the invariant, halts with exactly when accepts.
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
- 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
- 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
- Christos H. Papadimitriou, Computational Complexity, Addison-Wesley, 1994, Chapter 2, the section on random access machines
- 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
- J. C. Shepherdson and H. E. Sturgis, Computability of recursive functions, Journal of the ACM 10(2), 1963, pp. 217 to 255
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag ZBD1SF Edit this page History Report an error Markdown JSON