# Solutions to Final Exam Fall 2018

### Directions:

• Answer the problems on the exam pages.
• There are four problems, each with multiple parts, for 120 total points. Actual scale was A = 100, C = 60.
• Some useful definitions precede the questions below.
• No books, notes, calculators, or collaboration.
• In case of a numerical answer, an arithmetic expression like "217 - 4" need not be reduced to a single integer.

Question text is in black, solutions in blue.

```  Q1: 30 points
Q2: 30 points
Q3: 30+10 points
Q4: 30 points
Total: 120+10 points
```

Here are definitions of some terms, sets, predicates, and statements used on this exam.

Question 1 deals with the following scenario:

During one cold week in December, each dog in the set S = {c, d, n, p} = {Cardie, Duncan, Nina, Pushkin} went for a walk on each day in the set Y = {Mon, Tue, Wed, Thu, Fri}. On each day, each dog chose whether to wear a coat, based on the temperature that morning.

Let Z be the set of integers {..., -3, -2, -1, 0, 1, 2, 3,...}. Let f be the function from S to Z defined so that f(x) is the "critical temperature" (in Celsius) for dog x: the meaning of this will come from Statement I below. Let t be the function from Y to Z defined so that t(y) is the outdoor temperature, in Celsius, on the morning of day y. Let W be the predicate defined so that W(x, y) means "dog x wore a coat on day y".

Question 1 also refers to the following four statements, where the variables are of type "dog", type "day", or type "integer":

The statements are:

• Statement I: ∀x:∀y: W(x, y) ↔ (t(y) ≤ f(x))

• Statement II: Duncan was the one and only dog to wear a coat on Tuesday, and Nina was the one and only dog to not wear a coat on Wednesday.

• Statement III: ¬((W(p, Mon) ∧ (t(Tue) = -16)) → ((t(Thu) = -16) ∧ W(c, Mon)))

• Statement IV: On Friday, the temperature was -17 and exactly two of the dogs wore coats (on Friday).

