# Solutions to Final Exam

### Directions:

• Answer the problems on the exam pages.
• There are six problems, some with multiple parts, for 120 total points. Actual scale was A = 105, 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.

Question text is in black, solutions in blue.

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

### Definitions Used on the Exam:

• Remember that natural number always means "non-negative integer".

• The scope of any quantifier is always to the end of the statement it is in.

• Questions 1 and 2 use a set of dogs A = {b, c, d} that includes exactly the three named (and distinct) dogs Biscuit, Cardie, and Duncan. We define the following four unary predicates for any dog x in the set A:

• M(x) means "x is muddy"

• P(x) means "x went in the pond"

• S(x) means "x went in the swamp"

• W(x) means "x is wet"

• The four statements of Question 1 are:
• (I) Either Cardie or Biscuit, but not both, went in the pond, and if Cardie is not muddy, then Biscuit is muddy.
• (II) ∀x: W(x) ↔ (S(x) ∨ P(x))
• (III) Every muddy dog went in the swamp, and every dog who went in the pond is not muddy.
• (IV) ¬∀z: W(z)

• A state s in an ordinary NFA is defined to be reachable if there exists a string w such that there is a w-path from the start state to s.

• The λ-NFA N has state set {1, 2, 3, 4}, start state 1, final state set {2}, and the following five transitions: (1, b, 2), (1, b, 3), (2, a, 4), (3, λ, 1), and (4, λ, 3). It looks like this, with "L" representing "λ":

``````
b
>(1) ----> ((2))
|^         |
b||L        |a
||         |
V|         V
(3) <---- (4)
L

(Diagram corrected 8 December 2013 -- it had (4, a, 2) instead of (2,
a, 4).)
``````

Let n be any positive integer. Define the DFA Dn to have alphabet {a}, state set {0, 1, 2,..., n-1}, start state 0, final state set {1}, and transition function δ(i, a) = (i+1) % n where "%" is the Java modular division operator.

• Question 1 (10): Translate each statement as indicated, using the set of dogs {Biscuit, Cardie, Duncan} and the four predicates given above.

• (a, 3) (to symbols) (Statement I) Either Cardie or Biscuit, but not both, went in the pond, and if Cardie is not muddy, then Biscuit is muddy.

(P(b) ⊕ P(c)) ∧ (¬M(c) → M(b))

• (b, 2) (to English) (Statement II) ∀x: W(x) ↔ (S(x) ∨ P(x))

"Given any dog x, x is wet if and only if it either went in the swamp or went in the pond (or both)."

• (c, 3) (to symbols) (Statement III) Every muddy dog went in the swamp, and every dog who went in the pond is not muddy.

∀x: (M(x) → S(x)) ∧ (P(x) → ¬M(x)). Or, equivalently (and actually more literally what the English says), (∀x: M(x) → S(x)) ∧ (∀y: P(y) → ¬M(y)).

• (d, 2) (to English) (Statement IV) ¬∀z: W(z)

"It is not the case that all dogs are wet." Or, equivalently, "There exists a dog that is not wet."

• Question 2 (20): These questions use the definitions, assumptions, and predicates from Question 1, which are also listed above.

• (a, 5) Convert Statement IV to an equivalent statement beginning with an existential quantifier (∃). What conclusion can you draw from this statement by the Existential Instantiation rule?

∃z:¬ W(z), meaning "there exists a dog that is not wet". The EI rule tells us "¬W(z)", where z is some dog for which we know only that it is not wet.

• (b, 5) What conclusion about Duncan can you draw from Statement III by the Universal Instantiation rule?

We may conclude "M(d) → S(d)", meaning "if Duncan is muddy then he went in the swamp", and "P(d) → ¬M(d)", meaning "if Duncan went in the pond then he is not muddy".

• (c, 10) Prove that if Statements I, II, III, and IV are all true, then Duncan is not muddy. You may use English rather than symbols if you are convincing and include all necessary steps of the argument.

