Sei sulla pagina 1di 100

COMP 335:

Turing Machines

N. Houari, PhD, P.Eng.


Fall 2015
Reading Chap#9

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 *
COMP 335 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 *
COMP 335 3
A Turing Machine
Tape
...... ......

Read-Write head
Transition diagram -- program

COMP 335 4
The Tape

No boundaries; use as many cells as needed!


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

Read-Write head

The head moves 1 cell to the Left or the Right

COMP 335 5
...... ......

Read-Write head

At each step of a TM computation, it


1. Reads the symbol in the cell under the head
2. Replaces that symbol with a symbol
3. Moves 1 cell to the Left or to the Right

COMP 335 6
Example:
Time 0
...... a b a c ......

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

1. Reads a
2. Writes k
3. Moves Left
COMP 335 7
Time 1
...... a b k c ......

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

1. Reads b
2. Writes f
3. Moves Right
COMP 335 8
Example Input String: “abac”

Blank symbol Input string Blank symbol

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

head
Convention: When a TM begins its execution,
the R/W head is under the leftmost symbol
in the input
COMP 335 9
Input string Blank symbol

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

head

Note: the input string is never empty

COMP 335 10
States & Transitions

Read Write
Move Left

q1 a  b, L q2

Move Right

q1 a  b, R q2
COMP 335 11
Example:
Time 1
......   a b a c    ......

q1
current state

q1 a  b, R q2
COMP 335 12
Time 1
......   a b a c    ......

q1

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

q2

q1 a  b, R q2
COMP 335 13
Example:
Time 1
......   a b a c    ......

q1

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

q2

q1 a  b, L q2
COMP 335 14
Example:
Time 1
......   a b a c    ......

q1

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

q2

q1   g, R q2
COMP 335 15
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


COMP 335 16
Partial Transition Function
Example:

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

q1

a  b, R q2 Allowed:
There is no transition
q1
in q1 for the input
b  d, L q3 symbol c
COMP 335 17
Halting

Turing machine halts in a state


if there is no possible transition
to follow

COMP 335 18
Example:

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

q1

a  b, R q2
No possible transition
q1
HALT!!!
b  d, L q3
COMP 335 19
Final States

q1 q2 Allowed

q1 q2 Not Allowed

• Final states have no outgoing transitions

• In a final state, the TM halts


COMP 335 20
Acceptance

If TM halts
Accept Input
in a final state

If TM halts in a
non-final state
Reject Input or
if it enters an
infinite loop ?!
COMP 335 21
Turing Machine; An Example

Ex. a Turing machine for the language L(a*)

a  a, R

  , L
q0 q1

COMP 335 22
Time 0   a a a  

q0

a  a, R

  , L
q0 q1

COMP 335 23
Time 1   a a a  

q0

a  a, R

  , L
q0 q1

COMP 335 24
Time 2   a a a  

q0

a  a, R

  , L
q0 q1

COMP 335 25
Time 3   a a a  

q0

a  a, R

  , L
q0 q1

COMP 335 26
Time 4   a a a  

q1

a  a, R Halt & Accept

  , L
q0 q1

COMP 335 27
Rejection Example

Time 0   a b a  

q0

a  a, R

  , L
q0 q1
COMP 335 28
Time 1   a b a  

q0
No possible Transition
a  a, R Halt & Reject

  , L
q0 q1
COMP 335 29
Infinite Loop Example

b  b, L
a  a, R

  , L
q0 q1

COMP 335 30
Time 0   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

COMP 335 31
Time 1   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

COMP 335 32
Time 2   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

COMP 335 33
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
COMP 335 34
Because of the infinite loop:

•The final state cannot be reached

•The machine never halts

•The input is not accepted

COMP 335 35
Another Example

A Turing machine for {a b : n  1}


n n

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
COMP 335 36
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
COMP 335 37
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
COMP 335 38
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
COMP 335 39
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
COMP 335 40
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
COMP 335 41
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
COMP 335 42
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
COMP 335 43
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
COMP 335 44
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
COMP 335 45
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
COMP 335 46
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
COMP 335 47
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
COMP 335 48
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
COMP 335 49
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
COMP 335 50
Observation:

We can easily modify the machine


for the language {a b : n  1}
n n

to construct a TM for
the language {a nb n c n : n  1}

COMP 335 51
Formal Definitions
for
Turing Machines

COMP 335 52
Transition Function

