You are on page 1of 101

Turing Machines

Fall 2020 M.Anand 1


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 2020 M.Anand 2
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 2020 M.Anand 3
A Turing Machine
Tape
...... ......

Read-Write head
Control Unit

Fall 2020 M.Anand 4


The Tape

No boundaries -- infinite length


...... ......

Read-Write head

The head moves Left or Right

Fall 2020 M.Anand 5


...... ......

Read-Write head

The head at each transition (time step):

1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
Fall 2020 M.Anand 6
Example:
Time 0
...... a b a c ......

Time 1
...... a b k c ......

1. Reads a
2. Writes k
3. Moves Left
Fall 2020 M.Anand 7
Time 1
...... a b k c ......

Time 2
...... a f k c ......

1. Reads b
2. Writes f
3. Moves Right
Fall 2020 M.Anand 8
The Input String

Input string Blank symbol

......   a b a c    ......

head

Head starts at the leftmost position


of the input string
Fall 2020 M.Anand 9
States & Transitions

Read Write
Move Left

q1 a  b, L q2

Move Right

q1 a  b, R q2
Fall 2020 M.Anand 10
Example:
Time 1
......   a b a c    ......

q1
current state

q1 a  b, R q2
Fall 2020 M.Anand 11
Time 1
......   a b a c    ......

q1

Time 2
......   a b b c    ......

q2

q1 a  b, R q2
Fall 2020 M.Anand 12
Example:
Time 1
......   a b a c    ......

q1

Time 2
......   a b b c    ......

q2

q1 a  b, L q2
Fall 2020 M.Anand 13
Example:
Time 1
......   a b a c    ......

q1

Time 2
......   a b b c g   ......

q2

q1   g, R q2
Fall 2020 M.Anand 14
Determinism
Turing Machines are deterministic

Allowed Not Allowed


a  b, R q2 a  b, R q2

q1 q1
q3 a  d, L q3
b  d, L

No lambda transitions allowed


Fall 2020 M.Anand 15
Partial Transition Function
Example:

......   a b a c    ......

q1

a  b, R q2 Allowed:

q1 No transition
for input symbol c
b  d, L q3
Fall 2020 M.Anand 16
Halting

The machine halts in a state if there is


no transition to follow

Fall 2020 M.Anand 17


Halting Example 1:

......   a b a c    ......

q1

q1 No transition from q1
HALT!!!

Fall 2020 M.Anand 18


Halting Example 2:

......   a b a c    ......

q1

a  b, R q2
No possible transition
q1 from q1 and symbol c
b  d, L q3 HALT!!!
Fall 2020 M.Anand 19
Accepting States

q1 q2 Allowed

q1 q2 Not Allowed

•Accepting states have no outgoing transitions


•The machine halts and accepts

Fall 2020 M.Anand 20


Acceptance

If machine halts
Accept Input
string in an accept state

If machine halts
in a non-accept state
Reject Input or
string
If machine enters
an infinite loop
Fall 2020 M.Anand 21
Observation:
In order to accept an input string,
it is not necessary to scan all the
symbols in the string

Fall 2020 M.Anand 22


Turing Machine Example

Input alphabet   {a , b }

Accepts the language: a*


a  a, R

  , L
q0 q1

Fall 2020 M.Anand 23


Time 0   a a a  

q0

a  a, R

  , L
q0 q1

Fall 2020 M.Anand 24


Time 1   a a a  

q0

a  a, R

  , L
q0 q1

Fall 2020 M.Anand 25


Time 2   a a a  

q0

a  a, R

  , L
q0 q1

Fall 2020 M.Anand 26


Time 3   a a a  

q0

a  a, R

  , L
q0 q1

Fall 2020 M.Anand 27


Time 4   a a a  

q1

a  a, R Halt & Accept

  , L
q0 q1

Fall 2020 M.Anand 28


Rejection Example

Time 0   a b a  

q0

a  a, R

  , L
q0 q1
Fall 2020 M.Anand 29
Time 1   a b a  

q0

No possible Transition
a  a, R Halt & Reject

  , L
q0 q1
Fall 2020 M.Anand 30
A simpler machine for same language
but for input alphabet   {a }

Accepts the language: a*

q0

Fall 2020 M.Anand 31


Time 0   a a a  

q0

Halt & Accept

q0

Not necessary to scan input

Fall 2020 M.Anand 32


Infinite Loop Example
A Turing machine
for language a * b( a  b) *

b  b, L
a  a, R

  , L
q0 q1

Fall 2020 M.Anand 33


Time 0   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

Fall 2020 M.Anand 34


Time 1   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

Fall 2020 M.Anand 35


Time 2   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

Fall 2020 M.Anand 36


Time 2   a b a  
q0
Time 3   a b a  

Infinite loop
q0

Time 4   a b a  
q0

Time 5   a b a  
q0
Fall 2020 M.Anand 37
Because of the infinite loop:

•The accepting state cannot be reached

•The machine never halts

•The input string is rejected

Fall 2020 M.Anand 38


Another Turing Machine Example
n n
Turing machine for the language {a b }
n 1

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 2020 M.Anand 39
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

If there is a remaining a or b reject

Fall 2020 M.Anand 40


Time 0  a a b 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 2020 M.Anand 41
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 2020 M.Anand 42
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 2020 M.Anand 43
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 2020 M.Anand 44
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 2020 M.Anand 45
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 2020 M.Anand 46
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 2020 M.Anand 47
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 2020 M.Anand 48
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 2020 M.Anand 49
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 2020 M.Anand 50
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 2020 M.Anand 51
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 2020 M.Anand 52
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 2020 M.Anand 53
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 2020 M.Anand 54
Observation:

