# Solutions to Final Exam

### Directions:

• Answer the problems on the exam pages.
• There are four problems for 125 total points. Actual scale is A = 105, C = 68.
• If you need extra space use the back of a page.
• No books, notes, calculators, or collaboration.
• In case of a numerical answer, an arithmetic expression like "32× 26 + 13 × 41" need not be reduced to a single integer.

```  Q1: 20 points
Q2: 50 points
Q3: 25 points
Q4: 30 points
Total: 125 points
```

Here are several definitions used on the exam.

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

This exam's boolean logic problem involves four atomic propositions and three compound propositions:

• DB means "Duncan is barking".
• HF means "Duncan heard a fox".
• HO means "Duncan heard an owl".
• IN means "Duncan imagined a noise".
• Statement 1 is "If Duncan is not barking, then he heard an owl if and only if he imagined a noise."
• Statement 2 is "¬(¬HF ∨ IN) → DB".
• Statement 3 is "If Duncan heard an owl, then he is barking, and if he is not barking, then either he heard a fox or he imagined a noise or both."

• Here are two properties that a formal language X might or might not have, expressed in predicate logic where the type of the variables is "string". Since I can't think of good descriptive names for these properties, I have named them after my dogs.
• X has the Cardie property if ∃u:∀v: uv ∈ X.
• X has the Duncan property if ∀u:∃v: uv ∈ X.

• A death state of a DFA is a non-final state that has all its arrows to itself. That is, if d is the state, δ(d, a) = d for all letters a in the alphabet. A victory state in a DFA is a final state that has all its arrows to itself.

• The function g from naturals to naturals is defined by the rules g(0) = 1 and g(n + 1) = 2g(n) + 2. So g(1) = 4, g(2) = 10, and g(3) = 22.

• The function h from naturals to naturals is defined by the following recursive pseudo-Java code:

``````
public natural h (natural n) {
if (n == 0) return 0;
if ((n % 2) == 1) return 1;
return 2 * h(n/2);}
``````

• The λ-NFA M has state set {1, 2, 3, 4}, start state 1, final state set {4}, and transitions (1, a, 2), (1, b, 3), (2, b, 2), (2, λ, 4), and (3, a, 4). It looks like this:

``````

/---\ b
\   /
\ V
/-->(2)--\
a /          \ lambda
/            V
>(1)           ((4))
\            ^
b \          / a
\-->(3)--/
``````

• Question 1 (20): This boolean logic problem deals with the atomic propositions and the Statements 1, 2, and 3 defined above.

• (a, 5) Translate statements 1 and 2 into symbols, and translate Statement 2 into English.

Statement 1: ¬DB → (HO ↔ IN)

Statement 2: If it is not the case that either Duncan did not hear a fox or that he imagined a noise, then Duncan is barking.

Statement 3: (HO → DB) ∧ (¬DB → (HF ∨ IN))

• (b, 15) From Statements 1, 2, and 3, prove that Duncan is barking. You may use either truth tables or propositional proof rules. (Hint: The similar "murder mystery" made heavy use of Proof by Cases. You might also try Proof by Contradiction.)

1. By Statement 1, HO ↔ IN.

2. By Statement 2, using contrapositive and Modus Ponens, ¬HF ∨ IN.

3. By the first part of Statement 3, ¬HO.

4. By the second part of Statement 3, HF ∨ IN.

5. From steps 1 and 3, we get ¬IN.

6. Then from step 2 we get ¬HF.

7. But step 4 gives us HF and we have a contradiction.

8. Since the assumption of ¬DB, plus the three given statements, led to a contradiction, then the three statements imply DB as desired.

• Question 2 (50): These questions all deal with the Cardie and Duncan properties of a formal language, defined above.

• (a, 5) Give English descriptions of the Cardie property and the Duncan property.

X has the Cardie Property: There is a string such that if it is followed by any string, the result is in X.

X has the Duncan Property: Given any string, we can find a string to follow it such that the result is in X.

