impera Open Impera

A levels Computer Science · Theory of computation

Turing machines

1The model

A Turing machine, described by Alan Turing in 1936, is a simple model of a computer:

  • an infinite tape divided into cells, each holding one symbol (or a blank, □);
  • a read/write head that looks at one cell at a time;
  • a finite set of states, including a start state and one or more halting states;
  • a transition function saying, for each state and symbol read, what to write, which way to move, and which state to go to next.

Unlike an FSM, a Turing machine has unlimited memory: the tape.

Check yourself

What gives a Turing machine more power than a finite state machine?

  • It runs faster because it processes symbols in parallel

  • It has more states than any finite state machine

  • It has no halting state, so it can run forever

  • Its infinite tape, which it can read and write

Show the answer

Its infinite tape, which it can read and write

Unlimited memory.

2Transition rules

A rule is written

$$\delta(\text{current state}, \text{symbol read}) = (\text{new state}, \text{symbol to write}, \text{move})$$

where the move is L (left) or R (right).

For example, δ(S0, 1) = (S0, 0, L) means: "in state S0, if you read 1, write 0, move left and stay in S0".

The rules can also be shown as a state transition diagram, with each arrow labelled read | write, move.

Check yourself

What does δ(S1, 0) = (S2, 1, R) mean?

  • Stop the machine

  • In S1 reading 0: write 1, move right, go to S2

  • In S2 reading 1: write 0, move left

  • In S1 reading 1: write 0, move right

Show the answer

In S1 reading 0: write 1, move right, go to S2

State, write, move.

3Worked example: adding 1 in binary

This machine adds 1 to a binary number. The head starts on the rightmost digit, in state S0.

StateReadWriteMoveNext state
S010LS0
S001LHalt
S0□1LHalt

The binary-increment Turing machine turning 1011 into 1100. The tape continues with blanks (□) in both directions.

Worked example

1011 + 1

  1. Read 1 (rightmost): write 0, move left. Tape 1010.
  2. Read 1: write 0, move left. Tape 1000.
  3. Read 0: write 1, halt. Tape 1100.
  4. Check: 11 + 1 = 12 = 1100 in binary.
Check yourself

What does the machine produce from 111?

  • 000

  • 1110

  • 1000

  • 110

Show the answer

1000

The blank becomes 1.

4What can be computed

The Church–Turing thesis says that anything that can be computed by an algorithm can be computed by a Turing machine. Every real computer and programming language can do exactly the same things as a Turing machine (given enough memory and time).

So the Turing machine gives a precise meaning to "computable": a problem is computable if some Turing machine solves it and always halts.

Check yourself

What does the Church–Turing thesis state?

  • Turing machines are faster than any real computer at solving problems

  • A Turing machine needs no states, only a tape

  • Every problem can be solved by some algorithm if given enough time

  • Anything computable by an algorithm can be computed by a Turing machine

Show the answer

Anything computable by an algorithm can be computed by a Turing machine

Defines computable.

5The universal Turing machine

A universal Turing machine (UTM) can simulate any other Turing machine. The description of that machine (its transition rules) and its input are both written on the tape.

This is the key idea behind the stored-program computer: programs are just data held in memory, so one machine can run any program. Modern computers follow this von Neumann idea.

Check yourself

What is the significance of the universal Turing machine?

  • It can solve the halting problem by simulating every other machine

  • It needs no tape, because the program is stored in its states

  • It shows one machine can run any program stored as data, the basis of stored-program computers

  • It is the fastest possible computer, so nothing can beat it in speed

Show the answer

It shows one machine can run any program stored as data, the basis of stored-program computers

Programs as data.

6The halting problem

The halting problem asks: is there an algorithm that, given any program and its input, decides whether the program will eventually halt?

Turing proved there is not. The idea of the proof:

  1. Suppose a program H(P, I) always correctly says whether P halts on input I.
  2. Build a program Z(P) that runs H(P, P) and does the opposite: if H says "halts", Z loops forever; if H says "loops", Z halts.
  3. Ask what happens with Z(Z). If Z halts, H said it loops, so Z halts — contradiction. If Z loops, H said it halts, so Z loops — contradiction.
  4. So H cannot exist.

This shows there are problems no computer can ever solve, however powerful.

Check yourself

What does the halting problem show?

  • All problems are intractable for large inputs

  • Every program eventually halts if it is given enough time

  • Some problems cannot be solved by any algorithm

  • Turing machines always loop when given an unknown input

Show the answer

Some problems cannot be solved by any algorithm

Non-computable.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera