# Solutions to Final Exam

### Directions:

• Answer the problems on the exam pages.
• There are five problems, some with multiple parts, for 120 total points plus 10 extra credit. Actual scale was A = 110, C = 75.
• 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.

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

#### Definitions:

Question 2 deals with a set D of dogs, containing exactly the three distinct dogs Cardie (c), Duncan (d), and Whistle (w), and a set M of mealtimes. As all hobbits know, there are six possible mealtimes in a day: breakfast (b), second breakfast (sb), lunch (l), tea (t), supper (s), and evening snack (e). The relation F ⊆ D × M is defined so that F(x, y) means "dog x is fed at mealtime y".

Question 2 also refers to the following five statements, where the variables are of type "dog" or type "mealtime". The statements are:

• Statement I: Whistle is fed at second breakfast, tea, evening snack, and no other mealtime.
• Statement II: ∀m: ¬(F(c, m) ∧ ¬F(d, m))
• Statement III: There is no mealtime when no dog is fed.
• Statement IV: ∃m: ∀x: F(x, m) ↔ (x = d)
• Statement V: Each dog is fed at two or more mealtimes.

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

Z is the set of all integers, {..., -3, -2, -1, 0, 1, 2, 3,...}.

Question 3 uses a function g from Z × N to Z. This function is defined recursively on its second argument. We define g(n, 0) to be n for any integer n. For any integer n and natural k, we define g(n, k+1) to be 2g(n, k) - 3.

Question 4 uses a recursively defined function h from {0, 1}* to {0, 1}*. We define h(λ) = 1, and for any string w we define h(w0) to be 0h(w)0 and define h(w1) to be 1h(w).

Question 5 begins with the following λ-NFA N. The alphabet is {a, b}, the state set is {1, 2, 3, 4, 5}, the start state is 1, the final state set is {5}, and the transition relation Δ is

{(1, λ, 2), (1, a, 3), (2, a, 1), (2, λ, 4), (3, b, 4), (4, λ, 5), (5, b, 5)}.

Here is a diagram of N, with "L" meaning λ:

``````
L
>(1) - - - - - > (2)
| < - - - - - - |
|      a        |
|               |                    b
|a              |L                 ----
|               |                 /    \
V      b        V      L         V      |
(3) - - - - - > (4) - - - - - > ((5)) - -|

``````

• Question 1 (10): Identify each of the following five concepts, giving enough detail to make it clear that you are familiar with them (2 points each):

• (a, 2) A one-to-one function from a set X to a set Y

A one-to-one function f from X to Y is a function (a well-defined and total relation) that satisfies the additional rule ∀x1:∀x2: (f(x1) = f(x2)) → (x1 = x2). In other words, "every output has at most one input". Many people said some variant of "every input has exactly one output", which is just the definition of a function.

• (b, 2) The contrapositive of an implication

If "p → q" is any implication, its contrapositive is the statement "¬q → ¬p". The contrapositive is logically equivalent to the original implication.

• (c, 2) The open list of a search

The open list is a set of nodes still being processed. Nodes go on the open list when discovered, and come off when we are ready to consider their neighbors. The order in which items come off the open list defines the type of search.

• (d, 2) A Turing decidable language

A TD language X is one where there exists a Turing machine M such that for any string w over the input alphabet, M accepts w if and only if w ∈ X and M (halts and) rejects w if and only if w ∉ X.

• (e, 2) The blank symbol of a Turing machine

The blank symbol is an element of the TM's tape alphabet, but not of its input alphabet. In the standard starting configuration, all tape cells to the right of the input start off with the blank symbol.

• Question 2 (20): This question deals with five statements about a set of dogs D, consisting of the three dogs Cardie (c), Duncan (d), and Whistle (w), and a set of mealtimes M. As all hobbits know, there are six possible mealtimes in a day: breakfast (b), second breakfast (sb), lunch (l), tea (t), supper (s), and evening snack (e). The relation F ⊆ D × M is defined so that F(x, y) means "dog x is fed at mealtime y".

• (a, 5) Translate the five statements as indicated:

• Statement I: (to symbols) Whistle is fed at second breakfast, tea, evening snack, and no other mealtime.

∀m: F(w, m) ↔ (m ∈ {sb, t, e}. Alternatively, we could say "F(w, sb) ∧ F(w, t) ∧ F(w, e) ∧ ¬F(w, b) ∧ ¬F(w, l) ∧ ¬F(w, s)".

• Statement II: (to English) ∀m: ¬(F(c, m) ∧ ¬F(d, m))

There is no mealtime when Cardie is fed and Duncan is not.

• Statement III: (to symbols) There is no mealtime when no dog is fed.

∀m:∃x:F(x, m). Following the English more closely, we could say ¬∃m:∀x:¬F(x, m), which is equivalent by the DeMorgan quantifier translation rules.

• Statement IV: (to English) ∃m: ∀x: F(x, m) ↔ (x = d)

There exists a mealtime at which Duncan, and only Duncan, is fed.

• Statement V: (to symbols) Each dog is fed at two or more mealtimes.

∀x:∃m:∃n:F(x, m) ∧ F(x, n) ∧ (m ≠ n). The last part is crucial, as otherwise the dog might be fed at only one mealtime that was both m and n. Solution corrected 28 April 2016.

• (b, 5) What do the five statements tell us about which dogs are fed at tea? That is, what (if anything) can be implied from each of the five statements about the truth values of the propositions F(c, t), F(d, t), and F(w, t)?

I tells us that F(w, t) is true. II, specialized to t, tells us that if F(c, t) is true, then so is F(d, t). III tells us that at least one of the three atomic propositions is true, which gives us no new information since F(w, t) is true by I. IV and V give no information about these three atomic propositions. So (F(c, t), F(d, t), F(w, t)) might have three possible truth settings: (0, 0, 1), (0, 1, 1), or (1, 1, 1).

• (c, 10) Using any of all of Statements I-V, prove that Duncan is fed at supper. Make clear whenever you are using one of the predicate proof rules.

1. Write III as ∀m:∃x:F(x, m).
2. Specialize to m - s, so we have ∃x:F(x, s).
3. Instantiate, letting x be the dog such that F(x, s) is true. This dog must be d, c, or w.
4. Case 1: x = d and so F(d, s) and our conclusion is true.
5. Case 2: x = c. Specialize II to m = s, getting F(c, s) → F(d, s).
6. In this case by Modus Ponens we get our conclusion F(d, s).
7. Case 3: x = w. Specializing I to m = s, we get F(w, s) ↔ s ∈ (sb, t, e) and hence ¬F(w, s). This contradicts the assumption of Case 3 and so Case 3 cannot hold.
8. We have shown F(d, s) to be true in all possible cases.

• Question 3 (20): This question uses the function g from Z × N to Z, defined above. For any integer n, g(n, 0) is defined to be n. For any integer n and natural k, g(n, k+1) is defined to be 2g(n, k) - 3.

• (a, 5) Find the values of g(-1, 3), g(1, 3), g(3, 3), and g(5, 3).

For any integer n, g(n, 0) = n, g(n, 1) = 2n - 3, g(n, 2) = 2(n - 3) - 3 = 4n - 9, and g(n, 3) = 2(4n - 9) - 3 = 8n - 21. This makes g(-1, 3) = -8 - 21 = -29, g(1, 3) = 8 - 21 = -13, g(3, 3) = 24 - 21 = 3, and g(5, 3) = 40 - 21 = 19.

• (b, 5) Prove that for any positive natural k and for any integer n, g(n, k) is odd. (One way to do this is to let n be arbitrary, and use induction on all positive naturals k. Another is to apply the result of part (c) of this problem. You may use commonly known facts about even and odd numbers without proof.)

For the induction, we take k = 1 as our base. We observed above that g(n, 1) = 2n - 3 which must be odd. Now assume that g(n, k) is odd (though we don't actually need the assumption). Then g(n, k+1) = 2g(n, k) - 3 which is odd.

Directly from part (c), we could observe that if k ≥ 1, 2k is an even number and so 2k(n - 3) + 3 must be odd.

• (c, 10) Prove by induction that for any integer n and for any natural k, g(n, k) = 2k(n - 3) + 3. Let n be arbitrary and use induction on k.

• We let n be arbitrary and use induction on all naturals k.
• Our base is k = 0. By definition g(n, 0) = n, and this equals 20(n - 3) + 3 = n - 3 + 3.
• Now assume as IH that g(n, k) = 2k(n - 3) + 3.
• The definition says that g(n, k+1) = 2g(n, k) - 3 = 2(2k(n - 3) + 3) - 3 = 2k+1(n - 3) + 6 - 3 = 2k+1(n - 3) + 3.
• This proves the inductive case and completes the induction.

• Question 4 (20+10): This question uses the following recursively defined function from {0, 1}* to {0, 1}*. We define h(λ) to be 1, and for any string w we define h(w0) to be 0h(w)0 and define h(w1) to be 1h(w).

• (a, 5) Compute h(00), h(01), h(10), h(11), and h(010).

h(λ) = 1, h(0) = 010, h(1) = 11, h(00) = 00100, h(01) = 1010, h(10) = 0110, h(11) = 111, and h(010) = 010100.

• (b, 15) Prove by induction on all strings w that the length of h(w) is the length of w, plus the number of 0's in w, plus 1. (Inventing some notation, we can write our statement P(w) as "|h(w)| = |w| + |w|0 + 1".)

• Base w = λ: h(λ) = 1, so |h(λ)| = 1, and |λ| + |λ|0 + 1 = 0 + 0 + 1 = 1.
• Assume as IH that |h(w)| = |w| + |w|0 + 1.
• Now h(w0) = 0h(w)0, so |h(w)| = |h(w)| + 2 = |w| + |w|0 + 3.
• Since |w0| = |w| + 1 and |w0|0 = |w|0 + 1, we have that |h(w0)| = |w0| + |w0|0 + 1, completing the first inductive case.
• Now h(w1) = 1h(w), so |h(w1)| = |h(w)| + 1 = |w| + |w|0 + 2.
• Since |w1| = |w| + 1 and |w1|0 = |w|0, we have that |h(w1)| = |w1| + |w1|0 + 1, completing the other inductive case.

• (c, 10XC) Let X be the language that is the range of the function h, that is, the language {h(w): w ∈ {0, 1}*}. Is X a regular language? Prove your answer. (Hint: What strings in X have exactly one 1?)

The strings w such that h(w) has exactly one 1 are exactly the strings in the language 0*. (By an induction similar to part (b), we could prove that |h(w)|1 = |w|1 + 1 for all strings w.)

If w = 0n, it is easy to see (and prove by induction) that h(w) = 0n10n. So the string 0i10j is in the language X if and only if i = j.

This means that the set of strings {1, 0, 001, 0001,...} is a pairwise X-distinguishable set (since 0i10i is in X and 0i10j is not if i ≠ j) and is an infinite set.

So by the Myhill-Nerode Theorem, X is not a regular language.

• Question 5 (30): This question is the usual one about Kleene's Theorem constructions, staring with the λ-NFA N given above. The transition relation of that machine is {(1, λ, 2), (1, a, 3), (2, a, 1), (2, λ, 4), (3, b, 4), (4, λ, 5), (5, b, 5)}, its start state is 1, and its final state set is {5}.

• (a, 10) Using the construction given in lecture and in the text, find an ordinary NFA N' such that L(N') = L(N).

The state set and start state are the same, and the new final state set is {1, 5} since there is a λ-path from 1 to 5.

The transitive closure of the λ-moves includes (1, λ, 4), (1, λ, 5), and (2, λ, 5) as well as the three given λ-moves.

Move (1, a, 3) gives rise only to itself.

Move (2, a, 1) gives rise to (1, a, 1), (1, a, 2), (1, a, 4), (1, a, 5), (2, a, 1), (2, a, 2), (2, a, 4), and (2, a, 5).

Move (3, b, 4) gives rise to itself and (3, b, 5).

Move (5, b, 5) gives rise to (1, b, 5), (2, b, 5), (4, b, 5), and (5, b, 5).

We thus have 15 letter moves in N', nine labeled with a and six with b.

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

The start state is {1}, which is final.

There is an a-move from {1} to the final state {1, 2, 3, 4, 5}, and a b-move from {1} to the final state {5}.

There is an a-move from {1, 2, 3, 4, 5} to itself, and a b-move from {1, 2, 3, 4, 5} to the final state {4, 5}.

There is an a-move from {5} to the nonfinal state ∅, and a b-move from {5} to itself.

There is an a-move from {4, 5} to ∅, and a b-move from {4, 5} to {5}.

We thus have five total states, four of them final.

If we minimize this DFA (although we were not asked to do so) we find that {1} and {1, 2, 3, 4, 5} merge, and that {5} and {4, 5} merge, so that the minimal DFA has three states: Start state X (final) with an a-move to itself and a b-move to final state Y, state Y with an a-move to non-final state N and a b-move to itself, and a death state N.

• (c, 10) Find a regular expression R such that L(R) = L(N). You may begin with any of the machines N, N', or D.

(Solution corrected 13 December 2015 -- the final regular expression is as it was but we add a new initial state.) Starting from N, we add a new initial state i, a new final state f and two λ-moves (i, λ, 1) and (5, λ, f).

We eliminate 5, making one new move (4, b*, f).

We eliminate 3, making one new move (1, ab, 4).

We eliminate 4, making two new moves (1, abb*, f) and (2, b*, f).

We eliminate 2, making two new moves (1, a, 1) and (1, b*, f). The latter merges with an existing move to make (1, abb* + b*, f).

Finally we eliminate 1 to make the one new move (i, a*(abb* + b*, f) and read off the final regular expression as a*(abb* + b*).

• Question 6 (20): Indentify each of the following ten statements as true or false. There are two points for each correct answer, with no explanation requested or wanted. Note that there is no penalty for guessing.

• (a) If X is a language containing only strings of odd length, then X* also contains only strings of odd length.

FALSE. If X has any strings in it at all, X2 has strings of even length.

• (b) If the statements p → q, r → q, and p ∨ r are all true, then q must be true.

TRUE. This is easy to prove by cases.

• (c) If n is any natural with n > 2, then n2 - 1 is not prime.

TRUE. We can factor n2 - 1 as (n + 1)(n - 1), and if n > 2, then each of those factors is greater than one.

• (d) If a natural x is congruent to 17 modulo 3 and congruent to 17 modulo 6, then x must be congruent to -1 modulo 18.

FALSE. This is similar to a Chinese Remainder Theorem instance but not a correct one since 3 and 6 are not relatively prime. If x = 5, for example, the first two congruences hold but the third does not.

• (e) If δ(p, a) = (q, b, L), then the Turing machine configuration abpa☐b becomes aqbb☐b on the next step. (Here p and q are states and a, b, and ☐ are letters.)

TRUE. The machine has overwritten the a with a b, moved left, and entered state q.

• (f) There exists a two-way DFA whose language is {anbn: n ≥ 0}.

FALSE. It is proved in the text that 2WDFA's can recognize only regular languages, and we proved that language to not be regular.

• (g) If both the language X and its complement are Turing recognizable, then both those languages are also Turing decidable.

TRUE. This is an instance of the TR/TD Theorem. Each language is TR and has a TR complement, so each is TD.

• (h) The set of pairs {(M, w): M is a Turing machine, w is a string, and M accepts w in ten or fewer steps} is Turing decidable.

TRUE. We need only use a universal TM to simulate M for ten steps on input w, and see what happens.

• (i) The set of pairs {(M, w): M is a Turing machine, w is a string, and M accepts w in ten or more steps} is Turing decidable.

FALSE. If it were we could decide whether an arbitrary M ever halts on an arbitrary w by forming a machine M', that does nothing for ten steps and then simulates M, and seeing whether (M', w) was in this language.

• (j) If δ(i, ☐) = (q, a, L), where i is the start state and ☐ is the blank symbol, then the language of the Turing machine is empty.

TRUE. Given the definitions in the text, on any input this TM is guaranteed to hang on its first step by trying to go off the left end of its tape.