14-Turing Machine-Updated

You might also like

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

Turing Machine

1
Standard Turing Machine

δ(q0, a) = (q0, 0, R)
δ(q0, b) = (q0, b, R)
δ(q0, 0) = (q0, 0, L)
δ(q0, B) = (q1, B, L)
δ : Q × Γ → Q × Γ × {L, R}

2
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a b Y b B B B

q0q2 q1
q0
q2 q1

3
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a X b Y b Y B B B

q0q2 q1
q0
q2 q1

4
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a X b Y b Y B B B

q0 q3q4 q3
String accepted –
q4-final state

5
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a b Y b B B B

q0q2 q1
q0
q2 q1

δ(q0, a) = (q1, X, R) δ(q2, a) = (q2, a, L)


δ(q1, a) = (q1, a, R) δ(q2, X) = (q0, X, R)
δ(q1, b) = (q2, Y, L)

6
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a X b Y b Y B B B

q0q2 q1
q0
q2 q1

δ(q0, a) = (q1, X, R) δ(q2, a) = (q2, a, L) δ(q2, Y) = (q2, Y, L)


δ(q1, a) = (q1, a, R) δ(q2, X) = (q0, X, R)
δ(q1, b) = (q2, Y, L) δ(q1, Y) = (q1, Y, R)

7
Example 1
• Design a TM to accept the language L={anbn : n>=1}

Infinite tape Infinite tape


B B a X a X b Y b Y B B B

q0 q3q4 q3
String accepted –
q4-final state
δ(q0, Y) = (q3, Y, R)
δ(q3, Y) = (q3, Y, R)
δ(q3, B) = (q4, B, L)

8
Example 1
L={anbn : n>=1}
δ(q0, a) = (q1, X, R)
δ(q1, a) = (q1, a, R)
δ(q1, b) = (q2, Y, L)
δ(q2, a) = (q2, a, L)
δ(q2, X) = (q0, X, R)
δ(q1, Y) = (q1, Y, R)
a b X Y B
δ(q2, Y) = (q2, Y, L)
q0 (q1, X, R) (q3, Y, R)
δ(q0, Y) = (q3, Y, R)
q1 (q1, a, R) (q2, Y, L) (q1, Y, R)
δ(q3, Y) = (q3, Y, R)
q2 (q2, a, L) (q0, X, R) (q2, Y, L)
δ(q3, B) = (q4, B, L)
q3 (q3, Y, R) (q4, B, L)
9
Example 1 - n
L={a b n : n>=1}
• Check if aabb belongs to the automata.
Initial ID
q0w => q0aabb
q0aabb Ͱ Xq1abb Ͱ Xaq1bb Ͱ Xq2aYb
Check for aab
Ͱ q2XaYb Ͱ Xq0aYb Ͱ XXq1Yb q0aab Ͱ Xq1ab Ͱ Xaq1b Ͱ Xq2aY
Ͱ XXYq1b Ͱ XXq2YY Ͱ Xq2XYY Ͱ q2XaY Ͱ Xq0aY Ͱ XXq1Y
Ͱ XXq0YY Ͱ XXYq3Y Ͱ XXYYq3B Ͱ XXYq1B
Ͱ XXYq4YB No B defined on q1
Accept the string Hence reject the string

10
Language accepted by TM

11
Example 2
Design a TM to accept the language
L={anbncn : n>=1}
δ(q0, a) = (q1, X, R)
δ(q1, a) = (q1, a, R)
δ(q1, b) = (q2, Y, R)
δ(q2, b) = (q2, b, R)
δ(q2, c) = (q3, Z, L)
δ(q3, b) = (q3, b, L) δ(q1, Y) = (q1, Y, R)
δ(q3, Y) = (q3, Y, L) δ(q2, Z) = (q2, Z, R) δ(q4, Y) = (q4, Y, R)
δ(q3, a) = (q3, a, L) δ(q4, Z) = (q4, Z, R)
δ(q3, Z) = (q3, Z, L)
δ(q3, X) = (q0, X, R) δ(q4, B) = (q5, B, L)
δ(q0, Y) = (q4, Y, R)
12
Example 3
• L={anb2n:n>=0}
δ(q0, a) = (q1, X, R)
δ(q1, a) = (q1, a, R)
δ(q1, b) = (q2, Y, R)
δ(q2, b) = (q3, Y, L)
δ(q3, Y) = (q3, Y, L)
δ(q3, a) = (q3, a, L)
δ(q3, X) = (q0, X, R) a b X Y B
δ(q1, Y) = (q1, Y, R) q0 (q1, X, R) (q4, Y, R) (q5, B, L)
δ(q0, Y) = (q4, Y, R) q1 (q1, a, R) (q2, Y, R) (q1, Y, R)
δ(q4, Y) = (q4, Y, R) q2 (q3, Y, L)
δ(q4, B) = (q5, B, L) q3 (q3, a, L) (q0, X, R) (q3, Y, L)
δ(q0, B) = (q5, B, L) q4 (q4, Y, R) (q5, B, L) 13
Example 4
• L={wwR : w Є {a,b}*}
δ(q0, b) = (q4, B, R)
δ(q0, a) = (q1, B, R)
δ(q4, a) = (q4, a, R)
δ(q1, a) = (q1, a, R)
δ(q4, b) = (q4, b, R)
δ(q1, b) = (q1, b, R)
δ(q4, B) = (q5, B, L)
δ(q1, B) = (q2, B, L)
δ(q5, b) = (q6, B, L)
δ(q2, a) = (q3, B, L)
δ(q3, a) = (q3, a, L) δ(q6, a) = (q6, a, L)
δ(q3, b) = (q3, b, L) δ(q6, b) = (q6, b, L)
δ(q3, B) = (q0, B, R) δ(q6, B) = (q0, B, R)
δ(q0, B) = (q7, B, R) //accept
14
Example 5 q0
a
(q1, a, R)
b B