There are two parts to the argument -- we show that Biscuit and Cardie are both wet, and thus that the non-wet dog guaranteed by Statement IV is Duncan, then show that any non-wet dog is not muddy. Here's one way to spell out this argument fully in English.

Let z be the dog given by EI on Statement IV, so that we know "¬W(z)". By UI on Statement II, we have "W(z) ↔ (S(z) ∨ P(z))" and hence "¬(S(z) ∨ P(z))", "¬S(z) ∧ ¬P(z)", and finally "¬S(z)" by propositional rules. By UI on Statement III and separation we have "M(z) → S(z)", which gives us "¬S(z) → ¬M(z)" by contrapositive, and thus "¬M(z)" by Modus Ponens.

Now we must show that this dog z is equal to d, to get our desired conclusion "¬M(d)". Here we argue by cases to show "W(b) ∧ W(c)". For Case 1, assume that "P(c)" is true. Then by UI on Statement III and MP, "¬M(c)", and by MP on Statement I, "M(b)". By UI on Statement III and MP, we get "S(b)", and by UI on Statement II, Addition, and MP, we get "W(b)". Also by UI on Statement II, Addition, and MP, we get "W(c)".

Case 2 is that P(c) is false, from which Statement I tells us that P(b) is true. We can follow the argument of Case 1 exactly, switching the b's and c's, to get "W(b) ∧ W(c)" again. (Note that the contrapositive of "¬M(c) → M(b)" is "¬M(b) → M(c)".)

So we know that the non-wet dog z cannot be either b or c, and since the universe of discourse is {b, c, d} it must be d, and our conclusion "¬M(d)" follows.

• Question 3 (20): Here are two induction proofs involving finite-state machines.

• (a, 10) Prove by induction on all positive integers k that if M is any ordinary NFA with k states, and M has fewer than k - 1 transitions, then there exists a state of M that is not reachable. ("Reachable" is defined above.)

Let P(k) be the statememnt "If M is an ordinary NFA with k states and fewer than k - 1 transitions, M has an unreachable state".

To prove P(k) for all positive integers, our base case is P(1). With k = 1, it is impossible for any M to have fewer than k - 1 = 0 transitions, so all such M's have unreachable states and P(1) is true. It's also useful to prove P(2), which says that any M with two states and fewer than 2 - 1 = 1 transition has an unreachable state. This is true because the non-start state is clearly unreachable with no transitions. I was lenient with bad base cases in this problem, because the k = 1 case was unnecessarily confusing.

The IH says that any M with k states and fewer than k - 1 transitions must have an unreachable state. The inductive step is to prove that any M' with k + 1 states and fewer than k transitions must have an unreachable state.

Most of you asserted without proof that M' was made from some k-state M by adding one state and one transition. But this isn't necessarily true -- what if the state added has more than one transition? Here is a complete argument:

Let M' be any ordinary NFA with k + 1 states and fewer than k transitions. Let s be any state of M', other than the start state, that has no transitions out of it. Such an s must exist because M' has k non-start states and fewer than k transitions. Case 1: s has no transitions into it -- in this case s is the desired unreachable state. Case 2: s has a transition into it. Let M be the ordinary NFA obtained by deleting s and all transitions into it. Then M has fewer than k - 1 transitions, and by the IH it has an unreachable state t. This state t remains unreachable in M' because the new transitions cannot be used to reach any node other than s, since s has no transitions out. So in either case, M' has an unreachable state.

• (b, 10) Let N be the λ-NFA defined above. Prove by induction for all natural numbers i that the string b(ab)i is in the language L(N).

Let P(i) be the statement "b(ab)i ∈ L(N)". To prove P(i) for all natural numbers, we must begin with P(0), "b ∈ L(N)". This is true because there is a b-transition from the start state 1 to the final state 2.

