0% found this document useful (0 votes)
28 views

Turing Machine

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

Uploaded by

tridentweapon
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Turing Machine

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

Uploaded by

tridentweapon
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

TURING MACHINE

A Turing Machine is an accepting device which accepts the


languages (recursively enumerable set) generated by type 0
grammars. It was invented in 1936 by Alan Turing.
A Turing Machine (TM) is a mathematical model which consists of
an infinite length tape divided into cells on which input is given.
The tape consists of infinite cells on which each cell either contains
input symbol or a special symbol called blank. It also consists of a
head pointer which points to cell currently being read and it can
move in both directions.
After reading an input symbol, it is replaced with another symbol,
its internal state is changed, and it moves from one cell to the right
or left.
If the TM reaches the final state, the input string is accepted,
otherwise rejected.
There are various features of the Turing machine:
• It has an external memory which remembers arbitrary
long sequence of input.
• It has unlimited memory capability.
• The model has a facility by which the input at left or
right on the tape can be read easily.
• The machine can produce a certain output based on its
input. Sometimes it may be required that the same
input has to be used to generate the output.

So in this machine, the distinction between input and


output has been removed. Thus a common set of
alphabets can be used for the Turing machine.
A TM can be formally described as a 7-tuple
(Q, X, ∑, δ, q0, B, F)
where −
Q is a finite set of states
X is the tape alphabet
∑ is the input alphabet
δ is a transition function;
δ : Q × X → Q × X × {Left_shift, Right_shift}.
q0 is the initial state
B is the blank symbol
F is the set of final states
Comparison with the previous automaton
The following table shows a comparison of how a Turing machine
differs from Finite Automaton and Pushdown Automaton.

Machine Stack Data Structure Deterministic?

Finite Automaton N.A Yes

Pushdown Automaton Last In First Out(LIFO) No

Turing Machine Infinite tape Yes


Representation of TM
We can describe TM with three different ways, they are
1.Instantaneous description
2.Transition table
3.Transition diagram

1.Instantaneous Description:

An Instantaneous Description (ID) of the TM M is denoted by α1 q α2


Here q ϵ Q is the current state of M.
α 1α2 is the string in Γ * that is the contents of the tape up to the
rightmost non blank symbol or symbol to the left of the head,
whichever is rightmost.
A snapshot of TM is shown in fig. Obtain the Instantaneous description

B a1 a2 b1 a3 b2 a4 b3 b4 B

R/W head

State
q0

The present symbol under R/W head is b2. The present state is q0.
So b2 is written to the right of q0
The Non blank symbols to the left of b2 form the string a1a2b1a3 which is written
to the left of q0.
The sequence of non blank symbols to the right of b2 is a4b3b4.

a1a2b1a3 q0 b2 a4b3b4
Present state Symbol under R/W head
NOTE: For ID simply inset the Current state in the I/P string to the left of the symbol under
R/W head
Example :
Show the situation before and after the move caused by the transition
δ (q0, a) = (q1, d, R)

Solu:

Situation before the move


a b c

internal state q0

After the move

d b c

internal state q1
Consider the TM defined as
(Q = {q0,q1}, ∑= {a.b}, Γ= {a,b,B} δ, q0, B, F= {q1})
and
δ (q0,a) = (q0, b, R)
δ (q0,b) = (q0, b, R)
δ (q0,B) = (q1, B , L)
Process the string W=aa, and the initial state of TM is q0.

Solu:
Step1:
TM is in state q0, with the symbol a on the RW head, the applicable transition is δ (q0,a) = (q0,
b, R).
So the RW head will replace a by b and then move right on the tape
ID
B a a B Bq0aa = q0aa

q0
B b a B
ID
bq0a
q0
Step 2:
The machine is still in state q0. The subsequent a will also be replaced by a b, but b’s will
not be modified
ID
bbq0B
B b b B

q0

Step 3:
The machine halts on encountering first blank in state q1 (Final state)
ID
B b b B bq1b

q1
Let us construct a turing machine for L={0n1n|n>=1}

Q = {q0,q1,q2,q3} where q0 is initial state.


T = {0,1,X,Y,B} where B represents blank.
∑ = {0,1}
F = {q3}
Transition function δ is given in Table 1 as:
Let us see how this turing machine works for 0011. Initially head
points to 0 which is underlined and state is q0 as:
B 0 0 1 1 B

The move will be δ(q0, 0) = (q1, X, R). It means, it will go to state


