2018 My Turing Slides Part1
2018 My Turing Slides Part1
(At last!)
Here Arrived
The Big Daddy of all the
machines
The Language Hierarchy
n n n
a b c ? ww ?
Context-Free Languages
n n R
a b ww
Regular Languages
a* a *b *
Fall 2005 Costas Busch - RPI 3
Languages accepted by
Turing Machines
n n n
a b c ww
Context-Free Languages
n n R
a b ww
Regular Languages
a* a *b *
Fall 2005 Costas Busch - RPI 4
Turing Machines (TM)
Recursive Languages
Context-Free Languages
Regular Languages
5
Another Part of the Hierarchy:
Recursive Languages
Context-Sensitive Languages
Context-Free Languages - ε
Regular Languages - ε
6
Recursively enumerable languages are also
known as type 0 languages.
7
A Turing Machine
Tape
...... ......
Read-Write head
Read-Write head
Read-Write head
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
Fall 2005 Costas Busch - RPI 10
The Turing Machine
Time 1
...... a b k c ......
1. Reads a
2. Writes k
3. Moves Left
Fall 2005 Costas Busch - RPI 12
Time 1
...... a b k c ......
Time 2
...... a f k c ......
1. Reads b
2. Writes f
3. Moves Right
Fall 2005 Costas Busch - RPI 13
The Input String
...... a b a c ......
head
...... a b a c ......
head
Infinite tape
1 1 1 0 1 0 b b -----
Read/write head ---
Internal
states/program
16
IPQI-2010-Anu Venugopalan
The deterministic Turing Machine
- the four main elements of the DTM are
2. Tape
4. Program
IPQI-2010-Anu Venugopalan 17
DTM- Finite State Control (FSC)
- Finite State Control
qs : starting state
IPQI-2010-Anu Venugopalan 18
The Tape
- the tape is a one dimensional strip – the tape squares
are labelled and each contains one symbol drawn from
some alphabet
IPQI-2010-Anu Venugopalan 19
The read/write head
- the read/write tape head identifies a single
square on the DTM tape as the square that is
being currently accessed by the machine
IPQI-2010-Anu Venugopalan 20
Differences between finite automata and
Turing machine
1. A Turing machine can both write on the tape and read
from it.
2. The read-write head can move both to the left and to
the right.
3. The tape is infinite.
4. The special states for rejecting and accepting take
immediate effect.
5. The Turing machine may not read all of its input.
Three Possible Outcomes
TM may halt and accept the input
TM may halt and reject the string
TM may not halt at all
States & Transitions
Read Write
Move Left
q1 a b, L q2
Move Right
q1 a b, R q2
Fall 2005 Costas Busch - RPI 23
Example:
Time 1
...... a b a c ......
q1
current state
q1 a b, R q2
Fall 2005 Costas Busch - RPI 24
Time 1
...... a b a c ......
q1
Time 2
...... a b b c ......
q2
q1 a b, R q2
Fall 2005 Costas Busch - RPI 25
Example:
Time 1
...... a b a c ......
q1
Time 2
...... a b b c ......
q2
q1 a b, L q2
Fall 2005 Costas Busch - RPI 26
Example:
Time 1
...... a b a c ......
q1
Time 2
...... a b b c g ......
q2
q1 g, R q2
Fall 2005 Costas Busch - RPI 27
Deterministic Turing Machines
Deterministic Turing Machines
q1 q1
q3 a d, L q3
b d, L
...... a b a c ......
q1
a b, R q2 Allowed:
q1 No transition
for input symbol c
b d, L q3
Fall 2005 Costas Busch - RPI 29
Halting
...... a b a c ......
q1
q1 No transition from q1
HALT!!!
...... a b a c ......
q1
a b, R q2
No possible transition
q1 from q1 and symbol c
b d, L q3 HALT!!!
Fall 2005 Costas Busch - RPI 32
Accepting States
q1 q2 Allowed
q1 q2 Not Allowed
If machine halts
Accept Input
in an accept state
If machine halts
in a non-accept state
Reject Input or
If machine enters
an infinite loop
Fall 2005 Costas Busch - RPI 34
Turing Machine Example
a a, R
, L
q0 q1
q0
a a, R
, L
q0 q1
q0
a a, R
, L
q0 q1
q0
a a, R
, L
q0 q1
q0
a a, R
, L
q0 q1
q1
, L
q0 q1
Time 0 a b a
q0
a a, R
, L
q0 q1
Fall 2005 Costas Busch - RPI 41
Time 1 a b a
q0
No possible Transition
a a, R Halt & Reject
, L
q0 q1
Fall 2005 Costas Busch - RPI 42
Infinite Loop Example
A Turing machine
for language
b b, L
a a, R
, L
q0 q1
q0
b b, L
a a, R
, L
q0 q1
q0
b b, L
a a, R
, L
q0 q1
q0
b b, L
a a, R
, L
q0 q1
Infinite loop
q0
Time 4 a b a
q0
Time 5 a b a
q0
Fall 2005 Costas Busch - RPI 47
Because of the infinite loop:
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 49
Basic Idea:
Match a’s with b’s:
Repeat:
replace leftmost a with x
find leftmost b and replace it with y
Until there are no more a’s or b’s
q0
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 51
Time 1 x a b b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 52
Time 2 x a b b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 53
Time 3 x a y b
q2
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 54
Time 4 x a y b
q2
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 55
Time 5 x a y b
q0
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 56
Time 6 x x y b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 57
Time 7 x x y b
q1
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 58
Time 8 x x y y
q2
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 59
Time 9 x x y y
q2
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 60
Time 10 x x y y
q0
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 61
Time 11 x x y y
q3
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 62
Time 12 x x y y
q3
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 63
Time 13 x x y y
q4
Halt & Accept
q4 y y, R y y, L
y y, R a a, R a a, L
, L
y y, R a x, R b y, L
q3 q0 q1 q2
x x, R
Fall 2005 Costas Busch - RPI 64
Observation:
If we modify the
n n
machine for the language {a b }
q1 a b, R q2
(q1, a ) (q2 , b, R )
q1 c d, L q2
(q1, c) (q2 , d , L)
Input Tape
alphabet alphabet
States
M (Q, , , , q0 , , F )
Transition Accept
function states
Initial blank
Fall 2005
stateCostas Busch - RPI 69
Def:
A Turing Machine is a 7-tuple (Q, Σ, Γ, S, q0, qaccept, qreject),
where Q, Σ, Γ are all finite sets and
1. Q: set of states,
2. Σ: the input alphabet NOT containing the special blank
symbol B,
3. Γ: the tape alphabet, where {B} Γ and Σ Γ,
4. δ:Q×Γ→Q×Γ×{L,R} ~ transition func.
5. q0Q is the start state,
6. qacceptQ is the accept state, and
7. qrejectQ is the reject state, where qreject ≠ qaccept
The Turing Machine
Hopcroft and Ullman define a one-tape Turing machine
formula: M=(Q, Γ, b, ∑, δ, q0, F) where:
c a b a
q1
Instantaneous description: ca q1 ba
q2 q0
q2 q0
Time 6 Time 7
x x y b x x y b
q1 q1
Equivalent notation: q2 xayb xxy q1 b
Input string
w
a a b b
q0
(starting State)
q1 (q2, #,R) (q1,a,L) (q1,b,L)
q2 ---- (q4,#,R) (q3,#,R)
q4
(q1,b,L) ---- ----
(Final State)
L( M ) {w : q0 w x1 q f x2 }
• Deterministic
f (w)
w D f ( w) S
f ( x, y ) x y
Decimal: 5
Binary: 101
Unary: 11111
A function f is computable if
there is a Turing Machine M such that:
q0 w q f f ( w)
Initial Final
Configuration Configuration
x, y are integers
Turing Machine:
Start 1 1 1 0 1 1
q0
initial state
Start 1 1 1 0 1 1
q0 initial state
x y
Finish 1 1 1 1 0
q f final state
Fall 2005 Costas Busch - RPI 93
The 0 helps when we use
the result for other operations
x y
Finish 1 1 1 1 0
q f final state
Fall 2005 Costas Busch - RPI 94
Turing machine for function f ( x, y ) x y
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 95
Execution Example: Time 0
x y
x 11 (2)
1 1 0 1 1
y 11 (2) q0
Final Result
x y
1 1 1 1 0
q4
Fall 2005 Costas Busch - RPI 96
Time 0 1 1 0 1 1
q0
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 97
Time 1 1 1 0 1 1
q0
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 98
Time 2 1 1 0 1 1
q0
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 99
Time 3 1 1 1 1 1
q1
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 100
Time 4 1 1 1 1 1
q1
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 101
Time 5 1 1 1 1 1
q1
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 102
Time 6 1 1 1 1 1
q2
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 103
Time 7 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 104
Time 8 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 105
Time 9 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 106
Time 10 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 107
Time 11 1 1 1 1 0
q3
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
q4
Fall 2005 Costas Busch - RPI 108
Time 12 1 1 1 1 0
q4
1 1, R 1 1, R 1 1, L
q0 0 1, R q1 , L q 1 0, L q3
2
, R
HALT & accept q4
Fall 2005 Costas Busch - RPI 109
Another Example
x is integer
Turing Machine:
Start 1 1 1
q0 initial state
2x
Finish 1 1 1 1 1
q f final state
Fall 2005 Costas Busch - RPI 111
Turing Machine Pseudocode for f ( x) 2 x
• Repeat:
• Find rightmost $, replace it with 1
1 $, R 1 1, L 1 1, R
q0 , L q1 $ 1, R q2
, R
1, L
q3
Fall 2005 Costas Busch - RPI 113
Example
Start Finish
1 1 1 1 1 1
q0 q3
1 $, R 1 1, L 1 1, R
q0 , L q1 $ 1, R q2
, R
1, L
q3
Fall 2005 Costas Busch - RPI 114
Another Example
1 if x y
The function f ( x, y )
0 if x y
is computable
1 if x y
f ( x, y )
0 if x y
Input: x0 y
Output: 1 or 0
Fall 2005 Costas Busch - RPI 116
Turing Machine Pseudocode:
• Repeat
Turing
input output
Machine
x, y
Adder x y
x, y x y
Comparator
x y Eraser 0