Sub topic 1
What a Turing machine is: the 7-tuple model
Prerequisites
This chapter assumes you know the following:
- Finite automata and regular languages: states, transitions, and what it means for a machine to accept a string.
- Context-free grammars and pushdown automata, at least at the level of knowing that a stack gives extra power.
- Basic set notation: sets, tuples, functions, and the Cartesian product.
You do not need any computability theory yet. This chapter only defines the machine. The next chapter covers how it runs.
Why a new model is needed
A finite automaton has a fixed number of states and reads its input once, left to right. A pushdown automaton adds a stack, but it can only touch the top symbol. Neither can recognise a^n b^n c^n, because that needs to compare three counts.
A Turing machine (TM) removes both limits. It has an unbounded tape that it can read and write, and its head can move in both directions. The control is still a finite set of states. That one change gives a model strong enough to capture everything we call computation, and weak enough to prove precise results about.
The picture
A Turing machine has three parts:
- A tape divided into cells, infinite to the right (some books make it infinite in both directions). Each cell holds one symbol.
- A head that sits on one cell. In each step it reads that cell, writes a symbol, and moves one cell left or right.
- A finite control that holds the current state and chooses what to do from the state and the symbol under the head.
At the start, the input string is written at the left end of the tape. Every other cell holds the blank symbol.
The 7-tuple
Formally, a deterministic Turing machine is a 7-tuple:
M = (Q, Σ, Γ, δ, q0, qaccept, qreject)
Many GATE sources write it as (Q, Σ, Γ, δ, q0, B, F) instead, with a blank symbol B and a set of final states F. The two forms describe the same power. This chapter uses the first form, and the table below shows where each component sits in both.
| Component | Meaning | Notes |
|---|---|---|
| Q | Finite set of states | Includes the start, accept and reject states. |
| Σ | Input alphabet | Never contains the blank symbol. |
| Γ | Tape alphabet | Σ ⊂ Γ, and the blank is in Γ but not in Σ. |
| δ | Transition function | δ : (Q \ {q_accept, q_reject}) × Γ → Q × Γ × {L, R} |
| q0 | Start state | q0 ∈ Q. |
| q_accept | Accept state | Halts and accepts. |
| q_reject | Reject state | Halts and rejects. Must differ from q_accept. |
The transition function
The transition function is where the behaviour lives. One rule reads:
δ(q1, a) = (q2, X, R)
This means: in state q1, with a under the head, write X, move right, and enter state q2. The function is defined on a state and a tape symbol, not on an input symbol only. That is why the machine can read back what it wrote.
The domain excludes the accept and reject states. Once the machine enters either one, it stops, so no further moves are needed.
Details that cause mistakes
- Σ is a strict subset of Γ. The input uses only Σ symbols, but the machine may write marker symbols such as
XandY, and it sees the blank at the end of the input. The blank is in Γ only. - The head cannot stay put in the basic model. Each move is L or R. A variant with an S (stay) move gives the same power, because a stay can be simulated by moving right and then left.
- Moving left at cell 0. On a tape with a left end, the head stays at cell 0. Many textbook machines avoid this case on purpose.
- δ is a function, so it is deterministic. For each state and symbol there is at most one move. A missing entry is treated as an implicit move to the reject state.
- Final states versus two halting states. In the (Q, Σ, Γ, δ, q0, B, F) form, the machine accepts when it reaches a state in F, and it usually has no transitions out of F. The two-state form is equivalent.
How the machine decides
A TM has three possible outcomes on an input w:
- It accepts by reaching the accept state.
- It rejects by reaching the reject state, or by having no transition.
- It loops by never reaching either state.
The third outcome has no counterpart in finite automata, which always finish after reading the input. It is the source of everything that follows in this tutorial, so keep it in mind. The language of M, written L(M), is the set of strings on which M accepts. Strings on which M rejects and strings on which it loops are both outside L(M).
A concrete machine
Here is a machine for { a^n b^n : n ≥ 0 }. The idea: mark the leftmost unmarked a with X, walk right to the first unmarked b, mark it with Y, and walk back. Repeat. When no a remains, check that no b remains either.
- Q = {q0, q1, q2, q3, qa}
- Σ = {a, b}
- Γ = {a, b, X, Y, }, where `` is the blank
- Start state q0, accept state qa. Any missing transition rejects.
The transitions are:
| State | Symbol read | Write | Move | Next state |
|---|---|---|---|---|
| q0 | a | X | R | q1 |
| q0 | Y | Y | R | q3 |
| q0 | _ | _ | R | qa |
| q1 | a | a | R | q1 |
| q1 | Y | Y | R | q1 |
| q1 | b | Y | L | q2 |
| q2 | a | a | L | q2 |
| q2 | Y | Y | L | q2 |
| q2 | X | X | R | q0 |
| q3 | Y | Y | R | q3 |
| q3 | _ | _ | R | qa |
State q0 looks for the next a to mark. State q1 scans right for a b. State q2 returns left to the last X. State q3 confirms that only Y symbols remain.
You can run this table with a small simulator. A dictionary maps (state, symbol) to (state, symbol, move), and a missing key means reject.
def run(delta, w, start='q0', accept='qa', blank='_', max_steps=10000):
tape = dict(enumerate(w))
head = 0
state = start
for _ in range(max_steps):
if state == accept:
return True
key = (state, tape.get(head, blank))
if key not in delta:
return False
state, sym, move = delta[key]
tape[head] = sym
head += 1 if move == 'R' else -1
return None # step limit reached: no verdict
# Accepts { a^n b^n : n >= 0 }
delta = {
('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'),
}
for w in ['', 'ab', 'aabb', 'aab', 'abb', 'ba']:
print(repr(w), run(delta, w))
# '' True, 'ab' True, 'aabb' True, 'aab' False, 'abb' False, 'ba' FalseThe function returns None when it hits the step limit. That value does not mean the machine loops. It only means the simulator gave up. Simulation can confirm that a machine halts, but it can never confirm that one runs forever. Later chapters prove that no program can do this in general.
Common errors
- Writing δ with the input alphabet Σ in place of the tape alphabet Γ as the domain. The machine must be able to read blanks and markers.
- Allowing the blank symbol in Σ. If it were input, the machine could not find the end of the string.
- Defining moves out of the accept or reject state. Halting states have no outgoing moves.
- Counting the tape as part of the states. The tape is unbounded, while Q is finite. This is exactly why a TM is stronger than a finite automaton.
Exercise
A Turing machine has Q = {q0, q1, qa}, Σ = {0}, Γ = {0, } and these transitions only: δ(q0, 0) = (q1, 0, R), δ(q1, 0) = (q0, 0, R), δ(q1, ) = (qa, _, R). The start state is q0 and a missing transition rejects. Describe L(M), and say whether any input makes M loop.
Show answer
M alternates q0, q1 as it reads 0s. It reaches the blank in state q1 only after an odd number of 0s, so it accepts exactly the strings 0^n with n odd. On an even number of 0s (including the empty string) it reaches the blank in state q0, where no transition exists, and rejects. The head always moves right and the input is finite, so M never loops. L(M) = { 0^n : n is odd }.
Exercise
Which statement about the 7-tuple is true? (a) Σ may contain the blank symbol. (b) δ is defined on the accept state. (c) Σ is a proper subset of Γ. (d) The tape alphabet is the same as the input alphabet.
Show answer
(c). The tape alphabet Γ must contain Σ and also the blank, so Σ is a proper subset of Γ. Option (a) is false because the blank is not an input symbol. Option (b) is false because halting states have no outgoing moves. Option (d) is false for the same reason as (c).