You are on page 1of 6

Solutions for Homework Six, CSE 355 1.

(8.1, 10 points) Let M be the Turing machine deded by B a b c q0 q1 ,B,R q1 q2 ,B,L q1 ,a,R q1 ,c,R q1 ,c,R q2 q2 ,c,L q2 ,b,L

a) Trace the computation for the input string aabca. b) Trace the computation for the input string bcbc. c) Give the state diagram of M . d) Describe the result of a computation in M . Solution: a) q0 BaabcaB Bq1 aabcaB Baq1 abcaB Baaq1 bcaB Baacq1 caB Baaccq1 aB Baaccaq1 B Baaccq2 aB Baacq2 ccB Baaq2 cbcB Baq2 abbcB Bq2 acbbcB q2 BccbbcB c) The state diagram of M is
a/aR, b/cR, c/cR q0 B/BR q1 B/BL a/cL, c/bL q2

b) q0 BbcbcB Bq1 bcbcB Bcq1 cbcB Bccq1 bcB Bcccq1 cB Bccccq1 B Bcccq2 cB Bccq2 cbB Bcq2 cbbB Bq2 cbbbB q2 BbbbbB

d) The result of a computation is to replace the as in the input string with cs and the cs with bs.

2.

(8.2, 10 points) Let M be the Turing machine dened by 1

q0 q1 q2

B q1 ,B,R q1 ,B,R

a q1 ,a,R q2 ,b,L

b q1 ,b,R q2 ,a,L

c q2 ,c,L

a) Trace the computation for the input string abcab. b) Trace the rst six transitions of the computation for the input string abab. c) Give the state diagram of M . d) Describe the result of a computation in M . Solution: a) q0 BabcabB Bq1 abcabB Baq1 bcabB Babq1 cabB Baq2 bcabB Bq2 aacabB q2 BbacabB
B/BR, a/aR, b/bR

b) q0 BababB Bq1 ababB Baq1 babB Babq1 abB Babaq1 bB Bababq1 B BababBq1
a/bL, b/aL q2

c)

q0

B/BR

q1

c/cL

d) The result of a computation is to replace the as before the rst c with bs and the bs before the rst c with as. If there is no c in the input string, just keep the same.

(8.3 (a,b,d), 20 points) Construct a Turing machine with input alphabet {a, b} to perform each of the following operations. Note that the tape head is scanning position zero in state qf whenever a computation terminates. a) Move the input one space to the right. Input conguration q0 BuB , result qf BBuB . b) Concatenate a copy of the reversed input string to the input. Input conguration q0 BuB , result qf BuuR B . d) Erase the bs from the input. Input conguration q0 BbabaababB , result qf BaaaaB . Solution:

3.

a) Move the input one space to the right. First the head moves to the rst blank following the input by transitions to states q0 and q1 , and then moves to the left. At the state q2 , starting with the rightmost symbol in the input and working in a right-to-left manner, the machine moves each symbol one position to the right.
a/aR, b/bR q0 B/BR q1 B/BL q2 a/BR b/BR q5 B/BL q3 B/aL B/bL q4

If the input is the null string, the computation halts in the nal state q2 with the tape head in position zero as desired. Otherwise, an a is moved to the right by transitions to states q3 and q4 . Similarly, states q5 and q4 shift a b. This process is repeated until the entire string has been shifted. b) Concatenate a copy of the reversed input string to the input. First the head moves to the rightmost symbol in the input. At state q2 , the machine works in a right-to-left manner, and copy a symbol to the end of the current string.
a/aR, b/bR a/aR, b/bR q0 B/BR q1 B/BL q2 a/XR b/Y R q3 a/aR, b/bR q4 X/aL, Y /bL B/aL B/bL a/aL, b/bL q5

d) Erase the bs from the input. The machine works in a left-to-right manner. At state q1 , it skips as and whenever it reads a b or a previously deleted position, denoted by symbol X , the machine move the rst a in the following string. If no further a is nd, it goes to the nal state q4 and change all the remaining symbols b, X and Y into blanks.
Y /aR a/aR q0 B/BR q1 b/bR, X/XR b/Y R, X/Y R q2 a/XL q3 b/bL, X/XL

B/BL

