G27
Self-checkedTier 1 11 min readtoc.turing-machines

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 M=(Q,Σ,Γ,δ,q0,qacc,qrej)M = (Q, \Sigma, \Gamma, \delta, q_0, q_{\text{acc}}, q_{\text{rej}}):

  • QQ finite set of states; q0q_0 the start state; qaccq_{\text{acc}} the accepting state; qrejq_{\text{rej}} the rejecting state (qacc≠qrejq_{\text{acc}} \ne q_{\text{rej}});
  • Σ\Sigma the input alphabet (no blank); Γ⊇Σ\Gamma \supseteq \Sigma the tape alphabet; a blank symbol ⊔∈Γ∖Σ\sqcup \in \Gamma \setminus \Sigma (written _ in the animations);
  • δ:(Q∖{qacc,qrej})×Γ→Q×Γ×{L,R}\delta : (Q \setminus \{q_{\text{acc}}, q_{\text{rej}}\}) \times \Gamma \to Q \times \Gamma \times \{L, R\}.

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 qq reading aa, if δ(q,a)=(q′,b,D)\delta(q, a) = (q', b, D) the machine writes bb over aa, moves the head one cell left (D=LD = L) or right (D=RD = R) and enters q′q'. A missing transition means the machine halts and rejects (the convention of this site; many books add that explicit qrejq_{\text{rej}} transition).

A configuration records the state, the tape contents and the head position, usually written u q vu\,q\,v: the tape is uvuv (with blanks outside), the head is on the first symbol of vv and the state is qq. A configuration yields the next one by one move.

On input ww exactly one of three things happens:

  • the machine accepts (reaches qaccq_{\text{acc}} and halts),
  • it rejects (reaches qrejq_{\text{rej}} or has no applicable move and halts),
  • it loops (never halts).

The language recognised by MM is L(M)={w:M accepts w}L(M) = \{w : M \text{ accepts } w\}. MM decides LL if it recognises LL 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 anbna^n b^n

Idea: mark one aa with XX, move right to the first unmarked bb and mark it YY, return to the left end, repeat. When no aa is left, check that only YY's remain.

States and moves (qaq_a accepts):

  • q0q_0: on aa: write XX, move right, go to q1q_1; on YY: go to q3q_3 (all aa's used up); on blank: accept (empty string).
  • q1q_1 (looking for a bb): on aa or YY: skip right; on bb: write YY, move left, go to q2q_2.
  • q2q_2 (going back): on aa or YY: skip left; on XX: move right, go to q0q_0.
  • q3q_3 (checking): on YY: skip right; on blank: accept.

Any other pair has no transition: the machine rejects. Run on aabbaabb (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 typed
stateq0
inputaabb
q0, a → q1, X, R
q0, Y → q3, Y, R
q0, _ → qa, _, R
q1, a → q1, a, R
q1, Y → q1, Y, R
q1, b → q2, Y, L
q2, a → q2, a, L
q2, Y → q2, Y, L
q2, X → q0, X, R
q3, Y → q3, Y, R
q3, _ → qa, _, R

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).

0 / 13
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc4-traces.py

On aabaab there is an aa left over: the head reaches the blank at the right end while still looking for a bb and the machine rejects:

The same machine on aab: start

Simulated, not typed
stateq0
inputaab
q0, a → q1, X, R
q0, Y → q3, Y, R
q0, _ → qa, _, R
q1, a → q1, a, R
q1, Y → q1, Y, R
q1, b → q2, Y, L
q2, a → q2, a, L
q2, Y → q2, Y, L
q2, X → q0, X, R
q3, Y → q3, Y, R
q3, _ → qa, _, R

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).

0 / 8
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc4-traces.py

Correctness: each round marks exactly one aa and one bb, so the string is accepted iff the number of aa's equals the number of bb's and all aa's come before all bb's. (Checked on all strings over {a,b}\{a, b\} up to length 1010.) The machine halts on every input: it is a decider.

Example 2: a decider for anbncna^n b^n c^n

The same idea with three symbols: in each round mark one aa (XX), one bb (YY) and one cc (ZZ), return to the left end by passing over the marks, and repeat; when no aa is left check that only YY's and ZZ'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 typed
stateq0
inputaabbcc
q0, a → q1, X, R
q0, Y → q4, Y, R
q0, _ → qa, _, R
q1, a → q1, a, R
q1, Y → q1, Y, R
q1, b → q2, Y, R
q2, b → q2, b, R
q2, Z → q2, Z, R
q2, c → q3, Z, L
q3, a → q3, a, L
q3, b → q3, b, L
q3, Y → q3, Y, L
q3, Z → q3, Z, L
q3, X → q0, X, R
q4, Y → q4, Y, R
q4, Z → q4, Z, R
q4, _ → qa, _, R

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).

0 / 23
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc4-traces.py

Example 3: a machine that computes a function

Binary increment: go right to the end of the number, then turn trailing 11's into 00's going left until a 00 (or the blank before the number) becomes 11. On 10111011 it halts with 11001100 on the tape:

Binary increment of 1011: start

Simulated, not typed
stateq0
input1011
q0, 0 → q0, 0, R
q0, 1 → q0, 1, R
q0, _ → q1, _, L
q1, 1 → q1, 0, L
q1, 0 → qa, 1, R
q1, _ → qa, 1, R

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).

0 / 8
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc4-traces.py

