Homework Five Solution– CSE 355

Homework Five Solution– CSE 355
Due: 17 April 2012
Please note that there is more than one way to answer most of
these questions. The following only represents a sample solution.
Problem 1: Linz 8.1.13 and 8.2.7
8.1.13: Show that the language L = {w ∈ {a, b, c}∗ : na (w) + nb (w) = nc (w)} is
context-free, but not linear.
In order to show that the language is context-free, we will give the machine definition of the
corresponding npda and by Theorem 7.2, we know that there exists a CFL for the npda.
Define M = ({q0 , q1 }, Σ, {a, c, z}, δ, q0 , z, {q1 }) with δ(q0 , a, z) = {(q0 , az)}, δ(q0 , a, a) = {(q0 , aa)},
δ(q0 , a, c) = {(q0 , λ)}, δ(q0 , b, z) = {(q0 , az)}, δ(q0 , b, a) = {(q0 , aa)}, δ(q0 , b, c) = {(q0 , λ)}, δ(q0 , c, z) =
{(q0 , cz)}, δ(q0 , c, a) = {(q0 , λ)}, δ(q0 , c, c) = {(q0 , cc)}, δ(q0 , λ, z) = {(q1 , λ)}.
Then M accepts L since it keeps count of how many as and bs are read with the stack symbol
a and how many cs with the symbol c. For each a and b seen it will cancel a c if that is on the top
of the stack or add an a. Similarly, for each c seen, it will add a c to the stack or cancel an a if it
is on top of the stack. Finally, we only accept if the number of cs is the same as the number of as
and bs combined, that is they all cancel each other on the stack.
In order to show that the language is not linear, we will use the pumping lemma from Theorem
8.2. Assume that the language is linear and apply the Theorem 8.2 to the string
w = am c2m bm .
Inequality (8.5) |uvyz| ≤ m shows that in this case the strings u, v must both consist entirely of
a0 s, that is v = ak and y, z must both consist entirely of b0 s, that is y = bl . If we pump this string
once, we get am+k c2m bm+l , with either k ≥ 1 or l ≥ 1 due to inequality (8.6), a result that is not
in L. This contradiction of Theorem 8.2 proves that the language is not linear.
8.2.7: Show that the family of context-free languages is not closed under difference in general, but is closed under regular difference, that is, if L1 is context-free
and L2 is regular, then L1 − L2 is context-free.
To show that context free languages are not closed under difference in general, we will give two
context free languages whose difference is no longer context free. Let L1 = {an bn cm : m, n ≥ 0} and
L2 = {am bn cn : m, n ≥ 0}. L1 and L2 are both context free languages (in fact determinstic context
free languages), but L1 − L2 = L1 ∩ L2 = {an bn cm : m, n ≥ 0} ∩ {am bn cn : m, n ≥ 0} = {an bn cm :
1
m, n ≥ 0} ∩ {am bn cn : m, n ≥ 0} = {an bn cn : n ≥ 0} which is not context free by Example 8.1 in
Linz. Thus, the family of context free languages is not closed under difference.
Next, we will show that the class of context free languages is closed under regular difference. Let
L1 be any context free langauge and L2 be any regular language. We know from Theorm 4.1 that
regular languages are closed under complement, therefore L2 is a regular language. From Theorem
8.5 of Linz we also know that context free languages are closed under regular intersection. Thus,
L = L1 − L2 = L1 ∩ L2 is a result of a regular intersection and is therefore a context free language.
Problem 2: Linz 9.1.11 and 9.2.7
9.1.11: Design Turing machines to compute the following functions for x and y
positive integers represented in unary.
For each of these problems let u(x) = 1x where 1x is 1 written x times. We will assume that
u(0) = λ and will be represented by the tape having only blanks on it.
(a) f (x) = 3x.
The input to our tape will be u(x) and the output will be u(3x). Our strategy follows Example
9.10 of Linz. We will first mark all the 1s and then for every marked 1 we see we will write two 1s
at the end of the string, thus tripling the string.
We will define the TM as follows:
• Q = {q0 , q1 , q2 , q3 , qf }, where q0 is the start state and qf the only final state,
• Σ = {1},
• Γ = {1, 1̇, } where is the blank tape symbol, and
• δ defined as follows:
–
–
–
–
–
–
–
–
δ(q0 , 1) = (q0 , 1̇, R),
δ(q0 , ) = (q1 , , L), these two transitions mark all the 1s on the tape.
δ(q1 , 1) = (q1 , 1, L), go left until a marked 1 is seen,
δ(q1 , 1̇) = (q2 , 1, R), unmark it
δ(q2 , 1) = (q2 , 1, R), go to the end of the tape
δ(q2 , ) = (q3 , 1, R),
δ(q3 , ) = (q1 , 1, L), write two 1s and repeat looking for the next marked 1.
δ(q1 , ) = (qf , , R), if no marked 1 is found, we are done and halt.
(b) f (x, y) = x − y if x > y; and f (x, y) = 0 otherwise.
The input to our tape will be u(x) − u(y) and the output will be u(x − y) if x > y and the blank
tape otherwise. Our strategy will be to remove a 1 from the end of the string (the y portion) and
then remove a corresponding 1 from the front of the string (the x portion). We continue until either
we are out of 1s in the y portion, in which case we have u(x − y) on the tape, or we are out of 1s
in the x portion in which case x ≤ y, so we clear the tape and halt with the blank tape.
We will define the TM as follows:
2
• Q = {q0 , q1 , q2 , q3 , q4 , q5 , qf }, where q0 is the start state and qf the only final state,
• Σ = {1, −},
• Γ = {1, −, } where is the blank tape symbol, and
• δ defined as follows:
– δ(q0 , 1) = (q0 , 1, R),
– δ(q0 , −) = (q0 , −, R),
– δ(q0 , ) = (q1 , , L), moved to the end of the input
– δ(q1 , 1) = (q2 , , L), we delete the end 1
– δ(q2 , 1) = (q2 , 1, L),
– δ(q2 , −) = (q2 , −, L),
– δ(q2 , ) = (q3 , , R), moved to the start of the input
– δ(q3 , 1) = (q0 , , R), and delete the first one, essentially substracting one of the sybmols
in y from x. We then repeat moving to the end of the string.
– δ(q1 , −) = (q4 , , L), if out of input with y first (x ≥ y), clear the minus and
– δ(q4 , 1) = (q4 , 1, L), move back to the beginning
– δ(q4 , ) = (qf , , R), and halt
– δ(q3 , −) = (q5 , , R), if no 1 in the x portion to match to a 1 in the y portion (y > x)
– δ(q5 , 1) = (q5 , , R), clear the tape
– δ(q5 , ) = (qf , , R), and halt
(c) f (x, y) = 2x + 3y.
The input to our tape will be u(x) + u(y) and the output will be u(2x + 3y). Our strategy follows
Examples 9.9 and 9.10 of Linz. We will first mark all the 1s before the plus with one dot and then
change the plus to a one with two dots and continue marking every one after it with two dots. We
will remove the last 1 as extra to the sum. Then for every 1 with two dots seen we will write two
1s and the end of the string and for every 1 with one dot we will write one 1 and the end of the
string, yielding the required total.
We will define the TM as follows:
• Q = {q0 , q1 , q2 , q3 , q4 , qf }, where q0 is the start state and qf the only final state,
• Σ = {1, +},
• Γ = {1, +, 1̇, 1̈, } where is the blank tape symbol, and
• δ defined as follows:
– δ(q0 , 1) = (q0 , 1̇, R), replace every 1 before the + with a single dot
– δ(q0 , +) = (q1 , 1̈, R),
– δ(q1 , 1) = (q1 , 1̈, R), replace the + and every 1 after it with two dots
3
– δ(q1 , ) = (q1 , , L), until we reach the end of the input
– δ(q1 , 1̈) = (q2 , , L), remove the last one (its place was taken by the +)
– δ(q2 , 1) = (q2 , 1, L), search for the next marked one
– δ(q2 , 1̈) = (q3 , 1, R), if the 1 has two marks
– δ(q3 , 1) = (q3 , 1, R), go to the end of the string
– δ(q3 , ) = (q4 , 1, R),
– δ(q4 , ) = (q2 , 1, L), and write two 1s. Repeat scanning the string for the next mark
– δ(q2 , 1̇) = (q5 , 1, R), if the 1 has one mark
– δ(q4 , 1) = (q4 , 1, R), go to the end of the string, where we would write a single 1 at the
blank (as above for q4 )
– δ(q2 , ) = (qf , , R), If no mark is found, we have transformed the string, and so we
halt.
x
2 , or f (x) =
x
2
if x is even and f (x) =
x+1
2
if x is odd.
x
The input to our tape will be u(x) and the output will be u( 2 ). Our strategy is to mark a 1 at the
start of the string and remove a corresponding one at the end of the string. We will repeat marking
the next unmarked 1 at the beginning and removing another 1 at the end. We will continue until
there is no unmarked 1 left (x was even) and we cut the output in half, or there is no one left to
remove at the end (x was odd) and the output is the fraction rounded up. Then we clear all marks
and halt.
We will define the TM as follows:
(d) f (x) =
• Q = {q0 , q1 , q2 , q3 , qf }, where q0 is the start state and qf the only final state,
• Σ = {1},
• Γ = {1, 1̇, } where is the blank tape symbol, and
• δ defined as follows:
– δ(q0 , 1) = (q1 , 1̇, R), Mark the first one
– δ(q1 , 1) = (q1 , 1, R),
– δ(q1 , ) = (q2 , , L), move to the end of the string
– δ(q2 , 1) = (q3 , , L), remove the last 1
– δ(q3 , 1) = (q3 , 1, L),
– δ(q3 , 1̇) = (q0 , 1̇, R), move back to the first unmarked 1 in the string and repeat from the
start
– δ(q0 , ) = (q2 , , L), If there are no unmarked 1s remaining to mark (x was even)
– δ(q2 , 1̇) = (q2 , 1, L), clear the marks moving to the start of the string (This also takes
care of the case when x was odd since if there are no unmarked 1s and the end of the
string to remove after marking a 1, q2 will start on a marked 1 and we have to remove
the marks).
– δ(q2 , ) = (qf , , R), and halt
4
(e) f (x) = x mod 5.
The input to our tape will be u(x) and the output will be u(x mod 5). Our strategy is to mark
all the 1s as we scan to the right using our states to remember how many 1s we have seen mod 5.
Then we will remove the marks from that many 1s and scan back to the left over the remaining
marked 1s. Then we will delete all the marked ones and halt with x mod 5 on the tape.
We will define the TM as follows:
• Q = {q0 , q1 , q2 , q3 , q4 , q5 , qf }, where q0 is the start state and qf the only final state,
• Σ = {1},
• Γ = {1, 1̇, } where is the blank tape symbol, and
• δ defined as follows:
– δ(q0 , 1) = (q1 , 1̇, R), Mark a one and move to the state of 1 mod 5
– δ(q1 , 1) = (q2 , 1̇, R), Mark a one and move to the state of 2 mod 5
– δ(q2 , 1) = (q2 , 1̇, R), Mark a one and move to the state of 3 mod 5
– δ(q3 , 1) = (q4 , 1̇, R), Mark a one and move to the state of 4 mod 5
– δ(q4 , 1) = (q0 , 1̇, R), Mark a one and move to the state of 0 mod 5, repeating until the
end
– δ(q4 , ) = (q4 , , L),
– δ(q3 , ) = (q3 , , L),
– δ(q2 , ) = (q2 , , L),
– δ(q1 , ) = (q1 , , L),
– δ(q0 , ) = (q0 , , L), whichever state we ended in we remember
– δ(q4 , 1̇) = (q3 , 1, L),
– δ(q3 , 1̇) = (q2 , 1, L),
– δ(q2 , 1̇) = (q1 , 1, L),
– δ(q1 , 1̇) = (q0 , 1, L), and unmark that many 1s (this will be our answer)
– δ(q0 , 1̇) = (q0 , 1̇, L), We then scan back to the beginning
– δ(q0 , ) = (q5 , , R),
– δ(q5 , 1̇) = (q5 , , R), and remove the marked 1s
– δ(q5 , 1) = (q5 , 1, L), until we find an unmarked one
– δ(q5 , ) = (qf , , R), and then halt.
(f ) f (x) =
x
2 , or f (x) =
x
2
if x is even and f (x) =
x−1
2
if x is odd.
The input to our tape will be u(x) and the output will be u( x2 ). Our strategy is similar to part
(e) above, but now if we don’t find an unmarked 1 at the end of the tape to match with a marked
one at the front of the tape, we remove the last marked one before clearing the marks and halting.
We will define the TM as follows:
5
• Q = {q0 , q1 , q2 , q3 , q4 , qf }, where q0 is the start state and qf the only final state,
• Σ = {1},
• Γ = {1, 1̇, } where is the blank tape symbol, and
• δ defined as follows:
– δ(q0 , 1) = (q1 , 1̇, R), Mark the first one
– δ(q1 , 1) = (q1 , 1, R),
– δ(q1 , ) = (q2 , , L), move to the end of the string
– δ(q2 , 1) = (q3 , , L), remove the last 1
– δ(q3 , 1) = (q3 , 1, L),
– δ(q3 , 1̇) = (q0 , 1̇, R), move back to the first unmarked 1 in the string and repeat from the
start
– δ(q0 , ) = (q4 , , L), If there are no unmarked 1s remaining to mark (x was even)
– δ(q4 , 1̇) = (q4 , 1, L), clear the marks moving to the start of the string
– δ(q4 , ) = (qf , , R), and halt
– δ(q2 , 1̇) = (q4 , , L), If no unmarked 1s to match at the end of the string (x was odd),
remove the last marked 1 and go to the state to clear the marks
9.2.7: Sketch the construction of a Turing machine that can perform the addition
and multiplication of positive integers x and y given in the usual decimal notation.
For decimal addition f (x, y) = x + y. We will start with x + y on the tape and end with the sum
on the tape. The idea is to decrement y and increment x, until y becomes 0. First we decrement
y with the following TM:
• Move to the rightmost digit in y.
• Repeat the following steps until done.
– If the current symbol under the tape head is +, then clear y and go to the start of x
and stop (the addition is complete).
– If the current symbol is any digit is anything other than a 0, decrement the current
digit under tape head by 1 (replace the digit with the next lower digit) and proceed to
increment x by one. The decrementing is done.
– If the current symbol under tape is 0, replace it with 9 and move the tape head to the
left. Continue from Repeat.
Then we increment x with the following TM:
• Move to the rightmost digit in x.
• Repeat the following steps until done.
– If the symbol under the tape head is a blank, write a 1.
6
– If the symbol under the tape head is any digit other than 9, increment the current
digit under tape head by 1 (replace the digit with the next higher digit) and proceed to
decrement y. We have finished the increment.
– If the current digit under tape head is 9, replace it with 0 and move the tape head to
the left. Continue from Repeat.
For decimal multiplication f (x, y) = xy, we start with x ∗ y on the tape and stop with the
product. The idea is to decrement y until it becomes 0 and for each decrement of y, add x to a
running sum. Decrementing y can be done using the decrement module mentioned above. To keep
adding x to x we first copy x to another part of the tape and keep a running sum on that part.
We then use the adding module above on the copy of x and the running sum. After the sum is
complete we decrement y again and repeat.
Problem 3: Linz 8.2.16
8.2.16: Let L be a deterministic context-free language and define a new language
L1 = {w : aw ∈ L, a ∈ Σ}. Is it necessarily true that L1 is a deterministic contextfree language?
No! We will use a variation of Example 7.11 to give a counter-example. Let L = {can bn : n ≥
0} ∪ {dan b2n : n ≥ 0}, then L is a deterministic context-free language, if we start with a c we follow
a branch where we accept or if we read an a we push one extra a on the stack and then pop one a
for each b read, accepting only if the stack is empty. If we start with a d, then we follow another
branch where we accept or if we read an a we push two extra as on the stack and then for each b
read we pop one a, accepting only if the stack is empty. In every configuration we can only move
to one other configuration, meeting the criteria for determinism.
However, then L1 = {an bn : n ≥ 0} ∪ {an b2n : n ≥ 0} (removing the first symbol from every
string in L), which is the language in Example 7.11 which was shown in the example to be nondeterministic.
Problem 4: Linz 9.2.5
9.2.5: Provide a “high-level” description for Turing machines that accept the
following languages on {a, b}. For each problem, define a set of appropriate
macroinstructions that you feel are reasonably easy to implement. Then use
them for the solution.
(a) L = {wwR }.
We have to match the first input symbol with the last input symbol on the tape, then proceed to
match the second input symbol and second last input symbol, and so on until we reach the middle
of the string.
On input w ∈ {a, b}∗ :
1. Repeat the following steps until the input string contains no more a’s or b’s.
• Move to the leftmost a or b on the tape. At this stage, if it cannot find a a or b, go to 2.
7
• If the symbol on the tape head is an a:
– Replace it with another symbol 0.
– Then, scan to a 0 or 1 or blank. If the symbol to the left of it is an a then change
it to a 0 and continue from step 1. If the symbol before it is not an a, computation
of the TM halts there in a non-accepting state.
• If the symbol on the tape head is a b:
– Replace it with another symbol 1.
– Then, scan to a 0 or 1 or blank. If the symbol to the left of it is b then change it
to a 1 and continue from step 1. If the symbol before it is not an b, computation of
the TM halts there in a non-accepting state.
2. Replace all 0s with as and 1s with bs and halt in an accepting state.
(b) L = {w1 w2 : w1 6= w2 : |w1 | = |w2 |}.
We have to match the length of the first half of the string with the second half of the string and
ensure that the first-half substring is not the same as the second-half substring.
On input w ∈ {a, b}∗ :
1. Repeat the following steps until the input string contains no more a’s or b’s (This part checks
if the string has even length).
• Move to the leftmost a or b on the tape. At this stage, if it cannot find an a or b, move
to the left end of the tape (until it finds a blank symbol) and go to step 2.
• If the current symbol is an a, replace it with another symbol 0. If the current symbol is
a b, replace it with another symbol 1.
• Then scan to the first 0, 1, or blank and move left.
• If the currenty symbol is an a replace it with a 0 and move left. If it is a b replace it
with a 1 and move left. If an a or a b is not found at this stage, reject the string (it
doesn’t have an even length).
• If the current symbol is a 0, replace it with a #0. If it is a 1, replace it with a #1. (This
marks the last symbol of w1 in the string). Repeat from step 1.
2. Repeat the following steps until there are no more 0s or 1s on the tape (This part checks if
the two halves are equal):
• Move to the leftmost 0 or 1 on the tape. If a 0 or 1 cannot be found reject and halt.
(The strings were identical).
• If the next symbol is a 0:
– Replace it with another symbol a.
– Then, move to the first 0 or 1 after the #. If that symbol is a 1, accept (Different
input at this position, so not equal). If it is a 0, replace it with an a. Repeat from
2.
• If the next symbol in the input is a 1:
8
– Replace it with another symbol b.
– Then, move to the first 0 or 1 after the #. If that symbol is a 0, accept (Different
input at this position, so not equal). If it is a 1, replace it with a b. Repeat from 2.
(c) The complement of the language in part (a).
The solution to part (a) has explicitly stated the transitions to accepting and non-accepting states.
If the TM in part (a) accepts, reject. If the TM in part (a) rejects, accept. Such a TM will
essentially represent the complement of the language in part (a).
(d) L = {an bm : m = n2 , n ≥ 1}.
The idea here is to remove n bs for each a seen. This is done with the following TM:
On input w ∈ {a, b}∗ :
1. Make sure the string is in L(a∗ b∗ ). If it is continue. Otherwise, halt and reject. (This can be
done by scanning to the left over the as until a b or blank. If any as are seen after the first b
reject.)
2. Mark the first unmarked a. If there are no unmarked as go to 5.
3. Scan to the end of the string and remove n bs. (This can be accomplished by zigzagging
between the as and bs giving the as a second mark and deleting bs). If there are not enough
bs to remove, then halt and reject (fewer than n2 bs).
4. Scan to the start of the string and repeat from 2.
5. If the first symbol after the marked as is a b (more than n2 bs), halt and reject. If the first
symbol is a blank, halt and accept.
(e) L = {an : n is a prime number}.
The idea here is to check if there is an 1 < i < n such that n mod i = 0. If there is then n is not
prime. We implement this as follows:
On input w ∈ {a, b}∗ :
1. Make sure the string is in L(a+ ). If it is continue. Otherwise, halt and reject. (This can be
done by scanning to the left over any as. If a b is seen before the end blank, reject).
2. If there is exactly one a on the tape, halt and reject (1 is not prime). If there are exactly two
as on the tape, halt and accept (2 is a prime number). Otherwise, continue. (Those were the
base cases).
3. For every a on the tape write an x on another part of the tape.
4. Delete the first x (we only want to check divisibility up to n − 1) and replace the next x with
a y.
5. Find the next x on the tape and replace it with a y. If there are no more xs halt and accept
(n was not divisible by any number smaller than itself and so is prime).
9
6. Then for the number of ys on the tape, call it i, check if n mod i = 0. If it is halt and reject.
If it is not continue from 5. (This can be done using a modified version of our answer to
Problem 9.1.11 (e) above, where we zigzag between ys and as placing new marks on them.
After we finish the ys we remove the marks on them and start the matching over from the
first y. We continue to do so until we run out of as to match (this does the mod ). If we
reach the end of the as and ys at the same time, then n is divisible by i and hence not prime;
whence, we reject).
Problem 5: Linz 10.3.5
Write a simple program for a nondeterministic Turing machine that accepts the
language L = {xwwR y : x, y, w ∈ {a, b}+ , |x| ≥ |y|}. How would you solve this
problem deterministically?
Nondeterminism: We will use the following nondeterministic TM:
On input z ∈ {a, b}+ :
1. Scan to the right and nondeterministically mark any two symbols in the string.
2. Check if there are at least as many symbols to the left of (and including) the first marked
symbol as there are to the right of (and including) the last marked symbol. If there are
continue. Otherwise, halt in a nonaccepting state.
3. Check if there is any sybmols between the two marks. If not reject. If there are check if the
symbols between the marks are of the form wwR (this can be done by zigzagging from front
to the end matching symbols). If it is accept and halt. Otherwise reject.
This machine marks two symbols, which breaks the string up into a possible x, y and wwR
portions. It does this nondeterministically, meaning that for any way to split the string there is a
branch of computation where that split was checked. If at least one of the splits is of the correct
form then we accept.
Determinism: Here we have to keep updating the marks and check the corresponding split until
all possible ways to split are tried. The following TM implements this algorithm:
On input z ∈ {a, b}+ :
1. Mark the first and last symbol in z.
2. If the inside part of the two marks is not empty and can be expressed as wwR , accept and
halt.
3. If the first mark is not on the next to last symbol, continue. Otherwise go to 8.
4. Move the first mark one to the right.
5. Place the last mark on the last symbol.
6. If the middle part of the two marks is not empty and can be expressed as wwR , accept and
halt.
10
7. If there are more symbols to the left of the first mark than to the right of the second mark
and there is an unmarked symbol between the two marks, move the second mark left and
repeat from 6. Otherwise repeat from 3.
8. Halt in a non-accepting state (we have tried all possible ways to split the string and none of
them produced a string in L).
11