Modelintro
Random access machine
Also called RAM.
Idealized sequential machine with a random-access memory of registers, the standard model for algorithm analysis.
The random access machine (RAM) is a model of a sequential computer with an unbounded sequence of numbered registers, each of which holds one integer. A program is a finite list of instructions, and an instruction can read or write the register whose number is held in another register. The time of a run is the cost of the instructions it executes. The model has two cost rules: under the unit-cost rule each instruction costs 1, and under the logarithmic-cost rule an instruction costs the number of bits of the numbers it touches. Most algorithm texts use the RAM with a unit-cost rule for numbers of bits, and this page names the rule each time.
The machine
The memory is a sequence of registers . Each holds a non-negative integer, and all registers hold at the start. The register is the accumulator. The register numbers in the text of a program are at least . An indirect operand whose register holds names the accumulator. A program is a finite numbered list of instructions, and the machine starts at instruction 1. After an instruction it goes to the next one, unless the instruction is a jump or a halt. The input is a finite sequence of non-negative integers , and the machine reads them in order. The machine used here is the accumulator machine of Aho, Hopcroft and Ullman, with the sequential input and a few changes. An operand is one of three forms: a constant , a register (the value ), or an indirect register (the value ). The instructions are these.
LOAD Xsets to the value of .STORE isets to .STORE *isets to .ADD Xsets to , where is the value of .SUB Xsets to .READsets to the next input integer, or to if no input integer is left.JUMP kgoes to instruction .JZERO kgoes to if .JEOF kgoes to if all input integers are read.HALTstops the run. The output of a decision is the value of at the stop: rejects and any other value accepts. The programs on this page halt with .
The indirect operand makes the model random access: a program can compute an address and then read or write that register in one instruction, so every register is as near as every other. Texts differ in the details. Some use three-address instructions such as , some add multiplication and division, and some put the input in the first registers. Multiplication changes the cost analysis (see below). An input in registers that the program can index changes what a Turing machine can simulate (see RAM and Turing machine equivalence).
Shepherdson and Sturgis (1963) defined the unlimited register machine, a register machine with no indirect operand. Elgot and Robinson (1964) studied the RASP, a RAM whose program is stored in the registers, so that a run can change its own instructions. Cook and Reckhow (1973) gave the standard time-bounded RAM with both cost rules. Aho, Hopcroft and Ullman (1974, Chapter 1) show that the RAM and the RASP simulate each other with a constant-factor loss of time.
What it counts
Under the unit-cost rule, each executed instruction costs 1, so the time is the number of instructions executed. The space is the number of distinct registers that the run writes.
Under the logarithmic-cost rule, the length of a non-negative integer is for and . An instruction costs the sum of the lengths of the numbers it reads, writes or uses as an address. With for the value of , the costs are:
| Instruction | Cost |
|---|---|
LOAD =c |
|
LOAD i |
|
LOAD *i |
|
STORE i |
|
STORE *i |
|
ADD X, SUB X |
plus the cost of LOAD X |
READ |
for the integer read |
JZERO |
|
JUMP, JEOF, HALT |
The time is the sum of the costs of the executed instructions. The space is the largest total of over the registers at one moment of the run. The size of an input is the sum of over the input integers, which is the length of the input as a string. A time bound is the worst case over the inputs of size .
Example. The program below adds the input integers and halts with the sum in . The register holds the sum.
1 LOAD =0
2 STORE 1
3 JEOF 8
4 READ
5 ADD 1
6 STORE 1
7 JUMP 3
8 LOAD 1
9 HALT
Instructions 1 and 2 run once. Instructions 3 to 7 run once for each input integer. Instructions 3, 8 and 9 run once more at the end. So the unit-cost time is : for the input it is and the output is .
For the logarithmic cost, write for the sum of the first integers and for the -th one. The -th round costs . The set-up costs and the end costs . So the total is
For this is . Suppose that each has exactly bits. Then , so . The sum of for is , so the logarithmic-cost time is . The input size is , so the time is , which is more than linear in when , for example when every . The unit-cost time hides the cost of the large numbers.
Relation to other models
The unit-cost rule does not match real computers when numbers can grow without bound. If the instruction set has multiplication (MULT X sets to ), the three instructions LOAD 1, MULT 1, STORE 1 square . Start with . After rounds, , a number of bits, and the unit-cost time is . Time polynomial in produces a number with exponentially many bits. With addition and subtraction only, a value at most doubles in each instruction, so after instructions every value has at most bits, where bounds the length of the program constants and of the input integers. Then each instruction costs in the logarithmic-cost rule, and the logarithmic-cost time is for a unit-cost time .
The RAM with logarithmic cost and the Turing machine simulate each other with a polynomial loss of time. RAM and Turing machine equivalence states and proves the bounds: steps of a multitape machine for a RAM of logarithmic-cost time , and logarithmic-cost time of a RAM for a multitape machine of time . So the class of polynomial-time languages is the same on both models. See unit-cost model and logarithmic-cost model for the two rules.
The word RAM limits each register to bits, with , and it counts each instruction as 1. This is the model of CLRS Section 2.2: a word has bits for a constant . The limit keeps the unit-cost rule realistic. The real RAM lets each register hold a real number with exact arithmetic at unit cost. Real computers do not do this, so its bounds do not carry over to them directly.
A RAM can use a key as an address, so on integer keys it can beat the lower bounds of the comparison model. The cell-probe model counts only the reads and writes of memory cells. A cell-probe lower bound for cells of bits is also a lower bound for the word RAM with -bit words, because each instruction reads or writes cells.
Known results in the model
- Counting sort. Sorting integers in the range to takes time on the unit-cost RAM with extra space (CLRS Section 8.2). This is when , and linear when . See counting sort and the sorting problem.
- Polynomial equivalence with the Turing machine. The two models decide the same languages in polynomial time when the RAM has logarithmic cost, or has unit cost without multiplication. See RAM and Turing machine equivalence.
- Exponential gap with multiplication. With unit-cost multiplication, the RAM builds a number of bits in steps, as shown above. A machine that writes this number out needs at least steps, so a step-by-step simulation that stores the register contents is not polynomial. This does not by itself separate the languages decided. Hartmanis and Simon (1974) showed that polynomial time on a unit-cost RAM with multiplication and bitwise Boolean instructions equals PSPACE.
References
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Section 2.2, the RAM model of computation
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Section 8.2, counting sort
- 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 random access machines and on their cost
- 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
- 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
Where this leads
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 | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 8HFM94 Edit this page History Report an error Markdown JSON