q1, replace 0 by X and head will move to right as:

The move will be δ(q1, 0) = (q1, 0, R) which means it will remain in


same state and without changing any symbol, it will move to right
as:
The move will be δ(q1, 1) = (q2, Y, L) which means it will move to q2 state and changing 1 to
Y, it will move to left as:

Working on it in the same way, the machine will reach state q3 and head will point to B as
shown:

Using move δ(q3, B) = halt, it will stop and accepted.


Note:

• In non-deterministic turing machine, there can be


more than one possible move for a given state and
tape symbol, but non-deterministic TM does not add
any power.
• Every non-deterministic TM can be converted into
deterministic TM.
• In multi-tape turing machine, there can be more
than one tape and corresponding head pointers, but
it does not add any power to turing machine.
• Every multi-tape TM can be converted into single
tape TM.
Consider the TM described by the transition table.
Describe the processing of (a) 011 b) 0011 c)001
Which of the above strings are accepted by M?

Present Tape Symbols


State
0 1 x y B

q1 xRq2 BRq5

q2 0Rq2 yLq3 yRq2

q3 0Lq4 xRq5 yLq3

q4 0Lq4 XRq1

q5 yRq5 BRq6

q6
a) q1011 Ⱶ xq211 Ⱶ q3xy1 Ⱶ xq5y1 Ⱶ xyq51
as δ (q5,1) is not defined, the input string 011 is rejected or not
accepted

b) q10011 Ⱶ xq2011 Ⱶ x0q211 Ⱶ xq30y1 Ⱶ q4x0y1 Ⱶ xq10y1 Ⱶ xxq3y1


Ⱶ xxyq 1 Ⱶ xxq yy Ⱶ xq xyy Ⱶ xxq yy Ⱶ xxyq y Ⱶ xxyyq B Ⱶ xxyyBq
2 3 3 5 5 5 6

M halts, as q6 is an accepting state the input string 0011 is accepted.

c) q1001 Ⱶ xq201 Ⱶ x0q21 Ⱶ xq30y Ⱶ q4x0y Ⱶ xq10y Ⱶ xxq2y Ⱶ xxyq2


M halts, as q2 is not an accepting state, 0011 is not accepted in M
Language accepted by Turing machine
The Turing machine accepts all the language even
though they are recursively enumerable.
Recursive means repeating the same set of rules
for any number of times and enumerable means a
list of elements.

The TM also accepts the computable functions,


such as addition, multiplication, subtraction,
division, power function, and many more.
The Language accepted by TM (M) denoted by L(M) is defined as:
L(M) = { w : w ϵ Σ * and q0w Ⱶ* α1pα2, for some p ϵ F, α1. α2 ϵ Γ*}

When a TM processes an input string w, there are three


possibilities. They are

i) TM can accept the string by entering accepting state


ii) It can reject the string by entering non-accepting state
iii) It can enter an infinite loop so that it never halts
Design of TM
The guidelines for designing a turing machine is,
i) The fundamental objective in scanning a symbol by R/W
head is to ‘know’ what to do in the future. The machine
must remember the past symbols scanned. The TM can
remember this by going to the next unique state.
ii) The number of states must be minimized. This can be
achieved by changing the state only when there is a
change in the written symbol or when there is a change
in the movement of R/W head.
Design a TM M to accept the language
L= { 0n1n: n>=1 } compute 0011
δ (q0,0) = (q1,x, R)
δ (q1,0)= (q1, 0, R)
δ(q1,y) = (q1,y R)
δ(q1,1) = (q2, y , L)
δ(q2,y) = (q2,y, L)
δ(q2, 0) = (q2,0, L)
δ(q2,x) = (q0, x, R)
δ(q0, y) = (q3,y, R)
δ( q3, y) = (q3, y, R)
δ( q3, B) = (q4, B, R)
Design a TM to recognize all strings consisting of even
number of 1’s
q1 – initial state and final state
δ (q1,1) = (q2,B, R) Compute a) 11 b)111
δ (q2,1) = (q1,B, R)
Design a Turing Machine that recognizes all
strings consisting of an odd number of 'a's,
 Transition from the start state (q0) to a new state (q1)
when reading 'a'. Move the tape head to the right.
 Transition from q1 to q0 when reading 'a'. Move the
tape head to the right.
 Transition from q0 to qaccept when reading the blank
symbol (B). This indicates that the input string has an
odd number of 'a's.
 Transition from q1 to qreject when reading the blank
