AQA 7517, section 4.4.5.1
A Turing machine is a simple, theoretical model of a computer, devised by Alan Turing in 1936. It is very basic, but it can carry out any computation that a real computer can. Exam questions only use Turing machines with one tape that is infinite in one direction.
Structure of a Turing machine
A Turing machine can be viewed as a computer with a single fixed program. It has:
- a finite set of states, shown in a state transition diagram;
- a finite alphabet of symbols (for example
0,1and a blank, shown as_); - an infinite tape divided into squares, each holding one symbol;
- a read-write head that senses the symbol in the current square, can write a new symbol there, and then moves one square left or right.
One state is the start state. A state with no outgoing transitions is a halting state, and the machine stops when it reaches one.
How it works
At each step the machine looks at the current state and the symbol under the head. A rule then tells it three things:
- which symbol to write (it can write the same one back);
- which direction to move the head (left or right);
- which state to go to next.
Transition functions
The rules can be written as a transition function, . It takes the current state and the symbol read, and gives the new state, the symbol to write and the direction:
This reads: "in state reading a 0, write 1, move right, and stay in state ."
State transition diagrams
The same rules can be drawn as a state transition diagram. Each arrow is labelled read / write, move, so 0/1, R means "read 0, write 1, move right".
There's an equivalence between a transition function and a state transition diagram: either can represent exactly the same machine, and you should be able to convert from one to the other.
Example: inverting a binary number
Task: a machine that changes every 0 to 1 and every 1 to 0 on the tape, then stops at the first blank.
States: (start, scanning) and (halting).
| State | Read | Write | Move | Next state |
|---|---|---|---|---|
| 0 | 1 | R | ||
| 1 | 0 | R | ||
| _ | _ | R |
As a transition function:
As a state diagram:
Hand-tracing
Start with the tape 1 0 1 _ and the head on the first square, in state :
| Step | State | Head position | Symbol read | Action | Tape after |
|---|---|---|---|---|---|
| 1 | 0 | 1 | write 0, move R | 0 0 1 _ | |
| 2 | 1 | 0 | write 1, move R | 0 1 1 _ | |
| 3 | 2 | 1 | write 0, move R | 0 1 0 _ | |
| 4 | 3 | _ | write _, move R, go to | 0 1 0 _ | |
| 5 | 4 | halt | 0 1 0 _ |
The tape has changed from 101 to 010, which is the inverted number. The machine halts because has no outgoing transitions.
Example: a machine that adds one in unary
In unary, a number is a row of 1s, so 3 is 1 1 1. To add one, move to the first blank and write a 1.
| State | Read | Write | Move | Next state |
|---|---|---|---|---|
| 1 | 1 | R | ||
| _ | 1 | R |
Tape 1 1 1 _ becomes 1 1 1 1 _, so 3 becomes 4. The head runs right over the 1s, then writes a 1 on the first blank and halts.
Why are Turing machines important?
- They provide a general, formal model of computation. If a Turing machine can carry out a computation, so can any real computer, and the other way round.
- They give a definition of what is computable. A problem is computable if and only if some Turing machine can solve it. This is how problems such as the halting problem were shown to be unsolvable.
- The Universal Turing machine is a single Turing machine that can simulate any other Turing machine. It reads a description of the other machine (its program) and the input from its tape, and then does what that machine would do. This is the idea behind the modern stored-program computer: one general machine that runs any program you give it, instead of a different machine for every task.
Worked example
Task: a machine has the rules , , . Trace it on the tape 1 1 _.
| Step | State | Head | Read | Action | Tape after |
|---|---|---|---|---|---|
| 1 | 0 | 1 | write 1, move R, go to | 1 1 _ | |
| 2 | 1 | 1 | write 1, move R, stay in | 1 1 _ | |
| 3 | 2 | _ | write 0, move R, go to | 1 1 0 | |
| 4 | 3 | halt | 1 1 0 |
The machine adds a 0 to the end of a row of 1s and halts.
Exam tip: in a trace, record the state, the head position, the symbol read, and the tape after every step. Check each transition carefully for which symbol to write and which way to move. The machine halts when it reaches a state with no transition to follow.
Check yourself
Name the four components of a Turing machine
A finite set of states (in a state transition diagram), a finite alphabet of symbols, an infinite tape of squares, and a read-write head that moves along the tape one square at a time.
What does the transition rule δ(q1, 0) = (q2, 1, L) mean?
In state q1, reading 0: write 1, move the head left, and go to state q2.
How can you tell which states are halting states?
They are the states that have no outgoing transitions.
Why is the Universal Turing machine important?
It can simulate any other Turing machine by reading that machine's description and input from its tape, which is the idea behind general-purpose, stored-program computers.