# Final Exam Solutions

#### 22 May 2006

Questions are in black, solutions in blue

• Question 1 (35): This problem involves the following four languages over the alphabet Σ = {a,b}:
• L0 is the set of strings that do not end in b (including λ).
• L1 is the set of strings that end in exactly one b.
• L2 is the set of strings that end in exactly two b's.
• L3 is the set of strings that end in three or more b's.

• (a,5) Give regular expressions denoting each of the languages L0, L1, L2, and L3.

L0 = λ+Σ*a = (Σ*a)*, L1 = b + Σ*ab, L2 = bb + Σ*abb, L3 = Σ*bbb.

• (b,10) Build a four-state DFA that decides the language L2. (Hint: Your DFA should have one state corresponding to each of the four given languages.)

States {q0, q1, q2, q3}, start state q0, final state set {q2}. For all i, δ(qi,a) = q0. For i = 0, 1, or 2, δ(qi,b) = qi+1. Finally, δ(q3,b) = q3.

• (c.5) Argue, without using the state minimization algorithm, that there is no three-state DFA that decides L2.

Since the language is non-empty the DFA must have at least one final state. It must have at least three final states because the strings λ, b, and bbb are pairwise L2-inequivalent. To see this, we must take each of the three pairs (u,v) and find a string w so that exactly one of the strings uw and vw are in L2. For λ and b, or λ and bbb, we may take w = bb. For b and bbb, we may take w = b.

• (d,15) Label the states of your DFA q0, q1, q2, and q3 to correspond to the four languages. Then prove by induction on all strings u that with your DFA, δ*(s,u) = qi if and only if u ∈ Li. (Here "s" refers to whichever of the qi's you have designated as the start state.) (Hint: Your inductive step will have four cases, one for each i. In each of these you must show that if u is in Li, then both ua and ub are in the sets Lj indicated by your DFA.)

Let P(u) be the proposition "∀i:δ*(q0,u) = qi iff u ∈ Li". We prove P(u) for all strings u by induction.

For the base case, we prove P(λ) as follows. By definition, δ*(q0,λ) = q0, and λ is in L0 because it does not end with a b.

For the inductive case, we assume P(u) as IH. Since δ*(q0,u) must be one of the four states, we break into four cases:

• Case 0: δ*(q0,u) = q0. Here u is in L0 and so does not end in b. Then ua is in L0 because it does not end in b, and ub is in L1 because it ends in exactly one b. This matches δ(q0,ua) which is q0 and δ(q0,ub) which is q1.

• Case 1: δ*(q0,u) = q1. Here u is in L1 and so ends in exactly one b. Then ua is in L0 because it does not end in b, and ub is in L2 because it ends in exactly two b's. This matches δ(q0,ua) which is q0 and δ(q0,ub) which is q2.

• Case 2: δ*(q0,u) = q2. Here u is in L2 and so ends in exactly two b's. Then ua is in L0 because it does not end in b, and ub is in L3 because it ends in exactly three b's. This matches δ(q0,ua) which is q0 and δ(q0,ub) which is q3.

• Case 3: δ*(q0,u) = q3. Here u is in L0 and so ends in three or more b's. Then ua is in L0 because it does not end in b, and ub is in L3 because it ends in four or more b's. This matches δ(q0,ua) which is q0 and δ(q0,ub) which is q3.

In each case we have shown P(ua) and P(ub), so we have completed the string induction.

• Question 2 (25): Let Z be the language over alphabet {a,b,c} that consists of all strings wcw, where w is a string in (a+b)*. (That is, Z = {u: ∃w: u = wcw ∧ w ∈ (a+b)*}.)

• (a,5) Given a natural n, how many strings of length n are in the language Z? (There will be separate answers for even and odd n.)

If k is the length of w, the length of the string in z is n = 2k + 1. Thus if n is even, the string cannot be of this form and there are thus 0 strings of that length in Z. If n is odd, there is exactly one string in Z of length n for every possible string w of length k. There are thus 2k = 2(n-1)/2 strings of length n in Z.

• (b,5) Suppose we choose an n-letter string at random from the language (a+b+c)*, with every string equally likely. What is the probability that we choose a string in Z? (Again, answer separately for odd and even n.)

The probability is the answer to (a) divided by the total number of strings of length n from the three-letter alphabet, which is 3n. So with even n the probability is 0, and with odd n it is 2(n-1)/2)/3n.

• (c,5) Is it possible for a two-way DFA to decide the language Z? Justify your answer, based on results from lecture and observations about Z.

It is not possible. We know that a two-way DFA exists for the language if and only if an ordinary DFA exists, by a theorem proved in lecture. But there can be no ordinary DFA for this language, because it has an infinite number of Myhill-Nerode classes. If u and v are any distinct strings in (a+b)*, then uc and vc are Z-inequivalent because ucu is in Z but vcu is not. So all the infinitely many strings of the form wc are pairwise inequivalent and are in different classes.

