What Is a Computation?

Before asking how hard a problem is, pin down what solving it means. Program a Turing machine, count its steps, and meet the argument that gives more time strictly more power

What Is a Computation?

To ask whether a problem is hard, you first have to say what solving it means and what counts as one step. In 1936 Alan Turing gave an answer simple enough to run by hand: a tape, a head that reads and writes one symbol at a time, and a finite table of rules. It turned out to be so robust that every computer built since is, for the purposes of this module, the same machine.

The Machine Workbench

Press Play and watch the head zig-zag while the picture underneath records the whole run, one row per step. Then type another input, switch to the rule table and change a rule, and see what the chart at the bottom says about the slowest input of each length.

0→□ R1→□ R□ S0,1 R□ L0,1 R□ L0→□ L□ S1→□ L□ S0,1 L□ Rstarthave0have1check0check1backacceptreject
0
steps so far
1
tape cells visited
start
state
15
steps for the whole run
In state start reading 0: write □, move right, go to state have0. That is one step, and it is all the machine ever does.
How long can it take? Every input of every length up to 12
0%
of the inputs run so far
…
worst case for length 4
…
worst case for length 12
…
fitted exponent (slope on log–log axes, n ≥ 4)

Top: the tape, with the head’s cell framed in indigo and blank cells left empty. Bottom: the same run with time going down the page, one row per step, the head’s cell marked in each. The chart runs the machine on all 8,191 inputs of lengths 0 to 12 and plots the slowest of each length in sky; the ring is the input in the box above. The measured points settle nothing beyond length 12 on their own. For the palindrome machine the formula can be proved for every length by following the head’s zig-zag. The run shown stops after 400 steps and the measurement after 2,000 per input.

Go deeper:Turing’s Machine and What “Time” Means

Alan Turing introduced the machine in his 1936 paper “On Computable Numbers, with an Application to the Entscheidungsproblem”. It has a finite set of states, a tape of cells that is unbounded in both directions, and a table. At each step the machine looks at its state and the one symbol under its head; the table says which symbol to write there, whether to move left or right, and which state to enter. Two special states, accept and reject, end the run. Nothing else is allowed, and nothing else turns out to be needed.

The Church–Turing thesis says that anything that can be computed at all, by any mechanical procedure, can be computed by such a machine. It is a thesis and not a theorem, because “mechanical procedure” is an informal idea. The evidence for it is that every precise definition of computation proposed since the 1930s has turned out to compute exactly the same functions.

Once a step is defined, time can be counted. The time complexity of a machine is the function that sends n to the largest number of steps the machine takes on any input of length n: a worst case, as a function of input length. Juris Hartmanis and Richard Stearns introduced this way of measuring in 1965, and it is the measure in which P versus NP is stated. The palindrome machine here takes exactly (n + 1)(n + 2)/2 steps in the worst case, about n2/2, because it crosses the remaining input once for each pair of symbols it matches.

The space–time picture is worth remembering. A whole computation is a grid in which each row follows from the row above by a rule that only looks at a few neighbouring cells. See also: The Cook–Levin Theorem, where that grid is turned into a logical formula.

Models and Exponents

Start the race and watch three kinds of machine check the same palindrome. Then drag the step budget and compare how far each one gets, and try feeding one program’s output into another at the bottom.

66
one tape: steps on this input
33
two tapes: steps on this input
5
random access: comparisons on this input
The same three machines on longer and longer palindromes
On these axes a running time of nk is a straight line of slope k. One tape climbs with slope …; two tapes and random access both climb with slope about 1 (…). Changing the machine changed the exponent from 2 to 1. It did not turn a polynomial into anything else.
ModelSteps for length nLongest input within the budgetWith 1,000 times the budget
One tape(n + 1)(n + 2)/21,41244,719 (× 32)
Two tapes3n + 3333,332333,333,332 (× 1000)
Random accessn/2, rounded down2,000,0012,000,000,001 (× 1000)
For contrast: trying all strings2n1929 (+10)
Feed one program’s output into another
First program takes
second takes
Polynomial into polynomial. The first program runs for 400 steps, so it writes at most 400 symbols. The second then runs for at most 64,000,000 steps: that is n6, still a polynomial.
Exponential into exponential. The first program runs for 220 = 1,048,576 steps and may write that many symbols. The second then runs for up to 2 to the power 1,048,576 steps, a number with 315,653 digits. That is no longer 2n, nor 2 to any fixed power of n.

The race runs real machines: the one-tape table from the workbench, a two-tape machine that copies, rewinds and compares, and a loop that reads positions i and n − 1 − i directly. The chart runs each on a palindrome of every length shown, up to 512 (a palindrome is the slowest input for all three; the tests check this exhaustively for lengths up to 10), and fits the slope from length 64 upwards. Rings mark the race above. Random access is counted in comparisons, each of which is a few instructions on a real computer, so its row is for scale. The table’s formulas are the measured worst cases; the last row is not a way to recognise palindromes, only a 2ⁿ curve to compare with.

