Turing Machines
Courtesy Costas Busch - RPI
1
The Language Hierarchy
ww ?
n n n ?
a b c
Context-Free Languages
n n
R
a b
ww
Regular Languages
a*
a *b *
Courtesy Costas Busch - RPI
2
Languages accepted by
Turing Machines
ww
n n n
a b c
Context-Free Languages
n n
R
a b
ww
Regular Languages
a*
a *b *
Courtesy Costas Busch - RPI
3
Tape
......
A Turing Machine
......
Read-Write head
Control Unit
Courtesy Costas Busch - RPI
4
The Tape
No boundaries -- infinite length
......
......
Read-Write head
The head moves Left or Right
Courtesy Costas Busch - RPI
5
......
......
Read-Write head
The head at each time step:
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
Courtesy Costas Busch - RPI
6
Example:
Time 0
......
a b a c
Time 1
......
1. Reads
2. Writes
a b k c
......
......
a
k
3. Moves Left
Courtesy Costas Busch - RPI
7
Time 1
......
a b k c
Time 2
......
1. Reads
2. Writes
a f k c
......
......
b
f
3. Moves Right
Courtesy Costas Busch - RPI
8
The Input String
Input string
......
a b a c
Blank symbol
......
head
Head starts at the leftmost position
of the input string
Courtesy Costas Busch - RPI
9
Input string
......
a b a c
Blank symbol
......
head
Remark: the input string is never empty
Courtesy Costas Busch - RPI
10
States & Transitions
Read
q1
Write
a b, L
Move Left
q2
Move Right
q1
a b, R
Courtesy Costas Busch - RPI
q2
11
Example:
Time 1
......
a b a c
......
q1
current state
q1
a b, R
q2
Courtesy Costas Busch - RPI
12
......
Time 1
a b a c
......
......
q1
......
Time 2
a b b c
q2
q1
a b, R
q2
Courtesy Costas Busch - RPI
13
Example:
......
Time 1
a b a c
......
......
q1
......
Time 2
a b b c
q2
q1
a b, L
q2
Courtesy Costas Busch - RPI
14
Example:
......
Time 1
a b a c
......
q1
......
Time 2
a b b c g
......
q2
q1
g, R
q2
Courtesy Costas Busch - RPI
15
Determinism
Turing Machines are deterministic
Not Allowed
Allowed
a b, R
q2
a b, R
q2
a d, L
q3
q1
q1
b d, L
q3
No lambda transitions allowed
Courtesy Costas Busch - RPI
16
Partial Transition Function
Example:
......
a b a c
......
q1
a b, R
q2
q1
b d, L
q3
Allowed:
No transition
for input symbol
Courtesy Costas Busch - RPI
c
17
Halting
The machine halts if there are
no possible transitions to follow
Courtesy Costas Busch - RPI
18
Example:
......
a b a c
......
q1
a b, R
q2
No possible transition
q1
b d, L
q3
Courtesy Costas Busch - RPI
HALT!!!
19
Final States
q1
q2
Allowed
q1
q2
Not Allowed
• Final states have no outgoing transitions
• In a final state the machine halts
Courtesy Costas Busch - RPI
20
Acceptance
Accept Input
If machine halts
in a final state
Reject Input
If machine halts
in a non-final state
or
If machine enters
an infinite loop
Courtesy Costas Busch - RPI
21
Turing Machine Example
A Turing machine that accepts the language:
aa *
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
22
Time 0
a a a
q0
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
23
Time 1
a a a
q0
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
24
Time 2
a a a
q0
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
25
Time 3
a a a
q0
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
26
Time 4
a a a
q1
a a, R
q0
Halt & Accept
, L
Courtesy Costas Busch - RPI
q1
27
Rejection Example
Time 0
a b a
q0
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
28
Time 1
a b a
q0
No possible Transition
Halt & Reject
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
29
Infinite Loop Example
A Turing machine
for language aa * b( a b) *
b b, L
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
30
Time 0
a b a
q0
b b, L
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
31
Time 1
a b a
q0
b b, L
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
32
Time 2
a b a
q0
b b, L
a a, R
q0
, L
Courtesy Costas Busch - RPI
q1
33
Time 2
a b a
q0
a b a
q0
Time 4
a b a
q0
Time 5
a b a
q0
Courtesy Costas Busch - RPI
Infinite loop
Time 3
34
Because of the infinite loop:
•The final state cannot be reached
•The machine never halts
•The input is not accepted
Courtesy Costas Busch - RPI
35
Another Turing Machine Example
Turing machine for the language
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
n n
{a b }
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
36
a a b b
Time 0
q0
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
37
x a b b
Time 1
q1
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
38
x a b b
Time 2
q1
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
39
x a y b
Time 3
q2
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
40
x a y b
Time 4
q2
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
41
x a y b
Time 5
q0
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
42
x x y b
Time 6
q1
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
43
x x y b
Time 7
q1
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
44
x x y y
Time 8
q2
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
45
x x y y
Time 9
q2
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
46
x x y y
Time 10
q0
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
47
x x y y
Time 11
q3
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
48
x x y y
Time 12
q3
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
49
x x y y
Time 13
q4
Halt & Accept
y y, R
q3
q4
, L
y y, R
q0
y y, R
a a, R
a x, R
q1
y y, L
a a, L
b y, L
q2
x x, R
Courtesy Costas Busch - RPI
50
Observation:
If we modify the
machine for the language
we can easily construct
a machine for the language
Courtesy Costas Busch - RPI
n n
{a b }
n n n
{a b c }
51
Formal Definitions
for
Turing Machines
Courtesy Costas Busch - RPI
52
Transition Function
q1
a b, R
q2
(q1, a) (q2 , b, R)
Courtesy Costas Busch - RPI
53
Transition Function
q1
c d, L
q2
(q1, c) (q2 , d , L)
Courtesy Costas Busch - RPI
54
Turing Machine:
States
Input
alphabet
Tape
alphabet
M (Q, , , , q0 , , F )
Transition
function
,
q0 Q
Initial
blank
F
Q
state
Courtesy Costas Busch - RPI
Final
states
55
Configuration
c a b a
q1
Instantaneous description:
Courtesy Costas Busch - RPI
ca q1 ba
56
Time 4
Time 5
x a y b
x a y b
q0
q2
A Move:
q2 xayb x q0 ayb
Courtesy Costas Busch - RPI
57
Time 4
Time 5
x a y b
x a y b
q0
q2
Time 6
Time 7
x x y b
x x y b
q1
q1
q2 xayb x q0 ayb xx q1 yb xxy q1 b
Courtesy Costas Busch - RPI
58
q2 xayb x q0 ayb xx q1 yb xxy q1 b
Equivalent notation:
q2 xayb xxy q1 b
Courtesy Costas Busch - RPI
59
Initial configuration:
q0 w
Input string
w
a a b b
q0
Courtesy Costas Busch - RPI
60
The Accepted Language
For any Turing Machine
L( M ) {w :
M
q0 w x1 q f x2 }
Initial state
Courtesy Costas Busch - RPI
Final state
61
Standard Turing Machine
The machine we described is the standard:
• Deterministic
• Infinite tape in both directions
•Tape is the input/output file
Courtesy Costas Busch - RPI
62
Computing Functions
with
Turing Machines
Courtesy Costas Busch - RPI
63
A function
Domain:
f (w)
Result Region:
D
f (w)
w D
has:
S
f ( w) S
Courtesy Costas Busch - RPI
64
A function may have many parameters:
Example:
Addition function
f ( x, y ) x y
Courtesy Costas Busch - RPI
65
Integer Domain
Decimal:
5
Binary:
101
Unary:
11111
We prefer unary representation:
easier to manipulate with Turing machines
Courtesy Costas Busch - RPI
66
Definition:
f
A function
is computable if
there is a Turing Machine M such that:
Initial configuration
w
Final configuration
q0 initial state
For all
w D
f (w)
q f final state
Domain
Courtesy Costas Busch - RPI
67
In other words:
f
A function
is computable if
there is a Turing Machine M such that:
q0 w q f f ( w)
Initial
Configuration
For all
Final
Configuration
w D
Domain
Courtesy Costas Busch - RPI
68
Example
The function
f ( x, y ) x y is computable
x, y
are integers
Turing Machine:
Input string:
x0 y
unary
Output string:
xy0
unary
Courtesy Costas Busch - RPI
69
y
x
Start
1 1
1 0 1 1
q0
initial state
The 0 is the delimiter that
separates the two numbers
Courtesy Costas Busch - RPI
70
y
x
Start
1 1
1 0 1 1
q0 initial state
x y
Finish
1 1
1 1 0
q f final state
Courtesy Costas Busch - RPI
71
The 0 helps when we use
the result for other operations
x y
Finish
1 1
1 1 0
q f final state
Courtesy Costas Busch - RPI
72
Turing machine for function
1 1, R
f ( x, y ) x y
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
73
Execution Example:
x 11 (2)
y 11
(2)
Time 0
y
x
1 1 0 1 1
q0
Final Result
x y
1 1 1 1 0
q4
Courtesy Costas Busch - RPI
74
Time 0
1 1 0 1 1
q0
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
75
Time 1
1 1 0 1 1
q0
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
76
Time 2
1 1 0 1 1
q0
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
77
Time 3
1 1 1 1 1
q1
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
78
Time 4
1 1 1 1 1
q1
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
79
Time 5
1 1 1 1 1
q1
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
80
Time 6
1 1 1 1 1
q2
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
81
Time 7
1 1 1 1 0
q3
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
82
Time 8
1 1 1 1 0
q3
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
83
Time 9
1 1 1 1 0
q3
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
84
Time 10
1 1 1 1 0
q3
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
85
Time 11
1 1 1 1 0
q3
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
Courtesy Costas Busch - RPI
q4
86
Time 12
1 1 1 1 0
q4
1 1, R
1 1, R
1 1, L
,
L
0
1
,
R
1
0
,
L
q
q0
q3
q1
2
, R
HALT & accept
Courtesy Costas Busch - RPI
q4
87
Another Example
The function
f ( x) 2 x
x
is computable
is integer
Turing Machine:
Input string:
Output string:
x
unary
xx
unary
Courtesy Costas Busch - RPI
88
x
Start
1 1
1
q0 initial state
2x
Finish
1 1
1 1 1
q f final state
Courtesy Costas Busch - RPI
89
Turing Machine Pseudocode for
f ( x) 2 x
• Replace every 1 with $
• Repeat:
• Find rightmost $, replace it with 1
• Go to right end, insert 1
Until no more $ remain
Courtesy Costas Busch - RPI
90
Turing Machine for
1 $, R
f ( x) 2 x
1 1, L
1 1, R
q0 , L q1 $ 1, R
, R
q3
q2
1, L
Courtesy Costas Busch - RPI
91
Start
Example
1 1
Finish
1 1 1 1
q0
q3
1 $, R
1 1, L
1 1, R
q0 , L q1 $ 1, R
, R
q3
q2
1, L
Courtesy Costas Busch - RPI
92
Another Example
The function
f ( x, y )
is computable
Courtesy Costas Busch - RPI
1
if
x y
0
if
x y
93
Turing Machine for
f ( x, y )
Input:
Output:
1
if
x y
0
if
x y
x0 y
1
or
0
Courtesy Costas Busch - RPI
94
Turing Machine Pseudocode:
• Repeat
Match a 1 from
Until all of
x with a 1 from y
x or y is matched
• If a 1 from x is not matched
erase tape, write 1
else
erase tape, write 0
Courtesy Costas Busch - RPI
( x y)
( x y)
95
Combining Turing Machines
Courtesy Costas Busch - RPI
96
Block Diagram
input
Turing
Machine
Courtesy Costas Busch - RPI
output
97
Example:
x y if x y
f ( x, y)
if x y
0
x, y
x, y
Comparer
Adder
x y
Eraser
0
x y
x y
Courtesy Costas Busch - RPI
98
© Copyright 2025 Paperzz