# Final Exam Solutions

#### 17 May 2005

Question text is in black, solutions in blue.

• Question 1 (15): Suppose we throw three identical, fair six-sided dice. (In gaming terms, we "throw 3D6".) The six sides of each die are labeled 1, 2, 3, 4, 5, and 6.

• (a,5) What is the probability that the three numbers of the three dice are all different? Is this probability greater than, equal to, or less than 1/2?

The total number of possible throws, each equally likely, is 63 = 216 (first counting problem). The total number of throws with the three results all different is 63 = 6*5*4 = 120 (second counting problem). The probability is thus 120/216 = 5/9, which is greater than 1/2.

• (b,5) How many possibilities are there for the set of numbers appearing on the three dice? (For example, if all three dice show 5, this set is {5}. If the numbers are 4, 2, and 3, this set is {2,3,4}.)

The possible sets are exactly the subsets of {1,2,3,4,5,6} of size either 1, 2, or 3. There are (6 choose 1) = 6 sets of size 1, (6 choose 2) = 15 sets of size 2, and (6 choose 3) sets of size 3, for a total of 6 + 15 + 20 = 41. (For each individual size, this is an instance of the third counting problem.)

• (c,5) How many possibilities are there for the multiset of numbers appearing on the three dice? (For example, if all three dice show 5, the muliset is {5,5,5}. If all three numbers are different, the multiset is the same as the set.)

By the fourth counting problem, with n=6 and k=3, the number of multisets is (6+3-1 choose 3-1) or (6+3-1 choose 6), which is 8*7 or 56. Another way to see this is that the sets of size 1 or 3 in part (c) are in one-to-one correspondence with multisets, but a set of size 2 such as {2,5} corresponds to two multisets ({2,2,5} and {2,5,5}). So the total number of multisets is 6 + 2*15 + 20 = 56.

• Question 2 (25): Both this question and Question 3 deal with a predicate P(w), where w represents a string over the alphabet {a,b}. The predicate is defined recursively by four rules:
1. P(a) is true.
2. If w is any string and P(w) is true, then P(bbw) is also true.
3. If w is any string and P(w) is true, then P(wb) is also true.
4. If P(w) is not forced to be true by rules 1, 2, and 3, then it is false.

• (a,10) Prove by induction on this definition that for any string w such that P(w) is true, w has exactly one a.

Base case: The string "a" has exactly one a. Inductive cases: Assume that w has exactly one a. Then bbw (rule 2) and wb (rule 3) also have exactly one a. So all strings formed by rules 1, 2, and 3 have exactly one a, and by rule 4 these are all the strings for which P(w) is true.

• (b,5) Let A be the language {w: P(w)}. Give a regular expression denoting A.

(bb)*ab*

• (c,10) Describe or draw a λ-NFA whose language is A. (The λ-NFA made from the regular expression by the construction has nine states. There is a correct λ-NFA that has only three states. Remember that any ordinary NFA is also a &lambda-NFA.)

ASCII art:

``````
From the construction ("L" means "lambda"):
L
__________                         L
/          \                       ____
L    / b      b   V  L      a      L    / b  V    L
>(1)--->(2)--->(3)--->(4)--->(5)--->(6)--->(7)--->(8)--->((9))
<-----------/                      <---/
L                               L

A simpler, ordinary NFA:

(2)
|A
b||b              _
||              / \ b
V|     a        V  \
>(1)---------->((3))/
``````

• Question 3 (25): Let f(n) be the function defined recursively by the following rules:
1. f(0) = 0, f(1) = 1, and f(2) = 1
2. If n ≥ 3, then f(n) = f(n-1) + f(n-2) - f(n-3)

• (a,10) Explain why f(n) is exactly the number of strings of length n in the language A from Question 2. (Hint: Use the result of Question 2 part (a) to explain the base case. For the inductive case, use the Double Counting Rule to count the strings of length n formed by Rule 2 and Rule 3, determining exactly how many strings are formed in both ways.)

For the base case, by (2a) we know that there are 0 strings of length 0 in A. We know that there is at most one string of length 1 by (2a), and by Rule 1 there is exactly 1. For n=2 (2a) rules out all strings except ab and ba. We see that ab is in by Rules 1 and 3, and that ba is out because we cannot form it by either rule. So the three base values of f(n) for n=0, n=1, and n=2 match the number of strings in A of those lengths.

For the inductive case, let n ≥ 3 be arbitrary. A string in A of length n must be formed either by Rule 2 or by Rule 3. Since Rule 2 can form a string in A from any string of length n-2 in A, and Rule 3 can do so from any string of length n-1 in A, we have that the strings of length n are the union of two sets, whose size (by the strong IH) are f(n-2) and f(n-1) respectively. To find the size of this union we must subtract out the number of strings in both sets, i.e., those that can be formed by either Rule 2 or Rule 3. These are exactly the strings bbub for every string u in A of size n-3, so there are exactly f(n-3) of these by the strong IH, and our total number of strings of length n is f(n-1) + f(n-2) - f(n-3) as claimed.

• (b,15) Compute f(n) for n up to at least 5, and determine a formula or rule that gives the value of f(n) for any n. (You may find the Java integer division operator useful.) Prove by induction that your rule is correct. (A good way to do this is by strong induction, with separate cases for odd and even n in the inductive step.)

We have f(0)=0, f(1)=1, f(2)=1, f(3) = 1 + 1 - 0 = 2, f(4) = 2 + 1 - 1 = 2, f(5) = 2 + 2 - 1 = 3, f(6) = 3 + 2 - 2 = 3, f(7) = 3 + 3 - 2 = 4, and f(8) = 4 + 3 - 3 = 4. The rule is that f(n) = (n+1)/2, where the "/" is Java integer division. That is, if n = 2k then f(n) = k, and if n = 2k+1 then f(n) = k+1.

Let g(n) be (n+1)/2. We must prove by strong induction for all naturals n that f(n) = g(n):

• For the base cases n=0, n=1, and n=2, we need only verify that g(0) = (0+1)/2 = 0, g(1) = (1+1)/2 = 1, and g(2) = (2+1)/2 = 1.
• For the inductive case, assume that f(i) = g(i) for all i with i ≤ n. We must prove that f(n+1) = g(n+1).
• If n is even, that is n = 2k, then g(n+1) = k+1 and f(n+1) = f(n) + f(n-1) - f(n-2) = (by the strong IH) g(n) + g(n-1) - g(n-2) = k + k - (k-1) = k+1.
• If n is odd, that is n = 2k+1, then g(n+1) = k+1 and f(n+1) = f(n) + f(n-1) - f(n-2) = (by the strong IH) g(n) + g(n-1) - g(n-2) = (k+1) + k - k = k+1.
• So in each case we have proved f(n+1) = g(n+1) and we have completed the strong induction.

• Question 4 (25+10): Define the following four predicates on strings over the alphabet {0,1}:
• C(x) means "x is a valid description of a Turing machine"
• H(x,y) means "C(x) is true, and x halts when run on input y"
• A(x,y) means "H(x,y) is true, and x outputs "yes" when run on input y"
• T(x) means "∀y:H(x,y)"

(If you are unsure about Turing machines, don't panic. Parts (a) and (b) of this question can be answered by the rules of logic without specific knowledge about Turing machines.)

• (a,5) Prove that for any string x, T(x) → C(x). You may use either symbolic language or clear English.

Let x be an arbitrary string and assume that T(x) is true. By the definition of T(x), H(x,y) is true for all y so it is true for y = λ. (We could pick any specific string for y but we must specify a particular y to continue the reasoning.) By the definition of H, H(x,λ) means that C(x) and something else is true, so by left separation C(x) is true. We have completed a direct proof of T(x) → C(x).

• (b,15) Using quantifier rules carefully, prove the statement

¬∃u:∀v: C(u) ∧ [H(u,v) ↔ ¬H(v,v)]

(Hint: Assume the negation of this statement and derive a contradiction using quantifier rules.)

• Assume the negation of the given statement, which is

∃u:∀v: C(u) ∧ [H(u,v) ↔ ¬H(v,v)].

• By Instantition (∃-elim), let a be a string making

∀v: C(a) ∧ [H(a,v) ↔ ¬H(v,v)] true.

• By Specification (∀-elim) with v = a, conclude

C(a) ∧ [H(a,a) ↔ ¬H(a,a)].

• By right separation, conclude

H(a,a) ↔ ¬H(a,a).

• Since this is of the form "p ↔ ¬p", it is a contradiction.
• Since we derived a contradiction from the negation of the original statement, we have proved that original statement.

• (c,5) Explain the meaning of the statement of part (b) in English. What does it assert to be impossible?

There is no string u that denotes a Turing machine such that for any string v, u halts on v if and only if v does not halt on v. That is, it is impossible to design a Turing machine that halts on exactly those inputs that are not Turing machines that halt on themselves. In other words, the set of descriptions of TM's that do not halt on themselves (called in lecture the "Barber of Seville language") is not Turing recognizable.

• (d,10) Explain the meaning of the statement

∀x:[T(x) → ∃z: C(z) ∧ ∀w: [H(z,w) ↔ A(x,w)]]

Explain informally why it is true.

If a string x denotes a TM that halts on every input, there is another string z that denotes a TM that halts on any input if and only if x says yes on it. That is, any Turing decidable language {w: x says yes on w} is also a Turing recognizable language {w: z halts on w}.

This statement is true because we can take any TM x and modify it so that when it was going to halt, it actually halts if it was going to say yes and it runs forever if it was going to say no.

• Question 5 (35): Define the following λ-NFA M: The state set is {1,2}, the start state is 1, the final state set is {1}, and the transition relation Δ is {(1,a,1), (1,λ,2), (2,a,1), (2,b,2)}.
• (a,5) Draw a diagram for M. Remember that you should have one arrow for each triple in Δ.

``````

_ a         _ b
| V    L    | V
>((1))------->(2)       Again, "L" means "lambda".
<--------/
a
``````
• (b,10) Using the "killing λ-moves" construction, build an ordinary NFA N that is equivalent to M.

The state set and start state are the same, and the final state set is the same because the start state is already final. The transition (1,a,1) gives us itself and (1,a,2). The transition (2,a,1) gives us itself, (1,a,1), (1,a,2), and (2,a,1). The transition (2,b,2) gives us itself and (1,b,2). We thus have six distinct transitions in the ordinary NFA:

``````
_ a         _ a,b
| V   a,b   | V
>((1))------->(2)
<--------/
a
``````
• (c,10) Use the subset construction to build a DFA D that is equivalent to M and N. (Partial credit for any correct DFA for this language. If you have trouble with (b), make sure you give some answer here so that you can do part (d).)

The initial state is {1}, a final state. It has an a-move to {1,2}, a final state, and a b-move to {2}, a non-final state. The state {1,2} has an a-move to itself and a b-move to {2}. The state {2} has an a-more to {1,2} and a b-move to itself. The DFA can be drawn as follows:

``````
_____
|     \ a
a           V     /
>(({1}))----------->(({1,2}))/
|                 |  A
| b              b|  | a
V                 |  |
->({2})<---------------  |
|   |   \_________________/
\__|b
``````
• (d,10) Apply the state minimization construction to your D to get a minimal DFA (which may or may not be D itself).

We divide the states into two sets F = {{1}, {1,2}} and N = {{2}}. Since N contains only one state, which is reachable, we are finished with it. Both the states in F have a-arrows into F and b-arrows into N, so we may merge these two states into a single state to get the following minimal DFA:

``````

_ a         _ b
| V    b    | V
>((F))------->(N)
<--------/
a
``````