N is the set of naturals (non-negative integers, {0, 1, 2, 3,...}.

The Koch snowflake is obtained by recursively defining the polygons Sn in the following way:

• S0 is an equilateral triangle.

• Sn+1 is obtained from Sn by dividing each side of the polygon into three equal parts and replacing the middle one with the other two sides of an equilateral triangle, drawn outwards.

• Question 1 (30): This question deals with the scenario described above, and with the four statements about a set of dogs S, consisting of exactly the four dogs Cardie (c), Duncan (d), Nina (n), and Pushkin (p), and a set Y of five days {Mon, Tue, Wed, Thu, Fri}. The functions f and t, and the predicate W, are defined above.

• (a, 10) Translate each of these four statements as indicated.

• Statement I: (to English) ∀x:∀y: W(x, y) ↔ (t(y) ≤ f(x))

"Any given dog, on any given day, wore a coat if and only if the outdoor temperature on that day was less than or equal to the critical temperature for that dog."
Many people didn't get the "if and only if".

• Statement II: (to symbols) Duncan was the one and only dog to wear a coat on Tuesday, and Nina was the one and only dog to not wear a coat on Wednesday.

"∀x: (W(x, Tue) ↔ (x = d)) ∧ (W(x, Wed) ↔ (x ≠ n))"
Again, many people missed the "if and only if" in each part. Some people, most of them correctly, did this as a pure propositional statement by ANDing together the eight statements about particular dogs on particular days.

• Statement III: (to English) ¬((W(p, Mon) ∧ (t(Tue) = -16)) → ((t(Thu) = -16) ∧ W(c, Mon)))

"It is not the case that if Pushkin wore a coat on Monday and the temperature on Thursday was -16, then the temperature on Thursday was -16 and Cardie wore a coat on Monday."
Lots of people put the "if" before the "it is not the case", or otherwise messed up the parentheses. Some did part or all of the work of part (b) here, but if you just stated the alternate form of part (b) I wanted some kind of justification.

• Statement IV: (to symbols) On Friday, the temperature was -17 and exactly two of the dogs wore coats (on Friday).

"(t(Fri) = -17) ∧ ∃x:∃y:∀z: (x = y) ∧ (W(z, Fri) ↔ ((z = x) ∨ (z = y))"
In order to say "exactly two dogs", you needed to say both "two different dogs wore coats" and "no other dogs wore coats". This could be done propositionally as well, using the fact that there are only four dogs in the set. You could say "there were two different dogs who wore coats, and two different dogs who did not", for example.

• (b, 5) Prove that Statement III is equivalent to the statement
W(p, Mon) ∧ (t(Thu) = -16) ∧ ¬W(c, Mon).

This is much simpler if we assign boolean variables to the three atomicpropositions in the two statements, making Statement III into "¬((p ∧ q) → (q ∧ r)" and the alternate form into "p ∧ q ∧ ¬r". Then we can either do a three-variable truth table or make an equational sequence proof as follows:

Take Statement III, apply the Definition of Implication to get "¬(¬(p ∧ q) ∨ (q ∧ r))", apply DeMorgan (and Double Negation) to get "(p ∧ q) ∧ ¬(q ∧ r)", apply DeMorgan again to get "p ∧ q ∧ (¬q ∨ ¬ r)", apply the Distributive Law to get "(p ∧ q ∧ ¬q) ∨ (p ∧ q ∧ ¬r)", and finish by using Double Negation and the Right Zero rule for OR to reduce the first term to 0, then using the Left Identity rule for OR to remove it entirely, leaving the second term which is our alternate form.

• (c, 15) Using Statements I, II, III, and IV, determine whether Cardie wore a coat on Thursday, and prove your answer. (You many use the alternate form of Statement III give in part (b) of this problem.) You may use English, symbols, or a combination, but make your use of quantifier proof rules clear. (Hint: There are nine total temperatures given by values of f(x) for dogs x and t(y) for days y. Use the statements to derive inequality relations among these.)

The informal English argument is simplest: Statement II tells us Duncan feels most cold of all dogs, and Nina is the most resilient to cold. Cardie did not wear a coat on Monday, but Pushkin did. So by increasing critical temperature, the dogs are Nina, Cardie, Pushkin, Duncan. The two dogs not wearing coats on Friday must be Nina and Cardie. Thursday was warmer, so Cardie did not wear a coat.

Formally:
From II by Separation: (¬W(x, Wed) ↔ (x = n)). Specifying x to n, we get ¬W(n, Wed). Specifying x to c, we get W(c, Wed).
In statement I, specifying x in turn to Cardie and Nina, and y to Wednesday, we get (using the conclusions above): f(n) < t(Wed) ≤ f(c) (1)
In statement I, specifying x in turn to Cardie and Pushkin, and y to Monday, and using W(p, Mon) and ¬W(c,Mon) obtained by Separation from III, we get f(c) < t(Mon) ≤ f(p) (2)
From II by Separation: ∀x : (W(x, Tue) ↔ (x = d)). Specifying x to d, we get W(d, Tue). Specifying x to p, we get ¬W(p, Tue).
In Statement I, specifying x in turn to Duncan and Pushkin, and y to Tuesday, we get (using the conclusions above): f(p) < t(Tue) ≤ f(d) (3)
From (1), (2) and (3) we get by transitivity f(n) < f(c) < f(p) < f(d)
Statement IV implies two dogs did not wear coats on Friday, these must be Nina and Cardie; we must have f(c) < t(Fri) in order to satisfy that.
We then have f(c) < t(Fri) = -17 < -16 = t(Thu), thus f(c) < t(Thu). Specifying x to c and y to Thu in Statement I we get ¬W(c, Thu)

• Question 2 (30): Identify each of the following fifteen statements as true or false. There are two points for each correct answer, with no explanation needed or wanted. Note that there is no penalty for guessing. Some of these statements refer to terms defined at the beginning of the exam sheet.

• (a) Let Σ be a nonempty alphabet, L be the set of all languages over Σ, and R be the set of all regular expressions over Σ. Then the function L from R to L that gives the language L(R) of a regular expression R is neither one-to-one nor onto.

TRUE. There are many regular expressions for the same language, and there are many languages, even over an single-letter alphabet, that are not regular. (We had the example of strings whose lengths are prime, or are perfect squares.)

• (b) The following is not an equivalence relation on strings: R(u, v) means there exists a string w such that uw and vw either both have an even number of a's or both have an even number of b's.

TRUE. This relation is not transitive. For example R(a, ab) is true with w = a, R(ab, b) is true with w = b, but R(a, b) is false.

• (c) The number of solutions of the congruence system x ≡ a (mod m) and x ≡ b (mod n) satisfying 0 ≤ x < mn is equal to gcd(m, n).

FALSE. The statement is true if the system has any solutions. But if the two congruences are inconsistent, such as a = 0, b = 1, and m = n = 2, then there are no solutions even though gcd(m, n) > 0.

• (d) Let P be a predicate on strings over {a, b}. Then we can derive ∀w:P(w) if we know P(λ) and ∀w:(P(w) → P(wab)) ∧ (P(w) → P(wbb)) ∧ (P(wb) → P(w)).

TRUE. We can derive "P(w) → P(wa)" and "P(w) → P(wb)" by specifying the last implication to get "P(wab) → P(wa)" and "P(wbb) → P(wb)", then using Hypothecal Syllogism. Together with P(λ), this is enough for an ordinary string induction.

• (e) The depth of a DFS tree for an undirected graph with cycles is always one less than the length of the longest cycle.

FALSE. This is true if the graph is a cycle, but in general the depth could be defined by some other long path that has nothing to do with any cycles.

• (f) Let p and q be two states in a DFA such that δ(p, x) ≠ δ(q, x) for any letter x in Σ. Then p and q may be equivalent states for the purpose of minimizing the DFA.

TRUE. If δ(p, x) and δ(q, x); are always equivalent states, and p and q are either both final or both non-final, then p and q are equivalent and could still meet the conditions of this statement.

• (g) The language {an: n is not prime} is regular.

FALSE. This is the complement of a language shown in the text to not be regular, and the complement of regular languages are regular.

• (h) There is some regular expression R so that the regular expressions ((b + R)*a)* and (b*a)* are not equivalent. (Here the alphabet is assumed to be {a, b}.)

FALSE. The right-hand language is clearly contained in the left-hand language. But since every string in the left-hand language is either empty or ends in a, and the right-hand language contains all such strings, the two languages are equal no matter what R is.

• (i) Let L be the language of strings from Σ = {a, b} with no three consecutive equal letters. Then the strings λ, a, aa, ab, and bb are all pairwise distinguishable.

TRUE. Any two strings that differ in their last two letters cannot be equivalent. The string a is equivalent to ba, and λ is in a class by itself.

• (j) Let L be the language of strings of a's and b;s where the number of a's is congruent modulo n to the number of b's. Then the L-equivalence relation has n2 classes.

FALSE. The equivalence class is determined by the difference modulo n between the number of a's and the number of b's. There are only n possibilities for this number, so there are only n classes.

• (k) Let w be a binary string (of 0's and 1's). There is an NFA with |w| + 1 states that accepts exactly the binary strings ending in w, but any DFA with the same language has at least 2|w| states.

FALSE. If w happens to be a string of n 0's, then a DFA with only n+1 states can count the number of 0's seen since the last 1, using a single state for numbers that are n or more. Even for general w of length n, we can make a DFA with 2n-1 + 1 states, because if the last n letters are not equal to w we only care about the last n-1 letters.

• (l) If a 2WDFA can move left only from a single state, which is not the initial state, then it cannot hang.

FALSE. There could be a state z from which the 2WDFA, on any letter, moves left and stays in z, so that it will hang if it ever enters this state. The first Turing machine example in lecture had such a state, but it is also possible in a 2WDFA.

• (m) If language X is Turing decidable, and the complement of Y is Turing recognizable, than X \ Y must be Turing recognizable.

TRUE. X is decidable, and therefore recognizable. X \ Y is the intersection of X and Y-bar, and the intersection of two recognizable languages is recognizable.

• (n) The set of pairs consisting of (i) a description of a DFA, and (ii) a string accepted by that DFA, is Turing recognizable but not Turing decidable.

FALSE. It is Turing decidable. Since we can simulate a TM with a TM, we can certainly simulate a DFA with a TM, and we just report how the DFA terminates on the string (which it has to do).

• (o) The set of Turing machines that use only a bounded amount of the infinite tape is Turing recognizable.

TRUE. We simulate the arbitrary TM with a universal TM, recording every configuration is is ever in. If it uses a bounded amount of tape, it must repeat a configuration and enter a loop, and we can detect this.

• Question 3 (30+10): This question refers to the Koch snowflake defined above.

• (a, 10) Let En be the number of sides of the polygon Sn. Derive a recurrence (recursive definition) for En and then find a formula for En in terms of n, and prove it correct by induction.

We have E0=3, since S0 is a triangle. For any successive figure, a side becomes four sides (the two outermost thirds, and the added sides of a triangle). Thus, we have En+1 = 4En.
The problem statement said to "derive" a recurrence, which requires an argument, not just guessing from the first three values 3, 12, 48.
The found recurrence is a geometric sequence, we can thus give the general formula En = 3*4n for any natural n. We prove this by induction. The base case (for n=0) holds, since En = 3 = 3 * 40. Assuming En = 3*4n as inductive hypothesis, our recurrence gives us En+1 = 4En = 4*3*4n = 3*4n+1, and we are done.

• (b, 10) Let Qn be the number of acute angles (π/3, or 60°) of the polygon Sn, and let Rn be the number of other angles (4π/3, or 240°). Prove by induction that Qn = 4n + 2 and Rn = 2(4n) - 2. (Hint: The total number of angles in any polygon is equal to the number of sides. Another hint: Use the result of part (a).)

We deal with Qn first, by induction. For the base case (n=0), we have 40 + 2 = 3, and the figure is a triangle, with three acute angles. For the inductive step, note that Sn+1 has all acute angles of Sn and adds one more such angle for the new triangle created on each side, thus Qn+1 = Qn+En=4n+2+3*4n=4*4n+2=4n+1+2 (q.e.d.). For Rn we can just use the hint and get Rn=En - Qn = 3*4n - (4n + 2) = 2*4n - 2.
A very common mistake was to (attempt to) prove Qn + Rn = En by induction, or rather, to use this relation and the base case to confirm that the individual formulas for Qn and Rn are indeed correct. This is not valid reasoning, as one could find an arbitrary number of incorrect formulas for which the base case and the sum still work, e.g., Qn = 2*4n+1+100*n and Rn=4n-1-100*n.

• (c, 10XC) Let An be the area of Sn. Prove by induction that An = A0(1 + (3/5)(1 - (4/9)n)).

The base case (n=0) holds, as we get A0(1+3/5(1-(4/9)0)) = A0(1+3/5(1-1)) = A0.
For the inductive step, An+1 contains An, and En extra triangles (one for each side); each triangle has side 1/3 of the side of figure Sn, which in turn is 1/3n of the side of S0. So each new triangle has area A0/3n+1. We get: An+1=An + 3*4n/9n+1A0 = A0(1 + 3/5(1 - (4/9)n) + 3/4 * (4/9)n+1) = A0(1 + 3/5(1 - (4/9)n+1(9/4 - 5/3 * 3/4))) = A0(1 + 3/5(1 - (4/9)n+1(9/4-5/4))) = A0(1 + 3/5(1 - (4/9)n+1)).
A number of people had the idea right, but did not manage to do the arithmetic all the way, a pity since it only involved fractions and powers. Some tried to claim reaching a correct result with numbers that were clearly wrong, which in general will earn less partial credit than stopping without making false claims.

• (d, 10) Let Pn be the path length (or perimeter) of the figure Sn. Prove the following statement: ∀m:∃n:Pn > m, where the variables n and m range over the natural numbers. (Note that the only thing you know about P0 is that it is a positive real number.)

Denoting by Ln the side length of Sn, we have Ln = L0/3n. We then have Pn = EnLn = 4nE0 * L0/3n = (4/3)nP0. Since 4/3 > 1, the exponential will grow arbitrarily large with n. In particular, for arbitrary m we have Pn > m for n > log4/3(m/P0), and by the rules of Existence and then Generalization we obtain the conclusion.
A number of solutions merely stated that the number of sides En grows exponentially (without taking into account the side length, which could potentially decrease at the same or a faster rate). Others stated that the perimeter grows as n grows (without quantifying the growth), which is insufficient since there are increasing sequences with an upper bound (and thus a limit). The other common mistake was to attempt a proof by induction on n, whereas in the formula, m is the arbitrary quantity, and the desired n depends on m.

• Question 4 (30): This question is the usual one about Kleene's Theorem constructions, using the regular language L = ((a + bb)*b)*.

• (a, 5) Use the construction from lecture and the text to find a λ-NFA whose language is denoted by the regular expression language L = ((a + bb)*b)*. For full credit, use the construction exactly, without any simplifications (except that, if you like, you may use the alternate form of the Kleene star construction proved to be correct on Homework #6).

The construction from the textbook and the one from Homework #6 are depicted in the figures (in both cases, 0 is the initial state).
For the second construction, states 1 through 4 represent (a+bb)*, the transition (4, b, 1) closes the loop when constructing ((a+bb)*b)*.

The single most common mistake was not to do the construction from the text as given, but to try to simplify it by merging states 1 and 2, joining the loops for the inner and outer Kleene star. This happened although the problem statement asked (in emphasis) to use the construction exactly (the next problem point allowed simplification). Simplifying prematurely and without argumentation is often wrong, and it was in this case (since it breaks the required property that at any point in the construction, the initial state has no incoming transitions). Merging states 1 and 2 allows returning to 1 after an 'a' has been consumed, and then skipping the outer loop to 6. The automaton then accepts strings ending in 'a' (and in fact, any string at all), which is not true for the original regular expression.

• (b, 10) Using the construction from the text on your λ-NFA N from part (a), build an ordinary NFA N' such that L(N') = L(N). You may make simplifications of N before doing this, as long as they do not change the language of the machine and you make clear what you are doing.

It is easiest to start with the homework construction. We can eliminate state 4, through which there is a single path, and instead have a transition (2, b, 1). We can also eliminate states 0 and 5, with a single λ-transition each; state 1 becomes both initial and final. There is a single λ-transition, from 1 to 2, so no transitive closure to construct. We need to consider letter transitions into 1 and out of 2. Transition (2, b, 1) yields (1, b, 1), (1, b, 2), and (2, b, 2); transition (2, a, 2) yields (1, a, 2), and transition (2, b, 3) gives (1, b, 3).

The first construction is more complex. With the same arguments, we can eliminate state 5, introducing a transition (4, b, 6), and eliminate states 0 and 7, making state 1 initial and 6 final.
The states with λ-moves are 1, 2, 4, and 6. The transitive closure gives us: from 1, transitions into 2, 4, 6; from 2, into 4; from 4, into 2; from 6, into 1, 2, and 4.
This in turn gives us: for (2, a, 4): an a-move from any of {1,2,4,6} into {2,4}; for (2, b, 3): a b-move from any of {1,2,4,6} into 3; for (3, b, 4): b-moves from 3 to {2, 4}; for (4, b, 6): a b-move from any of {1, 2, 4, 6} into {1, 2, 4, 6}. Since λ is in the language, state 1 becomes final.
Many did not show their work, instead only drawing a complicated automaton with 6, 7, or 8 states. This is not a good approach for partial credit, since a wrong result will be apparent, but the understanding of the underlying construction is not. Many did not explicitly construct the transitive closure for λ-moves and missed transitions in the resulting NFA, especially since it was complex. Some stated that state 1 in the first construction can be eliminated since its only outgoing transitions are λ-moves. This is true, but requires carefully redirecting its incoming transitions, getting (6, λ, 6) and (6, λ, 2), to avoid errors like merging 1 and 2.

• (c, 5) Using the Subset Construction on N', find a DFA D such that L(D) = L(N').