B/BL q4

b/BL, X/BL, Y /BL, a/aL

(8.5, 10 points) Construct a Turing machine with input alphabet {a, b} to accept each of the following languages by nal state. a) {ai bj | i 0, j i} b) {ai bj ai bj | i, j > 0} c) Strings with the same number of as and bs d) {uuR | u {a, b} } e) {uu | u {a, b} } Solution: a) {ai bj | i 0, j i}
B/BL a/aR q0 B/BR q1 b/XL X/XL q2 a/XR b/XL B/BR X/XR, b/bR q5 B/BR qf B/BR X/XR q3 B/BL X/XL q4

4.

b) {ai bj ai bj | i, j > 0}
a/XR a/aR q0 B/BR q1 a/XR q2 b/bR, Y /Y R b/bR q3 a/aL, b/bL, Y /Y L a/Y L q4 X/XR q5

b/bL, Y /Y L qf q8 B/BR

X/XR q7 Y /Y R

b/XR Y /Y R q6 b/bR

b/Y L

c) Strings with the same number of as and bs A computation of the machine begins by nding the rst a on the tape and replacing it with an X (state q1 ). The tape head is then returned to position zero and a search is initiated for a corresponding b. If a b is encountered in state q3 , an X is written and the tape head is repositioned to repeat the cycle q2 , q3 , q4 , q5 . If no matching b is found, the computation halts in state q3 rejecting the input. After all the as have been processed, the entire string

is read in q5 and q6 is entered upon reading the trailing blank. The computation halts in the accepting state qf if no bs remain on the tape.
b/bR q0 B/BR q1 a/aL, b/bL, X/XL a/XL q2 a/XL B/BL q5 b/bR, X/XR B/BR a/aR, X/XR q3 b/XL q4 B/BR

B/BL qf B/BR q6 X/XL

a/aL, b/bL, X/XL

d) {uuR | u {a, b} }
a/aR, b/bR q2 a/XR q0 B/BR q1 b/XR B/BR, X/XR q4 qf a/aR, b/bR B/BL, X/XL X/XR b/XL q5 B/BL, X/XL q3 a/XL q6 a/aL, b/bL

e) {uu | u {a, b} }
X/aR, Y /bR

a/aR, b/bR q0 B/BR q1 a/XR, b/Y R q2 B/BL, X/aL, Y /bL q3

a/aL, b/bL a/XL, b/Y L q4

Y /Y L

X/XL B/BR, Z/ZR q5 q6 a/XL a/aL, b/bL q11 b/Y L q8 q9

a/ZR X/aR B/BL Y /bR b/ZR

q7 a/aR, b/bR qf

B/BR, Z/ZR

q10 a/aR, b/bR

a/aL, b/bL

(8.6, 10 points) Modify your solution to Exercise 5(a) to obtain a Turing machine that accepts the language {ai bj | i 0, j i} by halting. Solution:
B/BL a/aR q0 B/BR q1 b/bR b/XL X/XL q2 a/aR a/XR b/XL a/aR, b/bR, X/XR X/XR qe a/aR, b/bR, X/XR, B/BR B/BR B/BR B/BR q6 a/aR, b/bR X/XR q3 B/BL X/XL q4

5.

a/aR

q5

X/XR, b/bR

6. (8.16, 20 points) Construct a two-tape Turing machine with input alphabet {a, b, c} that accepts the language {ai bi ci | i 0}.
Solution:
q0 [a/aR, B/aR] [B/BR, B/BR] q1 [b/bR, a/bL] q2 [c/cR, b/cR] q3 [B/BS, B/BS ] qf

[b/bS, B/BL]

[c/cS, B/BR]

7. (8.18, 20 points) Construct a two-tape Turing machine that accepts strings in which each a is followed by an increasing number of bs; that is, the strings are of the form
abn1 abn2 abnk , k > 0, where n1 < n2 < < nk . Solution:
q0 [B/BR, B/BR] q1 [b/bR, b/bR] [a/aR, B/BS ] q2 [b/bR, B/bR] q3 [B/BS, B/BS ] qf

[b/bR, B/bR]

[b/bS, B/BR]

[a/aR, B/BL] q4 [b/bS, b/bL]

You might also like