Let i be an arbitrary natural number. The IH says that w = b(ab)i ∈ L(N). We must prove that b(ab)i+1 = wab is in L(N). By the IH, there is a path labeled w from state 1 to state 2. We append the edges (2, a, 4), (4, λ, 3), (3, λ, 1), and (1, b, 2) to this path to get a wab-path from state 1 to state 2, proving the inductive step and completing the induction.

• Question 4 (30): Here is a series of constructions implementing parts of the proof of Kleene's Theorem. If you cannot get the answer to an earlier part of the question, you can still get some credit for demonstrating the construction of a later part on another appropriate example.

• (a, 10) Construct an ordinary NFA N' with the same language as the λ-NFA N given above.

We make the λ-moves transitively closed by adding (4, λ, 1). The start state remains non-final because there is no λ-path from the start state to the final state. The letter move (1, b, 2) of N gives us the moves (1, b, 2), (3, b, 2), and (4, b, 2) of N'. The letter-move (1, b, 3) of N gives us (1, b, 1), (1, b, 3), (3, b, 1), (3, b, 3), (4, b, 1), and (4, b, 3) of N'. The letter-move (2, a, 4) of N gives us (2, a, 1), (2, a, 3), and (2, a, 4). This gives us twelve total moves in N'.

There were many other possible constructions leading to ordinary NFA's with the same language as N, and since I only said "construct an NFA with the same language" I gave full credit for any of them. The most interesting, I think, was to use state elimination on N, killing first state 4 (producing (2, a, 3)) and then state 3 (producing (1, b, 1) and (2, a, 1)). It happens that the r.e.-NFA we get is an ordinary NFA, and we know that state elimination preserves the language of the machine.

• (b, 10) Use the Subset Construction to construct a DFA D with the same language as N and N'.

Using my N' above, we get nonfinal start state {1} with a-arrow to ∅ and b-arrow to {1, 2, 3}, then final state {1, 2, 3} with a-arrow to {1, 3, 4} and b-arrow to itself, nonfinal state {1, 3, 4} with a-arrow to ∅ and b-arrow to {1, 2, 3}, and finally nonfinal state ∅ with both arrows to itself.

The simpler ordinary NFA above has nonfinal start state 1, final state 2, and transitions (1, b, 1), (1, b, 2), and (2, a, 1). The resulting DFA has nonfinal start state {1} with a-arrow to ∅ and b-arrow to {1, 2}, final state {1, 2} with a-arrow to {1} and b-arrow to ∅, and nonfinal state ∅ with both arrows to itself. This is the minimal DFA for the language.