• (d,10) Informally describe how a (deterministic, one-tape) Turing machine could decide the language Z. (I don't want a state table, just a high-level description that indicates how the state table would be built.)

The machine should first scan the tape, counting the c's among the a's and b's and rejecting unless there is exactly one c. (It also rejects if any letter other than a, b, or c occurs before the first blank.) It then marks the cell past the last letter of the input with a d -- this will make the termination condition easier to check. It then returns to the start square, which is now the next blank to its left.

Then it repeats the following steps:

1. Blank out the first a or b and remember it.
2. Move to the first a or b past the c.
3. Reject if this is not the same as the remembered letter, otherwise blank it out.
4. Return to the start of the string, which is the first blank to the left of the c.

This loop can end in only three ways:

• Step 1 fails to find an a or b. Move right and accept if there are no a's or b's between the c and the d -- reject otherwise.
• Step 2 fails to find an a or b before the d -- reject.
• Step 3 finds a mismatch and rejects.

• Question 3 (35): This question begins with a λ-NFA M. It has state set {p,q,r}, start state p, final state set {r}, and four transitions in Δ: (p,λ,q), (p,b,r), (q,a,q), and (q,λ,r).
• (a,5) Draw a diagram of this λ-NFA M. Give three examples of strings in L(M) and three examples of strings not in L(M).

``````
>(p)---------->((r))
|     b        ^
|              |
\            /
lambda  \          / lambda
\        /
\      /
\    /
V  /
->(q)\
|      | a
\____/
``````

In: λ, a, b. Out: ab, ba, bb.

• (b,10) Using the construction from lecture, create an ordinary NFA N, with the same state set, such that L(N) = L(M). Remember that N's final state set may or may not be the same as M's.

The transitive closure of the λ-moves includes (p,λ,r). Since there is a λ-move in the closure from p to r, we make p a final state in N. The move (p,b,r) gives rise only to itself. The move (q,a,q) gives rise to (p,a,q), (p,a,r), (q,a,q), and (q,a,r). This gives us:

``````
>((p))--------->((r))
|     a,b      ^
|              |
\            /
a  \          / a
\        /
\      /
\    /
V  /
->(q)\
|      | a
\____/
``````
• (c,10) Using the Subset Construction, make a DFA D from your NFA N with L(D) = L(N). (If you could not do part (b), you may give any correct DFA for this language by any method for partial credit.)

The start state (also final) is {p}, with a-move to {q,r} and b-move to {r}. The final state {q,r} has a-move to itself and b-move to ∅. The final state {r} has both a-move and b-move to ∅, as does the nonfinal state ∅ itself. We finish with four states:

``````
>((p))--------->((r))
|       b      \
|               \
\               \
a  \               \ a,b
\               \
\               \
\               \
V        b      V
->((qr))---------->(d)--
|        \           ^   \ a,b
\_______/ a         \___/
``````
• (d,10) Using the construction from lecture on your DFA D, make a regular expression R such that L(D) = L(R).

We add a new start state s and new final state f, with λ-moves from s to p, p to f, qr to f, and r to f. We first eliminate d, which has no moves out of it and thus needs no moves to replace it. We then eliminate r, which has one move in, one move out, and no loop, so it is replaced by the move (p,b,f) which merges with an existing move to form (p,λ+b,f). Now we eliminate qr which has one move in, one out, and one loop to create the move (p,aa*,f) which merges to create (p,λ+b+aa*,f). Now eliminating p gives us the regular expression λ + b + aa*, which simplifies to b + a*.

• Question 4 (30): This question involves the following four predicates on cards in a standard 52-card deck. Remember that such a deck has thirteen cards in each of the four suits.
• S(c) means that c is a spade.
• R(c) means that c is a red card (a diamond or heart).
• B(c) means that c is a black card (a club or spade).
• E(c,d) means that cards c and d have the same rank (for example, both sevens).

We are given the following assumptions:

• (I) Every card is either red or black, but not both.
• (II) Every spade is black.
• (III) Given any card, there is a spade that has the same rank as it.

• (a,5) Translate statements I, II, and III into symbolic logic with quantifiers, using the predicates defined above.

• (I) ∀c: R(c) ⊕ B(c)
• (II) ∀c: S(c) → B(c)
• (III) ∀c:∃d: S(d) ∧ E(c,d)

• (b,15) Using only statements I, II, and III and the rules for proofs with quantifiers, prove the statement:

¬∃c:∀d: E(c,d) → R(d).

We first translate the statement to be proved, using the DeMorgan law for quantifiers twice and the rule ¬(p→q) ↔ (p∧¬q), to get:

∀c:∃d: E(c,d) ∧¬R(d)

1. Let a be arbitrary.
2. By specification to a on III, we have ∃d: S(d) ∧ E(a,d).
3. By instantiation on 2, let b be such that S(b) and E(a,b).
4. By specification to b on II, we have S(b) → B(b).
5. By specification to b on I, we have R(b) ⊕ B(b).
6. By left separation on 3 and modus ponens on 4 we get B(b).
7. From 5 and 6 and the truth table for ⊕ we get ¬R(b).
8. By right separation on 3, we get E(a,b).
9. By conjunction on 8 and 7 we get E(a,b) ∧ ¬R(b).
10. By existence on 9 we get ∃d: E(a,d) ∧¬R(d).
11. Since a was arbitrary, by generalization on 10 we get the desired statement.

• (c,5) If we choose five cards from a standard 52-card deck, with any set of five cards being equally likely, what is the probability that every one of those five cards is either red or a spade?

The number of cards that are either red or spades is 39, so there are (39 choose 5) sets of five of these cards. The probability is thus (39 choose 5) divided by (52 choose 5). If we view both the numerator and denominator as numbers of sequences rather than sets, we get 395/525 = (39/52)(38/51)(37/50)(36/49)(35/48) = (19*37*3)/(4*17*5*7*4) = 2109/9520 = about 0.2215. This last product of fractions is the natural way to compute the probability as a sequence of five events.

• (d,5) If we deal out five cards from a standard 52-card deck in order and without replacement, with any no-repeat sequence of five cards being equally likely, what is the probability of the event "the first three cards are red and the last two cards are spades"?

The number of such sequences is 263*132 = 26*25*24*13*12. The total number of sequences is 525 = 52*51*50*49*48, so the probability is the first number divided by the second, which cancels to 13/17*2*49 = 13/1666 = about 0.0078.