For all binary strings up to length 1212 the result is the binary successor of the number (the same number of digits, or one more after 1…11\ldots1).

Example 4: a machine that loops

The recogniser that moves right over aa's, accepts when it sees bb, and treats a blank like an aa (keeps moving right) accepts exactly the strings that contain a bb. On a string with no bb 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 1414 moves the animation stops without a verdict:

A recogniser for strings containing b, on aa: start

Simulated, not typed
stateq0
inputaa
q0, a → q0, a, R
q0, b → qa, b, R
q0, _ → q0, _, R

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).

0 / 14
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc4-traces.py

This machine recognises the language "contains bb" 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
na^n b^n decidera^n b^n c^n deciderincrement 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.

0 / 9
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc4-traces.py

The machine for anbna^n b^n makes 2n2+2n+12n^2 + 2n + 1 moves on anbna^n b^n, the one for anbncna^n b^n c^n makes 4n2+3n+14n^2 + 3n + 1 moves, and the increment machine makes 2n+22n + 2 moves on 1n1^n (formulas fitted to simulations and confirmed for nn up to 4040). The quadratic growth is typical of one-tape machines that go back and forth: each of the nn rounds walks over a string of length about 2n2n. (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 X,Y,ZX, Y, Z are only a habit.
  • Moves: some books allow the head to stay in place (SS) 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

Easy

Machine with δ(q0,0)=(q0,0,R)\delta(q_0, 0) = (q_0, 0, R), δ(q0,1)=(q1,1,R)\delta(q_0, 1) = (q_1, 1, R), δ(q1,⊔)=(qacc,⊔,R)\delta(q_1, \sqcup) = (q_{\text{acc}}, \sqcup, R), all other moves missing (reject). Input 0101: q0q_0 reads 00, moves right; q0q_0 reads 11, goes to q1q_1; q1q_1 reads the blank and accepts. Input 011011: after the 11, q1q_1 reads 11: no move, reject. The machine accepts 0∗10^* 1.

EX 2Halt or loop

Easy

δ(q0,a)=(q0,a,R)\delta(q_0, a) = (q_0, a, R) and δ(q0,⊔)=(q0,⊔,R)\delta(q_0, \sqcup) = (q_0, \sqcup, R) and nothing else: on aa's followed by blanks the head moves right for ever: the machine never halts on any input over {a}\{a\}. If δ(q0,⊔)=(qacc,⊔,R)\delta(q_0, \sqcup) = (q_{\text{acc}}, \sqcup, R) instead, it halts and accepts exactly a∗a^*.

EX 3Count the moves of the a^n b^n machine

Medium

On aabbaabb the machine makes 1313 moves: two rounds of 55 moves (mark an aa, walk right, mark a bb, walk back to the XX, step right) and a final check of 33 moves. In general there are nn rounds of 2n+12n + 1 moves and a final check of n+1n + 1 moves: n(2n+1)+n+1=2n2+2n+1n(2n + 1) + n + 1 = 2n^2 + 2n + 1 (confirmed for nn up to 1010 by splitting the run at the returns to q0q_0).

EX 4Why the machine rejects aab

Medium

After the first round the tape is XaYX a Y and the machine is back in q0q_0 looking at the second aa. It marks it (XXYX X Y) and in q1q_1 walks right over YY to the blank, looking for a bb. There is no transition for (q1,⊔)(q_1, \sqcup), so it halts and rejects. That is 77 moves and then a halt without a move. An input with too many bb's is rejected too: after all aa's are marked, q0q_0 meets a bb and has no transition for (q0,b)(q_0, b).

EX 5Typical questions

GATE level

(a) "A TM can accept anbncna^n b^n c^n; 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 nn can be:" any function; for the anbna^n b^n machine above 2n2+2n+12n^2 + 2n + 1 for anbna^n b^n.

Cheat box

Cheat box

  • TM =(Q,Σ,Γ,δ,q0,qacc,qrej)= (Q, \Sigma, \Gamma, \delta, q_0, q_{\text{acc}}, q_{\text{rej}}); move: write, move LL/RR, change state; tape infinite, blank outside the input.
  • Three outcomes: accept, reject, loop. Recognise: accepts exactly LL. Decide: also halts on all inputs.
  • anbncna^nb^nc^n and wwww are decided by TMs; no PDA does them.
  • Mark symbols with X,Y,ZX, Y, Z 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

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 δ(q,a)=(p,b,L)\delta(q, a) = (p, b, L) do?

Show answer

In state qq reading aa: write bb over aa, move the head one cell to the left, enter state pp.

Question 2

What is the difference between a Turing machine that recognises a language and one that decides it?

Show answer

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 anbncna^n b^n c^n while a PDA cannot?

Show answer

The tape can be read and rewritten in both directions, so the machine can match the aa's with the bb's and then re-read to match with the cc's; the PDA's stack is destroyed by the first comparison.

Question 4

How many moves does the anbna^n b^n machine of this page make on a3b3a^3 b^3?

Show answer

2⋅9+6+1=252 \cdot 9 + 6 + 1 = 25.

Revision card

One-screen revision

  • State, tape symbol →\to new state, written symbol, LL or RR; infinite tape; blank outside the input.
  • Accept, reject or loop; recognise versus decide.
  • anbna^nb^n: mark XX, mark YY, return; anbncna^nb^nc^n: three marks; increment: right, then carry left.
  • One-tape time often quadratic (2n2+2n+12n^2 + 2n + 1 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