0% found this document useful (0 votes)
13 views87 pages

Lecture 11 TMs

Uploaded by

mahmoud adel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views87 pages

Lecture 11 TMs

Uploaded by

mahmoud adel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

Theory of Computation

Lecture 11
Turing Machine (TM)

Dr. Samar Hesham


Department of Computer Science
Faculty of Computers and AI
Cairo University
Egypt

1
Syllabus and Terminologies

 Regular Languages .. Regular Sets


 REs (Regular Expressions)
 FSMs (or FSAs) … Finite State Machines/Automata
 DFSM vs. NDFSM … Deterministic vs. Non-deterministic FSM
 Comparison and conversion
 Pumping Lemma & Closure Operations
 Context Free Languages
 CFGs … Context Free Grammars
 PDAs … Push Down Automata
 Parsing: CFG generating strings vs. PDA recognizing strings
 Turing Machine

5/31/2024 FCI-CU-EG 2
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 2006 Costas Busch - RPI 3
Where do we go from here?
 From the earlier example, we see that not all
languages are context-free.
 Is there a mathematical model for even more
complex computations?
 There are many.
 We will study one in particular: an automaton
called a Turing machine.

4
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 2006 Costas Busch - RPI 5
Turing Machines

 Proposed by Alan Turing, 1936. England


 “Turing Award”

control
infinite tape

a b a b B B B B …

B: blank symbol
Differences between finite automata and Turing
machine
1. A Turing machine can both write on the input 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 … don’t need to consume all the input.
TMs can be represented in 3 different, but
yet equivalent ways:
1. Transition Graph
2. Instructions
3. Configurations
TM
in the form of
Transition Graph

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

Read-Write head

Control Unit

Fall 2006 Costas Busch - RPI 10


The Tape
No boundaries -- infinite length

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

Read-Write head

The head moves Left or Right

Fall 2006 Costas Busch - RPI 11


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

Read-Write head

The head at each transition (time step):

1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right

Fall 2006 Costas Busch - RPI 12


Example:
Time 0
...... ......
a b a c

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

1. Reads a
2. Writes k
3. Moves Left
Fall 2006 Costas Busch - RPI 13
Time 1
...... ......
a b k c

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

1. Reads b
2. Writes f
3. Moves Right
Fall 2006 Costas Busch - RPI 14
The Input String

Input string Blank symbol

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

head

Head starts at the leftmost position


of the input string

Fall 2006 Costas Busch - RPI 15


Turing Machine:

Input Tape
alphabet alphabet
States

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

Transition Accept
function states
Initial Blank
Symbol
Fall 2006
state Costas Busch - RPI 16
States & Transitions
Write
Read Move Left

q1 a  b, L q2

Move Right

q1 a  b, R q2
Fall 2006 Costas Busch - RPI 17
Transition Function

q1 a  b, R q2

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

Fall 2006 Costas Busch - RPI 18


Transition Function

q1 c  d, L q2

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

Fall 2006 Costas Busch - RPI 19


Example: Time 1

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

q1
current state
Time 2

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

q2
q1 a  b, R q2
new state

Fall 2006 Costas Busch - RPI 20


Example: Time 1

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

q1

Time 2

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

q2

q1 a  b, L q2
Fall 2006 Costas Busch - RPI 21
Example:
Time 1

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

q1

Time 2

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

q2

q1   g, R q2
Fall 2006 Costas Busch - RPI 22
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 2006 Costas Busch - RPI 23
Partial Transition Function
Example:
  a b a c   
...... ......

q1

a  b, R q2
Allowed:
No transition
for input symbol c
q1
b  d, L q3
Fall 2006 Costas Busch - RPI 24
Halting

The machine halts in a state if there is


no transition to follow

Fall 2006 Costas Busch - RPI 25


Halting Example 1:

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

q1

No transition from q1
q1 HALT!!!

Fall 2006 Costas Busch - RPI 26


Halting Example 2:

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

q1
No possible transition
a  b, R q2 from q1 and symbol c

HALT!!!
q1
b  d, L q3
Fall 2006 Costas Busch - RPI 27
Accepting States
q1 q2 Allowed

q1 q2 Not Allowed

•Accepting states have no outgoing transitions


•The machine halts and accepts

Fall 2006 Costas Busch - RPI 28


Acceptance
If machine halts
Accept Input string
in an accept state