If we modify the
n n
machine for the language {a b }

we can easily construct


n n n
a machine for the language {a b c }

Fall 2020 M.Anand 55


Formal Definitions
for
Turing Machines

Fall 2020 M.Anand 56


Transition Function

q1 a  b, R q2

 (q1, a )  (q2 , b, R )

Fall 2020 M.Anand 57


Transition Function

q1 c  d, L q2

 (q1, c)  (q2 , d , L)

Fall 2020 M.Anand 58


Turing Machine:

Input Tape
alphabet alphabet
States

M  (Q, , ,  , q0 , , F )

Transition Accept
function states
Initial blank
Fall 2020
state M.Anand 59
Configuration

  c a b a  

q1

Instantaneous description: ca q1 ba

Fall 2020 M.Anand 60


Time 4 Time 5
 x a y b    x a y b  

q2 q0

A Move: q2 xayb  x q0 ayb


(yields in one mode)

Fall 2020 M.Anand 61


Time 4 Time 5
 x a y b    x a y b  

q2 q0

Time 6 Time 7
 x x y b    x x y b  

q1 q1
A computation
q2 xayb  x q0 ayb  xx q1 yb  xxy q1 b
Fall 2020 M.Anand 62
q2 xayb  x q0 ayb  xx q1 yb  xxy q1 b


Equivalent notation: q2 xayb  xxy q1 b

Fall 2020 M.Anand 63


Initial configuration: q0 w

Input string
w

 a a b b  

q0

Fall 2020 M.Anand 64


The Accepted Language

For any Turing Machine M


L( M )  {w : q0 w  x1 q f x2 }

Initial state Accept state

Fall 2020 M.Anand 65


If a language L is accepted
by a Turing machine M
then we say that L is:

•Turing Recognizable

Other names used:


•Turing Acceptable
•Recursively Enumerable

Fall 2020 M.Anand 66


Computing Functions
with
Turing Machines

Fall 2020 M.Anand 67


A function f (w) has:

Domain: D Result Region: S

f (w)
w D f ( w)  S

Fall 2020 M.Anand 68


A function may have many parameters:

Example: Addition function

f ( x, y )  x  y

Fall 2020 M.Anand 69


Integer Domain

Decimal: 5

Binary: 101

Unary: 11111

We prefer unary representation:

easier to manipulate with Turing machines


Fall 2020 M.Anand 70
Definition:

A function f is computable if
there is a Turing Machine M such that:

Initial configuration Final configuration


 w   f (w) 

q0 qf
initial state accept state

For all w D Domain


Fall 2020 M.Anand 71
In other words:

A function f is computable if
there is a Turing Machine M such that:


q0 w  q f f ( w)

Initial Final
Configuration Configuration

For all w D Domain


Fall 2020 M.Anand 72
Example

The function f ( x, y )  x  y is computable

x, y are integers

Turing Machine:

Input string: x0 y unary

Output string: xy 0 unary

Fall 2020 M.Anand 73


x y

Start  1 1  1 0 1  1 

q0
initial state

The 0 is the delimiter that


separates the two numbers

Fall 2020 M.Anand 74


x y

Start  1 1  1 0 1  1 

q0 initial state

x y

Finish  1 1  1 1 0 

q f final state
Fall 2020 M.Anand 75
The 0 here helps when we use
the result for other operations

x y

Finish  1 1  1 1 0 

q f final state
Fall 2020 M.Anand 76
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 2020 M.Anand 77
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 2020 M.Anand 78
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 2020 M.Anand 79
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 2020 M.Anand 80
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 2020 M.Anand 81
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 2020 M.Anand 82
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 2020 M.Anand 83
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 2020 M.Anand 84
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 2020 M.Anand 85
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 2020 M.Anand 86
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 2020 M.Anand 87
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 2020 M.Anand 88
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 2020 M.Anand 89
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 2020 M.Anand 90
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 2020 M.Anand 91
Another Example

The function f ( x)  2 x is computable

x is integer

Turing Machine:

Input string: x unary

Output string: xx unary


Fall 2020 M.Anand 92
x

Start  1 1  1 

q0 initial state

2x

Finish  1 1  1 1 1 

q f accept state
Fall 2020 M.Anand 93
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

Fall 2020 M.Anand 94


Turing Machine for f ( x)  2 x

1  $, R 1  1, L 1  1, R

q0   , L q1 $  1, R q2
  , R   1, L
q3
Fall 2020 M.Anand 95
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 2020 M.Anand 96
Another Example

1 if x y
The function f ( x, y ) 
is computable 0 if x y

Input: x0 y

Output: 1 or 0
Fall 2020 M.Anand 97
Turing Machine Pseudocode:

• Repeat

Match a 1 from x with a 1 from y

Until all of x or y is matched

• If a 1 from x is not matched


erase tape, write 1 ( x  y)
else
Fall 2020
erase tape, write 0
M.Anand
( x  y) 98
Combining Turing Machines

Fall 2020 M.Anand 99


Block Diagram

Turing
input output
Machine

Fall 2020 M.Anand 100


Example: x  y if x  y
f ( x, y ) 
0 if x  y

x, y
Adder x y
x, y x y
Comparator

x y Eraser 0

Fall 2020 M.Anand 101

You might also like