CS 281 – Homework 1 Solutions Exercise 8.2.2: Design ... · Exercise 8.2.2: Design Turing...

2
CS 281 – Homework 1 Solutions 1. Exercise 8.2.2: Design Turing machines for the following languages: (b) {a n b n c n | n 1} (10 pts) (c) {ww R | w is any string of 0’s and 1’s} (10 pts) Solution: (b) {a n b n c n | n 1} M =({q 0 ,q a ,q b ,q c ,q Y ,q Z ,q f }, {a, b, c}, {a, b, c, X, Y, Z }, δ, q 0 ,B, {q f }) where δ is as follows: State a b c X Y Z B q 0 (q a ,X,R) - - - (q Y ,Y,R) - - q a (q a , a, R)(q b ,Y,R) - - (q a ,Y,R) - - q b - (q b , b, R)(q c ,Z,L) - - (q b ,Z,R) - q c (q c , a, L)(q c , b, L) - (q 0 ,X,R)(q c ,Y,L)(q c ,Z,L) - q Y - - - - (q Y ,Y,R)(q Z ,Z,R) - q Z - - - - - (q Z ,Z,R)(q f ,B,R) Note: The above TM is the 3-character version of the same machine given in the textbook Example 8.2. (c) {ww R | w is any string of 0’s and 1’s} M =({q init ,q 0 ,q 1 ,q 0 R ,q 1 R ,q back ,q f }, {0, 1}, {0, 1}, δ, q init ,B, {q f }) where δ is as follows: State 0 1 B q init (q 0 ,B,R) (q 1 ,B,R) (q f ,B,R) q 0 (q 0 , 0,R) (q 0 , 1,R) (q 0 R ,B,L) q 1 (q 1 , 0,R) (q 1 , 1,R) (q 1 R ,B,L) q 0 R (q back ,B,L) - - q 1 R - (q back ,B,L) - q back (q back , 0,L) (q back , 1,L) (q init ,B,R)

Transcript of CS 281 – Homework 1 Solutions Exercise 8.2.2: Design ... · Exercise 8.2.2: Design Turing...

Page 1: CS 281 – Homework 1 Solutions Exercise 8.2.2: Design ... · Exercise 8.2.2: Design Turing machines for the following languages: (b) {anbncn | n ≥ 1} (10 pts) ... Solution: (a)

CS 281 – Homework 1

Solutions

1. Exercise 8.2.2: Design Turing machines for the following languages:

(b) {anbncn | n ≥ 1} (10 pts)

(c) {wwR | w is any string of 0’s and 1’s} (10 pts)

Solution:

(b) {anbncn | n ≥ 1}

M = ({q0, qa, qb, qc, qY , qZ , qf}, {a, b, c}, {a, b, c, X, Y, Z}, δ, q0, B, {qf})

where δ is as follows:

State a b c X Y Z B

q0 (qa, X, R) - - - (qY , Y, R) - -qa (qa, a, R) (qb, Y, R) - - (qa, Y, R) - -qb - (qb, b, R)(qc, Z, L) - - (qb, Z, R) -qc (qc, a, L) (qc, b, L) - (q0, X, R) (qc, Y, L) (qc, Z, L) -qY - - - - (qY , Y, R)(qZ , Z, R) -qZ - - - - - (qZ , Z, R)(qf , B, R)

Note: The above TM is the 3-character version of the same machine given in thetextbook Example 8.2.

(c) {wwR | w is any string of 0’s and 1’s}

M = ({qinit, q0, q1, q0R , q1R , qback, qf}, {0, 1}, {0, 1}, δ, qinit, B, {qf})

where δ is as follows:

State 0 1 B

qinit (q0, B, R) (q1, B, R) (qf , B, R)q0 (q0, 0, R) (q0, 1, R) (q0R , B, L)q1 (q1, 0, R) (q1, 1, R) (q1R , B, L)q0R (qback, B, L) - -q1R - (qback, B, L) -qback (qback, 0, L) (qback, 1, L) (qinit, B, R)

Page 2: CS 281 – Homework 1 Solutions Exercise 8.2.2: Design ... · Exercise 8.2.2: Design Turing machines for the following languages: (b) {anbncn | n ≥ 1} (10 pts) ... Solution: (a)

2. Exercise 8.2.3: Design a Turing machine that takes as input a number N and adds1 to it in binary. To be precise, the tape initially contains a $ followed by N in binary.The tape head is initially scanning the $ in state q0. Your TM should halt with N +1,in binary, on its tape, scanning the leftmost symbol of N + 1, in state qf . You maydestroy the $ in creating N + 1, if necessary. For instance, q0$10011 `∗ $qf10100, andq0$11111 `∗ qf100000.

(a) Give the transitions of your Turing machine, and explain the purpose of eachstate. (10 pts)

(b) Show the sequence of ID’s of your TM when given input $111. (5 pts)

Solution:

(a) Give the transitions of your Turing machine, and explain the purpose of each state.

State $ 0 1 B Explanation

q0 (qR, $, R) - - - Initial $ readerqR - (qR, 0, R) (qR, 1, R) (q1, B, L) Seeking end of stringq1 (qB, 1, L) (qL, 1, L) (q1, 0, L) - Carry bit = 1qL (qf , $, R) (qL, 0, L) (qL, 1, L) - Carry bit = 0qB - - (qf , B, R) - Formatting

(b) Show the sequence of ID’s of your TM when given input $111.

q0$111 ` $qR111 ` $1qR11 ` $11qR1 ` $111qR `$11q11 ` $1q110 ` $q1100 ` q1$000 ` qBB1000 ` qf1000

3. Exercise 8.3.2: A common operation in Turing-machine programs involves “shiftingover.” Ideally, we would like to create an extra cell at the current head position, inwhich we could store some character. However, we cannot edit the tape in this way.Rather, we need to move the contents of each of the cells to the right of the currenthead position one cell right, and then find our way back to the current head position.Show how to perform this operation. Hint: Leave a special symbol to mark the positionto which the head must return. (10 pts)

Solution:

Assuming Σ = {0, 1}, shift converts an ID of the form wq1v to ID wq6Bv. shift isdefined as follows:

State 0 1 B

q1 (q2, B, R) (q3, B, R) (q5, B, L)q2 (q2, 0, R) (q3, 0, R) (q4, 0, L)q3 (q2, 1, R) (q3, 1, R) (q4, 1, L)q4 (q4, 0, L) (q4, 1, L) (q5, B, L)q5 (q6, 0, R) (q6, 1, R) (q6, B, R)

Note: There is no extra character, B is used to keep track of the location of call.