Simple Algorithm
Algorithms & Data Structures · Optimal · 26 min

Models of Computation

RAM, word RAM and cell probe: what one “step” really costs.

Is multiplying two numbers one step? If it is, a loop of 300 squarings starting from 2 writes down a number with about 2×10902 \times 10^{90} bits, far more than there are atoms in the observable universe, and such a machine can decide every problem in PSPACE in polynomial time. A model of computation decides what one step costs, and with it what "fast" means. This lesson compares the models behind the bounds in this course, including the one in which data-structure lower bounds are proved.

What you’ll learn

  • Define the unit-cost RAM, the word RAM, the real RAM, the comparison model, the pointer machine and the cell-probe model, and say what each one charges for.
  • Explain why unit-cost arithmetic on unbounded integers is unrealistic, and when to count bit operations instead.
  • State the invariance thesis, and explain why polynomial time is the same on every reasonable model while linear time is not.
  • Explain why a cell-probe lower bound holds for every word-RAM data structure, and state the strongest known bounds.

What does one step cost?

Every bound in this course counts "steps", so it silently fixes a model of computation: what a machine can do in one step, and what each step costs. The default is the random-access machine (RAM): a processor with a few registers and an unbounded array of memory cells, where one step reads or writes a cell, does one arithmetic operation or comparison, or jumps. Any cell is reached by its address in one step, which is the "random access".

The question the RAM leaves open is how big a cell is. If cells hold integers of any size and every operation costs 1, the model breaks:

python
x = 2
for k in range(1, 9):
    x = x * x                   # one "step" on a unit-cost RAM
    print(k, x.bit_length())    # k squarings: 2**k + 1 bits

It prints 3, 5, 9, 17, 33, 65, 129 and 257 bits. After kk squarings x=22kx = 2^{2^k}, a number with 2k+12^k + 1 bits, so 300 unit-cost steps produce about 2×10902 \times 10^{90} bits.

Two repairs are standard. The logarithmic-cost RAM charges each operation by the number of bits of its operands (Cook & Reckhow, 1973). The word RAM, next, caps the size of a cell. Either way the squaring loop costs what it should: at least 2k2^k bit operations, not kk steps.

The word RAM, and a zoo of models

The word RAM is the model behind almost every bound in this course, and real hardware approximates it with w=64w = 64. It is strictly richer than comparisons: treating keys as bit strings, it can hash, index arrays by key, and sort nn integers in O(nlog⁡log⁡n)O(n\log\log n) time deterministically (Han, 2004), below the Ω(nlog⁡n)\Omega(n\log n) that comparison sorting needs. Each model below fixes the allowed operations and their cost, and a lower bound proved in one model binds only the algorithms that fit it.

ModelOne stepUsed for
unit-cost RAMany arithmetic on unbounded integersinformal analysis; unsound with multiplication
log-cost RAMone operation, charged by the bits of its operandsbit complexity, number theory
word RAMone operation on words of w≥log⁡2nw \ge \log_2 n bitsthe default for algorithms and data structures
real RAMexact arithmetic and comparison of real numberscomputational geometry
comparison modelone comparison of two input items; everything else is freelower bounds for sorting, searching, selection
pointer machinefollow or change a pointer; no address arithmeticlower bounds for linked structures, such as union–find
cell-probe modelread or write one memory cell; computation is freelower bounds for data structures
multitape Turing machineone move of each tape headcomplexity classes, time versus space
In the comparison model an algorithm is a decision tree, and its worst-case cost is the tree's height. Tarjan's Ω(α(n))\Omega(\alpha(n)) bound for union–find (1979) was first proved for pointer machines.
Check your understanding

Sorting needs Ω(nlog⁡n)\Omega(n\log n) comparisons, yet radix sort sorts nn integers below n2n^2 in O(n)O(n) time on the word RAM. Why is there no contradiction?

  1. Radix sort's O(n)O(n) bound counts only its comparisons
  2. The lower bound holds in the comparison model, and radix sort is not a comparison sort: it uses digits of the keys as array indices
  3. The lower bound is about the average case, and radix sort's bound is about the worst case
  4. The lower bound holds only when nn exceeds the word size