Curren
Curre Move
t New Next
nt Tape
Symbo Symbol State
State Head
l
q0 a A Right q1
q1 a A Right q0
qaccep
q0 B B -
t
q1 B B - qreject
Design a TM M to recognize the language L = { 1n2n3n: n>=1}

δ (q1,1) = (q2,b, R)
δ (q2,1) = (q2,1, R)
δ (q2,2) = (q3,b, R)
δ (q3,2) = (q3,2, R)
δ (q3,3) = (q4,b, R) δ (q1,1) = (q2,b, R)
δ (q4,3) = (q5,3, L) δ (q2,1) = (q2,1, R)
δ (q2,2) = (q3,b, R)
δ (q5,b) = (q5,b, L) δ (q3,2) = (q3,2, R)
δ (q5,2) = (q5,2, L) δ (q3,3) = (q4,b, L)
δ (q5,1) = (q6,1, L) δ (q4,b) = (q4,b, L)
δ (q4,2) = (q4,2, L)
δ (q6,1) = (q6,1, L) δ (q4,1) = (q4,1, L)
δ (q6,b) = (q1,b, R) δ (q4,b) = (q4,b,L)
δ (q2,b) = (q2,b, R)
δ (q3,b) = (q3,b, R)
δ (q4,b) = (q4,b, L)
δ (q4,B) = (q7,B, H)
Computation languages and Function
TM Addition
f(m,n) = m+n is computable by TM
m=3, n=4
m+n =7
000 1 0000 here 1 is separator
Let the input w=0m0n
i.e m= 0m n=0n
Therefore w=0m0n = 0m+n
(q1,0) = (q2,B,R)
(q2,0) = (q2,0,R)
(q2,1) = (q2,1,R)
(q2,B) = (q3,0,L)
(q3,1) = (q3,1,L)
(q3,0) = (q3,0, L)
(q3,B) = (q1,B,R)
(q1,1) = (q4,B,H)
Design TM for 1’s complement
(q0,0) = (q0,1,R)
(q0,1) = (q0,0 R)
(q0.B) = (q1,B, H)

Design TM for 2’s complement


(q0,1) = (q0,0, R)
(q0,0) = (q0,1,R)
(q0,B) = (q1,B,L)
(q1,1) = (q1,0,L)
(q1,0) = (q2,1,H)
Design a TM to perform proper substraction
Solu:
Proper subtraction f(x,y) = x-y
x-y if x > y
0 if x < y m=4, n=2

(q0,0) = (q1,B,R)
(q1,0) = (q1,0,R)
(q1,1) = (q2,1,R) (q0,1) = (q6,B,R)
(q2,0) = (q3,1,L) (q6,0) = (q6,B,R)
m<n
(q3,1) = (q3,1,L) (q6,1) = (q6,B,R)
(q3,0) = (q3,0,L) (q6,B) = (q5,B,H)
(q3,B) = (q0,B,R)
(q2,1) = (q2,1,R)
(q2,B) = (q4,B,L) When (q0,1) scans it will tell us m
(q4,1) = (q4,B,L) m>n is less than n, then change all 1’s
(q4,0) = (q4,0,L) and 0’s to blank then halt
0 0 0 0 1 0 0 B
B B B 0 1 1 1 B
(q0,0) = (q1,B,R)
(q4,1) = (q4,B,L) (q4,0) = (q4,0,L)
B 0 0 0 1 0 0 B
B B B 0 B B B B
(q1,0) = (q1,0,R) (q1,1) = (q2,1,R)
(q2,0) = (q3,1,L)
B B 0 0 B B B B

B 0 0 0 1 1 0 B (q4,B) = (q5,0,H)
(q3,1) = (q3,1,L) (q3,0) = (q3,0,L)
0 0 1 0 0 0 B
(q3,B) = (q0,B,R)
B B 0 0 1 1 0 B B 0 1 1 0 0
m<n
(q2,1) = (q2,1,R) (q2,0) = (q3,1,L)
B B 1 1 1 B
B B 0 0 1 1 1 B

(q3,B) = (q0,B,R) (q0,0) = (q1,B,R) When (q0,1) scans it will tell us m


