Tik-79.148 Spring 2001 Introduction to Theoretical Computer Science Tutorial 9 Solution to Demontration Exercie 4. Contructing a complex Turing machine can be very laboriou. With the help of machine chema it i poible to combine imple Turing machine to obtain more complex one. The imple baic machine are: For every ymbol in Σ there i a machine that write the ymbol in the tape and then top with out moving it read/write head. Machine R and L that move the head one tep to right or left, repectively, and then top. The machine in the exercie work a follow: (a) The machine move the head to the right in an eternal loop. (b) The machine firt move it head one tep to the right. If it now can an a, it will replace it by a b. If it can a b, it will replace by an a. (c) The machine move it head two tep to the right. Note that if the head already i in the left end of the tape, the behaviour of the machine i not well defined. (The tarting mark alway caue the head to move one tep to the right.) (d) The machine firt move it head one tep to the left, and if it now can a non-empty ymbol it move the head back to the right. 5. The three mot fundamental term related to Turing machine are: Turing decidable Turing acceptable, emi-decidable Turing computable A language L i Turing decidable if there exit a Turing machine M o that { (, w) (h, Y ), if w L M (h, N), if w / L For all tring w Σ it i poible to determine whether they belong to the language or not. A language L i Turing acceptable if there exit a Turing machine M o that M top with the input w w L Turing machine that accept (or emidecide) a language top if and only if the input tring belong to the language. In other cae the machine continue it computation forever. There are language that are Turing acceptable but not Turing decidable. For example: 1
L = {M M i a Turing machine that halt on the input e} i uch a language. You can think that a Turing machine that decide a language L actually compute the function f : Σ {y, n}. Thi can be generalized to arbitrary function on tring g : Σ Σ. We get the definition: a Turing machine compute a function g, if f(w) = u (, w) M (h, u) The machine ha a tring x a input, an in the end of the computation there i the tring f(x) in the tape. Function with everal argument can be defined in a imilar way. In thi cae, the argument are eparated by one blank from each other and written one after another in the beginning of the tape. Thi i the initial configuration of the machine M. (Example: w 1 w 2 w n ). (a) Turing machine that accept the language a ba b: M = (K, Σ, δ, ) K = {q 0, q 1, q 2, q 3 } Σ = {a, b, } = q 0 q σ δ(q, σ) q 0 (q 1, L) q 1 a (q 1, a) q 1 b (q 2, L) q 1 (q 1, ) q 2 a (q 2, L) q 2 b (q 3, L) q 2 (q 2, ) q 3 a (q 3, L) q 3 b (q 3, b) q 3 (h, ) The tarting point of the contruction of the machine wa a correponding tate machine. The machine can top only when the input tring belong to the language (by the definition of emideciding), o the machine i left to an eternal loop every time a wrong type of tring i found. In fact, the language given in the exercie i decidable. That i, it i poible to contruct a machine that alway halt and expree whether the input tring belonged to the language or not (compare to the next exercie). (b) You are aked to contruct a machine that get a input (w), and after the computing the tape contain (Y ), if w L or (N), if w / L. In the contruction, the beginning of the tape i firt marked with a ymbol S. After that the machine compare the firt letter of the word to the lat letter, erae them and repeat. 2
# R # L a,# b # L # L # S b R a a # R # L b,# C i,j : L #RYR #L RNR A machine L σ can the tape to the left until it find the firt occurrence of the ymbol σ. (c) We contruct a machine that compute the function f(m, n) = m+n. The input of the machine i of the form (I m I n ), and after the computation the tape contain (I m+n ). The eaiet way to accomplih thi i to move the tring I n one tep to the left: L # R # L I #LIR 6. Firt we define machine C i,j that copie the ymbol on the right ide of the head in the tape i to the tape j, until the firt blank i canned. R i,j σ i i σ j For example: The configuration 1 0 1 0 i tranformed to 1 0 1 0 1 0 M L : R Next we define another auxiliary machine M L that move the tring on the right ide of the head one tep to the left, and leave the head to the right end of the moved tring. L σ LσR 3
For example, the configuration 0 1 0 i tranformed to 0 1 0 E : An auxiliary machine E move tring on the right ide of the head to the beginning of the tape. L R M L L R L For example, the configuration 0 1 1 0 i tranformed to 1 0 With the help of the auxiliary machine we contruct a 2-tape machine 1,2 that um two binary number. The machine i baed on the text book example 4.3.2 and it i relatively complex 1 The machine in the example of the book only work correctly, if the two number have equal number of bit. Additionally, the machine leave the tape in an erroneou configuration. Correcting thee mitake take quite an effort. Example: Starting from the configuration 1 0 1 0 1 The machine 1,2 mark the beginning of the tape by ymbol, copie the firt word of the input to the econd tape, and move the head to the end of the tape: 1 0 1 0 1 0 1 1 If any of the reader find a impler olution, the aitant of the coure would be very grateful if it were not told to them. 4
After that the machine can both number from their end to the beginning, and calculate their um bit by bit. At the ame time, the econd tape i being cleaned. 1 0 1 1 1 Calculation end when either of the number end. In thi example, the upper number ended firt. The anwer, 111, i moved to the beginning of the firt tape, and ymbol are removed. 1 1 0 0 1 2 1,2 : 0 1 0 2 0 1 1 2, 1 1 0 2 L 1,2 1,2 C 1,2 R 2 R 1 L 1,2 1 1 2 1 σ 2 1 2 1 1 1 2 σ 1 2 0 1 2 1 1 2 σ 2 L 1,2 2 1,2 E 1 R 1 L 1 1 1 1 1 2 0 1 2 0 2 1 1 2 0 1 2 L 1,2 1 2 1 1 1 2 L 1,2 2 01 2 0 1 0 2 0 1 1 2, 1 1 0 2 1 1 1 2 1 1 2 0 1 2 1 1 L 1 1 0 1 2 L 1,2 0 1 1 1 2 L 1 1 1 1 Before implementing multiplication, two more auxiliary machine are needed: D i,j and W L. The machine D i,j compute the number of zero in 5
the end of the number in the firt tape, and multiplie the number in the econd tape by 2 for each zero. For example 1 0 i tranformed to 1 0 1 0 0 1 0 0 0 1 0 1 0 0 D i,j : L i 0 1 0 i 0 j R j R i W L : The machine W L clean the tape by removing the tring on the left ide of the head. L Multiplication of binary number can be accomplihed by erie of ummation and multiplication by two. For example: 101 2 110 2 = 101 2 2 2 + 101 2 2 1 Thu, multiplication can be accomplihed by the next algorithm and a 3-tape Turing machine: 1. Write 0 in the beginning of the econd tape. 2. Copy the firt factor to the econd tape. 3. Go through the econd factor from the end to the beginning. For each zero in the end of the number multiply the factor in the econd tape by two. 4. If there have been found a 1-bit in the econd factor in the end of the third tep, replace the 1-bit by a 0-bit. If the number only contained zero, multiplication i ready and the reult can be found in the beginning of the econd tape. 5. Compute the um of the number in the econd tape with the help of the third tape, and return to the tep 2. 6
1,2,3 : R 2 0 2 R 2 L 1,2 C 1,2 R 1 D 1,2 1 1 0 1 R 1 1,2 1 RW 1 1,2 L L2 WL 1C 2,1WL 2L2 Even if the machine 1,2,3 ha three tape, it only ue two tape explicitly, and the third tape erve only a an auxiliary tape for the ummation. Finally, let tudy how the machine calculate 2 3 (that i, 10 2 11 2 ). In the beginning the firt two tape contain: 1 0 1 1 The product i initialized to zero, and the firt factor i copied to the econd tape. 0 1 0 1 0 1 1 Becaue the lat bit of the econd factor i not zero, there i no need for multiplication by two but the ummation can be done directly. 1 0 1 0 1 0 The firt factor i copied to the econd tape again. 1 0 1 0 1 0 1 0 Thi time, the firt factor end with a 0-bit, o the other factor will be multiplied by two. 1 0 1 0 0 1 0 1 0 Next, the lat 1-bit in the firt tape i replaced by a zero, and the number in the econd tape will be ummed. 1 1 0 1 0 0 0 7
During the next iteration the product i ready and it will be copied to the beginning of the firt tape. 1 1 0 8