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

Chapter 6

Turing Machine

1
Alan Turing -- 1912 – 1954
• Founder of computer science, mathematician,
philosopher, codebreaker, strange visionary and
a gay man before his time.

• Influential
• development of computer sciences
• provided an influential formalisation
• of the concept of the algorithm
• and computation with the
• Turing Machine
• Turing Test contribute to the debate of AI
• Can machines think?

• http://www.turing.org.uk/turing/

2
Recursively Enumerated Languages
Turing Machines

n n n
a b c ww

Context-Free Languages
Push Down Automata
n n R
a b ww
Regular Languages
Finite Automata

a* a *b *
3
A Turing Machine

10 01 1 10 1 1 1 1 1 1 0 1 1 0 1 1 0 0 0

• A TM consists of an infinite length tape, on which input


is provided as a finite sequence of symbols.

• A head reads the input tape.


• The TM starts at start state s0.
• On reading an input symbol it optionally replaces it
with another symbol, changes its internal state and
moves one cell to the right or left. 4
A Turing Machine…
Tape - No boundaries -- infinite length

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

Read-Write head - The head moves Left or Right


• The head at each transition
Control Unit (time step):
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right

5
Example: Time 0

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

Time 1

...... ......
a b k c

1. Reads a
2. Writes
k
3. Moves Left
6
Time 1

...... ......
a b k c

Time 2

...... ......
a f k c

1. Reads
b
2. Writes
f
3. Moves Right
7
The Input String

Input string Blank symbol

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

head

Head starts at the leftmost position


of the input string

8
States & Transitions
Write
Read Move Left

q1 a  b, L q2

Move Right

q1 a  b, R q2
9
Example:
Time 1

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

q1
current state

q1 a  b, R q2
10
Time 1

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

q1
Time 2

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

q2

q1 a  b, R q2
11
Example: Time 1

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

q1
Time 2

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

q2

q1 a  b, L q2
12
Example: Time 1

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

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

q2

q1   g, R q2
13
A Turing Machine…
• 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

14
Partial Transition Function
Example:
...... ......
  a b a c   

q1
Allowed:
a  b, R q2
No transition
q1 for input symbol c
b  d, L q3
15
Halting
• The machine halts in a state if there is no transition to follow.

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

q1

No transition from q1
q1
HALT!!!

16
Halting…
Example
...... ......
  a b a c   

q1

a  b, R q2
No possible transition
from q1 and symbol c
q1
b  d, L q3 HALT!!!

17
Accepting States

q1 q2 Allowed

q1 q2 Not Allowed

• Accepting states have no outgoing transitions.


• The machine halts and accepts.

18
Acceptance…

Accept Input string If machine halts


in an accept state

If machine halts
in a non-accept state
Reject Input string or
If machine enters
an infinite loop

19
A Turing Machine… Example

Input alphabet
  {a , b }

Accepts the language:


a*
a  a, R

  , L
q0 q1

20
Time 0
  a a a  

q0

a  a, R

  , L
q0 q1

21
Time 1
  a a a  

q0

a  a, R

  , L
q0 q1

22
Time 2
  a a a  

q0

a  a, R

  , L
q0 q1

23
Time 3
  a a a  

q0

a  a, R

  , L
q0 q1

24
Time 4
  a a a  

q1

a  a, R Halt & Accept

  , L
q0 q1

25
Rejection Example

Time 0
  a b a  

q0

a  a, R

  , L
q0 q1
26
Rejection Example…

Time 1
  a b a  

q0
No possible Transition

Halt & Reject


a  a, R

  , L
q0 q1
27
Infinite Loop Example

A Turing machine for language a * b( a  b) *

b  b, L
a  a, R

  , L
q0 q1

28
Time 0
  a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

29
Time 1
  a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

30
Time 2
  a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

31
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 32
Infinite Loop….
Because of the infinite loop:

• The accepting state cannot be reached.

• The machine never halts .

• The input string is rejected.

33
A 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 34
A Turing Machine… Example

Basic Idea: Algorithm


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

35
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 36
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 37
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 38
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 39
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 40
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 41
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 42
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 43
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 44
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 45
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 46
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 47
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 48
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 49
A Turing Machine… Example

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 }

50
A Turing Machine: Formal Definition

Transition Function

q1 a  b, R q2

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

51
A Turing Machine: Formal Definition

Transition Function

q1 c  d, L q2

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

52
A Turing Machine: Formal Definition
Input Tape
alphabet alphabet

States

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

Transition Accept
function states

Initial
blank
state

53
A Turing Machine…Configuration

  c a b a  

q1

Instantaneous description: ca q1 ba

54
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)

55
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


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


Equivalent notation: q2 xayb  xxy q1 b

57
Initial configuration: q0 w

Input string

 a a b b  

q0

58
The Accepted Language
For any Turing Machine M


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

Initial state Accept state

59
The Accepted Language…

• If a language Lis accepted


by a Turing machine M then we say that Lis:
• Turing Recognizable

• Other names used:


• Turing Acceptable
• Recursively Enumerable

60
Turing Machine as Transducers:
Computing Functions with Turing Machines
• A function f(w) has:

Domain: D Result Region: S

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

61
Computing Functions with Turing Machines…
• A function may have many parameters:

• Example: Addition function

f ( x, y )  x  y

62
Computing Functions with Turing Machines…
Integer Domain

5
Decimal:

Binary: 101

Unary: 11111

We prefer unary representation:

easier to manipulate with Turing machines


63
Computing Functions with Turing Machines…

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


64
Computing Functions with Turing Machines…
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


65
Computing Functions with Turing Machines…
Example:
The function f ( x, y )  x  y is computable

x, y are integers

Turing Machine:

Input string: x0 y unary

Output string: xy0 unary

66
x y

Start  1 1  1 0 1  1 

q0
initial state

The 0 is the delimiter that


separates the two numbers

67
x y

Start
 1 1  1 0 1  1 

q0 initial state

x y

Finish
 1 1  1 1 0 

qf final state
68
Computing Functions with Turing Machines…

The 0 here helps when we use


the result for other operations

x y

 1 1 1 1 0 
Finish

qf final state
69
Computing Functions with Turing Machines…

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 70
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 71
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 72
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 73
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 74
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 75
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 76
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 77
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 78
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 79
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 80
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 81
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 82
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 83
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 84
Exercise
The function f ( x)  2 x is computable.

Design a Turing Machine that computes the above function.

85
Reading (Self Study)
• Turing Thesis
• Universal Turing Machine.

86
Group Assignment: Due date: June 7, 2022
• Form a group of five members and prepare a report not
more than five pages on the following topics.

• Complexity Theory
• Introduction
• Polynomial- Time Algorithm
• Non- Deterministic Polynomial Time Algorithm
• NP Problems

• Copying from one another is prohibited and will nullify your


mark.
• Prepare a presentation for evaluation.
• Submission: The section representatives will collect from all
groups and submit a zipped file which contains all groups
assignment report. 87

You might also like