UNIT – IV
1. Explain Turing Machine.
A Turing Machine (TM) is a mathematical model which consists of an infinite length tape
divided into cells on which input is given. It consists of a head which reads the input tape. A
state register stores the state of the Turing machine. 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.
A Turing machine consists of following components
◻An infinite Tape. This Tape is divided in cells. Each cell has an input character which is
processed by the machine as an input/output. These characters belong to a finite set of alphabets
Σ. It behaves like an infinite array of [Link] blank cells in the tape stores the character B.
◻Control State: This belongs to a finite set of states Q. this along with the character read from
the current cell defines the state of machine.
◻Head: The Head points to a cell in the machine. It can read/write from/on the cell it is
pointing to. The movement of head is controlled by the machine itself. The head can move
one step towards left or right at a time.
· Q is a finite set of states
· T is the tape alphabet (symbols which can be written on Tape)
· B is blank symbol (every cell is filled with B except input alphabet initially)
· is the input alphabet (symbols which are part of input alphabet)
· δ is a transition function which maps Q × T → Q × T × {L,R}. Depending on its present
state and present tape alphabet (pointed by head pointer), it will move to new state, change
the tape symbol (may or may not) and move head pointer to either left or right.
· q0 is the initial
F is the set of final states. If any state of F is reached, input string is accepted.
2. State the difference between finite automata and turing machine
Finite Automata Turing Machine
Finite state machines describe a Turing Machines are the mathematical
small class of languages where no description of a computer and accept a
memory is needed. much larger class of languages than
FSMs do.
Finite state machines have lower Turing Machines have has more
computational power than the Turing computational power than FSM
machine
Finite state machines describe the Turing Machines describe a much larger
class of regular languages class of languages, the class of
recursively enumerable languages
A finite state machine is just a set of A Turing machine is a finite state
states and transitions. The only machine plus a tape memory. Each
memory it has is what state it is in. transition may be accompanied by an
Thus, the number of memory states operation on the tape (move, read, write).
is... finite Its total possible configurations are
arbitrarily large, regardless of the size of
the program; it expands towards infinity.
The transition function in finite The transition function in turing machine
automata can be represented by: δ : can be represented by: δ : Q × T → Q ×
Q × Σ* → Q T × {L, R} where L and R specify the
left and right movement of the tape head.
3. Write the variants of Turing machines
Many other TM variations are equivalent to the original TM. This
includes the following −
i) Multiple track Turing Machine:
● A k-tack Turing machine(for some k>0) has k-tracks and one R/W head that
reads and writes all of them one by one.
● A k-track Turing Machine can be simulated by a single track Turing machine
ii) Two-way infinite Tape Turing Machine:
● Infinite tape of two-way infinite tape Turing machine is unbounded in
both directions left and right.
● Two-way infinite tape Turing machine can be simulated by one-way
infinite Turing machine(standard Turing machine).
iii) Multi-tape Turing Machine:
● It has multiple tapes and controlled by a single head.
● The Multi-tape Turing machine is different from k-track Turing machine
but expressive power is same.
● Multi-tape Turing machine can be simulated by single-tape Turing machine
iv) Multi-tape Multi-head Turing Machine:
● The multi-tape Turing machine has multiple tapes and
multiple heads
● Each tape controlled by separate head
● Multi-Tape Multi-head Turing machine can be simulated by
standard Turing machine.
v) Multi-dimensional Tape Turing Machine:
● It has multi-dimensional tape where head can move any direction
that is left, right, up or down.
● Multi dimensional tape Turing machine can be simulated
by one-dimensional Turing machine
vi) Multi-head Turing Machine:
● A multi-head Turing machine contain two or more heads to read
the symbols on the same tape.
● In one step all the heads sense the scanned symbols and move or
write independently.
● Multi-head Turing machine can be simulated by single head
Turing machine.
vii) Non-deterministic Turing Machine:
● A non-deterministic Turing machine has a single, one way
infinite tape.
● For a given state and input symbol has atleast one choice to move
(finite number of choices for the next move), each choice several
choices of path that it might follow for a given input string.
● A non-deterministic Turing machine is equivalent to
deterministic Turing machine
Multi-tape Turing Machine:
Multi-tape Turing Machines have multiple tapes where each tape is accessed with a separate
head. Each head can move independently of the other heads. Initially the input is on tape 1 and
others are blank. At first, the first tape is occupied by the input and the other tapes are kept blank.
Next, the machine reads consecutive symbols under its heads and the TM prints a symbol on
each tape and moves its heads.
A Multi-tape Turing machine can be formally described as a 6-tuple (Q, X, B, δ, q0, F) where −
● Q is a finite set of states
● X is the tape alphabet
● B is the blank symbol
● δ is a relation on states and symbols where
δ: Q × Xk → Q × (X × {Left_shift, Right_shift, No_shift })k
where there is k number of tapes
● q0 is the initial state
● F is the set of final states
Note − Every Multi-tape Turing machine has an equivalent single-tape Turing machine.
Multi-head Turing machine
It has a number of heads instead of one.
Each head independently reads/ writes symbols and moves left/right or keeps stationery.
Off-line Turing Machine
An offline Turing machine has two tapes, which are as follows −
● One tape is read-only and contains the input.
● The other is read-write and is initially blank.
MULTI-TRACK TURING MACHINE:
Multi-track Turing machines, a specific type of Multi-tape Turing machine,
contain multiple tracks but just one tape head reads and writes on all tracks. Here, a
single tape head reads n symbols from n tracks at one step. It accepts recursively
enumerable languages like a normal single-track single-tape Turing Machine accepts.
A Multi-track Turing machine can be formally described as a 6-tuple (Q, X, ∑, δ, q0, F) where
−
● Q is a finite set of states
● X is the tape alphabet
● ∑ is the input alphabet
● δ is a relation on states and symbols where
δ(Qi, [a1, a2, a3,....]) = (Qj, [b1, b2, b3,....], Left_shift or Right_shift)
● q0 is the initial state
● F is the set of final states
Note − For every single-track Turing Machine S, there is an equivalent multi-track Turing
Machine M such that L(S) = L(M).
4. Construct a turing machine for L={0n1n|n>=1}
· Q = {q0,q1,q2,q3,q4} where q0 is initial state.
· T = {0,1,X,Y,B} where B represents blank.
· ∑ = {0,1}
· F = {q4}
The Transition table for L={0n1n |n>=1}
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 - - - - -
5. Construct a Turing Machine for language L = {0n1n2n
The language L = {0n1n2n | n≥1} represents a kind of language where we
use only 3 characters, i.e., 0, 1 and 2. In the beginning language has
some number of 0’s followed by equal number of 1’s and then followed by
equal number of 2’s. Any such string which falls in this category will be
accepted by this language. The beginning and end of the string is marked
by $ sign.
Examples – Input: 001122 – Accepted
Input: 0001112222 – Not Accepted
Assumption: We will replace 0 by X, 1 by Y and 2 by Z
Approach used –
First replace a 0 from front by X, then keep moving right till you find a 1
and replace this 1 by Y. Again, keep moving right till you find a 2, replace
it by Z and move left. Now keep moving left till you find a X. When you
find it, move a right, then follow the same procedure as above.A condition
comes when you find an 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. If not then string is not accepted. If we reach $ then
the string is accepted.
· Step-1:
Replace 0 by X and move right, Go to state Q1.
· Step-2:
Replace 0 by 0 and move right, Remain on
same state Replace Y by Y and move right,
Remain on same state Replace 1 by Y and move
right, go to state Q2.
· Step-3:
Replace 1 by 1 and move right, Remain on
same state Replace Z by Z and move right,
Remain on same state Replace 2 by Z and
move right, go to state Q3.
· Step-4:
Replace 1 by 1 and move left, Remain on
same state Replace 0 by 0 and move left,
Remain on same state Replace Z by Z and
move left, Remain on same state Replace Y by
Y and move left, Remain on same state
Replace X by X and move right, go to state Q0.
· Step-5:
If symbol is Y replace it by Y and move right and Go to
state Q4 Else go to step 1
· Step-6:
Replace Z by Z and move right, Remain on
same state Replace Y by Y and move right,
Remain on same state
If symbol is $ replace it by $ and move left, STRING IS ACCEPTED, GO
TO FINAL STATE Q5
6. Construct a Turing Machine for language L = {wwr {0, 1}}
The language L = {wwr | w ∈ {0, 1}} represents a kind of language
where you use only 2 characters, i.e., 0 and 1. The first part of language
can be any string of 0 and
1. The second part is the reverse of the first part. Combining both these
parts out
string will be formed. Any such string which falls in this category will be
accepted by this language. The beginning and end of the string is marked
by $ sign.
For example, if the first part w = 1 1 0 0 1 then the second part w = 1 0
r
0 1 1. It is
clearly visible that w is the reverse of w, so the string 1 1 0 0 1 1 0 0 1 1
r
is a part of the given language.
Example :
Input : 0011 1100 – Accepted
Input : 10 100101 – Not Accepted
Assumption: We will replace 0 by Y and 1 by X.
Approach Used –
First check the first symbol, if it’s 0 then replace it by Y and by X if it’s
1. Then go to the end of the string. So the last symbol is the same as
the first. We replace it also by X or Y depending on it.
Now again come back to the position next to the symbol, replace from
the starting and repeat the same process as told above.
One important thing to note is that since w r is the reverse of w both of
them will have an equal number of symbols. Every time replace a nth
symbol from the beginning of the string, replace a corresponding nth
symbol from the end.
· Step-1:
If symbol is 0 replace it by Y and move right, Go to
state Q2 If symbol is 1 replace it by X and move
right, Go to state Q1
· Step-2:
If symbol is 0 replace it by 0 and move right, remain on
same state If symbol is 1 replace it by 1 and move right,
remain on same state If symbol is X replace it by X and
move right, Go to state Q3
If symbol is Y replace it by Y and move right, Go to
state Q3 If symbol is $ replace it by $ and move
right, Go to state Q3
· Step-3:
If symbol is 1 replace it by X and move left, Go to
state Q4 If symbol is 0 replace it by Y and move
left, Go to state Q5
· Step-4:
If symbol is 1 replace it by 1 and
move left If symbol is 0 replace it by
0 and move left Remain on same
state
· Step-5:
If symbol is X replace it by X and
move right If symbol is Y replace it by
Y and move right Go to state Q0
· Step-6:
If symbol is X replace it by X and
move right If symbol is Y replace it by
Y and move right Go to state Q6
ELSE
Go to step 1
· Step-7:
If symbol is X replace it by X and move right, Remain on
same state If symbol is Y replace it by Y and move right,
Remain on same state
If symbol is $ replace it by $ and move left, STRING IS ACCEPTED, GO
TO FINAL STATE Q7
7. Construct a Turing machine for L = {aibjck
In given language L = {aibjck | i>j>k; k ≥ 1}, every string of ‘a’, ‘b’ and ‘c’
have certain number of a’s, then a certain number of b’s and then a
certain number of c’s. The condition is that the count of 3rd symbols
should be atleast 1. ‘a’ and ‘b’ can thereafter be as many but count of c is
less than count of ‘b’ and count of ‘b’ is less than count of ‘a’. Assume
that string is ending with ‘$’.
Examples :
Input: aaabbc a=3,b=2,c=1 - Accepted
Input: aabbccc a=2,b=2,c=3 - Not Accepted [as it violates ]
Approach:
1. Comparing two elements by making A & D as a single element.
2. After that Comparing A & D.
3. If |C| is greater than |(A, D)|, then it is not accepted.
4. If |D| is greater than |A|, then it is not accepted.
5. Else it is accepted.
Steps:
Step-1: Convert A into X and move right and goto step 2.
Step-2: Keep ignoring A and Y and move towards the right. Convert D into
Y and move right and goto step-3.
Step-3: Keep ignoring D and Z and move towards [Link] C is found make it
Z and move left to step [Link] B is found ignore it and move left and goto
step-5.
Step-4: Keep ignoring Z, A, Y and D and move towards [Link] X is found
ignore it and move right and goto step-1.
Step-5: Keep ignoring D, Y and A and move towards the left. Ignore X,
move right and goto step-6.
Step-6: Convert A into X and move right and goto step-7.
Step-7: Keep ignoring Y and A and move towards [Link] B is found, ignore
it and move left and goto [Link] D make it Y and move right and goto
step-5.
Step-8: Stop the Machine (String is accepted)
Here, Q0 shows the initial state and Q1, Q2, Q3, Q4, Q5, Q6 shows the
transition state and Q7 shows the final state. A, C, D are the variables used
and R, L shows right and left.
Explanation:
· Using Q0, when A is found make it X and go to the right and to state Q1.
· On the state Q1, ignore all A and Y and goto right. If D found, make it Y and goto right
into next state Q2.
· In Q2, ignore all D, Z and move right. If B found ignore it, move left and goto the state Q4,
If C found make it Z move left and to Q3.
· In Q3 state, ignore all Z, D, Y, A and move left. If X found ignore it move right to Q0.
· In Q4, ignore all A, Y, D and move left. If X found ignore it move right to state Q6.
· In Q6 state, if A found make it X move right to state Q5
· In Q5, ignore all A, Y and move right. If D found make it Y and move right to state [Link]
B is found ignore it and move left to Q7
· If Q7 state is reached it will produce the result of acceptance of string.
8. Construct a Turing machine for anbn
TAPE movement is shown below:
Logic Explanation step by step:
1. Input is given in the TAPE as "aabb", "B" is BLANK symbol. R/W head will point
to first "a" (from left)
2. TM will mark first "a" with "X" (custom), and move to right, skipping all "a"s and will
stop at first "b"
3. TM will mark "b" with "Y" (custom) and move to left till it finds "X" and then stop at
one step ahead at "a"
4. TM will mark first "a" with "X", and move to right, skipping all "a"s and "Y"s and
will stop at "b"
5. TM will mark "b" with "Y" and move to left till it finds "X" and then after it there is "Y"
that means all "a"s are finished
6. TM will move to the right to check whether all "b"s are finished. If R/W reaches
"B"(BLANK) that means all "b"s are also finished. That means TM has accepted the
language
9. Explain Recursive Language/Recursive Enumerable
Language/Decidable/Recognizable Language.
Recursive/Decidable Language:
A TM decides a language if it accepts it and enters into a rejecting state for any input not in the
language. A language is recursive if it is decided by a Turing machine.
There may be some cases where a TM does not stop. Such TM accepts the language, but it does
not decide it. The language is decidable if and only if there is a machine which accepts the
string when the provided input lies in that language and rejects the string when provided input
doesn't lie in that language.
Example
● A = {hM, wi | M is a DFA and w ∈ L(M)} is decidable.
● A problem is decidable if there is an algorithm to answer it.
● Recall: An “algorithm,” formally, is a TM that halts on all inputs, accepted or not.
● Put another way, “decidable problem” = “recursive language.”
● Otherwise, the problem is undecidable.
For a decidable language, for each input string, the TM halts either at the accept or the reject
state as depicted in the following diagram
Undecidable Language:
For an undecidable language, there is no Turing Machine which accepts the language and
makes a decision for every input string w (TM can make decision for some input string though).
A decision problem P is called “undecidable” if the language L of all yes instances to P is not
decidable. Undecidable languages are not recursive languages, but sometimes, they may be
recursively enumerable languages.
Example
● The halting problem of Turing machine
● The mortality problem
● The mortal matrix problem
● The Post correspondence problem, etc.
Recursive Enumberable/Recognizable Language:
A TM accepts a language if it enters into a final state for any input string w. A language is
recursively enumerable (generated by Type-0 grammar) if it is accepted by a Turing machine.
A language can be recognizable if the TM either terminates and rejects the string or doesn't
terminate at all. This means that the TM continues with the computing when the provided input
doesn't lie in the language.
Whereas, A = {hM, wi | M is a TM and w ∈ L(M)} is recognizable
.
The major differences between a recognizable and a decidable in turning
machine are as follows −
Sr. No Turing Recognizable Turing decidable
1 A language which is Turing A language is said to be
Recognizable if there is a Machine Decidable if there is a Machine
that will halt and accept only the that will accept strings in the
strings in that language and not in language and reject strings not
that language, then that TM either in the language.
rejects, or does not halt at all.
A Language is called Turing A Language is called Turing
2 Recognizable if some Turing Decidable if some Turing
Machine recognizes it. Machine decides it.
If there exists a Turing Machine If there exists a Turing Machine
3 such that when encountering a such that when encountering a
string in that language, the string in that language, the
machine terminates and accepts machine terminates and
that string then we can say that accepts that string then we say
type of language is a Turing that type of language is Turing
recognizable. decidable.
If there exists a Turing Machine If there exists a Turing Machine
4 such that when encountering a such that when encountering a
string not in that language, the string not in that language, the
machine either terminates and machine terminates and
rejects that string or doesn’t rejects that string then we can
terminate at all then we can say it say it is Turing-Decidable.
is Turing-Recognizable.
5 It is not stronger condition than It is a stronger condition
Turing Decidable than Turing-Recognizable.
10. Closure Properties of Decidable and Turing Recognizable Languages
1. Union
Both decidable and Turing recognizable languages are closed under union. - For
decidable languages the proof is easy.
respectively. The machine for L1 ∪ L2 is designed as follows:
Suppose L1 and L2 are two decidable languages accepted by halting TMs M1 and M2
Given an input x, simulate M1 on x.
If M1 accepts then accept, else simulate M2 on x.
If M2 accepts then accept else reject.
- Now suppose L1 and L2 are two Turing recognizable languages accepted by TMs M1
and M2 respectively. Since L1 and L2 are Turing recognizable languages, therefore for
strings that do not belong to these languages, the corresponding machines may not even
halt.
The previous strategy will not work because we can have a scenario where M2 accepts x
but M1 loops forever. Here the trick is to simulate both M1 and M2 “simultaneously”.
In other words, we design a machine that executes one step of M1, followed by one step
of M2, then again one step of M1 and so on.
Recursive
If L1 is recursive and L2 is recursive then L1 ∪ L2 and L1 ∩ L2 are also recursive.
Use a multitape TM: • Copy input to tape 2 and tape 3 • Execute M1 on tape 2 and M2 on
tape 3 (neither will run forever; i.e. we get a result) • They will decide whether x is in L1
and/or L2 • Test if both M1 and M2 accepted (intersection) • Test if one of M1 and M2
accepted (union) If L1 and L2 are recursive then the difference L1 - L2 = L1 ∩ L2' is
recursive.
2. Concatenation
Both decidable and Turing recognizable languages are closed under concatenation. The
proof for decidable languages is similar.
Let L1 and L2 be two Turing recognizable languages. Given an input w, use
nondeterminism and guess a partition w (say w = xy). Now run the respective Turing
machines of L1 and L2 on x and y respectively. If both accept then accept else reject.
3. Star
Both decidable and Turing recognizable languages are closed under star operation. This
is also similar to concatenation. Nondeterministically first guess a number k, and then
guess a k partition of the given input. Now for each string in the partition, check whether
it belongs to the original language.
4. Intersection
Both decidable and Turing recognizable languages are closed under intersection. Run the
TMs of both the languages on the given input. accept if and only if both the machines
accept. In the case of intersection we can run the TMs of L1 and L2 one after the other
(as opposed to union).
5. Complementation
- Decidable languages are closed under complementation. To design a machine for the
complement of a language L, we can simulate the machine for L on an input. If it accepts
then accept and vice versa. - Turing recognizable languages are not closed under
complement.
11. TURING MACHINE - HALTING PROBLEM
Input − A Turing machine and an input string w.
Problem − Does the Turing machine finish computing of the string w in a finite number of
steps? The answer must be either yes or no.
Proof − At first, we will assume that such a Turing machine exists to solve this problem and
then we will show it is contradicting itself. We will call this Turing machine as a Halting
machine that produces a ‘yes’ or ‘no’ in a finite amount of time. If the halting machine finishes
in a finite amount of time, the output comes as ‘yes’, otherwise as ‘no’. The following is the
block diagram of a Halting machine –
Now we will design an inverted halting machine (HM)’ as −
● If H returns YES, then loop forever.
● If H returns NO, then halt.
The following is the block diagram of an ‘Inverted halting machine’ −
Further, a machine (HM)2 which input itself is constructed as follows −
● If (HM)2 halts on input, loop forever.
● Else, halt.
Here, we have got a contradiction. Hence, the halting problem is undecidable.
The Halting Problem is Undecidable (Turing, 1936)
✦ Example: Halting Problem: Does TM M halt on input w?
Equivalent language: AH = { | TM M halts on input w}
Need to show AH is undecidable
We know ATM = { | TM M accepts w} is undecidable
✦ Show ATM is reducible to AH
Suppose AH is decidable ⇒ there’s a decider MH for AH
Then, we can construct a decider DTM for ATM: On input , run MH on .
● If MH rejects, then REJ (this takes care of M looping on w)
● If M accepts, then ACC input ; else REJ L(DTM) = ATM ⇒ ATM is decidable! Contradiction
● If MH accepts, then simulate M on w until M halts
⇒ AH is undecidable