We work with the three-state automaton obtained above, and get: δ({1}, a) = {2}; δ({1}, b) = {1, 2, 3}. Next, δ({2}, a) = {2}, and δ({2}, b) = {1, 2, 3}. Finally, δ({1,2,3},a) = {2}, and δ({1,2,3}, b) = {1,2,3}. We have three states; {1} and {1,2,3} contain final state 1 and are thus final. In the figure we have renamed {1,2,3} to 3.

Starting from the first construction is not much more complex: we get δ({1},a) = {2,4} and δ({1}, b) = {1, 2, 4, 6}; then δ({2,4}), a) = {2, 4} and δ({2,4}, b) = {1,2,4,6}, and finally δ({1,2,4,6}, a) = {2, 4} and δ({1,2,4,6}, b) = {1,2,4,6}, so a three-state automaton having the same structure, with {1} and {1,2,4,6} final states.

• (d, 5) Find a minimal DFA D' with L(D') = L(D). You may use the minimization construction, or prove directly that your D is already minimal.

The initial partition has F = {1, 3} and N = {2}. We get δ{1, a) = δ(3, a) = N, and δ(1, b) = δ(3, b) = F, so the two final states stay equivalent. The minimal automaton has two states.
Some got to cases where the next states were different for any symbol and incorrectly deduced that the automaton could not be minimized (e.g., suppose we had δ(3, b) = 1 in our case). This is not the correct approach for minimization: we start with a coarsest partition and refine it until no longer possible.

• (e, 5) By using the State Elimination construction on either D or D', find a regular expression for L(N). (In fact, L(N) = L, so you have a correct regular expression already, but the construction may produce a more complicated one.)

We have a single initial and a single final state, so we don't need to add new states (but we must keep the existing initial and final state, which happen to be the same). We eliminate state 2, and get a new transition (1, aa*b, 1), resulting in (1, b+aa*b, 1). We can simplify b+aa*b = (λ+aa*)b = a*b. The resulting language has one more Kleene star (for the loop on state 1): (a*b)*, which is the language of all strings that do not end in a. We can see the regular expressions is equivalent to the original one, but simpler, see also Question 2(h).