q1 a  b, R q2

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

COMP 335 53
Transition Function

q1 c  d, L q2

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

COMP 335 54
Turing Machine:

Input Tape
alphabet alphabet
States

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

Transition Final
function states
Initial Blank in Γ
state COMP 335 55
Configuration

  c a b a  

q1

Instantaneous description: ca q1 ba

COMP 335 56
Time 4 Time 5
 x a y b    x a y b  

q2 q0

A Move: q2 xayb  x q0 ayb

COMP 335 57
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

q2 xayb  x q0 ayb  xx q1 yb  xxy q1 b


COMP 335 58
q2 xayb  x q0 ayb  xx q1 yb  xxy q1 b


Equivalent notation: q2 xayb  xxy q1 b

COMP 335 59
Initial configuration: q0 w

Input string

 a a b b  

q0

COMP 335 60
The Accepted Language

For any Turing Machine M


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

The initial state A final state

COMP 335 61
Standard Turing Machine

The Turing machine described is the standard:

• Deterministic

• Infinite tape in both directions

•Tape is the input/output file

COMP 335 62
Computing Functions
with
Turing Machines

COMP 335 63
A function f (w) has:

Domain: D Range: S

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

COMP 335 64
A function may have several
parameters / arguments:
Example: The Addition function:

f ( x, y )  x  y

COMP 335 65
Integer Domain

Decimal: 5

Binary: 101

Unary: 11111

We prefer unary representation:

easier to manipulate with Turing machines


COMP 335 66
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

COMP 335 67
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

w D (the domain)


COMP 335 68
Example

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


where x, y are integers

A Turing Machine M for f:

Input string: x0 y unary

Output string: xy0 unary

COMP 335 69
x y

Start  1 1  1 0 1  1 

q0
initial state

The 0 is the delimiter that


separates the inputs, x and y

COMP 335 70
x y

Start  1 1  1 0 1  1 

q0 initial state

x y

Finish  1 1  1 1 0 
q f final state
COMP 335 71
The 0 here may help when we use
the result for other operations

x y

Finish  1 1  1 1 0 
q f final state
COMP 335 72
Turing machine M for the 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
COMP 335 73
Execution Example: Input (time 0)
x y
x  11 (2)
 1 1 0 1 1 
y  11 (2)
q0

Output (final result)


x y
 1 1 1 1 0 

q4
COMP 335 74
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
COMP 335 75
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
COMP 335 76
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
COMP 335 77
Time 3  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
COMP 335 78
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
COMP 335 79
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
COMP 335 80
Time 6  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
COMP 335 81
Time 7  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
COMP 335 82
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
COMP 335 83
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
COMP 335 84
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
COMP 335 85
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
COMP 335 86
Time 12  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
COMP 335 87
Time 13  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
COMP 335 88
Another Example

The function f ( x)  2 x is computable

x is integer

Turing Machine:

Input string: x unary

Output string: xx unary


COMP 335 89
x

Start  1 1  1 

q0 initial state

2x

Finish  1 1  1 1 1 
q f final state
COMP 335 90
Turing Machine Pseudocode for f ( x)  2 x

• Replace every 1 with $

• Repeat:
• Find rightmost $, replace it with 1

• Go to right end, then insert 1

Until no more $ remains

COMP 335 91
A 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
COMP 335 92
Example
Initial Config. Final Config.
 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
COMP 335 93
Another Example

1 if x y
f ( x, y ) 
0 if x y

The function f is computable

COMP 335 94
Design Turing Machine for this function

1 if x y
f ( x, y ) 
0 if x y

Input: x0 y

Output: 1 or 0
COMP 335 95
Pseudocode/block diagram for
our Turing Machine:
• Repeat
Match a 1 from x with a 1 from y
Until all of x or y is matched

• If a 1 from x part is not matched


then erase the tape and write 1 ( x  y)
else erase tape and write 0 ( x  y )

COMP 335 96
Combining Turing Machines

COMP 335 97
Block Diagram

Turing
input output
Machine

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

x, y Adder
x y
A
x, y Comparer x y f ( x, y)

C
x y Eraser 0
E
COMP 335 99
Copyright © An Introduction to Formal Languages and Automata, Peter Linz
Copyright © Introduction to the Theory of Computation, Michael Sipser
Copyright © Costas Busch
Copyright © Nora Houari

100

Potrebbero piacerti anche