Lesson 15 A Turing Machine

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 87

Turing Machines

A Turing Machine
Tape
...... ......

Read-Write head
Control Unit

Courtesy Costas Busch - RPI 2


The Tape

No boundaries -- infinite length


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

Read-Write head

The head moves Left or Right

Courtesy Costas Busch - RPI 3


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

Read-Write head

The head at each transition (time step):

1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
Courtesy Costas Busch - RPI 4
Example:
Time 0
...... a b a c ......

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

1. Reads a
2. Writes k
3. Moves Left
Courtesy Costas Busch - RPI 5
Time 1
...... a b k c ......

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

1. Reads b
2. Writes f
3. Moves Right
Courtesy Costas Busch - RPI 6
The Input String

Input string Blank symbol

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

head

Head starts at the leftmost position


of the input string
Courtesy Costas Busch - RPI 7
States & Transitions

Read Write
Move Left

q1 a  b, L q2

Move Right

q1 a  b, R q2
Courtesy Costas Busch - RPI 8
Example:
Time 1
......   a b a c    ......

q1
current state

q1 a  b, R q2
Courtesy Costas Busch - RPI 9
Time 1
......   a b a c    ......

q1

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

q2

q1 a  b, R q2
Courtesy Costas Busch - RPI 10
Example:
Time 1
......   a b a c    ......

q1

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

q2

q1 a  b, L q2
Courtesy Costas Busch - RPI 11
Example:
Time 1
......   a b a c    ......

q1

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

q2

q1   g, R q2
Courtesy Costas Busch - RPI 12
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

Courtesy Costas Busch - RPI 13


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
Courtesy Costas Busch - RPI 14
Halting

The machine halts if there are


no possible transitions to follow

Courtesy Costas Busch - RPI 15


Example:

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

q1

a  b, R q2
No possible transition
q1
HALT!!!
b  d, L q3
Courtesy Costas Busch - RPI 16
Final States

q1 q2 Allowed

q1 q2 Not Allowed

• Final states have no outgoing transitions

• In a final stateCourtesy
the machine
Costas Busch - RPI
halts 17
Acceptance

If machine halts
Accept Input
in a final state

If machine halts
in a non-final state
Reject Input or
[If machine enters
an infinite loop ]

Courtesy Costas Busch - RPI 18


Turing Machine Example

A Turing machine that accepts the language:

a*
a  a, R

  , L
q0 q1

Courtesy Costas Busch - RPI 19


Time 0   a a a  

q0

a  a, R

  , L
q0 q1

Courtesy Costas Busch - RPI 20


Time 1   a a a  

q0

a  a, R

  , L
q0 q1

Courtesy Costas Busch - RPI 21


Time 2   a a a  

q0

a  a, R

  , L
q0 q1

Courtesy Costas Busch - RPI 22


Time 3   a a a  

q0

a  a, R

  , L
q0 q1

Courtesy Costas Busch - RPI 23


Time 4   a a a  

q1

a  a, R Halt & Accept

  , L
q0 q1

Courtesy Costas Busch - RPI 24


Rejection Example

Time 0   a b a  

q0

a  a, R

  , L
q0 q1
Courtesy Costas Busch - RPI 25
Time 1   a b a  

q0

No possible Transition
a  a, R Halt & Reject

  , L
q0 q1
Courtesy Costas Busch - RPI 26
Draw TM that accept these languages.

1){ all strings in {a,b}* with prefix ab }


2) { all binary strings containing substring001 }
3){ all binary strings without substring 001 }

4){a n
b : n  0} 
5) awa : w  a , b  * 
6){ } 7){a , b } * 8){  }

9){x : x  {a}* and | x | is even} --DO--


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

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
Courtesy Costas Busch - RPI 28
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