Go deeper:Why Polynomial Time, and the Class P

The exponent depends on the machine. A second tape brings palindromes down from about n2/2 steps to about 3n, and the gap is real: Frederick Hennie proved in 1965 that every one-tape machine for palindromes needs on the order of n2 steps. But the damage is limited. A two-tape machine that runs in time T can be simulated on one tape in about T2 steps, and the other reasonable models of computation simulate one another with polynomial overhead as well. So whether a problem can be solved in polynomial time does not depend on which of them you choose.

P is the class of yes-or-no problems that some machine solves in a number of steps bounded by a polynomial in the input length. Alan Cobham (1964) and Jack Edmonds (1965) proposed polynomial time as the mathematical stand-in for “feasible”. Edmonds, in “Paths, Trees, and Flowers”, called polynomial-time algorithms “good”. Besides being independent of the model, the class is closed under composition: a polynomial-time program that calls another as a subroutine, or runs on its output, is still polynomial, because a polynomial of a polynomial is a polynomial.

It is a convention, and it has known weaknesses. An algorithm that takes n100 steps is polynomial and useless; one that takes 1.0001n steps is exponential and would be fine for any input you will ever meet. In practice the convention has matched experience remarkably well: problems found to be in P have usually turned out to have algorithms with small exponents. That is an observation about the problems people have studied, not a theorem.

The Diagonal

Drag the step budget and watch the bottom row change: it always answers the opposite of the outlined diagonal. Then try to defeat the argument by adding that row’s own machine to the list.

16 of 16
machines that D disagrees with on their own input
4
machines still running on their own input after 12 steps
5
inputs where the bottom row changed (budget 4 → 12)
39 > 12
steps the clocked simulator for D needs at most, against the budget
Read down the outlined diagonal and then along the bottom row: wherever machine i accepts input i within 12 steps, D rejects it, and everywhere else D accepts. So D is not the behaviour of any machine on the list within this budget. Yet D is easy to compute: run machine i against a clock for 12 steps and answer the opposite, which takes up to 39steps here. Tap any cell for details.

Rows are machines, columns are the binary strings ε, 0, 1, 00, 01, … drawn as stacks of dark (0) and light (1) squares. Emerald: accepted within the budget. Grey: rejected. Hatched: still running when the budget ran out, which the bottom row counts as “did not accept”. Indigo outlines mark machine i on input i. The list is a toy: 24 small machines, some written by hand and some with tables filled in at random, where the real argument lists every machine there is. The clocked simulator is charged 3 steps for each step it simulates, a stand-in for the real overhead, which grows with the machine being simulated and includes a logarithmic factor for the clock. An added machine’s hatched cells carry a dot showing the answer it gives once it is allowed to finish.

Go deeper:The Time Hierarchy Theorem

More time buys strictly more power. This is one of the few separations in complexity theory that has been proved, and the proof is Cantor’s diagonal argument run on machines. List every machine. Define D to accept the i-th input exactly when the i-th machine fails to accept it within the budget. Then D differs from every machine on the list somewhere, so no machine decides D within the budget. But D can be decided by a machine that simulates another machine while counting its steps on a clock, and that costs only a little more than the budget itself.

The time hierarchy theorem makes “a little more” precise. Hartmanis and Stearns proved it in 1965, and Hennie and Stearns sharpened it in 1966: if g grows a little faster than f · log f, then there are problems that can be decided in time g and cannot be decided in time f. One consequence is that exponential time strictly contains polynomial time: P ≠ EXP is a theorem. The demo is a toy version. Its list is finite, and its simulator is charged a flat three steps per simulated step, where the theorem’s logarithmic factor comes from the real cost of simulating any machine on a fixed number of tapes.

Take away the clock and the same argument is Turing’s proof that the halting problem is undecidable: a machine that could predict whether any machine halts could be used to build one that does the opposite of itself.

Diagonalisation separates a class from a bigger budget of the same resource. It has never separated P from NP, where the difference is not the size of the budget but whether the machine may guess. See also: The Barriers, which explains why this kind of argument cannot settle P versus NP unaided.

Key Takeaways

  • A Turing machine is a tape, a head and a table of rules, and one application of a rule is one step.
  • A machine’s time complexity is its worst-case number of steps as a function of the input’s length.
  • Changing the model changes the exponent but not whether the time is polynomial, which is why P is defined by polynomial time.
  • More time strictly increases what can be computed (the time hierarchy theorem), so P ≠ EXP is proved; P versus NP is not.

Spot a mistake or have an idea?

Search