TOA Unit-V Turing Machine
TOA Unit-V Turing Machine
q
Finite State
Control
Mathematical Definition of Turing
Machine (TM)
A Turing machine is described by a 7-tuple
M=(Q, Σ, Γ, δ, q0, B, F) where,
States 0 1 x y B
q0 (q1, x, R) (q3, y, R)
q3 (q3, y, R) (q4, B, R)
q4
Turing Machine
Part-2
Representation of TM(continue)
By transition diagram
(0, 0 ,L)
(0, 0 ,R) (y, y, L)
(y, y, R)
(y, y, R)
(x, x ,R)
(B, B ,R)
q3 q4
(y, y, R)
Processing or working of TM
Ex. Check the acceptability of following strings
(1) 0011 (2) 011 (3) 00101
by the TM in the previous example.
Solution:
(1) For string 0011
q00011 ⊢ xq1011 ⊢ x0q111 ⊢ xq20y1 ⊢ q2x0y1 ⊢ xq00y1 ⊢
xxq1y1 ⊢ xxyq11 ⊢ xxq2yy ⊢ xq2xyy ⊢ xxq0yy ⊢ xxyq3y ⊢
xxyyq3B ⊢ xxyyBq4B (machine halts at final state)
Since machine halts at final state, therefore this string is
accepted by TM.
Processing or working of TM(continue)
(2) For string 011
q0011 ⊢ xq111 ⊢ q2xy1 ⊢ xq0y1 ⊢ xyq31 (machine halts at
non-final state)
Since machine halts at non-final state, therefore this string is
not accepted by TM.
(3) For string 00101
q000101 ⊢ xq10101 ⊢ x0q1101 ⊢ xq20y01 ⊢ q2x0y01 ⊢
xq00y01 ⊢ xxq1y01 ⊢ xxyq101 ⊢ xxy0q11 ⊢ xxyq20y ⊢
xxq2y0y ⊢ xq2xy0y ⊢ xxq0y0y ⊢ xxyq30y (machine halts at
non-final state)
Since machine halts at non-final state, therefore this string is
not accepted by TM.
Construction of TM
In this section, we shall see how TM’s can be constructed.
(1, 1, R)
(0, 0, R)
(1, 1 , R) (0, 0, R)
(1, 1, R)
q4
L={ n
01 n !n≥1}
Transition diagram is (0, 0 ,L)
(0, 0 ,R) (y, y, L)
(y, y, R)
(y, y, R)
(x, x ,R)
(B, B ,R)
q3 q4
(y, y, R)
Processing and Verification of TM
Acceptance
Consider string w = 0011 .
q00011 ⊢ xq1011 ⊢ x0q111 ⊢ xq20y1 ⊢ q2x0y1 ⊢ xq00y1 ⊢ xxq1y1
⊢ xxyq11 ⊢ xxq2yy ⊢ xq2xyy ⊢ xxq0yy ⊢ xxyq3y ⊢ xxyyq3B ⊢
xxyyBq4B (machine halts at final state)
Since machine halts at final state, therefore this string is accepted by
TM.
Rejection
Consider string w = 011 .
q0011 ⊢ xq111 ⊢ q2xy1 ⊢ xq0y1 ⊢ xyq31 (machine halts at non-
final state)
Since machine halts at non-final state, therefore this string is not
accepted by TM.
Ex. Construct Turing machine for the language
L = { 0n1n2n! n ≥ 1 }.
Solution:
The TM corresponding to this language is (0, 0 ,L)
(y, y, L)
(0, 0 ,R) (1, 1,R) (1, 1, L)
(y, y, R) (z, z, R) (z, z, L)
(y, y, R)
(x, x ,R)
(B, B ,R)
q4 q5
(z, z, R)
(y, y, R)
Processing and Verification of TM
Acceptance
Consider string w = 001122 .
q0001122 ⊢ xq101122 ⊢ x0q11122 ⊢ x0yq2122 ⊢ x0y1q222 ⊢
x0yq31z2 ⊢ x0q3y1z2 ⊢ xq30y1z2 ⊢ q3x0y1z2 ⊢ xq00y1z2 ⊢
xxq1y1z2 ⊢ xxyq11z2 ⊢ xxyyq2z2 ⊢ xxyyzq22 ⊢ xxyyq3zz ⊢
xxyq3yzz ⊢ xxq3yyzz ⊢ xq3xyyzz ⊢ xxq0yyzz ⊢ xxyq4yzz ⊢
xxyyq4zz ⊢ xxyyzq4z ⊢ xxyyzzq4B ⊢ xxyyzzBq5B
(machine halts at final state)
Since machine halts at final state, therefore this string is accepted
by TM.
Rejection
Consider string w = 00112 .
q000112 ⊢ xq10112 ⊢ x0q1112 ⊢ x0yq212 ⊢ x0y1q22 ⊢
x0yq31z ⊢ x0q3y1z ⊢ xq30yq11z ⊢ q3x0y1z ⊢ xq00y1z
⊢ xxq1y1z ⊢ xxyq11z ⊢ xxyyq2z ⊢ xxyyzq2B
(machine halts at non-final state)
Since machine halts at non-final state, therefore this string is
not accepted by TM.
Turing Machine
Part-4
Ex. Construct TM to accept the language
L = { wcwR ! w ∈ {a, b}* }
Solution:
Some strings of this set are c, aca, bcb, abcba, bacab, aabcbaa etc.
Clearly all these strings are palindrome. That is, first symbol and
last symbol are same. Similarly, second symbol and second last
symbol are same , and so on.
Procedure: TM is constructed in following steps. Let q0 is the
initial state.
If the first input symbol is a, then remove it and change its
state to q1. After this, machine move to the last input symbol, if last
input symbol is a, then machine remove it and back to first input
symbol of string. This process continue.
If the first input symbol is b, then remove it and change its
state to q5. After this, machine move to the last input symbol, if last
input symbol is b, then machine remove it and back to first input
symbol of string. This process continue.
L={ wcw R ! w ∈ {a, b}* }
Transition diagram of TM is (a, a ,R)
(a, a ,R) (b, b, R)
(b, b, R) (a, a ,L)
(B, B, L) (b, b, L)
(c, c ,R)
(c, c, L)
q1 q2 q3
(a, B ,R)
(a, B, L)
(B, B, R)
q0 q4
(a, a ,R) (a, a ,R)
(b, b, R) (b, b, R)
(b, B ,R)
(c, c ,R) q5 q6 q7 (b, B, L)
(c, c ,R) (B, B, L)
(B, B ,R)
q8 q9
Processing and Verification of TM
Acceptance
Consider string w = aabcbaa .
q0aabcbaa ⊢ Bq1abcbaa ⊢ Baq1bcbaa ⊢ Babq1cbaa ⊢
Babcq2baa ⊢ Babcbq2aa ⊢ Babcbaq2a ⊢ Babcbaaq2B ⊢
Babcbaq3aB ⊢ Babcbq4aB ⊢ Babcq4baB ⊢ Babq4cbaB ⊢
Baq4bcbaB ⊢ Bq4abcbaB ⊢ q4BabcbaB ⊢ Bq0abcbaB ⊢
BBq1bcbaB ⊢ BBbq1cbaB ⊢ BBbcq2baB ⊢ BBbcbq2aB ⊢
BBbcbaq2B ⊢ BBbcbq3aB ⊢ BBbcq4bBB ⊢ BBbq4cbBB ⊢
BBq4bcbBB ⊢ Bq4BbcbBB ⊢ BBq0bcbBB ⊢ BBBq5cbBB ⊢
BBBcq6bBB ⊢ BBBcbq6BB ⊢ BBBcq7bBB ⊢ BBBq4cBBB ⊢
BBq4BcBBB ⊢ BBBq0cBBB ⊢ BBBcq8BBB⊢ BBBcBq9BB
(machine halts at final state)
Since machine halts at final state, therefore this string is
accepted by TM.
Rejection
Consider string w = abcaa .
q0abcaa ⊢ Bq1bcaa ⊢ Bq1bcaa ⊢ Bbq1caa ⊢ Bbcq2aa ⊢ Bbcaq2a
⊢ Bbcaaq2B ⊢ Bbcaq3aB ⊢ Bbcq4aB ⊢ Bbq4caB ⊢ Bq4bcaB ⊢
q4BbcaB ⊢ Bq0bcaB ⊢ BBq5caB ⊢ BBcq6aB ⊢
BBcaq6B ⊢ BBcq7aB (machine halts at non-final state)
Since machine halts at non-final state, therefore this string is
not accepted by TM.
Ex. Construct TM to accept the language
L = { wwR ! w ∈ {a, b}* }
Transition diagram of TM is
(a, a ,R)
(b, b, R)
q6
Processing and Verification of TM
Acceptance
Consider string w = abba.
q0abba ⊢ Bq1bba ⊢ Bbq1ba ⊢ Bbbq1a ⊢ Bbbaq1B⊢ Bbbq2aB ⊢
Bbq3bBB ⊢ Bq3bbBB ⊢ q3BbbBB ⊢ Bq0bbBB ⊢ BBq4bBB ⊢
BBbq4BB ⊢ BBq5bBB ⊢ Bq3BBBB ⊢ BBq0BBB ⊢ BBBq6BB
(machine halts at final state)
Since machine halts at final state, therefore this string is accepted
by TM.
Rejection
Consider string w = abaa .
q0abaa ⊢ Bq1baa ⊢ Bbq1aa ⊢ Bbaq1a ⊢ Bbaaq1B ⊢ Bbaq2aB ⊢
Bbq3aBB ⊢ Bq3baBB ⊢ q3BbaBB ⊢ Bq0baBB ⊢ BBq4aBB ⊢
BBaq4BB ⊢ BBq5aBB (machine halts at non-final state)
Since machine halts at non-final state, therefore this string is not
accepted by TM.
Some additional problems
Construct TM for the following languages:-
(1) L = { an+2bn ! n ≥ 1}
(2) L = { anbncm ! m, n ≥ 0}
(3) L = {anbncn ! n ≥ 1}
Turing Machine
Part-5
Turing computable function
Def. A function f : Nn N is said to be Turing computable
function if there exist a Turing machine which compute
this function.
Here Nn = N×N×N……………………………×N(upto n times)
Note:
1) In the designing of Turing machine, we use unary
number to represent a number. Here we use the unary
number as a string of 1’s.
Ex. 4 = 1111, 3 = 111 and so on.
2) If the function has multiple arguments, then we separate
the arguments by 0.
Ex. Construct Turing machine for the following function
1) f(n) = n+2 n∈N
2) f(m,n) = m+n m, n ∈ N
Solution:
1) In this function, if input is 1111 then output will be 111111.
i.e. q01111 ⊢* qf111111
TM for this function will be
(1, 1 ,L)
(B, 1 ,L) (B, 1 ,L)
q0 q1 q2
(1, 1 ,L)
(1, 1 ,R) (B, B, L)
q6
Turing Machine
Part-6
Ex. Show that following function is Turing computable:-
f(m, n) = m-n m≥n
= 0 otherwise
Solution: Clearly this function is proper subtraction function.
We have to find TM corresponding to this function.
There are two cases of this function.
Case-1: If m ≥ n then value of the function is m-n. i.e. if m= 6
and n=4 then value = 2.
Case-2: If m < n then value of the function is 0. i.e. if m= 4 and
n=6 then value = 0.
Before constructing TM for this function, first we process the
input and develop rules through which machine move from initial
ID to final ID.
Case-1: when m ≥ n.
q01111011 ⊢ 1q0111011 ⊢ 11q011011 ⊢ 111q01011 ⊢
1111q0011 ⊢11110q111 ⊢1111q20y1 ⊢ 111q210y1 ⊢
111yq00y1⊢ 111y0q1y1⊢ 111y0yq11 ⊢111y0q2yy ⊢ 111yq20yy
⊢ 111q2y0yy⊢ 11q21y0yy⊢ 11yq0y0yy⊢ 11yyq00yy
⊢ 11yy0q1yy⊢ 11yy0yq1y⊢ 11yy0yyq1B⊢ 11yy0yq3yB
(machine halts at final state)
Case-2: when m < n.
q01101111 ⊢* yy0yyq111 ⊢ yy0yq2yy1 ⊢* q2Byy0yyy1 ⊢
Bq4yy0yyy1 ⊢* Byy0yyyq41 ⊢ Byy0yyyyq4B⊢ Byy0yyyq3yB
(y, y , L) (y, y , R)
(1, 1 ,R) (0, 0, L) (0, 0, R)
(y, y, R) (y, y , R) (1, y, R)
(1, y , R)
(B, B ,L)
(B, B ,L)
q3
Ex. Construct Turing machine for the following function
f(m,n) = m*n m, n ∈ N
Solution: This function multiply two numbers.
If inputs are 2 and 3 then output will be 6.
Processing:
q0110111 ⊢xq110111 ⊢ x1q10111 ⊢ x10q2111 ⊢ x10yq211 ⊢
x10yyq21 ⊢ x10yyyq2B ⊢ x10yyq3yB ⊢ x10yy1q4B ⊢ x10yy1B q5B⊢
x10yy1q6B1⊢ x10yyq31B1⊢ x10yq3y1B1
⊢ x10y1q41B1⊢ x10y11q4B1⊢ x10y11Bq51⊢ x10y11B1q5B⊢
x10y11Bq611⊢ x10y11q6B11 ⊢ x10y1q31B11 ⊢ x10yq311B11⊢
x10q3y11B11⊢ x101q411B11⊢ x1011q41B11⊢ x10111q4B11⊢
x10111Bq511⊢ x10111B1q51⊢ x10111B11q5B⊢ x10111B1q611⊢
x10111Bq6111⊢ x10111q6B111⊢ x1011q31B111⊢ x101q311B111⊢
x10q3111B111 ⊢ x1q30111B111 ⊢ xq310111B111 ⊢ q3x10111B111⊢
xq010111B111 ⊢ *xxq00111B111111 ⊢xxBq7111B111111
⊢xxBBq711B111111 ⊢xxBBBq71B111111 ⊢xxBBBBq7B111111
⊢xxBBBBBq8111111 (machine halts at final state)
Therefore, the TM corresponding this function will be
constructed as following:-
(1, 1 ,L)
(0, 0, L) (1, 1 ,R)
(1, 1 ,R) (1, y ,R) (1, 1 ,R)
(B, 1, L)
(0,B, R) (x, x, R)
(B, B, L)
(B, B ,R) q6
q7 q08
a b c c a b a a a b
R/W head
a b c c a b a a a b
R/W head
q
Finite State
Control
Multi-tape Turing Machine(TM)
This type of machine consists of n number of tapes.
Since number of tapes is n, therefore the number of
heads will also be n.
Transition function will be
δ: Q×Γn Q×Γn×{L, R}n
Where
Γn = Γ× Γ× Γ×……………….. × Γ(upto n times)
{L, R}n = {L, R}× {L, R}× {L, R}×……………….. × {L, R}
(upto n times)
Multi-head Turing Machine(TM)
Model of TM
Tape
a b c c a b a a a b
R/W head R/W head
R/W head
q
Finite State
Control
Multi-head Turing Machine(TM)
This type of machine consists of one tape with n heads.
Transition function will be
δ: Q×Γn Q×Γn×{L, R}n
Where
Γn = Γ× Γ× Γ×……………….. × Γ(upto n times)
{L, R}n = {L, R}× {L, R}× {L, R}×……………….. × {L, R}
(upto n times)
Multi-dimensional Turing Machine(TM)
Tow-dimensional tape
Model of TM
L Head R
D
q
Finite State
Control
Multi-dimensional Turing Machine(TM)
This type of machine consists of one multi-dimensional
tape with one heads.
The head of machine move in many directions.
If tape is n-dimensional then head move in 2n directions.
Transition function of two-dimensional TM is defined as
δ: Q×Γ Q×Γ×{L, R, U, D}
Where
L Left direction
R Right direction
U Up direction
D Down direction
Recursive and Recursive Enumerable language
Recursive language:
A language L is said to be recursive if there exists a Turing
machine M which accepts all the strings w belong into L and
rejects all the strings which do not belong into L.
Y
w
M
N
Y Y
Y w
w M
M N
N N
M’
Now, we construct a TM M’ using M as above.
Clearly, if w is accepted by M then w is rejected by M’ and if w is
rejected by M then w is accepted by M’. Since L is accepted by M,
therefore complement of L i.e. L is accepted by M’.
Since there exists a TM M’ corresponding to L, therefore L is
recursive language.
Theorem: If L and L are recursive enumerable languages then L will be
recursive language.
Proof: Since L and L are recursive enumerable language, therefore there exists
TM M1 and M2 corresponding to L and L respectively. M1 accepts all strings
belong into L and M2 accepts all strings belong into and L. These are
w Y w Y
M1 Y
M1
w Y Y N
M2 M2
M
w Y
M1 Y Y
N w
M1 N
w Y Y
M2 E
N w M2
N
N
Consider a string w ∈ L1 L2. Then w ∈ L1 and w ∈ L2.
Since w ∈ L1 therefore it is accepted by M1. therefore it is also accepted by M. Since w
∈ L2 therefore it is accepted by M2. Clearly, therefore it is also accepted by M.
But if w ∉ L1 L2 then w is either not belong into L1 or not belong into L2. therefore it
is either not accepted by M1 or not accepted by M2. Clearly, therefore w is not
accepted by M.
Hence M is a machine which accepts all strings belong into L1 L2 and rejects all
strings which do not belong into L1 L2 .
Therefore L1 L2 is recursive language.
Theorem: The intersection of two recursive enumerable languages is also
recursive enumerable i.e. if L1 and L2 are recursive enumerable then L1 L2
will be also recursive enumerable.
Proof: Since L1 and L2 are recursive enumerable languages then there exists
TM M1 and M2 corresponding to L1 and L2 respectively are of the form:
E-> enable M
w Y
M1 M1 Y
w Y
w Y E Y
M2 M2
w