Courtesy Costas Busch - RPI 29


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
Courtesy Costas Busch - RPI 30
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
Courtesy Costas Busch - RPI 31
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
Courtesy Costas Busch - RPI 32
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
Courtesy Costas Busch - RPI 33
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
Courtesy Costas Busch - RPI 34
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
Courtesy Costas Busch - RPI 35
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
Courtesy Costas Busch - RPI 36
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
Courtesy Costas Busch - RPI 37
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
Courtesy Costas Busch - RPI 38
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
Courtesy Costas Busch - RPI 39
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
Courtesy Costas Busch - RPI 40
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
Courtesy Costas Busch - RPI 41
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
Courtesy Costas Busch - RPI 42
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
Courtesy Costas Busch - RPI 43
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 }

Courtesy Costas Busch - RPI 44


Activity
Time
Write basic idea of TM for {v.v:
v Є {a,b}*}
Turing Machine:

Input Tape
alphabet alphabet
States

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

Transition Final
function Initial states
state blank
Courtesy Costas Busch - RPI 46
Turing Machine:

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


  ,   
q0  Q
F Q

Courtesy Costas Busch - RPI 47


Transition Function

q1 a  b, R q2

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

Courtesy Costas Busch - RPI 48


Transition Function

q1 c  d, L q2

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

Courtesy Costas Busch - RPI 49


Configuration

  c a b a  

q1

Instantaneous description: ca q1 ba

Courtesy Costas Busch - RPI 50


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

q2 q0

A Move: q2 xayb  x q0 ayb

Courtesy Costas Busch - RPI 51


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
Courtesy Costas Busch - RPI 52
q2 xayb  x q0 ayb  xx q1 yb  xxy q1 b


Equivalent notation: q2 xayb  xxy q1 b

Courtesy Costas Busch - RPI 53


Initial configuration: q0 w

Input string
w

 a a b b  

q0

Courtesy Costas Busch - RPI 54


The Accepted Language

For any Turing Machine M


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

Initial state Final state

Courtesy Costas Busch - RPI 55


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

Courtesy Costas Busch - RPI 56


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 57


Computing Functions
with
Turing Machines
A function f (w) has:

Domain: D Result Region: S

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

Courtesy Costas Busch - RPI 59


A function may have many parameters:

Example: Addition function

f ( x, y )  x  y

Courtesy Costas Busch - RPI 60


Integer Domain

Decimal: 5

Binary: 101

Unary: 11111

We prefer unary representation:

easier to manipulate with Turing machines


Courtesy Costas Busch - RPI 61
Definition:

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

Initial configuration Final configuration


 w   f (w) 

q0 initial state q f final state

For all w D Domain


Courtesy Costas Busch - RPI 62
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


Courtesy Costas Busch - RPI 63
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

Courtesy Costas Busch - RPI 64


x y

Start  1 1  1 0 1  1 

q0
initial state

The 0 is the delimiter that


separates the two numbers

Courtesy Costas Busch - RPI 65


x y

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 66
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 67
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
Courtesy Costas Busch - RPI 68
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
Courtesy Costas Busch - RPI 69
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
Courtesy Costas Busch - RPI 70
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
Courtesy Costas Busch - RPI 71
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
Courtesy Costas Busch - RPI 72
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
Courtesy Costas Busch - RPI 73
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
Courtesy Costas Busch - RPI 74
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
Courtesy Costas Busch - RPI 75
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
Courtesy Costas Busch - RPI 76
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
Courtesy Costas Busch - RPI 77
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
Courtesy Costas Busch - RPI 78
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
Courtesy Costas Busch - RPI 79
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
Courtesy Costas Busch - RPI 80
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
Courtesy Costas Busch - RPI 81
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
Courtesy Costas Busch - RPI 82
Another Example

The function f ( x)  2 x is computable

x is integer

Turing Machine:

Input string: x unary

Output string: xx unary


Courtesy Costas Busch - RPI 83
x

Start  1 1  1 

q0 initial state

2x

Finish  1 1  1 1 1 

q f final state
Courtesy Costas Busch - RPI 84
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 85


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
Courtesy Costas Busch - RPI 86
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
Courtesy Costas Busch - RPI 87

You might also like