Turing machines
The Turing machine as a finite control with an infinite tape: the formal definition, configurations and moves, the three possible outcomes (accept, reject, loop), recognising versus deciding, worked machines for a^n b^n and a^n b^n c^n (with move counts that grow with the square of n), a machine that computes a function (binary increment), a recogniser that never halts on some inputs, and the conventions that differ between textbooks.
Why it exists
Finite automata have a fixed amount of memory and pushdown automata a stack that can be read only at the top. A Turing machine (TM) has a finite control and an unbounded tape that it can read, write and move along in both directions. It is the standard model of "what can be computed": everything a computer program can do, a Turing machine can do, and the languages it recognises are exactly the recursively enumerable languages (top of the Chomsky hierarchy). GATE asks: trace a given machine, say which language it accepts, count moves or tape cells, and decide whether it halts.
Prerequisites
You need finite control, configurations and acceptance from the automata pages; the Turing machine generalises the PDA by replacing the stack with a tape (PDA).
Precise definitions
A (deterministic) Turing machine is :
- finite set of states; the start state; the accepting state; the rejecting state ();
- the input alphabet (no blank); the tape alphabet; a blank symbol (written
_in the animations); - .
The tape is infinite in both directions and holds blanks except for the input, which starts at one cell with the head on its first symbol. One move: in state reading , if the machine writes over , moves the head one cell left () or right () and enters . A missing transition means the machine halts and rejects (the convention of this site; many books add that explicit transition).
A configuration records the state, the tape contents and the head position, usually written : the tape is (with blanks outside), the head is on the first symbol of and the state is . A configuration yields the next one by one move.
On input exactly one of three things happens:
- the machine accepts (reaches and halts),
- it rejects (reaches or has no applicable move and halts),
- it loops (never halts).
The language recognised by is . decides if it recognises and halts on every input. The difference between recognising and deciding is the difference between recursively enumerable and recursive languages (decidable and recognisable languages).
A TM can also compute a function: the machine halts and the answer is what is left on the tape.
How it works
Example 1: a decider for
Idea: mark one with , move right to the first unmarked and mark it , return to the left end, repeat. When no is left, check that only 's remain.
States and moves ( accepts):
- : on : write , move right, go to ; on : go to (all 's used up); on blank: accept (empty string).
- (looking for a ): on or : skip right; on : write , move left, go to .
- (going back): on or : skip left; on : move right, go to .
- (checking): on : skip right; on blank: accept.
Any other pair has no transition: the machine rejects. Run on (the head is the arrow above the tape, the shaded cell was just written, the outlined transition was used):
A Turing machine for a^n b^n on aabb: start
Simulated, not typedThe tape is infinite in both directions (blank cells shown as _); only the cells the machine has visited, plus a margin, are drawn. The cell just written is shaded.
Step 0
Start state q0; the input aabb is written on the tape and the head is on its first cell (cells to the left and right are blank).
On there is an left over: the head reaches the blank at the right end while still looking for a and the machine rejects:
The same machine on aab: start
Simulated, not typedThe tape is infinite in both directions (blank cells shown as _); only the cells the machine has visited, plus a margin, are drawn. The cell just written is shaded.
Step 0
Start state q0; the input aab is written on the tape and the head is on its first cell (cells to the left and right are blank).
Correctness: each round marks exactly one and one , so the string is accepted iff the number of 's equals the number of 's and all 's come before all 's. (Checked on all strings over up to length .) The machine halts on every input: it is a decider.
Example 2: a decider for
The same idea with three symbols: in each round mark one (), one () and one (), return to the left end by passing over the marks, and repeat; when no is left check that only 's and 's remain. A Turing machine can do what no PDA can: it compares three blocks because it can go back and re-read.
A Turing machine for a^n b^n c^n on aabbcc: start
Simulated, not typedThe tape is infinite in both directions (blank cells shown as _); only the cells the machine has visited, plus a margin, are drawn. The cell just written is shaded.
Step 0
Start state q0; the input aabbcc is written on the tape and the head is on its first cell (cells to the left and right are blank).
Example 3: a machine that computes a function
Binary increment: go right to the end of the number, then turn trailing 's into 's going left until a (or the blank before the number) becomes . On it halts with on the tape:
Binary increment of 1011: start
Simulated, not typedThe tape is infinite in both directions (blank cells shown as _); only the cells the machine has visited, plus a margin, are drawn. The cell just written is shaded.
Step 0
Start state q0; the input 1011 is written on the tape and the head is on its first cell (cells to the left and right are blank).
For all binary strings up to length the result is the binary successor of the number (the same number of digits, or one more after ).
Example 4: a machine that loops
The recogniser that moves right over 's, accepts when it sees , and treats a blank like an (keeps moving right) accepts exactly the strings that contain a . On a string with no it never halts: the head runs off to the right over blank cells, and each step looks like the previous one except for the head position. After moves the animation stops without a verdict:
A recogniser for strings containing b, on aa: start
Simulated, not typedThe tape is infinite in both directions (blank cells shown as _); only the cells the machine has visited, plus a margin, are drawn. The cell just written is shaded.
Step 0
Start state q0; the input aa is written on the tape and the head is on its first cell (cells to the left and right are blank).
This machine recognises the language "contains " without deciding it. (A decider for it is easy to build: reject on the blank; but the animation shows why recognising is a weaker property.)
How many moves
The number of moves is the running time of the machine. For the three machines above:
How many moves the machines make: start
Computed by a script, not typed| n | a^n b^n decider | a^n b^n c^n decider | increment of 1^n |
|---|---|---|---|
| n = 0 | |||
| n = 1 | |||
| n = 2 | |||
| n = 3 | |||
| n = 4 | |||
| n = 5 | |||
| n = 6 | |||
| n = 7 | |||
| n = 8 |
Step 0
Number of moves until the machine halts. a^n b^n: 2n² + 2n + 1; a^n b^n c^n: 4n² + 3n + 1; increment of the all-ones string 1^n: 2n + 2. These formulas were found by simulation and confirmed for n up to 40 in scripts/enum-checks/toc_turing.py.
The machine for makes moves on , the one for makes moves, and the increment machine makes moves on (formulas fitted to simulations and confirmed for up to ). The quadratic growth is typical of one-tape machines that go back and forth: each of the rounds walks over a string of length about . (Faster machines exist for this language; the formula describes THIS machine.)
Conventions that differ between books
- Tape: two-way infinite (this site, Hopcroft et al.) or infinite to the right only with the head unable to move left from the first cell (Sipser). They are equivalent in power.
- Halting: one accepting state and one rejecting state (Sipser, this site) or a set of final states with the machine halting when no move applies (Hopcroft et al.).
- Blank and marks: names of symbols differ; the work-alphabet marks are only a habit.
- Moves: some books allow the head to stay in place () as well as move left and right; they are equivalent (a stay is a right move followed by a left move through an extra state).
Worked examples
EX 1Trace a given machine
EasyMachine with , , , all other moves missing (reject). Input : reads , moves right; reads , goes to ; reads the blank and accepts. Input : after the , reads : no move, reject. The machine accepts .
EX 2Halt or loop
Easyand and nothing else: on 's followed by blanks the head moves right for ever: the machine never halts on any input over . If instead, it halts and accepts exactly .
EX 3Count the moves of the a^n b^n machine
MediumOn the machine makes moves: two rounds of moves (mark an , walk right, mark a , walk back to the , step right) and a final check of moves. In general there are rounds of moves and a final check of moves: (confirmed for up to by splitting the run at the returns to ).
EX 4Why the machine rejects aab
MediumAfter the first round the tape is and the machine is back in looking at the second . It marks it () and in walks right over to the blank, looking for a . There is no transition for , so it halts and rejects. That is moves and then a halt without a move. An input with too many 's is rejected too: after all 's are marked, meets a and has no transition for .
EX 5Typical questions
GATE level(a) "A TM can accept ; a PDA cannot." True. (b) "A TM halts on every input." False; it may loop. (c) "The class of languages recognised by TMs:" recursively enumerable. (d) "A TM with a tape that is infinite only to the right is less powerful than one infinite both ways." False; equivalent. (e) "Is a missing transition an error?" In this site's convention it means reject. (f) "Number of moves of a machine on a string of length can be:" any function; for the machine above for .
Cheat box
Cheat box
- TM ; move: write, move /, change state; tape infinite, blank outside the input.
- Three outcomes: accept, reject, loop. Recognise: accepts exactly . Decide: also halts on all inputs.
- and are decided by TMs; no PDA does them.
- Mark symbols with and sweep back and forth; one-tape time is often quadratic.
- Equivalent models: one-way versus two-way tape, stay-moves, multiple tracks.
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
No cross-subject links yet.
The Turing machine is the top of the Chomsky hierarchy (hierarchy); its variants (several tapes, nondeterminism) and the Church–Turing thesis are next (TM variants); recognising versus deciding leads to undecidability (decidable and recognisable languages).
PYQs
No PYQs attached to this concept yet
Previous year questions are being added section by section from your PYQ book, each with my own re-checked answer. Nothing is shown from memory.
Self-check
Question 1
What does the transition do?
Show answerHide
In state reading : write over , move the head one cell to the left, enter state .
Question 2
What is the difference between a Turing machine that recognises a language and one that decides it?
Show answerHide
A recogniser accepts exactly the strings of the language but may loop on others; a decider also halts (rejecting) on every string outside it.
Question 3
Why can a TM accept while a PDA cannot?
Show answerHide
The tape can be read and rewritten in both directions, so the machine can match the 's with the 's and then re-read to match with the 's; the PDA's stack is destroyed by the first comparison.
Question 4
How many moves does the machine of this page make on ?
Show answerHide
.
Revision card
One-screen revision
- State, tape symbol new state, written symbol, or ; infinite tape; blank outside the input.
- Accept, reject or loop; recognise versus decide.
- : mark , mark , return; : three marks; increment: right, then carry left.
- One-tape time often quadratic ( here); models with a one-way tape are equivalent.
References
- ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 3.1 (Turing machines)
- ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Chapter 8 (Turing machines), Sections 8.1 and 8.2
Verification
scripts/enum-checks/toc_turing.py: the machine for a^n b^n equals the predicate and halts on all strings over {a, b} up to length 10, the machine for a^n b^n c^n on all strings over {a, b, c} up to length 9; the increment machine gives the binary successor on all binary strings up to length 12 (same length, or one longer after overflow); the recogniser for 'contains b' accepts exactly the strings with a b and on a* its head index equals the number of moves, so it never revisits a configuration (the empty tape to the right looks the same at every step); a second simulator written differently (list tape with an offset) agrees on verdict, number of moves and final tape for all four machines on all short strings; the move counts 2n^2 + 2n + 1, 4n^2 + 3n + 1 and 2n + 2 were confirmed for n up to 40; the animations replay: each step applies the transition it names. The conventions (two-way infinite tape, missing transition means reject) are those of this site; textbooks differ. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)