Show the answer

B. The lower bound holds in the comparison model, and radix sort is not a comparison sort: it uses digits of the keys as array indices

The Ω(nlog⁡n)\Omega(n\log n) bound counts comparisons in a decision tree, and an algorithm that computes addresses from the bits of keys is not a decision tree of comparisons. Keys below n2n^2 have two digits in base nn, so two passes of counting sort take O(n)O(n) time.

Bits versus operations

Algorithms on numbers have two costs: the number of arithmetic operations, and the number of bit operations, which is what a log-cost RAM or a Turing machine pays. For inputs of ℓ\ell bits:

  • Euclid's algorithm makes O(ℓ)O(\ell) divisions, and in total only O(ℓ2)O(\ell^2) bit operations, as the derivation below shows.
  • Multiplying two ℓ\ell-bit numbers costs Θ(ℓ2)\Theta(\ell^2) bit operations by the schoolbook method, O(ℓlog⁡23)≈O(ℓ1.585)O(\ell^{\log_2 3}) \approx O(\ell^{1.585}) by Karatsuba's (1962), and O(ℓlog⁡ℓ)O(\ell\log\ell) by Harvey and van der Hoeven's (2021), which is conjectured to be optimal.
  • Linear programming is solvable in time polynomial in the number of bits of the input (Khachiyan, 1979; Karmarkar, 1984). Whether the number of arithmetic operations can be bounded by a polynomial in the numbers of variables and constraints alone, a strongly polynomial algorithm, is open: it is the ninth of Smale's problems for the 21st century.
DerivationEuclid costs O(ℓ²) bit operations
  1. costi=O((1+log⁡2qi) ℓ)\text{cost}_i = O\big((1 + \log_2 q_i)\,\ell\big)

    Write the run as ri−1=qiri+ri+1r_{i-1} = q_i r_i + r_{i+1} for i=1,…,ki = 1, \dots, k, with r0=a≥r1=b>0r_0 = a \ge r_1 = b > 0 and rk+1=0r_{k+1} = 0. Schoolbook division of ri−1r_{i-1} by rir_i produces qiq_i one bit at a time, each bit costing O(log⁡2ri)=O(ℓ)O(\log_2 r_i) = O(\ell) bit operations:

  2. ∏i=1kqi  ≤  ∏i=1kri−1ri  =  r0rk  ≤  a<2ℓ\prod_{i=1}^{k} q_i \;\le\; \prod_{i=1}^{k} \frac{r_{i-1}}{r_i} \;=\; \frac{r_0}{r_k} \;\le\; a < 2^{\ell}

    Since ri−1≥qirir_{i-1} \ge q_i r_i, the product of the quotients telescopes:

  3. ∑i=1kcosti=O(ℓ (k+log⁡2∏iqi))=O(ℓ (ℓ+ℓ))=O(ℓ2)\sum_{i=1}^{k} \text{cost}_i = O\Big(\ell\,\big(k + \log_2 \textstyle\prod_i q_i\big)\Big) = O\big(\ell\,(\ell + \ell)\big) = O(\ell^2)

    And k=O(ℓ)k = O(\ell) by Lamé's theorem (see Euclid's Algorithm in Depth). Adding up, the long quotients and the many steps share one budget:

Turing machines and the invariance thesis

Why does "polynomial time" mean the same thing in every textbook, when the machines differ so much? Because reasonable models simulate one another with at most polynomial overhead. Multitape Turing machines and log-cost RAMs simulate each other within a polynomial factor in time (Cook & Reckhow, 1973), and a word RAM is a log-cost RAM whose operands happen to be short. Van Emde Boas named this the invariance thesis: reasonable machines simulate each other within a polynomial factor in time and a constant factor in space. Together with Cobham's and Edmonds's proposals (both 1965) to call polynomial-time algorithms efficient, it makes P a property of problems, not of machines.

The robustness stops at polynomial factors, though:

Proof idea. Take inputs x 0n/3 xRx\,0^{n/3}\,x^R with ∣x∣=n/3|x| = n/3. At a boundary between two cells of the middle block, record the sequence of states in which the head crosses it. If two different xx produced the same crossing sequence at the same boundary, cutting both tapes there and gluing the left part of one run to the right part of the other would give an accepting run on a non-palindrome. So at each middle boundary the 2n/32^{n/3} choices of xx need 2n/32^{n/3} distinct crossing sequences, and since a machine with qq states has fewer than (q+1)L+1(q+1)^{L+1} sequences of length at most LL, their average length is Ω(n/log⁡q)\Omega(n/\log q). Averaging over the n/3n/3 middle boundaries, some input makes Ω(n/log⁡q)\Omega(n/\log q) crossings per boundary, Ω(n2)\Omega(n^2) steps in all for a fixed machine, because every crossing is a step.

Two frontiers start here. The extended Church–Turing thesis says that every physically realizable computer can be simulated by a probabilistic Turing machine with polynomial overhead; quantum computers are the serious challenge, since Shor's algorithm (1994) factors integers in polynomial time on them, and no classical polynomial-time factoring algorithm is known.

And time versus space: a multitape machine running in time tt can be simulated in space O(t/log⁡t)O(t/\log t) (Hopcroft, Paul & Valiant, 1977). Williams (STOC 2025), building on Cook and Mertz's algorithm for tree evaluation (STOC 2024), improved this to O(tlog⁡t)O(\sqrt{t\log t}) space, the first big step in almost fifty years.

The cell-probe model

The model is generous to the data structure, which makes its lower bounds the strongest kind: they hold however clever the computation, and they count only the unavoidable movement of information. The landmark results:

  • Fredman and Saks (1989) proved Ω(log⁡n/log⁡log⁡n)\Omega(\log n/\log\log n) per operation for dynamic prefix sums (change a[i]a[i]; ask for a[0]+⋯+a[i]a[0] + \dots + a[i]) with cells of O(log⁡n)O(\log n) bits, and an Ω(α(n))\Omega(\alpha(n)) amortized bound for union–find, matching Tarjan's upper bound.
  • Pătraşcu and Demaine (2006) proved Ω(log⁡n)\Omega(\log n) for dynamic prefix sums and for dynamic graph connectivity: the larger of the update time and the query time must be Ω(log⁡n)\Omega(\log n). For prefix sums that is tight, since a Fenwick tree does both in O(log⁡n)O(\log n).
  • Larsen (2012) proved query time about (log⁡n/log⁡log⁡n)2(\log n/\log\log n)^2 for dynamic two-dimensional range counting with fast updates, and Larsen, Weinstein and Yu (2018) gave the first bounds above log⁡n\log n for dynamic problems with yes-or-no answers, of order log⁡1.5n\log^{1.5} n.
Check your understanding

A Fenwick tree supports point updates and prefix-sum queries in O(log⁡n)O(\log n) time each. What does the Ω(log⁡n)\Omega(\log n) cell-probe bound of Pătraşcu and Demaine say about it?

  1. Nothing: the Fenwick tree runs on a word RAM, not in the cell-probe model
  2. No data structure can make both updates and queries o(log⁡n)o(\log n), so the Fenwick tree is optimal up to a constant factor
  3. Every data structure needs Ω(log⁡n)\Omega(\log n) for updates and also Ω(log⁡n)\Omega(\log n) for queries
  4. It is optimal only among comparison-based structures
Show the answer

B. No data structure can make both updates and queries o(log⁡n)o(\log n), so the Fenwick tree is optimal up to a constant factor

The bound is on the larger of the update and query times, and every word-RAM structure is also a cell-probe structure. One side can be made fast at the other's expense: a plain array updates in O(1)O(1) but answers a query in O(n)O(n). What no structure can do is make both sides o(log⁡n)o(\log n).