• (b, 10) Prove, using quantifier proof rules, that the language a*b* does not have the Duncan property.

The negation of the Duncan Property is ∃u:∀v: uv ∉ X. Let u be the string ba. Let v be an arbitrary string, so that uv is bav. The string bav cannot be in a*b* because strings in that language never have an a after a b. Since v was arbitrary, we have proved ∀v: bav ∉ X by Generalization. Since we have found a choice for u that works, we have proved the desired statement by Existence.

• (c, 10) A given language might or might not have either of those properties. Give four regular expressions, representing:

• A language with neither property

The empty langauge has neither property because "uv ∈ X" can never be true.

• A language with the Cardie property but not the Duncan property

a(a+b)* works -- "a" followed by any string is in X, but no string following b results in a string in X.

• A language with the Duncan property but not the Cardie property

(a+b)*a works -- any string followed by "a" is in X, but no string followed by "b" is in X.

• A language with both properties

(a+b)* works because now "uv ∈ X" is always true.

You do not have to justify your answers, but make sure you indicate which regular expression is an answer to which of the four parts.

• (d, 15) Prove that a regular language has the Cardie property if and only if its minimal DFA has a victory state. (I don't need an inductive proof, but make sure you argue carefully from all the relevant definitions.)

Most of you failed to see that you are asked to prove an "if and only if" statement and thus have two directions to prove.

Suppose that X satisfies ∃u:∀v: uv ∈ X. Consider the minimal DFA for X. Let u be any string such that ∀v: uv ∈ X is true. I claim that δ*(i, u) is a victory state. Clearly it is final, since uλ = u must be in X. If a is any letter, I claim that ua ≡X u. If this is true, then ua and u are in the same equivalence class, so that δ*(i, ua) = δ*(i, u) and thus δ(δ*(i, u), a) = δ*(i, u). Since the a-arrow from the state goes to itself for every letter a, it is a victory state.

To see the equivalence, we must prove that ∀z: (uz ∈ X) ↔ (uaz ∈ X). This is true because both uz and uaz are guaranteed to be in X because ∀v: uv ∈ X is true and we can specify v to be either z or az.

We still need to prove the other half. Assume that the minimal DFA for X has a victory state s. All states in the minimal DFA are reachable, so there exists some string w such that δ*(i, u) = s. Now we must prove that for this w, ∀v: wv ∈ X. This is true because ∀v, δ*(i, wv) = s, a final state. We prove this by induction on v. For v = λ it is clear from the definition of s. If we assume that δ*(i, wv) = s, then δ*(i, wva) is also s because all arrows from s go to s. We have shown that X has the Cardie property.

• (e, 10) Suppose you are given a DFA M as a labeled directed graph. Explain how you could use our search algorithms on the directed graph to determine whether the language L(M) has the Duncan property.

If X has the Duncan property, and M is a DFA with L(M) = X, we know that for every reachable state s in M, there is a path from s to some final state. So we first do a DFS or BFS of the directed graph of M starting from i, to get a list of all the reachable states. Then for each of these states, we DFS or BFS the directed graph from that state, with our goal being a final state. If we reach our goal for each state, X has the Duncan property. If we fail for any state, it does not have the Duncan property.

• Question 3 (25): These problems refer to the functions g and h defined above.

• (a, 10) Prove, by ordinary induction on naturals, that for any natural n, g(n) = 3(2n) - 2.

Let P(n) be the statement "g(n) = 3(2n) - 2".

P(0) says that g(0) = 3(20) - 2 = 3 - 2 = 1. We are given that g(0) = 1 so P(0) is true.

Now assume that P(n) is true, and we will try to prove P(n+1). P(n+1) says that g(n+1) = 3(2n+1) - 2. We know that g(n+1) = 2g(n) + 1 by the definition, and by the IH we know that g(n+1) = 2(3(2n) - 2) + 2 = 3(2n+1) - 4 + 2 = 3(2n+1) - 2 as desired. This finishes the inductive step and so finishes the proof of P(n) for all n.

• (b, 10) Prove, by strong induction on naturals, that for any positive natural n, the number h(n) divides n.

P(n) is the statement "h(n) divides n".

Since we are using induction on positive naturals, the base case is n = 1. We know that h(1) = 1 because 1 is odd, and we know that 1 divides 1, so P(1) is true.

Now assume the statement Q(n), which says that P(i) is true for all i such that 1 ≤ i ≤ n. We want to prove that P(n+1) is true, which means that h(n+1) divides n+1. If n+1 is odd, then h(n+1) = 1 and P(n+1) is true because h(n+1) = 1 and 1 divides any natural. If n+1 is even, it equals 2k for some positive natural k, and we know that P(k) is true from the strong IH, Q(n). So we know that h(k) divides k. By the definition of h, h(2k) is equal to 2h(k). This number divides 2k because h(k) divides k. So P(n+1) is true and we have completed the inductive step of our strong induction, and thus proved that P(n) is true for all n.

• (c, 5) Describe the output of the function h(n) in terms of the prime factorization of the input n.

The number h(n) is the product of all the 2's occurring in the prime factorization of n. That is, n is equal to 2k times the product of zero or more odd primes, and h(n) is then just 2k.

You weren't required to prove this, but if you were you would use induction on k, the number of 2's in the prime factorization of n. If k = 0, then n is odd and h(n) = 1 which equals 2k. Assume that our characterization of h is true for all naturals with k 2's, and look at a natural n with k+1 2's. Then n is even, so h(n) = 2h(n/2). The IH says that h(n/2) is 2k, where k is the number of 2's in n/2, so h(n) is 2k+1 as desired.

• Question 4 (30): Here is the inevitable question on Kleene's Theorem constructions.

• (a, 5) Using the construction from lecture, build a λ-NFA from the regular expression ab* + ba. (This λ-NFA is equivalent to, but different from, the λ-NFA M given above. We will use M for the rest of the problem, to make your life easier.

Following the construction from lecture gives us a four-state λ-NFA for b*, with states 1, 2, 3, and 4, λ-moves from 1 to 2, 2 to 3, 3 to 2, and 3 to 4, and a b-move from 2 to 3. The total λ-NFA has these four states plus states 0 and 5, and additional moves (0, a, 1), (0, b, 5), and (5, a, 4). Here 0 is the start state and 4 is the final state.

• (b, 10) Build an ordinary NFA N that is equivalent to the given λ-NFA M, so that L(N) = L(M).

The only λ-move is from 2 to 4, so the λ-moves are closed and there is no change to the final state set. Move (1, a, 2) gives itself and (1, a, 4). Move (2, b, 2) gives itself and (2, b, 4). Moves (1, b, 3) and (3, a, 4) give only themselves, so we get an ordinary NFA with six moves. There are other correct ordinary NFA's -- you were not required to use the construction to get your ordinary NFA.

• (c, 10) Using the Subset Construction, build a DFA D such that L(D) = L(N).

From the N given above, we have nonfinal start state {1} with a-arrow to final state {2, 4} and b-move to nonfinal state {3}. State {2, 4} has a -arrow to nonfinal state ∅ and b-arrow to itself. State {3} has a-arrow to final state {4} and b-arrow to ∅. Both {4} and ∅ have both their arrows to ∅

• (d, 5) Show the steps of the State Elimination algorothm to get the regular expression ab* + ba from D.

We don't need a new start state, but we need a new final state f with λ-moves from both {2, 4} and {4}, which now become nonfinal states. We first delete ∅, making no new arrows. We delete {3}, making one new arrow ({1}, ba, {4}). We then delete {4}, making one new arrow ({1}, ba, f). We then delete {2, 4}, making one new arrow (1, ab*, f). The two parallel arrows merge to give a single arrow (1, ab* + ba, f), and we are done because there are only two states left.