q1 (q2, b, R)
• Generate a TM for L(aba*b) q2 (q2, a, R) (q3, b, R)
q3 (q4, B, L)
δ(q0, a) = (q1, a, R)
δ(q1, b) = (q2, b, R)
DFA
δ(q2, a) = (q2, a, R)
δ(q2, b) = (q3, b, R)
δ(q3, B) = (q4, B, L)

If you are able to draw a DFA then using DFA directly convert to a TM.
Add one extra transition for B. Can ignore the trap states while converting.
15
Example 6
• Generate a TM that recognizes the set of all strings on Σ = {a, b}
starting with the prefix ab. a b B
q0 (q1, a, R)
q1 (q2, b, R)
q2 (q2, a, R) (q2, b, R) (q3, B, L)

δ(q0, a) = (q1, a, R)
δ(q1, b) = (q2, b, R)
δ(q2, a) = (q2, a, R)
DFA’s Trap state transitions need not be δ(q2, b) = (q2, b, R)
defined in TM δ(q2, B) = (q3, B, L) //accept
16
Example 7
• Find the language accepted by the TM given below.
M=({q0, q1, q2, q3, q4}, {a, b}, {a, b, B}, , q0, B, {q3})
δ(q0, a) = (q1, a, R)
δ(q0, b) = (q2, b, R)
δ(q1, b) = (q1, b, R)
δ(q1, B) = (q3, B, R)
δ(q2, b) = (q2, b, R)
δ(q2, a) = (q3, a, R) L={abn : n>=0} ᴜ {bbma : m>=0}
or
L((ab*) ᴜ (bb*a)) 17
Example 8
• Find the language accepted by the TM
δ(q0, a) = (q1, a, R)
δ(q0, b) = (q1, b, R)
δ(q1, a) = (q0, a, R)
δ(q1, b) = (q0, b, R)
δ(q1, B) = (q2, B, R)

L= { w Є ∑* , |w| is odd} on ∑ = {a, b}


18
Example 9

Assume q2 as the
final state

…BB1111BB…

…BB00BB…

19
Example 10

L={w : w Є {0,1}*}

20
Variations of TMs
• Turing Machines with
• Stay-Option
• Semi-Infinite Tape
• Off-Line
• Multi-tape
• Multidimensional
• Nondeterministic

21
Turing Machines with Stay-Option
The head can stay in the same position

L,R,S: possible head moves

 aababbcac a
Left, Right, Stay

δ : Q × Γ → Q × Γ × {L, R, S}

22
TM with Semi-Infinite Tape
The head extends infinitely only to the right
.........
a b a c  

• Initial position is the leftmost cell

• When the head moves left from the border,


it returns to the same position

23
Offline TM
Input File read-only (once)
a b c
Input string
Input string Appears on
input file only
Control Unit

(state machine) δ : Q × ∑ x Γ → Q × Γ × {L, R, S}

Tape read-write
  g d e   24
Multi-track TM
A useful trick to perform more
complicated simulations

One Tape

  a b a b  track 1

  b a c d  track 2

One head
One symbol (a, b)
25
Multi-tape TMs
Control unit
(state machine)

Tape 1 Tape 2
 a b c   e f g 
Input string

26
Multi-dimensional TM
2-dimensional tape y


 c a x
 b

MOVES: L,R,U,D,S HEAD


U: up D: down Position: +2, -1 27
Non-deterministic TM
• If a Turing machine has transitions specified as the following, it is non-
deterministic

28
The language L = {ww | w ∈ {a, b}}. The logic for solving this
problem can be divided into 2 parts:
1. Finding the mid point of the string
Exercises 2. After we have found the mid point we match the symbols.
First find the mid point by changing a/b to X/Y and traverse till
the end and do the same for the last character. Do one step at
1. Obtain a TM for L = {ww : w ∈ {a,b}*} a time until the mid point is reached. Once mid point is
reached, change left side symbols back to a/b accordingly. Now,
2. L={w1aw2 : w1, w2 ∈ {a, b}*} match left half with right half.
3. L={ambncmdn: m, n>=0}
4. L={anbm: n=m+2, n,m>=0}
5. What language is accepted by the Turing machine whose transition graph is in the figure
below?

29

You might also like