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
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.
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.
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.
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.
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.
| Model | Steps for length n | Longest input within the budget | With 1,000 times the budget |
|---|---|---|---|
| One tape | (n + 1)(n + 2)/2 | 1,412 | 44,719 (× 32) |
| Two tapes | 3n + 3 | 333,332 | 333,333,332 (× 1000) |
| Random access | n/2, rounded down | 2,000,001 | 2,000,000,001 (× 1000) |
| For contrast: trying all strings | 2n | 19 | 29 (+10) |
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.
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.
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.
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.
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.
Spot a mistake or have an idea?