(q1,0) = (q1,0,R) (q1,1) = (q2,1,R) is less than n, then change all 1’s
(q2,B) = (q4,B,L) and 0’s to blank then halt
Programming Techniques in TM
A turing machine is exactly as powerful as a conventional computer.
1.Storage in finite control
2.Multiple Tracks
3.Shift over
4.Checking off symbol
5.Subroutine
Storage in Finite Control
A Turing machine has a finite number of states in its CPU.
However, the number of states is not always small.
For example, all real computers have registers but there
are only a fixed number of them, and each register can
only hold one of a fixed (and finite) number of bits.
Similarly, we define a state as a pair which stores the
details of control and the other stores the symbol.
To account for this modification we can define the Turing
machine as M (Q, ∑, Γ, δ, [q0,B], B, F) where Q is of the form
[q, a] where q is a state and a Ꞓ ∑, the transitions are defined
as ([Q, X ∑] , Γ) ([Q X ∑ ], Γ, {R L}).
For example
The transition δ ([q, a], b) = ([p, b], c, R) indicates that the
control is in state q and a is stored in finite control.
On the input symbol b it moves to p state and changes the
symbol in finite control to b, changes the cell content as c and
moves one cell right.
Design a Turing machine that accepts strings of the ab* or ba *.
Solu:
To design this system, it is required to define the transitions Such
that on seeing a it should move to a different state on b and in that
state be in same state until B is encountered.
On B, move to final state. Similarly, if the first symbol is b move to
different state and be in that state on a and finally move to final
state on B.
A sample diagram is shown in Fig
ab* or ba *

The same problem can be solved with two state but changing the data stored
in finite control.
Let the state be defined as {qo, qA} X {a, b, B}. Hence the states are [q0, a], [qo
b], [qo, B], [qA, a]. [qA, b] and [qA , B] where [qA, B] is the final state.
At state initial state [q0,B], if it finds ‘a’ or ‘b’, it stores in its state
without replacing and moves right and enters the state q A.
At state [qA,a], if it finds ‘b’, then it skips all ‘b’ and moves right
and remains in this same state.
At state [qA,b], if it finds ‘a’, then it skips all ‘a’ and moves right
and remains in this same state.
At state [qA,a], if it finds ‘B’, then it reaches the accepting state
[qA,B]
At state [qA,b], if it finds ‘B’, then it reaches the accepting state
[qA,B]
The transitions are defined as follows.
1. Let the initial configuration be [qo, B].
2. On seeing a symbol store it in finite control and move right.
δ ([qo, B], a) = ([q0, a], a, R)
δ([qo, B], b) = ([q0, b], b, R)
3. In [qo, a] state move right on seeing b and enter to final state [q A, B] on
seeing B.
δ ([q0, a], b) = ([q0, a], b, R)
δ ([q0, a], B) = ([ qA,, B], B, L)
4. In [q0, b] state move right on seeing a and enter to final state [qA,, B] on
Seeing B.
δ ([q0, b], a) = ([q0, b], a, R)
δ ([q0, b), B) = ([qA, B], B, L)
Multi-tape Tracks
The tape is imagined as divided into cells where input to be processed is
placed. We can further imagine that the tape is divided into k tracks for some
finite number k as shown below.

Φ 1 0 1 1 1 1 $ B B …. ….

B B B B 1 0 1 B B B …. ….

B 1 0 0 1 0 1 B B B …. ….

Finite
Control
The reading head considers k symbols each belonging to
different track in same column and processes it.
There are two special symbols Φ and $ used in the first
track which indicates the boundary of the input.
The other tracks are used to place the intermediate results
and the final result of the processing.
The blank input is identified as all B's in all tracks as [B, B,
B].
The input at the current position of the reading head is [1,
1, 1].
Design a Turing machine to find whether the given number is prime
or not.
Solution
To design'a TM to identify the number as prime or not it is required to find
whether the number has factors other than itself.
1. Let us place the given number on first track in binary form bounded by Φ
and $. For example 47 is represented as Φ101111$.
2. On the second track write 2 in binary form as 10.
3. Copy the number on first track to third track.
4. Perform repeated subtraction of number on third track with number on
second track until the number on third track is either 0 or less than number
on second track.
5. If the number on third track is zero and number on second is not
equal to number on first track then the number on first track is not
prime, otherwise prime.
6. If the number on third track in nonzero and increase number on
second track by one
7. Repeat the steps 4-6 until the number on second is equal to
number on first track.
Shifting Over
A Turing machine can make space on its tape by shifting non blank
symbols by a finite number of cells to the right.
To perform this operation we can use the state with small amount of
storage which can be used to store the symbols and replacing the
current cell by blank and move right.
Read the right symbol and replace it with the symbol stored in the
finite control.
To perform this operation without losing the data it requires storage
capacity to store at least two symbols.
The illustration is given to explain the procedure.
Design the Turing machine which can shift the data on the
tape by, two spaces.
Solution
To design the TM which creates two spaces on the left we need to
use finite control which can store two symbols.
Let the state be defined as [q,A1,A2] where q=q0,q1,qA and A1 and A2
can hold the symbols of ∑ or B.
Let X denote a special symbol to indicate the created blank spaces on
the left of input.
The TM starts with initial state [q0,B,B]
1.In [q0,B,B] state the transition is defined to store the current symbol
encountered in the A1 and replacing the cell content with X.
δ([q0,B,B], A1) = ([q0,B,A1], X, R)

