0% found this document useful (0 votes)
113 views24 pages

Turing Machines

Uploaded by

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

Turing Machines

Uploaded by

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

Turing Machines

Reading: Chapter 8

1
Turing Machines are…
 Very powerful (abstract) machines that could simulate any modern-
day computer (although very, very slowly!)
For every input,
answer YES or NO
 Why design such a machine?
 If a problem cannot be “solved” even using a TM, then it implies that the
problem is undecidable

 Computability vs. Decidability

2
Formal Definition of Turing Machine
A Turing Machine is represented by a 7-tuple T = (Q, Σ, Γ, , q0, qaccept,
qreject):
Q is a finite set of states
Σ is the input alphabet, where   Σ
Γ is the tape alphabet, a superset of Σ;   Γ
 : Q  Γ → Q  Γ  {L, R} is the transition func
q0  Q is the start state
qaccept  Q is the accept state
qreject  Q is the reject state, and qreject  qaccept

3
A Turing Machine (TM)

This is like
 M = (Q, ∑, , , q0,B,F) the CPU &
program
counter

Finite
control Tape is the
memory

Tape head
Infinite tape with tape symbols

… B B B X1 X2 X3 … Xi … Xn B B …

Input & output tape symbols


B: blank symbol (special symbol reserved to indicate data boundary)
4
Transition function You can also use:
 for R
 for L

 One move (denoted by |---) in a TM does the following:


 (q,X) = (p,Y,D)
 q is the current state X / Y,D
 X is the current tape symbol pointed by tape head q p
 State changes from q to p
 After the move:
 X is replaced with symbol Y
 If D=“L”, the tape head moves “left” by one position.
Alternatively, if D=“R” the tape head moves “right” by one position.

5
ID of a TM
 Instantaneous Description or ID :
 X1X2…Xi-1qXiXi+1…Xn means:
 q is the current state
 Tape head is pointing to Xi
 X1X2…Xi-1XiXi+1…Xn are the current tape symbols

 (q,Xi) = (p,Y,R) is same as:


X1…Xi-1qXi…Xn |---- X1…Xi-1YpXi+1…Xn
 (q,Xi) = (p,Y,L) is same as:
X1…Xi-1qXi…Xn |---- X1…pXi-1YXi+1…Xn
6
Way to check for Membership
 Is a string w accepted by a TM?

 Initial condition:
 The (whole) input string w is present in TM, preceded and followed by
infinite blank symbols
 Final acceptance:
 Accept w if TM enters final state and halts
 If TM halts and not final state, then reject

7
Example: L = {0n1n | n≥1}

 Strategy: w = 000111

B B 0 0 0 1 1 1 B B … B B X X 0 Y Y 1 B B …
… …

… B B X 0 0 1 1 1 B B … B B X X X Y Y 1 B B …
… …
… B B X 0 0 Y 1 1 B B … B B X X X Y Y Y B B …

… B B X X 0 Y 1 1 B B …
B B X X X Y Y Y B B …

Accept 8
TM for {0n1n | n≥1}
B B X 0 0 Y 1 1 B B …

Y / Y,R 1. Mark next unread 0 with X and move right


0 / 0,R
2. Move to the right all the way to the first unread
1, and mark it with Y
0 / X,R
q0 q1 3. Move back (to the left) all the way to the last
marked X, and then move one position to the
1 / Y,L right
Y / Y,R
4. If the next position is 0, then goto step 1.
X / X,R
q2 Else move all the way to the right to ensure
there are no excess 1s. If not move right to the
Y / Y,R q3
next blank symbol and stop & accept.
B / B,R Y / Y,L
0 / 0,L
q4
9
TM for {0n1n | n≥1}
*state diagram representation preferred

Next Tape Symbol

Curr. 0 1 X Y B
State
q0 (q1,X,R) - - (q3,Y,R) -

q1 (q1,0,R) (q2,Y,L) - (q1,Y,R) -

q2 (q2,0,L) - (q0,X,R) (q2,Y,L) -

q3 - - - (q3,Y,R) (q4,B,R)

*q4 - -- - - -

Table representation of the state diagram 10


Design a Turing Machine for L=0(1)*0
1 / Y,R

0 / X,R 0 / X,R B/ B,R


q0 q1 q2 q3

Next Tape Symbol

Curr. 0 1 X Y B
State
q0 (q1,X,R) - - - -

q1 (q2,0,R) (q1,Y,R) - - -

q2 - - - - (q3,B,R)

q3 - - - -

11
Design a Turing Machine for L=0n1n2n
 Possible Solution
 Here we will see how to make a Turing machine for language L = {0n1n2n | n ≥ n}. So
this represents a kind of language where we will use only three characters 0s, 1s and 2s.
The w is a string. So if w = 000111222, The Turing machine will accept it.

 To solve this, we will use this approach. First replace one 0 from front by x, then keep
moving right till we get one 1 and replace this 1 by y. Again, keep moving right till we
get one 2, replace it by z and move left. Now keep moving left till we will find one x.
When we will get it, move a right, then follow the same procedure as above.

 A condition comes when we find a x immediately followed by a y. At this point we


