Turing Machines
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
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 …
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
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 …
Curr. 0 1 X Y B
State
q0 (q1,X,R) - - (q3,Y,R) -
q3 - - - (q3,Y,R) (q4,B,R)
*q4 - -- - - -
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.
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