2. In [q0,B,A1] the state the transition is defined to move content of A 1 left and
store the current symbol encountered in A2 and replacing the cell content with X.
δ([q0,B,A1],A2) = ([q0,A1,A2],X,R)
3. In [q0,A1,A2] state the transition is defined move content of A2 to A1
and store the current symbol encountered in A2 and replacing the cell
content with A1.
δ([q0,A1,A2],A3) = ([q0,A2,A3],A1,R)
4. If B is encountered in [q0,A1,A2] state the transition is
defined move content of A2 to A1 and store B in A2 and
replacing the cell content with A1.
δ([q0,A1,A2], B) = ([q0,A2,B], A1, R)
5. If B is encountered in [q0,A1, B] state the transition is
defined replacing the cell content with A1 and change to
new state as the entire string is shifted to right
δ([q0,A1,B], B) = ([q1,B,B], A1, R)
Checking off Symbols
This is one useful technique that can be used to visualise how TM would
recognise the languages. This technique uses an extra track which indicates
the symbol on other track is processed.
The languages which have repeated strings or some Conditions relating to
other part of string can be solved with this procedure. Such languages are
listed below.
a){ww/ w in Σ* }
b){wwR /w in Σ*}
c){aibj / i>=1 }
d){aibjck/ i≠j or j≠ k }
e){wcw/ w in Σ*}
For the languages mentioned above we can use the
tape with two tracks where One track we place the
given input and on the other track we place either B
or .
.

If the upper track symbol is B it indicates the symbol


on lower track is not Considered. If the symbol on
upper track is ,it indicates that the symbol on
lower track is considered.
Design a Turing machine for the language {aibj / i,j>=1 }
Subroutines
A turing machine can simulate any type of subroutine found in programming
languages, including recursive procedures and any of the known parameter
passing mechanisms.
We can design a TM programme to serve as a subroutine, which has a
designated initial state and a designated return state which temporarily has to
move and which will be used to effect a return to the calling routine.
To design a TM to call the subroutine a new set of states are defined which are
used to enter the initial State of the subroutine and return from the return
state of subroutine.
As an example a TM is designed to accept strings with balanced parenthesis.
Design a Turing machine that accepts only the strings with
balanced parenthesis.
To solve this we need to frame an algorithm that can be used match
“(“ for every occurrence of " )" At the end if any parenthesis is
unmatched then it is not balanced.
Procedure:
1)First search for the occurrence of “)”
For this process in the initial state q0 ignore all “(“ until “)” is
seen. The transitions are as follows.
Therefore δ (q0, ( ) = (q0, (, R)
2. On finding “)” replace it by X, change to new state and travel left
for the first occurrence of “(“.
We consider this state as the initial state of subroutine which is
used to find the corresponding “( “ for “ )”.
While travelling back the possible elements it can see is X.
Transitions required are as follows,
δ(q0, ) ) = (q1, X, L)
δ(q1,X ) = (q1, X, L)
3. If “(“ found replace it by X. If not found enter into rejection state. In this
example q1 acts as both initial and return state of the subroutine.
δ(q1, ( ) = (q0, X, R)
δ(q1, B ) = (q3, B, R)
4. Repeat step 1 and step 2 until a B is encountered
δ(q0, X ) = (q0, X, R)
δ(q0, B ) = (q2, B, L)
5. If B is encountered enter into a new state and check if there is no “(“
unbalanced.
δ(q2, X ) = (q2, B, L)
δ(q2, ( ) = (q3, (, R)
δ(q2, B ) = (qA, B, R)
process the string w= (()) ()

You might also like