keep moving right and keep on checking that all 1’s and 2’s have been converted to y
and z or not. If not, then string is not accepted. If we reach $ then string is accepted.
12
13
TMs for calculations
 TMs can also be used for calculating values
 Like arithmetic computations
 Eg., addition, subtraction, multiplication, etc.

14
Turing Machine for addition
 A number is represented in binary format in different finite automata.
 For example, 5 is represented as 101. However, in the case of addition using a Turing machine, a unary format is followed.
 In unary format, a number is represented by either all ones or all zeroes. For example, 5 will be represented by a sequence of
five zeroes or five ones: 5 = 1 1 1 1 1 or 5 = 0 0 0 0 0. Let’s use zeroes for representation.

 A Turing machine can be designed to perform addition by using its tape to represent the numbers to be
added and its states to control the addition process. A high-level description of the process:
 The two numbers to be added are placed on the tape, separated by a blank symbol.
 The Turing machine starts at the leftmost digit of the first number and moves to the right, one digit at a time, checking the value
of each digit.
 If a digit is 0, the machine writes a 0 in the corresponding position of the sum. If it’s 1, it writes a 1.
 If both digits are 1, the machine writes a 0 and adds a carry of 1 to the next position.
 When the Turing machine reaches the end of the input, it adds any remaining carry and writes the result on the tape.

15
Add 2+3
 Step-1: Convert 0 into X and go to step 2. If symbol is “c” then convert it into
blank(B), move right and go to step 6.
 Step-2: Keep ignoring 0’s and move towards right. Ignore “c”, move right and go
to step 3.
 Step-3: Keep ignoring 0’s and move towards right. Convert a blank (B) into 0,
move left and go to step 4.
 Step-4: Keep ignoring 0’s and move towards left. Ignore “c”, move left and go to
step 3.
 Step-5: Keep ignoring 0’s and move towards left. Ignore an X, move right and go
to step 1.
 Step-6: End.
16
Solution

17
Draw a Turing machine that subtracts two numbers.
 Step-1. If 0 found convert 0 into X and go right
then convert all 0’s into 0’s and go right.
 Step-2. Then convert C into C and go right then
convert all X into X and go right.
 Step-3. Then convert 0 into X and go left then
convert all X into X and go left.
 Step-4. Then convert C into C and go left then
convert all 0’s into 0’s and go left then convert
all X into X and go right and repeat the whole
process.
 Step-5. Otherwise, if C found convert C into C
and go right then convert all X into B and go
right then convert 0 into 0 and go left and then
stop the machine.
18
Solution

 Here, q0 shows the initial state and q1, q2, q3, q4, q5are the
transition states and q6shows the final state. And X, 0, C are the
variables used for subtraction and R, L shows right and left.
19
Draw a Turing machine that subtract two numbers m and n,
where m is greater than n.
 Step-1. If 0 found convert all 0’s into 0’s and go right
then convert C into C and go right
 Step-2. If X found then convert all X into X and go
right or if 0 found then convert 0 into X and go left
and go to next step otherwise go to 5th step
 Step-3. Then convert all X into X and go left then
convert C into C and go left
 Step-4. Then convert all 0’s into 0’s and go left then
convert B into B and go right then convert 0 into B
and go right and repeat the whole process
 Step-5. Otherwise if B found convert B into B and go
left then convert all X into B and go left then convert
C into B and go left and then stop the machine.
20
Solution
 Here, q0 shows the initial state and
q1, q2, q3, q4, q5are the transition
states and q6shows the final state.
And B, X, 0, C are the variables
used for subtraction(m>n) and R, L
shows right and left and B variable
is a input symbol

21
Problem: Draw a Turing machine that multiply two numbers.
 Step-1. First ignore 0’s, C and go to right & then if B found convert
it into C and go to left.
 Step-2. Then ignore 0’s and go left & then convert C into C and go
right.
 Step-3. Then convert all X into X and go right if 0 found convert it
into X and go to left otherwise if C found convert it into B and go
to right and stop the machine.
 Step-4. If then X found convert it into X and go left then C into C
and left then Y into Y and left.
 Step-5. Then if B found convert it into B and right then if Y into 0
and right or if C into C and right and go to step 3 and repeat the
process otherwise if 0 found after 4th step then convert it into Y
and right then Y into Y and right then C into C and right then 0 into
0 or X into X and right then C into C and right then 0 into 0 and
right then B into 0 and left then 0 into 0 and left then C into C and
left then 0 into 0 or X into X and left then C into C and left.
 Step-6. Then repeat the 5th step
Solution
 Here, q0 shows the initial state and q1, q2,
….., q10, q11are the transition states and
q12shows the final state.
 And X, Y, 0, C are the variables used for
multiplication and R, L shows right and left.

23
Exercises
 Construct a Turing Machine for language L = {wwr | w ∈ {0, 1}}
 Construct a Turing Machine for language L = {ww | w ∈ {0,1}}
 Construct Turing machine for L = {an bm a(n+m) | n,m≥1}

24

You might also like