• (c, 10) Use the state elimination method (starting from N, N', or D -- your choice) to get a regular expression for the language L(N).

Starting from my four-state DFA D above, first add a new final state f and a transition ({1, 2, 3}, λ, f), then kill state ∅ which requires no new transitions. We then kill {1, 3, 4}, producing a loop on {1, 2, 3} labeled ab which merges with the existing loop there to make a loop labeled "b + ab". Finally we kill {1, 2, 3} to get a regular expression of b(b + ab)*.

• Question 5 (20): This question involves some number theory and the family of DFA's Dn defined above.

• (a, 10) Prove that the set of numbers {14, 27, 95} are pairwise relatively prime. You may either use the Euclidean Algorithm or argue carefully from the prime factorizations of these numbers. Make it clear that you understand the relevant definitions.

Method 1: The Euclidean Algorithm on 27 and 14 gives 27 = 1(14) + 13, 14 = 1(13) + 1, so 14 and 27 are relatively prime. On 95 and 14 it gives 95 = 6(14) + 11, 14 = 1(11) + 3, 11 = 3(3) + 2, 3 = 1(2) + 1, so 14 and 95 are relatively prime. On 95 and 27 it gives 95 = 3(27) + 14 and then continues as with 27 and 14, so 27 and 95 are relatively prime. We have checked all pairs among {14, 27, 95} and shown the set to be pairwise relatively prime.

Method 2: 14 = 2*7, 27 = 3*3*3, and 95 = 5*19. Since no single prime appears in more than one of the three prime factorizations, no two numbers in the set have a common factor larger than 1, and the set is pairwise relatively prime.

• (b, 10) Using the Chinese Remainder Theorem, describe a regular expression for the language L(D14) ∩ L(D27) ∩ L(D95). Explain your reasoning carefully.

The DFA Dn accepts exactly those strings ai such that i ≡ 1 (mod n). The intersection of the three languages is thus the set of strings ai such that i is congruent to 1 mod 14, mod 27, and mod 95. Since the three moduli are pairwise relatively prime from part (a), the Chines Remainder Theorem tells us that the three congruences are simultaneously satisfied if and only if i ≡ c (mod M), (where M = 14*27*95) for some number c in the range from 0 through M - 1. The regular expression is thus ac(am)*.

What is c? What number is congruent to 1 modulo 14, 27 and 95? Well, c = 1 clearly works. If you didn't see that, the CRT calculation would eventually get you the same answer: Compute numbers x, y, and z so that x ≡ 1 (mod 14), y ≡ 1 (mod 27), z ≡ 1 (mod 95), and each number is ≡ 0 modulo the other two bases. We could find x, for example, by computing the inverse of 14 (mod 27*95) and multiplying that by 27895. Then c would be just (x + y + z) % M.

• Question 6 (20): These ten questions involve specific vocabulary from the course. For the first five, parts (a)-(e), identify the give term. For the second five, parts (f)-(j), explain the difference between the two terms.

• (a, 2) prime factorization of a positive integer

A prime factorization of n is a sequence of prime numbers that multiply to n -- every positive integer has one.

• (b, 2) inductive step of an ordinary induction proof

If we are proving "∀k:P(k)", the inductive step is to prove "P(k) → P(k+1)" for an arbitrary natural number k.

• (c, 2) Myhill-Nerode Theorem

The Myhill-Nerode Theorem says that a language L has a DFA if and only if the set of equivalence classes of the L-equivalence relation is finite, and that if this set is finite, its size is equal to the number of states in the smallest DFA for L.

• (d, 2) ∅*

This is a regular expression denoting the Kleene star of the empty set, the language {λ}.

• (e, 2) construction of an λ-NFA for R*, given a λ-NFA for R

Given any λ-NFA for R, we can make a λ-NFA for R* by adding λ-moves from each final state to the start state, and making the start state final. (The second step is necessary in some cases because λ ∈ L(R*).) To get a λ-NFA meeting our normal form, assuming that the λ-NFA for R also meets our form, we add a new start state and a new final state, along with four λ-moves: new start to old start, old start to old final, old final to old start, and old final to new final.

• (f, 2) → and ↔

The symbol → is for implication: "p → q" is true unless both p is true and q is false. The symbol ↔ is for equivalence: "p ↔ q" is true if p and q have the same truth value, and false if not.

• (g, 2) proof by contradiction and proof by contrapositive

A proof by contradiction of some statement p proceeds by assuming ¬p and then deriving some false statement. A proof by contrapositive of an implication "p → q" proceeds by proving the contrapositive implication "¬q → ¬p", for example by assuming ¬q and deriving ¬p.

• (h, 2) tree and forest

A tree is a connected undirected graph with no cycles. A forest is an undirected graph with no cycles -- each of its connected components is a tree.

• (i, 2) r.e.-NFA and λ-NFA An r.e.-NFA is a machine with states and transitions, where each transition is labeled by a regular expression. It must also satisfy several other conditions -- there may be no transitions into the start state, there must be exactly one final state, not equal to the start state, with no transitions out of it, and there may be no parallel transitions. (Many people incorreectly said that an r.e.-NFA had to have exactly two states, and many others intrepreted "r.e.-NFA" as "ordinary NFA".)

A λ-NFA is a machine with states and

transitions, where each transition is labeled either by a letter of the input alphabet or by λ.

• (j, 2) ∅ and λ

∅ denotes the empty set, which is a set with no elements. λ denotes the empty string, which is a string with no letters.