# CMPSCI 250 Solution to Final Exam

#### Minor corrections (in red) made 6 May 2005

.

There are six questions for 120 total points.

Questions are in black, solutions in blue.

Question 1 (30): In this problem we will carry out several of our constructions, beginning with an ordinary NFA N. This NFA has alphabet {a,b}, state set {1,2}, start state 1, final state set {2}, and transition relation Δ = {(1,a,1), (1,a,2), (1,b,2)}.

• (a,5) Draw a diagram for N. Give three examples of strings in L(N) and three examples of strings not in L(N).

Here is another ugly ASCII diagram:

``````

___
/   \ a
/     \
\     /
\   V     a,b
>(1)--------------->((2))
``````

Strings in: a, b, aa, ab, aaa, aab, others

Strings out: λ, ba, bb, aba, abb, others

• (b,10) Using the subset construction, build a DFA D such that L(D) = L(N). Make sure that your DFA has exactly two transitions out of each of its states.

State {1} is the start state. It has a-arrow to {1,2} and b-arrow to {2}. State {1,2} has a-arrow to itself and b-arrow to {2}. State {2} has a-arrow and b-arrow to the death state. The death state has a-arrow and b-arrow to itself. The two final states are {1,2} and {2}. ASCII art:

``````
_____
|     \ a
a        V      |
>(1)-------------->((12))-/
|                 /
|b               /b
|  _____________/
| /                  ____
||                  |    \ a,b
VV    a,b           V     |
((2))------------->(death)-/
``````
• (c,10) If D is minimal (that is, if no DFA with fewer states than D has the same language), prove that it is minimal. If D is not minimal, give a smaller DFA with the same language. from your answer to (b).

The DFA is minimal. We run the minimization algorithm, which we proved in lecture to be correct. We begin with two classes, N for nonfinal states and F for final states. Of the nonfinal states, {1} has both arrows to F while the death state has both arrows to N. So these two states must be separated. Similarly, {1,2} has both arrows to F while {2} has both arrows to N, so they must be separated. We have divided the four states into four singleton classes and thus no state collapse is possible.

• (d,5) Using the state elimination construction, find a regular expression that denotes L(D).

We first discard the death state, which cannot remove any paths from the start to a final state. We do not need a new start state since {1} has no arrows into it, but we must add a new final state f, add edges (12,λ,f) and (2,λ,f), and make the states 12 and 2 nonfinal. We may then eliminate 2, creating edges (1,b,f) and (12,b,f). The latter merges with an existing edge to make (12,λ+b,f). Now eliminating state 12 gives us a two-state r.e.-NFA with a single transition, bearing the regular expression b + aa*(λ+b).

Question 2 (10): Suppose that we are converting a λ-NFA M to an ordinary NFA N, and that (p,a,q) is a letter move of M. Explain what letter moves must be added to N to account for this move of M. Explain why these moves in N account for every possible path in M where (p,a,q) is the only letter move in the path.

We must add the letter move (r,a,s) where r is any state with a λ-path to p and s is any state with at λ-path from q. (Either or both of r=p or s=q are possible.) Any path in M that has only the one letter move (p,a,q) must have zero or more λ-moves (a λ-path from some state r to p), the letter move (p,a,q), and then zero or more λ-moves from q to some state s. We thus account for this path with the move (r,a,s), which we have added to n. Clearly all the letter moves we add to N do correspond to paths in M in which (p,a,q) is the only letter move.

Question 3 (20): For any natural n, the undirected graph Kn has n vertices and all possible edges (that is, it has an edge between i and j whenever i ≠ j).

• (a,5) How many edges are there in the undirected graph Kn, as a function of n?

There is an edge for every set of exactly two vertices, so there are (n choose 2) = n(n-1)/2 edges.

• (b,5) A simple path is defined to be a path that never revisits a vertex. (In particular, it may not start and end at the same vertex.) Let t and n be naturals. How many different simple paths of length t (i.e., with t edges) are there in the graph Kn?

There are n choices for the first vertex, then n-1 for the second vertex, and so on until the last vertex, which is the (t+1)'st because there are t edges. This is a sequence of t+1 vertices with no repeats allowed. By the solution to the Second Counting Problem there are nt+1 = n(n-1)...(n-t) of these. Note that this product is zero if t ≥ n.

• (c,10) Let i and j be two vertices of Kn with i ≠ j, and let t be any natural. How many simple paths of length t in Kn start at i and end at j? (Partial credit for the cases where t is 0, 1, or 2.)

For t=0 there is no such path, and for t=1 there is exactly one such path, using the edge between i and j. For t=3 there are exactly n-2 such paths, because any of the the n-2 vertices other than i or j can be the midpoint. (The case of n=1 is impossible because we could not then choose i ≠ j.) For larger t we must choose a no-repeat sequence of vertices for the intermediate vertices of the path, choosing t-1 vertices from the n-2 vertices other than i or j. There are (n-2)(t-1) = (n-2)(n-3)...(n-t) of these, by the solution to the Second Counting Problem. Note that this product is also zero when t ≥ n.

Question 4 (25): Define E to be the set of strings in {a,b}* that have an equal number of a's and b's. For example, the strings in E whose length is at most four are: λ, ab, ba, aabb, abab, abba, baab, baba, and bbaa.

• (a,5) Let f(n) be the number of strings in E of length n. Give a formula or formulas for f(n) -- you may find it useful to give one formula for odd n and another for even n.

