CA320: C OMPUTABILITY AND C OMPLEXITY
7
1
Turing Machines
7.1
Introduction
Turing Machines
None of the automata which we have seen so far are general models of computation.
Turing machines provide such a general model.
Turing machines extend the idea of linear bounded automata by having an infinite
length of tape. The symbol < is used to mark the leftmost bound of the tape beyond which the tape head cannot move and which cannot be overwritten. There is no
rightmost bound on the tape.
As for linear bounded automata, Turing machines will perform one of the actions A ∈
{Y, N, L, R} at each step, where:
• Y denotes “Yes”, accept the input string
• N denotes “No”, do not accept the input string
• L denotes “Left”, move the read-write head one space to the left
• R denotes “Right”, move the read-write head one space to the right
Formal Definition of Turing Machines
A Turing machine is a 5-tuple M = (Q, Σ, Γ, q0 , δ ), where:
• Q is a finite set of states.
• Σ is an alphabet (input symbols).
• Γ is an alphabet (store symbols).
• q0 ∈ Q is the initial state.
• δ , the transition function, is from Q × (Γ ∪ {<}) to Q × (Γ ∪ {<}) × A .
If ((q, a), (q0 , b, action)) ∈ δ , then when in state q with a at the current read position on
the tape, M may replace a with b on the tape, perform the specified action, and enter
state q0 .
The symbol “#” is used to denote a blank tape square.
M accepts w ∈ Σ∗ iff it starts with configuration (q0 , w#) and the action Y is taken.
7.2
Turing Machine Examples
Turing Machine Example
To accept the language of strings of a’s
whose length is a power of 2.
M1 = (Q, Σ, Γ, q0 , δ ) where:
• Q = {s0 , s1 , s2 , s3 , s4 }
Geoff Hamilton
• Σ = {a}
• Γ = {a, x, #}
• q0 = s0
CA320: C OMPUTABILITY AND C OMPLEXITY
2
((s0 , a), (s1 , #, R))
((s0 , x), (s0 , x, N))
((s0 , #), (s0 , #, N))
((s1 , a), (s2 , x, R))
((s1 , x), (s1 , x, R))
((s1 , #), (s1 , #,Y ))
((s2 , a), (s3 , a, R))
• δ = ((s2 , x), (s2 , x, R))
((s2 , #), (s4 , #, L))
((s3 , a), (s2 , x, R))
((s3 , x), (s3 , x, R))
((s3 , #), (s3 , #, N))
((s4 , a), (s4 , a, L))
((s4 , x), (s4 , x, L))
((s4 , #), (s1 , #, R))
This will accept all strings of a’s whose length is a power of 2.
Turing Machine Example
This can be represented as a transition diagram as follows:
# → #,L
x → x,N
x → x,R
x → x,R
x → x,R
s0
a → #,R
s1
a → x,R
a → a,L
s2
a → a,R
s3
# → #,Y
s4
# → #,N
# → #,N
x → x,L
a → x,R
# → #,R
x → y, A denotes reading symbol x, writing symbol y and performing action A .
Notation
Initial configuration: (q0 , w#)
Halting configuration: Configuration when action Y or N is performed.
Hanging configuration: No transition for the current state and current input symbol.
A computation is a sequence of configurations of length n ≥ 0:
(s0 , aa#)
Geoff Hamilton
`M1
`M1
`M1
`M1
`M1
`M1
(s1 , #a#)
(s2 , #x#)
(s4 , #x#)
(s4 , #x#)
(s1 , #x#)
(s1 , #x#)
CA320: C OMPUTABILITY AND C OMPLEXITY
3
Therefore (s0 , aa#) `∗M1 (s1 , #x#)
Another Turing Machine Example
M2 = (Q, Σ, Γ, q0 , δ ) where:
• Q = {s0 }
• Σ = {a, b}
• Γ = {a, b, #}
• q0 = s0
((s0 , a), (s0 , a, L))
((s0 , b), (s0 , b, R))
• δ=
((s0 , #), (s0 , #,Y ))
((s0 , <), (s0 , <, R))
This is an example of a Turing machine which may not halt.
Computations
M is said to halt on input w iff (q0 , w) yields a halting configuration.
M is said to hang on input w if (q0 , w) yields a hanging configuration.
Turing machines compute functions from strings to strings.
Formally: Let f be a function from Σ∗0 to Σ∗1 . Turing machine M is said to compute f
if for any w ∈ Σ∗0 , if f (w) = u then:
(q0 , w#) `∗M (q, u#)
where (q, u#) is a halting configuration.
f is said to be a Turing–computable function.
Multiple parameters: f (w1 , . . . , wk ) = u:
(q0 , w1 #w2 # . . . #wk #) `∗M (q, u#).
A Non-Context-Free Language
Construct a Turing machine to recognize the following language:
{an bn cn |n ≥ 0}
• If the start cell is empty, then HALT.
• If the current cell contains a, then write X and scan right.
• Look for b, replace with Y .
• Look for c, replace with Z.
• Now, scan left to an a to right of X.
• Repeat process.
• Make sure nothing to right of c’s.
Geoff Hamilton
CA320: C OMPUTABILITY AND C OMPLEXITY
7.3
4
Functions on Natural Numbers
Functions on Natural Numbers
Represent numbers in unary notation using only the symbol 1 (zero is represented by
the empty string).
f : N → N is computed by M if M computes f 0 : {1}∗ → {1}∗ where f 0 (1n ) = 1 f (n) for
each n ∈ N.
Example: f (n) = n + 1 for each n ∈ N.
State
q0
q0
Symbol
1
#
δ (State, Symbol)
(q0 , 1, R)
(q0 , 1,Y )
(q0 , 11#) `M (q0 , 11#) `M (q0 , 11#) `M (q0 , 111)
In general, (q0 , 1n #) `∗M (q0 , 1n+1 #).
What about n = 0?
Functions on Natural Numbers
As another example, the following Turing machine calculates the addition of two unary
numbers. It is assumed that the initial configuration of the machine is (a, m#n#), where
m and n are the two numbers to be added.
State Symbol δ (State, Symbol)
a
1
(b, #, R)
a
#
(a, #,Y )
b
1
(b, 1, R)
b
#
(c, #, R)
c
1
(c, 1, R)
c
#
(d, 1, L)
d
1
(d, 1, L)
d
#
(e, #, L)
e
1
(e, 1, L)
e
#
(a, #, R)
Functions on Natural Numbers
This can be represented as a transition diagram as follows:
# → #,Y
a
1 → 1,R
1 → #,R
b
1 → 1,R
# → #,R
c
# → #,R
Geoff Hamilton
1 → 1,L
# → 1,L
d
1 → 1,L
# → #,L
e
CA320: C OMPUTABILITY AND C OMPLEXITY
5
Functions on Natural Numbers
Consider input 11#111## for the previous Turing machine:
(a, 11#111##)
`
`
`
`
`
`
`
`
`
`
`
`
`
(b, #1#111##)
(b, #1#111##)
(c, #1#111##)
(c, #1#111##)
(c, #1#111##)
(c, #1#111##)
(d, #1#1111#)
(d, #1#1111#)
(d, #1#1111#)
(d, #1#1111#)
(e, #1#1111#)
(e, #1#1111#)
(a, #1#1111#)
Functions on Natural Numbers
`
`
`
`
`
`
`
`
`
`
`
`
`
7.4
(b, ###1111#)
(c, ###1111#)
(c, ###1111#)
(c, ###1111#)
(c, ###1111#)
(c, ###1111#)
(d, ###11111)
(d, ###11111)
(d, ###11111)
(d, ###11111)
(d, ###11111)
(e, ###11111)
(a, ###11111)
Recursive and Recursively Enumerable Languages
Recursive Languages
A language L ⊆ Σ∗0 is recursive (also called Turing-decidable) iff the characteristic
function χL : Σ∗0 → {Y, N} is Turing–computable, where for each w ∈ Σ∗0 ,
Y, if w ∈ L
χL (w) =
N, otherwise
Example: Let Σ0 = {a}, and let L = {w ∈ Σ∗0 : |w| is even}.
M erases the marks from left to right, with the current parity encoded by the state. Once
a blank at the right is reached, mark Y or N as appropriate.
Geoff Hamilton
CA320: C OMPUTABILITY AND C OMPLEXITY
6
Recursively Enumerable Languages
M accepts a string w if M halts on input w.
• M accepts a language iff M halts on w iff w ∈ L.
• A language is recursively enumerable (also called Turing-acceptable or semidecidable) if there is some Turing machine that accepts it.
Example: Σ0 = {a, b}, L = {w ∈ Σ∗0 : w contains at least one a}.
State
q0
q0
q0
Symbol
a
b
#
δ (State, Symbol)
(q0 , a,Y )
(q0 , b, R)
(q0 , #, R)
Every recursive language is recursively enumerable.
7.5
Combining Turing Machines
Combining Turing Machines
Two Turing machine computations M1 and M2 can be combined into larger machines:
• M1 prepares string as input to M2 .
• M1 passes control to M2 with I/O head at end of input.
• M1 retrieves control when M2 has completed.
Some Simple Machines
Basic machines:
• Symbol-writing machines Ma (one for each symbol a ∈ Σ).
• Head-moving machines R and L move the head appropriately.
These can be combined to create more complex machines:
• If the current symbol a = b, then do M1 , else do M2 (IF(a = b, M1 , M2 ))
• Move head to the right until a blank is found (R# ).
• Find first blank square to left (L# ).
• Copy Machine: Transform w# into w#w# C = IF(a = #, R# , M# R# R# Ma L# L# Ma RC).
• Shift Machine: Transform #w# into w# S = IF(a = #, L, M# LMa RRS)
Geoff Hamilton
CA320: C OMPUTABILITY AND C OMPLEXITY
7
Extensions
The following extensions do not increase the power of Turing Machines.
• 2-way infinite tape
• Multiple tapes
• Multiple heads on one tape
• Two–dimensional “tape”
• Non–determinism
7.6
Unrestricted Grammars
Unrestricted Grammars
The languages which can be recognised by Turing machines are those which can be
described by an unrestricted or free grammar, also known as a rewriting system. This
is a grammar in which every production has the form α → β , where α contains at least
one non-terminal
Example: L = {w ∈ {a, b, c}∗ |w has equal numbers of a’s, b’s and c’s}.
S
S
AB
AC
BC
BA
7.7
→
→
→
→
→
→
ε
ABCS
BA
CA
CB
AB
CA
CB
A
B
C
→
→
→
→
→
AC
BC
a
b
c
Partial Recursive Functions
Partial Recursive Functions
We wish to characterise those functions from N to N which can be computed.
We first of all define the following basic functions:
• zerok (n1 , . . . , nk ) = 0.
• succ(n) = n + 1 for all n ∈ N.
• pi (n1 , . . . , nk ) = ni for 1 ≤ i ≤ n.
We now define a way of composing existing functions to define new ones:
f (x) = h(g1 (x), . . . , gm (x))
Geoff Hamilton
CA320: C OMPUTABILITY AND C OMPLEXITY
8
Primitive Recursion
We can construct a primitive recursive function f from existing functions h and g as
follows:
f (x, 0)
= h(x)
f (x,succ(y))) = g(x, y, f (x, y))
For example:
plus(m, 0)
plus(m,succ(n))
=
=
m
succ(plus(m, n))
mult(m, 0)
mult(m,succ(n))
=
=
zero(m)
plus(m,mult(m, n))
All primitive recursive functions are total recursive functions.
Primitive Recursion
There are computable functions from N to N which are not primitive recursive.
For example, the Ackerman function:
Ack(0, n)
Ack(succ(m),0)
Ack(succ(m),succ(n))
=
=
=
succ(n)
Ack(m,1)
Ack(m,Ack(succ(m),n))
µ-Recursion
Unbounded minimalisation: for a predicate P, the unbounded minimalisation of P is
the function f defined as follows:
f (x) = min{y|P(x, y) is true}
that is, the least value of y that satisfies P(x, y).
This is also denoted as follows:
f (x) = µy.P(x, y) is true
Functions defined in this way are called µ-recursive functions. For example:
minus(x, y) = µz.y + z = x
All partial recursive functions are µ-recursive functions.
Theorem: A function is µ-recursive iff it is computable.
Gödelization
We can convert numbers to strings by using unary notation.
We can convert strings to unique numbers as well.
Assign each character in Σ a unique number - the ith character has value i.
Denote the ith prime number as pi .
Represent string S = si1 si2 . . . sik of length k as:
i
g(si1 si2 . . . sik ) = pi11 pi22 . . . pkk
For example CAT is 23 31 520 .
Now we can use µ-recursive functions to compute functions from strings to strings.
Geoff Hamilton
CA320: C OMPUTABILITY AND C OMPLEXITY
7.8
9
The Church-Turing Thesis
The Church-Turing Thesis
We now have three “general purpose” forms of computation: Turing machines, unrestricted grammars and partial recursive functions.
Each of the three can be transformed into one of the other two. Thus, they are all
“equally powerful.”
Church-Turing Thesis: Turing machines are formal versions of algorithms, and no
computational procedure will be considered an algorithm unless it can be presented as
a Turing machine.
It is called a “thesis” because it is unprovable: it asserts that a certain informal concept
(algorithm) corresponds to a mathematical concept (Turing machine).
How to disprove the Church–Turing Thesis: Devise another model of computation that
does finite work at each step, and which can compute functions not computable by any
Turing machine.
Geoff Hamilton
© Copyright 2026 Paperzz