Turing Machine
Introduction
Around 1936, when there were no computers, Alen Turing proposed a model of an abstract machine called the Turing machine which could perform any computational process carried out by the present day’s computer. The machine was named after Turing, and it is called the Turing machine. The Turing machine is the machine format of unrestricted language, i.e., all types of languages are accepted by the Turing machine.
Based on the Turing machine, a new theory called the ‘theory of undecidable problems’ is developed. These types of problems cannot be solved by any computer.
8.1 Basics of Turing Machine
The Turing machine, in short TM, is defined by 7 touples
(Q, Σ, Γ, δ, q 0 , B, F)
where
Q : Finite set of states
Σ : Finite set of input alphabets
Γ : Finite set of allowable tape symbol
δ : Transitional function
q 0 : Initial state
B : A symbol of Γ called blank
F : Final state
and δ is a mapping from Q × Γ → (Q × Γ × {L, R, H}).
This denotes that from one state, by getting one input from the input tape, the machine moves to a state. It writes a symbol on the tape and moves to left, right or halts.
8.1.1 Mechanical Diagram
A TM consists of an input tape, a read–write head, and finite control. The input tape contains the input alphabets with an infinite number of blanks at the left and the right hand side of the input symbols. The read–write head reads an input symbol from the input tape and sends it to the finite control. The machine must be in some state. In the finite control, the transitional functions are written. According to the present state and the present input, a suitable transitional function is executed. Upon execution of a suitable transitional function, the following operations are performed.
Fig. 8.1 Mechanical Diagram of Turing Machine
- The machine goes into some state.
- The machine writes a symbol in the cell of the input tape from where the input symbol was scanned.
- The machine moves the reading head to the left or right or halts.
Mechanical diagram of Turing Machine is shown in Fig. 8.1 . Two-way finite automata has a movement in both the left and right side like the TM. But, in the TM, both the read and write operations are done from and on the input tape, but for two-way finite automata only the read operation is done, and no write operation is done. A string is accepted by a TM if the string is totally traversed and the machine halts. A string is accepted by a two-way finite automata if the string is totally traversed and the machine reaches to a final state.
8.1.2 Instantaneous Description (ID) in Respect of TM
The ID of the TM remembers the following at a given instance of time.
- The contents of all the cells of the tape, starting from the rightmost cell up to at least the last cell, containing a non-blank symbol and containing all cells up to the cell being scanned.
- The cell currently being scanned by the read–write head and
- The state of the machine.
The following example shows the read–write head movement of the TM.
Example 8.1
Show the movement of the read–write head and the content of the input tape for the string ‘abba’ traversed by the following TM
(The left-side diagram is before traversing and the right-side diagram is after traversing.)
Solution:
The following are some examples of the TM that accepts some languages.
Example 8.2
Design a TM to accept the language L = {a n b n , n ≥ 1}. Show an ID for the string ‘aaabbb’ with tape symbols.
Solution: The string consists of two types of input alphabets, ‘a’ and ‘b’. The number of ‘a’ is equal to the number of ‘b’. All the ‘a’s will come before ‘b’. In the language set, there is at least one ‘a’ and one ‘b’. The TM can be designed as follows.
When the leftmost ‘a’ is traversed, that ‘a’ is replaced by X and the head moves to one right.
The transitional function is
δ(q 0 , a) → (q 1 , X, R)
Then, the machine needs to search for the leftmost ‘b’. Before that ‘b’, there exist (n – 1) numbers of ‘a’. Those ‘a’ are traversed by
δ(q 1 , a) → (q 1 , a, R)
When the leftmost ‘b’ is traversed, the state is q 1 . That ‘b’ is replaced by Y, the state is changed to q 2 , and the head is moved to one left. The transitional functional is
δ(q 1 , b) → (q 2 , Y, L)
Then, it needs to search for the second ‘a’ starting from the left. The first ‘a’ is replaced by X, which means the second ‘a’ exists after X. So, it needs to search for the rightmost ‘X’. After traversing the leftmost ‘b’, the head moves to the left to find the rightmost X. Before that, it has to traverse ‘a’. The transitional function is
δ(q 2 , a) → (q 2 , a, L)
After traversing all the ‘a’ we get the rightmost ‘X’. Traversing the X the machine changes its state from q 2 to q 0 and the head moves to one right. The transitional function is
δ(q 2 , X) → (q 0 , X, R)
From the traversal of the second ‘b’ onwards, the machine has to traverse ‘Y’. The transitional function is
δ(q 1 , Y) → (q 1 , Y, R)
Similarly, after traversing ‘b’, the machine has to traverse some Y to get the rightmost ‘X’.
δ(q 2 , Y) → (q 2 , Y, L)
When all the ‘a’s are traversed, the state is q 0 , because before that state was q 2 and the input was X. The head moves to one right and gets a Y. Getting a Y means that all the ‘a’s are traversed and the same number of ‘b’s are traversed. Traversing right, if at last the machine gets no ‘b’ but a blank ‘B’, then the machine halts. The transitional functions are
ID for the String ‘aaabbb’
Example 8.3
Design a TM to accept the language
L = {WW R , where W ∊ (a, b) + }
Show an ID for the string ‘abaaaaba’ with tape symbols.
Solution: W R is the reverse of W. If W starts with ‘a’ or ‘b’, W R ends with ‘a’ or ‘b’, respectively. If W ends with ‘a’ or ‘b’, W R starts with ‘a’ or ‘b’, respectively. The TM can be designed as follows.
If the string W starts with ‘a’, upon traversal, that ‘a’ is replaced by X with a state change from q 0 to q 1 , and the head moves to one right. The transitional function is
δ(q 0 , a) → (q 1 , X, R)
W R ends with ‘a’ if W starts with ‘a’. The machine needs to search the end ‘a’ of W R . Before that, the machine needs to traverse the end symbols of W and the beginning symbols of W R . The transitional functions are
δ(q 1 , a) → (q 1 , a, R)
δ(q 1 , b) → (q 1 , b, R)
After the end symbol of W R , there exists the blank symbol B. In the traversal process, if the machine gets a B, it traverses back to the left side and gets the end symbol of W R . The transitional functions are
δ(q 1 , B) → (q 2 , B, L)
δ(q 2 , a) → (q 3 , X, L)
Now, the machine needs to search for the second symbol of W. Before that, it has to traverse the beginning symbols of W R and the end symbols of W. The transitional functions are
δ(q 3 , a) → (q 3 , a, L)
δ(q 3 , b) → (q 3 , b, L)
When the machine gets the rightmost X, it recognizes that the next symbol of W exists after that ‘X’. The transitional function is
δ(q 3 , X) → (q 0 , X, R)
If the string W starts with ‘b’, the transitions are the same as the previous one but with some states changed. The transitional functions are
After the first traversal, i.e., from the second traversal onwards, the machine need not traverse up to the end of W R . In state q 1 (W starts with ‘a’) or q 4 (W starts with ‘b’), if the machine gets X or Y, it traverses back to the left to point the rightmost ‘a’ or ‘b’. The transitional functions are
When all the symbols of W and W R are traversed, the machine gets an X or Y in the state q 0 . The machine halts if in state q 0 it gets an X or Y. The transitional functions are
The transitional functions can be given in a tabular format as follows.
ID for the String ‘abaaaaba’: (Here, the symbols in bold represent the read–write head position.)
Example 8.4
Design a TM to accept the language L = {set of all palindromes over a, b}. Show the IDs for the null string, ‘a’, ‘aba’, and ‘baab’.
Solution: Palindromes are of two types:
- Odd palindromes, where the number of characters is odd
- Even palindrome, where the number of characters is even. A null string is also a palindrome.
A string which starts with ‘a’ or ‘b’ must end with ‘a’ or ‘b’, respectively, if the string is a palindrome.
If a string starts with ‘a’, that ‘a’ is replaced by a blank symbol ‘B’ upon traversal with a state change from q 1 to q 2 and the right shift of the read–write head. The transitional function is
δ(q 1 , a) → (q 2 , B, R)
Then, the machine needs to search for the end of the string. The string must end with ‘a’, and that ‘a’ exists before a blank symbol B at the right hand side. Before getting that blank symbol, the machine needs to traverse all the remaining ‘a’ and ‘b’ of the string. The transitional functions are
The machine now needs to search for the second symbol (from the starting) of the string. That symbol exists after the blank symbol B, which is replaced at the first. Before that the machine needs to traverse the remaining ‘a’ and ‘b’ of the string. The transitional functions are
If the string starts with ‘b’, the transitional functions are the same as ‘a’ but some states are changed. The transitional functions are
When all ‘a’ and ‘b’ are traversed and replaced by B, the states may be one of q 3 or q 6 , if the last symbol traversed is ‘a’ or ‘b’, respectively. Transitional functions for acceptance are
A null string is also a palindrome. On the tape, a null symbol means blank B. In state q 1 , the machine gets the symbol. The transitional function is
δ(q 1 , B) → (q 7 , B, H)
The transitional functions in tabular form are represented as follows.
ID for Null String
ID for the String ‘a’
ID for the String ‘aba’
ID for the String ‘baab’
Example 8.5
Design a TM to accept the language L = a n b n c n , where n ≥ 1.
Solution: This is a context-sensitive language. The language consists of a, b, and c. Here, the number of ‘a’ is equal to the number of ‘b’ which is equal to the number of ‘c’. n number ‘c’ is followed by n number of ‘b’ which are followed by n number of ‘a’. The TM is designed as follows. For each ‘a’, search for ‘b’ and ‘c’ and again traverse back to the left to search for the leftmost ‘a’. ‘a’ is replaced by ‘X’, ‘b’ is replaced by ‘Y’, and ‘c’ is replaced by ‘Z’.
When all the ‘a’ are traversed and replaced by ‘X’, the machine traverses the right side to find if any untraversed ‘b’ or ‘c’ is left or not. If not, the machine gets Y and Z and at last a blank. Upon getting the blank symbol, the machine halts.
The transitional functions are
Upon executing the last transitional function, the machine halts.
The transitional functions in tabular form are represented as follows.
Example 8.6
Design a TM to perform the concatenation operation on string of ‘1’. Show an ID for w 1 = 111 and w 2 = 1111.
Solution: The TM does the concatenation operation on two strings w 1 and w 2 . These two strings are placed on the tape of the TM separated by the blank symbol B. After traversal, the blank symbol is removed and the two strings are concatenated.
The transitional functions are
[This machine is also applicable for performing Z = X + Y , where X = | w 1 | and Y = | w 2 |.]
ID for w 1 = 111 and w 2 = 1111 and the result is 1111111
Example 8.7
Design a TM to perform the following operation
f(x, y) = x –y, where x > y
Show an ID for 4 – 2 = 2.
Solution: This performs the subtraction operation. Here, x and y both are integer numbers. Let x = |W 1 | and y = |W 2 |, where W 1 and W 2 are the strings of ‘1’. The two strings are placed on the input tape separated by B.
Starting traversal from the left hand side ‘1’, the machine moves towards the right. It traverses B with a state changed from q 0 to q 1 . In state q 1 , it again traverses right to find B, that is, the end of the second string. It changes its state and traverses left and changes the rightmost ‘1’ by B (with state change and traversing left). It traverses the separating ‘B’ symbol and all ‘1’s of the first string and finds the beginning of the first string, which starts after B at the left hand side. It changes the first ‘1’ by B and again traverses right. This process continues till all the ‘1’s of the second string are replaced by ‘B’.
The transitional functions are
ID for 4 – 2 = 2
Example 8.8
Design a TM to accept the string L = (a, b)*, where N(a) + N(b) = even.
Solution: The string consists of any combination of ‘a’ and ‘b’. In the string, the total number of characters is even. The TM is designed as follows. If the machine gets ‘a’ or ‘b’, it moves right with the state changed from q 0 to q 1 . In state q 1 , if the machine gets ‘a’ or ‘b’, it moves right with state changed from q 1 to q 0 . In state q 0 , if the machine gets a blank symbol, it halts.
The transitional functions are
Example 8.9
Design a TM for infinite loop.
Solution: An infinite loop means the machine does not halt. It can be designed in the following way.
Let the string consist of only ‘a’. If ‘a’ is traversed in state q 1 , the machine moves right. After all the ‘a’ of the string are traversed, B is traversed and the machine moves left and changes the state to q 2 . Getting ‘a’ in state q 2 , the machine moves left again to find the beginning of the string.
When the machine gets ‘B’, it moves right again with changed state from q 2 to q 1 . This process continues and the machine falls in an infinite loop.
The transitional functions are
Example 8.10
Design a TM to perform the function f(x) = x + 1. x > 0.
Solution: The function adds a ‘1’ with the value of x. If one blank symbol at the right hand side is replaced by ‘1’, then the machine can be easily designed.
The transitional functions are
Example 8.11
Design a TM which copies a string of ‘0’ and pastes it just after the string. Show an ID for the string 00.
Solution: We have to design a TM which performs copy paste operation
The machine is designed in the following way.
- Replace each ‘0’ of the given string by X.
- Go to the right and find the blank and traverse left and replace the rightmost X by 0 again.
- Then traverse to the right and replace the first ‘B’ after the rightmost ‘0’ by ‘0’ and traverse left. Repeat the last two steps until there are no more x.
The transitional functions are
ID for the String 00
Example 8.12
Design a TM which performs 1’s complement operation on binary string.
Solution: A complement operation means that ‘0’ is converted to ‘1’ and ‘1’ is converted to ‘0’.
The transitional functions for the TM are
Example 8.13
Design a TM to perform 2’s complement operation on binary string. Show the IDs for the string 010 and 101.
Solution: To calculate 2’s complement, first a binary string is converted into 1’s complement, and then with that 1’s complement 1 is added to get 2’s complement. If in the 1’s complement the rightmost bit is ‘0’, then that ‘0’ is converted into ‘1’. If the rightmost bit is ‘1’, then that ‘1’ is converted into ‘0’ and traverses left to find a ‘0’. If it gets ‘1’, then that ‘1’ is converted into ‘0’ again. If it gets ‘0’, then that ‘0’ is converted into ‘1’ and traverses left to find the leftmost symbol of the string.
The TM is designed in the following way.
Starting from state q 1 if the machine gets ‘1’ or ‘0’, that is converted into ‘0’ or ‘1’, respectively, and the machine traverses right. When the machine gets B, it changes its state from q 1 to q 2 and traverses left. If it gets ‘0’, that is converted into ‘1’ with the state changed from q 2 to q 3 and traverses left to find the leftmost symbol.
ID for 010
ID for 101
Example 8.14
Design a TM for a set of all strings with equal number of ‘a’ and ‘b’.
Solution: The machine is designed as follows.
The machine first finds an ‘a’, replaces this by X and moves left. When it gets blank, it again moves right to search for ‘b’ and replaces it by Y. Again, it traverse left to search for ‘a’. The process continues till the right side blank symbol is traversed in state q 0 .
The transitional functions are
Example 8.15
Design a TM to accept the language L = a n b 2n , n > 0.
Solution: The number of ‘b’ is equal to twice the number of ‘a’. In the language, all ‘a’ appear before ‘b’. The TM is designed as follows.
Replace one ‘a’ by X and traverse right to search for the end of the string. After getting a ‘B’, traverse left and replace two ‘b’s by Y. In the state q 0 , if the machine gets Y as input it halts.
The transitional functions are
8.2 Transitional Representation of Turing Machine
The mathematical notation for a TM is (Q, Σ, Γ, δ, q 0 , B, F). In a TM, the transitional function δ consists of the form Q × Γ → (Q × Γ × {L, R, H}), where the first is a present state and the second is the present input (single character ∈ Γ). The transition produces a state, a symbol ∈ Γ written on the input tape, and the head moves either left or right or halts.
In the graphical notation of the TM, there are states. Among them, a circle with an arrow indicates a beginning state and a state with double circle indicates a final state, where the machine halts finally. The state transitions are denoted by arrows. The labels of the state transitions consist of the input symbol, the symbol written on the tape after traversal, and the direction of movement of the read–write head (left, right, or halt).
The following are some examples of the TM with the transitional representation.
Example 8.16
Design a TM by the transitional notation for the language L = a n b n , where n > 0.
Solution: When the machine traverses ‘a’, replace that ‘a’ by X and traverse right to find the leftmost ‘b’. Replace that ‘b’ by ‘Y’ and traverse left to find the second ‘a’. The second ‘a’ exists after ‘X’, which was replaced first for the first ‘a’. By this process, when n number of ‘a’ and n number of ‘b’ are traversed and replaced by X and Y, respectively, then by getting a blank (B) the machine halts.
The transitional representation of the TM is given in Fig. 8.2 .
Fig. 8.2 Transitional Representation of Turing Machine
Example 8.17
Design a TM by transitional notation for the language L = {set of all palindrome over a, b}.
Solution: A palindrome can be of two types, namely, an odd palindrome and an even palindrome. A null string is also a palindrome. There are three paths to reach to the final state with halt from q 1 , the beginning state. This is described in Fig. 8.3 .
Fig. 8.3 Turing Machine for Set of All Palindrome Over a, b
8.3 Non-deterministic Turing Machine
The concept of non-determinism is clear as we have already discussed about NDFA or NDPDA in the earlier chapters. There is also a non-deterministic TM. A non-deterministic TM is defined as (Q, Σ, Γ, δ, q 0 , B, F), where δ is Q × Γ → 2 Q X Γ X {L, R} .
Let there exist a transitional function δ(q 0 , 0) → (q 0 , 0, R), (q 1 , 1, R).
For traversing the input symbol, we have to consider two transitions and construct the following transitions accordingly.
The NTM is more powerful than the DTM. An NTM T 1 can be simulated to an equivalent DTM T 2 . One of the methods is to construct a computation tree of the NTM and perform a breadth first search of the computation tree from the root until the halt state is reached. At each level of the constructed tree, T 2 uses the transitional functions of T 1 to each configuration at that level and computes its children. These children are stored on a tape for the configuration of the next level. Among the children, if the halting state exists then T 2 halts by accepting the strings. T 2 accepts means that one branch of T 1 accepts it. It can be said that T 2 accepts a string if and only if T 1 accepts it.
Any language accepted by an NTM is also accepted by a DTM. But the time taken by a DTM to accept a language is more than the time taken by an NTM. In most of the cases, it is exponential in length. A famous unsolved problem in computer science, i.e., the P = NP problem, is related to this issue.
Example 8.18
Construct a TM over {a, b} which contains a substring abb.
Solution: In regular expression, it can be written as (a, b) * abb(a, b) * . In this expression, the substring is important. The string may be abb only. In that case, the machine gets ‘a’ as input in the beginning state. Before traversing the substring ‘abb’, there is a chance to traverse ‘a’ of {a, b} * . The transitional functions are
In state q 1 for input ‘a’, there are two transitional functions. So, it is a non-deterministic TM.
8.4 Conversion of Regular Expression to Turing Machine
Regular expression can be converted to an equivalent TM. The process is as follows.
- Convert the regular expression to an equivalent automata without the ∈ move.
- Change both the initial and final states of the automata to an intermediate state.
- Insert a new initial state with a transition (B, B, R) to the automata’s initial state.
- Convert the transitions with label ‘ip’ to (ip, ip, R).
- Insert a new final state with a transition (B, B, R) from the automata’s final state to the new final state.
Example 8.19
Construct a TM for the regular expression (a + b)*(aa + bb)(a + b)*.
Solution: The finite automata accepting the regular expression is
Inserting a new initial state q i and final state q H , the finite automata becomes
Converting the levels of the inputs, the TM becomes
Example 8.20
Construct a TM for the regular expression ab(aa + bb)(a + b)*b.
Solution: The finite automata accepting the regular expression is
Inserting a new initial state q i and final state q H , the finite automata becomes
Converting the levels of the inputs, the TM becomes
Example 8.21
Design a TM which converts (0, 1)*011(0, 1)* to (0, 1)*100(0, 1)*.
Solution: The problem is to design a TM which searches the substring 011 from a string of 0, 1, and then it replaces 011 by 100. The design is done by the following way.
- Convert (0, 1)*011(0, 1)* to the accepting DFA.
- Convert it to the corresponding TM.
- Modify the TM by modifying the transitions of 011 by 100.
The NFA for accepting (0, 1)*011(0, 1)* is
The equivalent DFA is
The converted TM is
The modified TM is (converting 011 by 100) as follows. The modifications are denoted in bold.
8.5 Two-stack PDA and Turing Machine
Two-stack PDA is already discussed in the pushdown automata chapter. It is also discussed that a PDA cannot be only two stack but may be more than two stacks (a n b n c n d n , n ≥ 0). In real, the language accepting power of a PDA increases by adding extra stacks. Here, the following question arises ‘is two-stack (or more than two stacks) PDA as strong as the TM?’. In this section, we shall learn about a theorem proposed by an American artificial intelligence scientist Marvin Minsky called the Minsky theorem which answers this question.
8.5.1 Minsky Theorem
Any language accepted by a two-stack PDA can also be accepted by some TM and vice versa.
8.5.1.1 General Minsky Model
A general Minsky model is shown in the following figure.
Fig. 8.4 Two Stack PDA
The model in Fig. 8.4 is a two-stack PDA containing an input tape, a reading head, and finite control as it was in PDA. The additionals are two stacks S 1 and S 2 . Let the language be a n b n c n for n ≥ 1. Scanning ‘a’, the symbols (say X) are put into stack S 1 . With the stack top ‘X’ in S 1 , ‘b’ is scanned and the symbols (say Y) are put into stack S 2 . At the time of traversing ‘c’, the stack symbols must be X (for S 1 ) and Y (For S 2 ). These are popped for traversing ‘c’.
A two-stack PDA is capable of accepting (a n b n c n d n , n ≥ 1), (a n b n c n d n e n , n ≥ 1), and so on, with the additional stack. Accepting these types of languages is not possible for a single-stack PDA but possible for a TM. By this process, the two-stack PDA simulates a TM.
The following example shows the acceptance of (a n b n c n d n , n ≥ 1) using the two-stack PDA.
Example 8.22
Design a two-stack PDA for (a n b n c n d n , n ≥ 1).
Solution: Traversing ‘a’, X are put into stack S 1 . Traversing ‘b’ with the stack top ‘X’ in S 1 , ‘X’ is popped from S 1 , and ‘Y’ is pushed into S 2 . Traversing ‘c’ with the stack top ‘Y’ in S 2 , ‘Y’ is popped from S 2 , and ‘Z’ is pushed into S 1 . Traversing ‘d’, ‘Z’ is popped from S 1 . The transitional functions are
What We Have Learned So Far
- The Turing machine was proposed by A.M. Turing in 1936 as a model of any possible combination. Any computational process carried out by the present day’s computer can be done on the Turing machine.
- The Turing machine is the machine format of unrestricted language, i.e., all types of languages are accepted by the Turing machine.
- Undecidable problems are not solved by the computer as no Turing machine can be developed for these problems.
- The mathematical description of the Turing machine consists of 7 touples (Q, Σ, Γ, δ, q 0 , B, F), where Q is the finite set of states, Σ is the finite set of input alphabets, Γ is the finite set of allowable tape symbol, δ is the transitional function, q 0 is the initial state, B is the blank symbol, and F is the final state.
- The transitional functions of the Turing machine are in the form Q × Γ → (Q × Γ × {L, R, H}). That is, from one state by getting one input from the input tape the machine moves to a state, writing a symbol on the tape, and moves to left, right, or halts.
- The mechanical diagram of the Turing machine consists of the input tape, the finite control and the read–write head.
- Upon execution of a transitional function, the machine goes to some state, writes a symbol in the cell of the input tape from where the input symbol was scanned, and moves the reading head to the left or right or halts.
- The instantaneous description (ID) of a Turing machine remembers the contents of all cells from the rightmost to at least the leftmost, the cell currently being scanned by the read–write head, and the state of the machine at a given instance of time.
Solved Problems
-
Consider the Turing machine’s description in the following table below. Draw the computation sequence of the input string 00.
[WBUT 2008]
(By this process, it will continue as there is no halt in the Turing Machine.)
-
Design a Turing Machine to test a string of balanced parenthesis. Show an ID for ( )(( ))
Solution: There are two types of parentheses—open parentheses and close parentheses. Balanced parenthesis means that for each open parenthesis there is a close parenthesis. It may be nested like ( ( ( ) ) ) or may be non-nested like ( ( ) ( ) ) or ( )( )( ) or any other form. But for each open parenthesis, there is a close parenthesis. [The definition that a balanced parenthesis means an equal number of open and close parentheses is wrong.
As an example, ) ( )( ) ( is not a balanced parenthesis.] The string must start with an open parenthesis and after that open parenthesis or close parenthesis can appear, but for any position of the string, the number of open parenthesis is greater than or equal to the number of close parenthesis.
The Turing Machine is designed as follows. Start from the leftmost symbol and traverse right to find the first close parenthesis. The transitional function is
δ(q 0 , ‘(‘) → (q 0 , (, R)
Upon getting the first close parenthesis, replace it by ‘X’, change the state to q 1 , and move left to find the open parenthesis for the replaced close parenthesis. The transitional function is
δ(q 0 , ‘)’ ) → (q 1 , X, L)
Getting the open parenthesis, replace it by ‘X’ and change the state to q 0 . The transitional function is
δ(q 1 , ‘(‘) → (q 0 , X, R)
Then, traverse towards the right to find the close parenthesis. Here, the machine may have to traverse X, which is the replaced symbol for close parenthesis. The transitional function for traversing this is
δ(q 0 , ‘X‘) → (q 0 , X, R)
For a nested parenthesis like (( )), the machine has to traverse X, which is the replaced symbol of the open parenthesis at the time of finding the open parenthesis. The transitional function for traversing this is
δ(q 1 , ‘X‘) → (q 1 , X, L)
Traversing right, if B appears as an input, then there is no parenthesis (open or close) left at the right side. Then, traverse left to find if any parenthesis is left at the left side or not. The transitional function is
δ(q 0 , ‘B‘) → (q 2 , B, L)
At the time of left traversing, the machine has to traverse X by the following transitional function
δ(q 2 , ‘X‘) → (q 2 , B, L)
At the left hand side if it gets B, the machine halts.
δ(q 2 , ‘B‘) → (q 3 , B, H)
ID for ( ) (( ))
-
Design a Turing machine to perform n mod 2. (Or check whether a number is odd or even) From here, by instantaneous description, check whether the number 5 is odd or even.
Solution: n is any number of same input alphabets. Let us take n number of ‘0’s. The machine is in state q 0 and the read–write head is on the beginning input symbol of the input tape. Getting input ‘0’, it moves towards the right replacing ‘0’ by ‘B’ and changes state to q 1 . The transitional function is
δ(q 0 , 0) → (q 1 , B, R)
In state q 1 , by getting the input ‘0’, it moves towards the right replacing ‘0’ by ‘B’ and changes state to q 0 . The transitional function is
δ(q 1 , 0) → (q 0 , B, R)
By the previous two transitional functions, all the ‘0’s are replaced by ‘B’. If the number of ‘0’ is even, then the machine will reach to the end of the string with state q 0 . At the end, it will get ‘B’ and traversing that the machine will halt (means the number is even).
The transitional function is
δ(q 0 , B) → (q 2 , B, H)
If the number of ‘0’ is even, then the machine will reach to the end of the string with state q 1 . With getting input symbol ‘B’, it will traverse left by changing the state to q 3 . The transitional function is
δ(q 1 , B) → (q 3 , B, L)
In state q 3 , by getting input B, the machine halts by replacing ‘B’ to ‘0’ (means the number is odd). The transitional function is
δ(q 3 , B) → (q 2 , 0, H).
ID for n \= 5: The string is 00000.
-
Construct a Turing machine to compute the function f (w) = w R , where w ∈ (a, b) + and w R is the reverse of w.
[UPTU 2004]
Solution: The string consists of ‘a’ and ‘b’. The string may start with ‘a’ or may start with ‘b’. If the string starts with ‘a’, the reverse of the string will end with ‘a’. The same for ‘b’ also.
If the beginning state is q 1 and if the string starts with ‘a’, the TM will traverse right replacing ‘a’ by ‘X’ and changing the state to q 2 . (This q 2 specifies that the symbol was ‘a’.) The transitional function is
δ(q 1 , a) → (q 2 , X, R)
At the time of traversing right, the machine can get an ‘a’ or ‘b’. The transitional functions for traversing ‘a’ and ‘b’ are
If it gets ‘B’, it means the string ends. The TM changes the state to q 3 and traverses left by the following transitional function
δ(q 2 , B) → (q 3 , B, L)
The string may end with ‘a’ or may end with ‘b’. As the beginning symbol was ‘a’ (which is memorized by the machine by state q 2 , then q 3 ), the end symbol will be replaced by X, whatever it may be. But the end symbol will be the beginning symbol. So, what the end symbol is, it will be memorized by different states. In this case, if the end symbol is ‘a’, it will be memorized by changing the state q 3 to q 6 and if the end symbol is ‘b’, it will be memorized by changing q 3 to q 7 . The transitional functions are
Traversing left, the TM has to traverse ‘a’ or ‘b’. If the state is q 6 , the transitional functions are
At the time of traversing left, the TM may get ‘X’ or ‘Y’, the replaced symbols of ‘a’ or ‘b’. The machine is in state q 6 , which means the rightmost symbol of the string was ‘a’. Upon getting ‘X’ or ‘Y’ at the left end, the TM replaces it by ‘X’ and changes the state to q 1 . The transitional functions are
If the string starts with ‘b’, the transitions are the same as the string starts with ‘a’, but only some changes of states. The transitional functions are
If the end symbol is ‘b’ at the time of traversing left, the transitional functions are
By the previous mentioned transitional functions, the total string is traversed and reversed but in the place of ‘a’ and ‘b’ there are ‘X’ and ‘Y’.
From the second time of traversing the string, the TM need not find the symbol ‘B’ to mark the right end of the string. In the state q 2 or q 5 , it will get ‘X’ or ‘Y’, which are representations of the already traversed symbol. The transitional functions are
The string may be of even length or odd length. For the odd length string, the middle element may be ‘a’ or ‘b’. If the middle element is ‘a’, then after replacing the middle element by ‘X’, the TM moves right but it finds either ‘X’ or ‘Y’ (because all the other symbols are already replaced). It backs left by changing the state to q 3 or q 5 using the transitional functions mentioned earlier. At the left, again it gets either ‘X’ or ‘Y’. The TM changes the state to q 8 and moves right by the following transitional functions
If the string is of even length, then the machine gets ‘X’ or ‘Y’ in the state q 1 after replacing all the ‘a’ and ‘b’. The transitional functions are
The machine traverses right to find the end of the string consists of ‘X’ or ‘Y’. When it gets ‘B’, it traverses left by changing the state from q 8 to q 9 .
In the state q 9 at the time of traversing left, the TM replaces all ‘X’s by ‘a’ and all ‘Y’s by ‘b’.
By this process, when it gets the symbol ‘B’ at the left it halts.
δ(q 9 , B) → (q 10 , B, H)
If the string is a blank string (no ‘a’ and/or ‘b’), the machine gets ‘B’ in the state q 1 and halts.
δ(q 1 , B) → (q 10 , B, H)
The transitional functions are represented in the tabular format.
-
Design a Turing machine for 0 n 1 n 0 n n ≥ 1.
[Cochin University 2000]
Solution: The language consists of 0 and 1. Here, n number of ‘1’ are placed in between n number of ‘0’s and n number of ‘0’. The Turing machine is designed as follows. For each leftmost ‘0’, search for ‘1’ and the leftmost ‘0’ placed after ‘1’ and again traverse back to the left to search for the leftmost ‘0’. ‘0’ is replaced by ‘X’, and ‘1’ is replaced by ‘Y’.
When all ‘0’ are traversed and replaced by ‘X’, the machine traverses right side to find if any untraversed ‘0’ or ‘1’ is left or not. If not, the machine gets Y and X and at last blank. Upon getting the blank symbol, the machine halts.
The transitional functions are
Upon executing the last transitional function, the machine halts.
-
Design a Turing machine that shifts the input string over alphabet (0, 1) by one position right by inserting ‘#’ as the first character.
[JNTU 2008]
Solution: According to the condition, a string B0110B will be B#0110B after traversing through the Turing machine. To design the machine, it has to traverse to the right to the end of the string (determined by the symbol ‘B’). Then, it has to traverse left. Getting ‘0’ or ‘1’, it changes to two different states (say, S 0 and S 1 ), respectively. The states signify the traversed symbol. If in the states the machine gets any symbol (‘0’ or ‘1’), it will be replaced by the symbol for which the state is changed and traverse left. This is performed recursively.
Diagrammatically, it is represented as follows.
-
Construct a TM over the alphabet {0, 1, #} where ‘0’ indicates blank, which takes a non-null string of ‘1’ and ‘#’ and transfers the rightmost symbol to the left hand end. Thus, 000#1#1#1000 becomes 0001#1#1#000.
[Jaipur University 2010]
Solution: The machine has to traverse three symbols ‘0’ as blank, 1, and ‘#’. To design the machine, traverse right up to ‘0’ (blank). Getting it, traverse left. The symbol may be ‘1’ or ‘#’. For these two symbols, change to two different states, replace the input by ‘0’ (blank) and traverse left to get ‘0’ (blank). Upon getting it, replace it by ‘1’ or ‘#’ according to the state.
The transitional functions are
-
Design a Turing machine for {a, b}*{aba}{a, b}*.
[GTU 2010]
Solution:
Way 1: This is a non-deterministic Turing machine. The important portion of the string is ‘aba’. The transitional functions are as follows.
Way 2: Converting the regular expression to an equivalent finite automata with ∈ moves we get the following.
Inserting a new initial state q i and final state q H , the finite automata forms as follows.
The final Turing machine is formed by modifying all inputs by (ip, ip, R).
-
Design a Turing machine that accepts L = {a n b 2n , n > 0}.
[UPTU 2005]
Solution: For each ‘a’, there are two ‘b’s. Getting ‘a’ in initial state, replace it by X with the state changed and traverse right. Getting first ‘b’, replace it by Y with the state changed and traverse right to replace the second ‘b’ by ‘Y’. Then, traverse left. By this process, if it gets Y in initial state, it means no ‘a’ is left. Traverse right to check whether any ‘b’ is left or not. If not, halt.
The transitional functions for the Turing machine are
Multiple Choice Questions
-
The Turing machine is the machine format of ____ language.
- Type 0
- Type 1
- Type 2
- Type 3
-
Which is not a part of the mechanical diagram of the Turing machine?
- Input tape
- Read–write head
- Finite control
- Stack
-
The difference between the Turing machine and the two-way FA is in the
- Input tape
- Read–write head
- Finite control
- All of these
-
The difference between the Turing machine and the pushdown automata is in the
- Head movement
- Finite control
- Stack
- All of these
-
Which is not true for the mechanical diagram of the Turing machine
- The head moves in both directions
- The head reads as well as writes
- The input string is surrounded by an infinite number of blank in both sides
- Some symbols are pushed into the stack.
-
In the Turing machine, the transitional function δ is in the form
- Q × Γ → (Q × Γ × {L, R, H})
- Q ×Σ→ (Q × {L, R, H})
- Q ×Σ→ (Q × Σ × {L, R, H})
- Q × Γ → (Q × Σ × {L, R, H})
(where Q is the finite set of states, Σ is the finite set of input alphabets, Γ is the allowable tape symbol, L means left, R means right, and H means halt.)
-
Which of the strings is accepted by the Turing machine?
- L = a n c m b n , where m, n >0
- L = a n b n c i , where n, i>0
- L = a n b n c n , where n >0
- All of the above
-
Which is not true for the instantaneous description (ID) of the Turing machine?
- It remembers the state of the machine
- It remembers the cell currently being scanned by the read–write head
- The contents of all the cells of the tape
- The content of the cell on which the head was in previously
Answers:
- a
- d
- b
- d
- d
- a
- d
- d
GATE Questions
-
Which of the following is not accepted by the PDA but accepted by the two-stack PDA?
- a n b n
- a n b m c m d n
- a n b n c i
- a n b n c n d n
-
Which is true of the following?
A finite automata can be changed to an equivalent Turing machine by
- Changing its reading head to read–write head
- Adding extra initial and final states with input B, B, R.
- Changing its i/p to i/p, i/p, R and adding extra initial and final states with input B, B, R.
- None of the above.
-
Consider the following Turing machine
D is the final state. The language accepted by the TM is
- aba*
- aba*ab
- aba*b
- a*ba
-
Consider the following Turing machine
D is the final state. The language accepted by the TM is
- ab*ab*
- ab*ab*a
- ab + ab + a
- a*(ba)*a
-
Consider the following Turing machine
C is the final state
On input ‘ab’, the machine will
- Halt on accepting state
- Go into infinite loop
- Crash
- Reach to final state but will not halt.
-
When does a Turing machine crash?
- If the machine traverses all the inputs without traversing some states
- If it traverses all its states till the input remains
- If the transitional function is not defined for the present state and the input combination
- None of these.
-
A single-tape Turing machine M has two states q 0 and q 1 , of which q 0 is the starting state. The tape alphabet of M is {0, 1, B} and its input alphabet is {0, 1}. The symbol B is the blank symbol used to indicate the end of an input string. The transition function of M is described in the following table.
The table is interpreted as illustrated in the following.
The entry (q 1 , 1, R) in row q 0 and column 1 signifies that if M is in state q 0 and reads 1 on the current tape square, then it writes 1 on the same tape square, moves its tape head one position to the right, and transitions to state q 1 .
Which of the following statements is true about M?
- M does not halt on any string in (0 + 1) +
- M does not halt on any string in (00 + 1)*
- M halts on all strings ending in a 0
- M halts on all strings ending in a 1
-
Consider the languages L 1 , L 2 , and L 3 as given in the following
Which of the following statements is not true?
- Pushdown automata (PDA) can be used to recognize L 1 and L 2
- L 1 is a regular language
- All the three languages are context free
- Turing machines can be used to recognize all the languages
Answers:
- d
- c
- c
- b
- b
- c
- a
- c
Hints:
5. Aab → aBb → Aab → aBb . . . . . infinite times
7. For (b), (c), and (d) the machine loops infinitely. It only accepts null string.
8. L 3 is not context free.
Fill in the Blanks
- All types of languages are accepted by _____________.
- According to the Chomsky hierarchy, type 0 language is called _____________.
- The diagram of the Turing machine is like finite automata, but here the head moves _____________.
- The head of the Turing machine is called _____________.
- The string a n b n c n , n > 0 is accepted by _____________.
- The Turing machine is called _____________.
- Two-stack PDA is equivalent to _____________.
- The crash condition occurs if the read-write head of a Turing machine is over the _____________.
- A Turing machine is said to be in _____________ if it is not able to move further.
Answers:
- Turing machine
- unrestricted language
- in both direction
- Read–write head
- Turing machine
- universal machine
- Turing machine
- leftmost cell
- halt state
Exercise
-
Construct a Turing machine for the following
- L = {a, b} +
- L = WW, where W ∈ (a, b) +
- L = WW R , where W ∈ (a, b) +
- L = {all even palindromes over (a, b)}
- L = a n b n c n , n > 0
- L = n – 1, where n > 0
-
Design a Turing machine which acts as an eraser.
(Hints: Starts from the left hand side, scans each symbol from left to right, and replaces each symbol by blank. Halts if gets blank.)
-
Design a Turing machine which replaces ‘0’ by ‘1’ and ‘1’ by ‘0’ of the string traversed.
-
Design a Turing machine to accept the string L = {a n b m c n + m , where n > 0, m > 0}.
(Hints: Traverse an ‘a’, replace it by X, and move right to find the first ‘c’ and replace it by Y. Then, traverse left to find the second ‘a’. By this process, replace n number of ‘a’ by X and n number of ‘b’ by Y. By the same process, replace m number of ‘b’ by Z and the last m number of ‘c’ by Z.)
-
Design a Turing machine to accept the string L = {a m b m + n c n , where m, n >0}.
-
Design a Turing machine by the transitional notation for the following languages
- L = a n b n , n > 0
- L = {a n b n c m d m , where m, n ≥ 1}
- L = {a n b n c m , where n, m ≥ 1}
-
Make comments on the following statement that a finite state machine with two stacks is as powerful as a Turing machine.
-
Design a two-stack PDA for adding and subtracting two numbers.
-
Design a two-stack PDA to accept the string L = {a m b m + n c n , where m, n > 0}.