If machine halts
in a non-accept state
Reject Input string or
If machine enters
an infinite loop
Fall 2006 Costas Busch - RPI 29
Observation:
In order to accept an input string,
it is not necessary to scan all the
symbols in the string

Fall 2006 Costas Busch - RPI 30


Turing Machine Example
Input alphabet   {a , b }

Accepts the language: a*


a  a, R

  , L
q0 q1

Fall 2006 Costas Busch - RPI 31


Example
Time 0   a a a  

q0

a  a, R

  , L
q0 q1

Fall 2006 Costas Busch - RPI 32


Time 1   a a a  

q0

a  a, R

  , L
q0 q1

Fall 2006 Costas Busch - RPI 33


Time 2   a a a  

q0

a  a, R

  , L
q0 q1

Fall 2006 Costas Busch - RPI 34


Time 3   a a a  

q0

a  a, R

  , L
q0 q1

Fall 2006 Costas Busch - RPI 35


Time 4   a a a  

q1

a  a, R Halt & Accept

  , L
q0 q1

Fall 2006 Costas Busch - RPI 36


Rejection Example

Time 0   a b a  

q0

a  a, R

  , L
q0 q1
Fall 2006 Costas Busch - RPI 37
Time 1   a b a  

q0
No possible Transition

a  a, R
Halt & Reject

  , L
q0 q1
Fall 2006 Costas Busch - RPI 38
Infinite Loop Example

b  b, L
a  a, R

  , L
q0 q1

Fall 2006 Costas Busch - RPI 39


Time 0   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

Fall 2006 Costas Busch - RPI 40


Time 1   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

Fall 2006 Costas Busch - RPI 41


Time 2   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

Fall 2006 Costas Busch - RPI 42


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

Infinite loop
Time 3

q0
Time 4   a b a  
q0
Time 5   a b a  
Fall 2006
q0
Costas Busch - RPI 43
Because of the infinite loop:

•The accepting state cannot be reached

•The machine never halts

•The input string is rejected

Fall 2006 Costas Busch - RPI 44


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
Fall 2006
x  x, R
Costas Busch - RPI 45
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 2006 Costas Busch - RPI 46


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
Fall 2006
x  x, R
Costas Busch - RPI 47
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
Fall 2006
x  x, R
Costas Busch - RPI 48
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
Fall 2006
x  x, R
Costas Busch - RPI 49
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
Fall 2006
x  x, R
Costas Busch - RPI 50
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
Fall 2006
x  x, R
Costas Busch - RPI 51
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
Fall 2006
x  x, R
Costas Busch - RPI 52
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
Fall 2006
x  x, R
Costas Busch - RPI 53
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
Fall 2006
x  x, R
Costas Busch - RPI 54
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
Fall 2006
x  x, R
Costas Busch - RPI 55
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
Fall 2006
x  x, R
Costas Busch - RPI 56
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
Fall 2006
x  x, R
Costas Busch - RPI 57
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
Fall 2006
x  x, R
Costas Busch - RPI 58
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
Fall 2006
x  x, R
Costas Busch - RPI 59
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
Fall 2006
x  x, R
Costas Busch - RPI 60
Computing Functions
with
Turing Machines

Fall 2006 Costas Busch - RPI 61


Example:
Addition function
A function may have many parameters:

f ( x, y )  x  y

Fall 2006 Costas Busch - RPI 62


Integer Domain

Decimal: 5

Binary: 101

Unary: 11111

We prefer unary representation:


easier to manipulate with Turing machines
Fall 2006 Costas Busch - RPI 63
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

Fall 2006 Costas Busch - RPI 64


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 2006 Costas Busch - RPI 65


x y

Start
 1 1  1 0 1  1 

q0
initial state

The 0 is the delimiter that


separates the two numbers

Fall 2006 Costas Busch - RPI 66


x y

Start
 1 1  1 0 1  1 

q0 initial state

x y

Finish
 1 1  1 1 0 

Fall 2006
qf final state
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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 

Fall 2006 Costas Busch - RPI


q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
Fall 2006 Costas Busch - RPI
q4 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 2
1  0, L q3

  , R
HALT & accept q4
Fall 2006 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

Fall 2006 Costas Busch - RPI 83


x

Start  1 1  1 

q0 initial state

2x

Finish
 1 1  1 1 1 
qf accept state

Fall 2006 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

Fall 2006 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
Fall 2006 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
Fall 2006
q3 Costas Busch - RPI 87

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy