# Solutions to Final Exam

### Directions:

• Answer the problems on the exam pages.
• There are five problems, some with multiple parts, for 125 total points. Actual scale A = 108, C = 68.
• 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: 25 points
Q3: 25 points
Q4: 35 points
Q5: 30 points
Total: 125 points
```

Question text is in black, solutions in blue.

Correction in orange made 1 May 2015.

Definitions and Other Notes: Question 2 refers to the following five statements, where the variables are of type "dog", the set of dogs include the four distinct dogs Cardie, Duncan, Jack, Rosa, and possibly others, the predicate A(x) means "dog x is asleep", and the predicate B(x) means "dog x is barking".

• Statement I: No dog is both barking and asleep.

• Statement II: (B(r) ∨ B(j)) → B(d)

• Statement III: If either Jack is barking or Rosa is not barking, then Jack is barking and Duncan is asleep.

• Statement IV: (∃x: B(x)) → A(c)

• Statement V: It is not that case that given any two dogs, if neither is barking, then they are the same dog.

N is the set of naturals, {0, 1, 2, 3,...}, and Z is the set of integers, {..., -3, -2, -1, 0, 1, 2, 3,...}.

Question 3 uses an inductively defined function from N to Z. We define f(0) = 1, and for any positive integer n we define f(n) = 2f(n-1) + n(n-3).

Question 4 uses the following two formal languages over the alphabet {a, b}. The language X is the set of all strings that have strictly more b's than they have a's. The language Y is defined inductively as follows:

1. The string b is in Y.

2. If the string w is in Y, then so are the strings wab and wbba.

3. The only strings in Y are those forced to be by rules (1) and (2).

Question 5 uses the following λ-NFA N. Th alphabet is {a, b}, the state set is {1, 2, 3}, the start state is 1, the final state set is {2}, and the transition relation Δ is {(1, λ, 2), (2, a, 2), (2, a, 3), (3, b, 1), (3, b, 3)}.

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

``````         L
>(1) ---> ((2))<-\
^       / |    |
|      /  |____| a
b |     /
|    /
|   / a
|  /
| /
|V
(3)<-\
|___| b
``````

• 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 free variable in a quantified statement

A free variable is one whose value must be provided to determine the meaning of the statement. For example, in "∃x: P(x, y)", y is a free variable and x is a bound variable.

• (b, 2) the congruence statement "a ≡ b (mod m)"

The statement "a ≡ b (mod m)" means that a and b have the same remainder when divided by m, i.e., that a % m = b % m. Equivalently, it means that m divides the integer a - b.

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

A one-to-one function from X to Y is a function from X to Y such that there are not two distinct elements of X mapping to the same element of Y. A common wrong answer was "every element of X maps to exactly one element of Y", which is wrong because any function has this property. Many people said "each element of X maps to a unique element of Y", which would be right if it meant "each element of X maps to a different element of Y", but could also mean the wrong answer as well.

• (d, 2) a directed multigraph

A directed multigraph is a set of nodes and arcs, where each arc is directed from one node to another (or from one node to itself), and multiple arcs may have the same source and destination. Hardly anyone had the correct definition of "multigraph" that included parallel edges.

• (e, 2) the concatenation product of two formal languages X and Y

The concatenation product is the language XY consisting of all strings uv where u is an element of X and v is an element of Y.

• Question 2 (25): This question deals with five statements about a set of dogs that includes the four distinct named dogs Cardie (c), Duncan (d), Jack (j), and Rosa (r), and possibly other dogs as well. The predicate A(x) means "dog x is asleep", and the predicate B(x) means "dog x is barking".

• (a, 5) Translate each of the five statements as indicated.

• Statement I: (to symbols) No dog is both barking and asleep.

¬∃x:(A(x) ∧ B(x)), or equivalently ∀x:¬(A(x) ∧ B(x)).

• Statement II:(to English) (B(r) ∨ B(j)) → B(d)

If either Rosa or Jack (or both) is barking, then Duncan is barking.

• Statement III: (to symbols) If either Jack is barking or Rosa is not barking, then Jack is barking and Duncan is asleep.

(B(j) ∨ ¬B(r)) → (B(j) ∧ A(d))

• Statement IV: (to English) (∃x: B(x)) → A(c)

If any dog is barking, then Cardie is asleep. (A common incorrect answer misread the parentheses to get "There exists a dog such that if it is barking, then Cardie is asleep".)

• Statement V: (to symbols) It is not that case that given any two dogs, if neither is barking, then they are the same dog.

¬∀x: ∀y:(¬B(x) ∧ ¬B(y)) → (x = y), or equivalently ∃x: ∃y: ¬((¬B(x) ∧ ¬B(y)) → (x = y)).

• (b, 10) Using Statments I, II, and III only, determine exactly which dogs (if any) in the set {Duncan, Jack, Rosa} are barking.

Assume B(j). By II, B(d) is true, but by III, A(d) is also true and "B(d) ∧ A(d)" contradicts I. So we have proved ¬B(j) by contradiction. The contrapositive of III says "(¬B(j) ∨ ¬A(d)) → (¬B(j) ∧ B(r))". From this we get B(r) by Modus Ponens, and this gives us B(d) by II. So B(d) and B(r) are true, and B(j) is false.

• (c, 10) Assuming that Statements I, II, III, and IV are all true, prove Statement V using proof rules for quantified statements. (Hint: Convert Statement V into an existential statement and prove it in that form.)

The existential form of V is ∃x:∃y: ¬((¬B(x) ∧ ¬B(y)) → (x = y)), or equivalently ∃x:∃y: ¬B(x) ∧ ¬B(y) ∧ (x ≠ y). (A disturbingly large fraction of you failed to compute the negation of the implication correctly.)

To prove this existential statement by the Rule of Existence, we need to identift two distinct dogs that are not barking. Since we are assuming I, II, and III, to be true, we may use the conclusions of part (b). We have ¬B(j), and from B(r) we may derive ∃x:B(x) by Existence and then A(c) by Modus Ponens on IV. From I (specialized to c) we have ¬(B(c) ∧ A(c)), from which ¬B(c) follows. Thus Jack and Cardie can be our dogs x and y, letting us prove the existential form of V by using Existence twice. (We are told that no two of the named dogs are equal to one another.)

• Question 3 (25): This question uses the function f from N to Z defined above. We define f(0) = 1 and, for positive n, f(n) = 2f(n-1) + n(n-3).

• (a, 5) Compute f(1), f(2), f(3), f(4), f(5), and f(6).

f(1) = 2f(0) + 1(-2) = 2 - 2 = 0.

f(2) = 2f(1) + 2(-1) = 0 - 2 = -2.

f(3) = 2f(2) + 3(0) = -4.

f(4) = 2f(3) + 4(1) = -8 + 4 = -4.

f(5) = 2f(4) + 5(2) = -8 + 10 = 2.

f(6) = 2f(5) + 6(3) = 4 + 18 = 22.

• (b, 10) Prove by induction on all positive integers n that f(n) is even. (Your base case should be n = 1.)

For the base case of n = 1, we showed above that f(1) = 0, and 0 is even. For the inductive case, we assume that f(n) is even and prove that f(n+1) is even. By the given rule, f(n+1) = 2f(n) + (n+1)(n-2). The number 2f(n) must be even because 2 times any integer is even. (We don't need the IH here, except perhaps to say that f(n) is an integer.) The number (n+1)(n-2) is guaranteed to be even, because if n is even then n-2 is even, and if n is odd then n+1 is even, so (n+1)(n-2) has an even factor. Hence the sum for f(n+1) is even.

• (c, 10) Prove by induction on all naturals n that f(n) = 2n - n(n+1). (Your base case should be n = 0.)

For the base case of n = 0, we are given that f(0) = 1 and we can compute that 20 - 0(0+1) = 1 - 0 = 1.

For the inductive case, we assume that f(n) = 2n - n(n+1) and we want to show that f(n+1) = 2n+1 - (n+1)(n+2).

By the given rule, f(n+1) = 2f(n) + (n+1)(n-2) and by the IH, this is equal to 2(2n - n(n+1)) + (n+1)(n-2). We can compute that this is 2n+1 - 2n(n+1) + (n+1)(n-2) = 2n+1 + (n+1)(-2n + n - 2) = 2n+1 - (n+1)(n+2). We have proved the desired statement for n+1 and completed the proof.

• Question 4 (35): This question uses the definition of the formal languages X and Y given above. Both languages are over the alphabet {a, b}. X is the set of strings with more b's than a's, and Y is given by the inductive definition (1) b is in Y, (2) if w is in Y, then so are wab and wbba, and (3) nothing else is in Y.

• (a, 10) Prove that Y ⊆ X. I think it is easiest to use induction on the definition of Y, but you might also use ordinary induction on the number of times step (2) is used to produce a string.

Let P(w) be the statement "w ∈ X". We want to prove P(w) for all strings w in Y. The base case of the induction on the definition is P(b), which is true because "b" has more b's than a's. For the inductive case, we assume P(w) and prove P(wab) and P(wbba). Because P(w) is true, we know that w has more b's than a's. Since wab has one more a and one more b than w, it also has more b's than a's. Since wbba has one more a and two more b's than w, it also has more b's than a's. This completes the induction on Y, because rule (3) in the definition of Y says we have now covered all cases of strings in Y.

• (b, 5) Quoting Kleene's Theorem as necessary, prove that Y is the language of some DFA.

The easiest way to do this is to note that Y is the language of the regular expression b(ab+bba)*, by the definition of Kleene star, and then quote Kleene's Theorem to the effect that any regular language has a DFA. It is also simple to build an NFA for Y with five states, which actually becomes a DFA for Y if you add a sixth state that is a death state and connect all missing arrows to it. Kleene's Theorem also says that an NFA may be converted to an equivalent DFA.

• (c, 10) Is X a regular language? Prove your answer.

X is not regular, because we can prove that it has no DFA. For any two natural numbers i and j with i > j, the strings ai and aj are X-distinguishable; we prove this by noting that aibi is not in X but aj is. Since there is an infinite set of pairwise X-distinguishable strings, the Myhill-Nerode Theorem says that X has no DFA. (Hardly anyone had a correct proof, though many claimed that there were infinitely many classes. A lot of people showed that Y is a regular language, which says nothing about whether X is.)

• (d, 10) Prove that the language X is Turing decidable. You may be informal in your description of a Turing machine, as long as you are clear (a state table is not necessary).

We need a Turing machine that when started on any string w, halts in an accept state if w is in X and halts in a reject state if it is not. There are two natural approaches to constructing such a Turing machine: (1) repeatedly eliminate one a and one b from the string until you can't find one of each, whereupon you accept if and only if there is a b left, or (2) construct one or two binary counters off to the right of w, then making a pass over w, updating the counter or counters for each letter in w and accepting at the end if the counters show more b's than a's. In approach (1), you need to indicate how the TM can organize the eliminations -- if you replace a's and b's with blanks you are no longer searching a string that contains only a's and b's. You could replace letters with c's rather than blanks, or just mark the limits of w at the start. In approach (2) you need to mark where you are in w when you move the head over to change the counter.

• 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 N is {(1, &lambda, 2), (2, a, 2), (2, a, 3), (3, b, 1), (3, b, 3)}, its start state is 1, and its final state set is {2}.

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

There is one λ-move, so we needn't worry about transitive closure, but we must make state 1 a final state because it has a λ-path (of one edge) to a final state. The letter move (2, a, 2) gives us itself and (1, a, 2). The letter move (2, a, 3) gives us itself and (1, a, 3). The letter move (3, b, 1) gives us itself and (3, b, 2). Finally the letter move (3, b, 3) gives us only itself.

We have an ordinary NFA N' with state set {1, 2, 3}, start state 1, final state set {1, 2}, and seven transitions: (1, a, 2), (1, a, 3), (2, a, 2), (2, a, 3), (3, b, 1), (3, b, 2), and (3, b, 3).

Some people got a correct ordinary NFA by merging states 1 and 2 to get an NFA with state set {2, 3}, start state 2, final state set {2}, and transitions (2, a, 2), (2, a, 3), (3, b, 2), and (3, b, 3). But they did not explain why this NFA is equivalent to N and thus did not get full credit.

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

Using the N' given above, we begin with start state {1} which has an a-move to {2, 3} and a b-move to ∅. Of course ∅ has both an a-move and a b-move to itself. The state {2, 3} has an a-move to itself and a b-move to {1, 2, 3}. The state {1, 2, 3} has an a-move to {2, 3} and a b-move to itself, so we have finished the construction finding only four of the eight possible states. The final states of D are {1}, {2, 3}, and {1, 2, 3}, since both 1 and 2 are final states of N'.

• (c, 5) Using the minimization construction or otherwise, find a minimal DFA D' such that L(D') = L(D). If you do not use the construction, prove that your D' is minimal.

We examine the three final states {1}, {2, 3}, and {1, 2, 3}: State {1} goes to final on a and nonfinal on b, while the other two go to final on both a and b. We thus make a second partition of the states with {1} and ∅ in classes by themselves and {2, 3} and {1, 2, 3} together in a class X. Both states in X go to X on both a and b, so we can merge X into a single state and get a three-state DFA D'. The state set is {{1}, ∅, X}, the start state is {1}, the final state set is {{1}, X}, and the transition function has δ({1}, a) = δ(X, a) = δ(X, b) = X, and δ(1, b) = δ(∅, a) = δ(∅, b) = ∅.

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

Starting from D', we add a new final state f. (We don't need a new start state as there is no arrow into {1}.) We add transitions ({1}, λ, f) and (X, λ, f). We kill ∅, which requires no new transitions. We kill X, which creates a new transition ({1}, a(a+b)*, f) which merges with the existing ({1}, λ, f) to make ({1}, λ + a(a+b)*, f). From this we can read off the regular expression λ + a(a+b)*.

If we instead start with N, for example, we need new start and final states i and f, with transitions (i, λ, 1) and (2, λ, f). We can kill 3 to get one new transition (2, ab*b, 1). We can then kill state 1 to get two new transitions (i, λ, 2) and (2, ab*b, 2) -- the latter merges with (2, a, 2) to make (2, a + ab*b, 2). Finally killing state 2 gives us a single transition with label λ + (a + ab*b)*.

Last modified 1 May 2015