You are on page 1of 6

CS 373 – Fall 2003 – NMG ***** ***** solution ***** **********

November 28, 2003

Homework Assignment 10 – “Fancy” Turing Machines


Due 12/3/03 (Wednesday)
Problem related to chapter 10:

1. A problem related to chapter 10. Design a Turing machine which accepts the non-context-free
language:
L = {an : where n  0 is a perfect square } … remember  is also accepted.
This is a recursive (hence a recursively enumerable) language – discussed soon in class. It cannot
be processed by an NPDA or a context free grammar.

You must use a multi-tape (three tapes) Turing machine with the stay option. That is each tape
could move left, right, or stay in place. An example transition function would be:

 (q1, x, y, z) = (q2, u, v, w, L, S, R)

and x, y, z, u, v, w  , and q1, q2  Q.

Hints:
Assume the string to be processed (n2 a’s, n  0 ) is stored on tape 1.
Generate sequences of strings of length n on tape 3. These strings are made up of the symbol x.
Generate sequences of strings of length n2 on tape 2. These strings are made up of the symbol x.
The above two strings are recursively generated from the previous string in the sequence, and
only the last string in the sequence is retained on the tape.
Here’s a hot tip for doing this: (n+1)2 = n2 + 2n + 1

You must write all the delta functions for this design at the low level. Pseudo code is OK to
describe your algorithm, but to be complete you must write the low level code. You must
comment the TM rules with your intentions and the meaning of the states – as you would
comment an assembly language program.
Solution
Problem 1: L = {an : n is a perfect square} more detail later ….

Using tapes 2 and 3 to generate sequences of strings of x’s on tape 2 whose lengths are
perfect squares using the formula (n+1)2 = n2 + 2n + 1. Then compare the length of each generated string on
tape 2 to the length of the input string.

If the length of the string on tape 2 is less than the string on tape 1, then increment n and try again. If the
length of the string on tape 2 is strictly greater than that on tape 1, then the input string is rejected. If the
lengths of these two strings get to be equal, then the input is accepted.

If tape 3 has a string of x’s of length n, then the algorithm for creating the sequences, is:
Add one x to the right end of the string on tape 2, then copy tape 3 to the end twice, next bump the length of
tape 3 by one.

let B be a blank symbol, and let D stand for 


D(q0, B,B,B) = (q1, B,B,B, R,R,R) initialize tape
D(q1, a,B,B) = (q2, a,X,X, S,S,S)

D(q2, a,X,X) = (q2, a,X,X, R,R,S) compare strings on tapes 1 and 2


D(q2, B,B,X) = (q3, B,B,X, S,S,S) accept
D(q2, a,B,X) = (q4, a,X,X, S,R,S)

D(q4, a,B,X) = (q5, a,X,X, S,R,S) rewrite tapes 2 and 3


D(q4, a,B,B) = (q6, a,B,X, L,L,L)
D(q5, a,B,X) = (q4, a,X,X, S,R,R)

D(q6, a,X,X) = (q6, a,X,X, L,L,L) reposition tape heads


D(q6, a,X,B) = (q6, a,X,B, L,L,S)
D(q6, a,B,B) = (q6, a,B,B, L,S,S)
D(q6, B,X,B) = (q6, B,X,B, S,L,S)
D(q6, B,B,B) = (q2, B,B,B, R,R,R) repeat comparisons cycles
For your information this is the solution showing more detail from Sudkamp (example 9.6.1) pp. 274-277:

You might also like