When n is odd, f(n) = 0 because all strings in E have even length. When n is even we can make a string in E of length n by choosing any subset consisting of n/2 of the n positions to place the a's in. There are (n choose n/2) such subsets, and thus exactly that many strings, by the solution to the Third Counting Problem.

• (b,10) For a string w, let g(w) be the number of a's in w minus the number of b's in w. (Thus g(w) is an integer that may be positive, negative, or zero.) Prove that two strings u and v are E-equivalent if and only if g(u) = g(v).

First note that from the definition, w is in E if and only if g(w) = 0. Suppose that g(u) = g(v) and let w be an arbitrary string. From the definition of g, g(uw) = g(u) + g(w) and g(vw) = g(v) + g(w). So g(uw) = g(vw). If both g(uw) and g(vw) are 0, then both uw and vw are in E. If neither is zero, neither string is in E. Since w was arbitrary, we have proved ∀w:(uw∈E)↔(vw∈E) and by the definition, u and v are E-equivalent.

Now suppose that g(u) ≠ g(v). Pick a string w so that g(uw)=0. (If g(w) = i ≥ 0, let w = bi. If g(w) = -i < 0, let w = ai.) Then g(vw) ≠ 0 so vw is not in E while uw is. We have shown that u and v are not E-equivalent. So we have proved that g(u) = g(v) if and only if u and v are E-equivalent.

• (c,5) What does the result of part (b) tell you about the existence of a DFA whose language is E?

There isn't one. The Myhill-Nerode Theorem says that a DFA exists for E if and only if there are finitely many E-equivalence classes. But (b) shows that there are infinitely many E-equivalence classes, one for each integer.

• (d,5) What does the result of part (b) tell you about the existence of a two-way DFA whose language is E?

There isn't one. In Section 10.1 we proved that a 2WDFA for a language exists if and only if a DFA for it exists. As in (c), we can argue that no DFA exists for E and hence no 2WDFA for it exists.

Question 5 (15): This problem deals with some statements in the predicate calculus. The type of the variables is "animal" and we are given four unary predicates: F(x) means "x can fly", G(x) means "x is a gnu", H(x) means "x has hooves", and W(x) means "x has wings".

• (a,5) Translate the following four sentences into the predicate calculus (into statements with quantifiers, with no free variables):
• (I) All gnus have hooves.

∀x:G(x)→H(x)

• (II) There does not exist an animal with both wings and hooves.

¬∃x:W(x)∧H(x)

• (III) Given any animal, if it does not have wings then it cannot fly.

∀x:¬W(x)→¬F(x)

• (IV) There is no such thing as a flying gnu.

¬∃x:F(x)∧G(x)

• (b,10) Using our proof rules for the predicate calculus, prove that statements (I), (II), and (III) together imply statement (IV). You may use valid rules of the propositional calculus without naming them.

We prove this by contradiction. Assume ¬(IV). By instantiation, let Fred be a flying gnu. By specification from (I), G(Fred)→H(Fred) and thus H(Fred) is true by modus ponens. By specification from (III), ¬W(Fred)→¬F(Fred). Since F(Fred) is true, by modus tollens we conclude that W(Fred) is true. So we have that W(Fred)∧H(Fred). By the Rule of Existence, we have ∃x:W(x)∧H(x). But this is exactly the negation of (II). Since we derived a contradiction from (I), (II), (III), and ¬(IV), we have proved that (I), (II), and (III) together imply (IV).

Question 6 (20): Define a function h(n) from naturals to naturals recursively as follows. If n ≤ 2, then h(n) = n. If n > 2, then h(n) = h(n-1) - h(n-2) + h(n-3).

• (a,5) Compute h(n) for all n with n ≤ 8.

h(0)=0, h(1)=1, h(2)=2, h(3) = 2-1+0 = 1, h(4) = 1-2+1 = 0, h(5) = 0-1+2 = 1, h(6) = 1-0+1 = 2, h(7) = 2-1+0 = 1, and h(8) = 1-2+1 = 0.

• (b,15) Prove by induction that for all naturals n, h(n+4) = h(n).

Let P(n) be the statement "h(n+4) = h(n)". The special cases computed in (a) verify P(n) for all n with n ≤ 4. We use this as the base case for a strong inductive proof of ∀n:P(n). Let n≥4 and assume that P(i) is true for all i with i≤n. We must prove P(n+1) to complete the induction, and P(n+1) says that h(n+5) = h(n+1).

By the definition, h(n+5) = h(n+4) - h(n+3) + h(n+2). By the SIH, h(n+4) = h(n), h(n+3) = h(n-1), and h(n+2) = h(n-2). (Note that n, n-1, and n-2 are all non-negative, because n ≥ 4, and so the SIH applies to them.) Thus by substitution we have that h(n+5) = h(n) - h(n-1) + h(n-2). But the right-hand side of this equation is equal to h(n+1) by the definition, so we have shown h(n+5) = h(n+1) and completed the induction.

One student showed me a simple proof that does not use induction. Take the equation h(n+4) = h(n+3) - h(n+2) + h(n+1) and substitute for h(n+3) using the definition. (Even without using (a), the definition for h(n+3) is valid for any natural n, because then n+3 > 2.) This gives us h(n+4) = (h(n+2) - h(n+1) + h(n)) - h(n+2) + h(n+1) = h(n).