Key takeaways

  • A bound is only as meaningful as its model: a unit-cost RAM that multiplies unbounded integers computes 22k2^{2^k} in kk steps and decides PSPACE in polynomial time, so realistic models charge for bits or cap the word size.
  • The word RAM, with words of w≥log⁡2nw \ge \log_2 n bits and unit-cost word operations, is the default for algorithms and data structures; comparison, pointer-machine and real-RAM lower bounds bind only algorithms of their form.
  • Bit complexity matters for numbers: Euclid's algorithm makes O(ℓ)O(\ell) divisions and O(ℓ2)O(\ell^2) bit operations, and multiplication costs O(ℓlog⁡ℓ)O(\ell\log\ell).
  • Reasonable models simulate each other with polynomial overhead, so P is robust, but linear time is not: palindromes need Θ(n2)\Theta(n^2) steps on one tape.
  • Cell-probe lower bounds count only memory accesses, so they bind every word-RAM data structure; the strongest known dynamic bounds are polylogarithmic, such as Ω(log⁡n)\Omega(\log n) for prefix sums.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

On which machine is "compute 22n2^{2^n} in O(n)O(n) steps" an accurate statement?

  1. A word RAM with 64-bit words
  2. A logarithmic-cost RAM
  3. A unit-cost RAM that multiplies unbounded integers
  4. A multitape Turing machine
Show the answer

C. A unit-cost RAM that multiplies unbounded integers

Only a model that charges 1 for multiplying numbers of any size can square nn times in O(n)O(n) steps. Every realistic model pays for the 2n+12^n + 1 bits of the result, which takes at least 2n2^n steps just to write down.

Question 2

Why does the word RAM assume that the word size is w≥log⁡2nw \ge \log_2 n?

  1. So that multiplication costs one step
  2. So that the address of any of the nn input items fits in a single word
  3. So that the model matches 64-bit hardware exactly
  4. So that every input number fits in a constant number of bits
Show the answer

B. So that the address of any of the nn input items fits in a single word

An algorithm must be able to index its own input: an array of nn items needs addresses of log⁡2n\log_2 n bits. With smaller words even reading a[i]a[i] would cost more than one step, and the model would stop resembling a computer that holds its whole input in memory.

Question 3

Deciding whether a string is a palindrome takes O(n)O(n) steps on a RAM but Θ(n2)\Theta(n^2) on a one-tape Turing machine. What does this show?

  1. Turing machines cannot recognize palindromes efficiently, so P depends on the model
  2. RAMs are exponentially faster than Turing machines
  3. Linear time depends on the model, while polynomial time does not
  4. The one-tape bound must be wrong, since the models are equivalent
Show the answer

C. Linear time depends on the model, while polynomial time does not

Reasonable models simulate each other with polynomial overhead, here a square, so the class of polynomial-time problems is the same for all of them. Finer distinctions such as O(n)O(n) versus O(n2)O(n^2) are not preserved, which is why algorithm courses fix the word RAM.

Question 4

Euclid's algorithm on two ℓ\ell-bit numbers makes O(ℓ)O(\ell) divisions. What is its total bit complexity with schoolbook division?

  1. O(ℓ)O(\ell)
  2. O(ℓ2)O(\ell^2)
  3. O(ℓ3)O(\ell^3)
  4. O(2ℓ)O(2^{\ell})
Show the answer

B. O(ℓ2)O(\ell^2)

A division with a qq-bit quotient costs O((1+q) ℓ)O((1 + q)\,\ell), and the quotient lengths add up to at most ℓ\ell because the quotients' product is at most aa. So the total is O(ℓ (k+ℓ))=O(ℓ2)O(\ell\,(k + \ell)) = O(\ell^2), not O(ℓ)O(\ell) divisions times O(ℓ2)O(\ell^2) each.

Further reading

  • S. A. Cook & R. A. Reckhow, "Time bounded random access machines," Journal of Computer and System Sciences 7(4) (1973).
  • A. C.-C. Yao, "Should tables be sorted?," Journal of the ACM 28(3) (1981).
  • M. Pătraşcu & E. D. Demaine, "Logarithmic lower bounds in the cell-probe model," SIAM Journal on Computing 35(4) (2006).
  • P. van Emde Boas, "Machine models and simulations," in J. van Leeuwen (ed.), Handbook of Theoretical Computer Science, Vol. A (Elsevier, 1990).
  • R. Williams, "Simulating Time With Square-Root Space," Proceedings of STOC (2025).