# Solutions to Final Exam

### Directions:

• Answer the problems on the exam pages.
• There are five problems, each with multiple parts, for 120 total points plus 10 extra credit. Actual scale 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+10 points
Q4: 30 points
Q5: 40 points
Total: 120+10 points
```

Question text is in black, solutions in blue.

Question 3 deals with a set D of dogs, consisting of exactly the five dogs Arly (a), Cardie (c), Duncan (d), Knight (k), and Nico (n), and a set of four activities A, consisting of "barking" (bar), "chasing" (cha), "swimming" (swi), and "wrestling" (wre). The predicate L(x, y, z) means "dog x likes activity y with dog z". If y is any activity, the binary relation Ly on D is defined by Ly(x, z) ↔ L(x, y, z).

Question 3 also refers to the following six statements, where the variables are of type "dog" or type "activity":

• Statement I: For every activity y, the relation Ly is both reflexive and symmetric.

• Statement II: Cardie likes to wrestle with Duncan, but she does not like to wrestle with any other dog (except herself) and she does not like any other activity with Duncan.

• Statement III: Cardie likes both swimming and chasing with both Nico and Knight.

• Statement IV: ∀y: ∀z: L(a, y, z) ↔ ((y = bar) ∨ (z = a))

• Statement V: ∃y: ∀z: L(c, y, z)

• Statement VI: ∀z: ∃y: L(c, y, z)

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

Question 4 uses a recursive function from N to N defined by the rules f(0) = 0 and f(n+1) = 2f(n) + n.

Questions 4 and 5 use the following DFA D, which has state set {p, q, r}, start state p, final state set {p, q}, and transition function δ given by δ(p, a) = p, δ(p, b) = q, δ(q, a) = r, δ(q, b) = q, and δ(r, a) = δ(r, b) = r.

We also use D to define three functions p(n), q(n), and r(n) from N to N as follows. For any natural n, p(n) is the number of strings w of length n such that D (started in state p) finishes reading w in state p. (That is, such that δ*(p, w) = p.) Similarly, q(n) is the number of strings w of length n such that D (again started in state p) finishes reading w in state q, and r(n) is the number finishing in state r.

Question 5 also uses the following λ-NFA N, which has state set {1, 2, 3, 4}, start state 1, final state set {4}, and transition relation Δ = {(1, λ, 2), (2, a, 2), (2, λ, 3), (3, b, 3), (3, λ, 4)}.

``````The DFA D:

___          ___         ___
|   | a      |   | b     |   | a, b
|   V        |   V       |   V
>((p)) -----> ((q)) -----> (r)
b             a

The lambda-NFA N ("L" = lambda):

___        ___
|   | a    |   | b
|   V      |   V
>(1) -----> (2) -----> (3) -----> ((4))
L           L         L

``````

• Question 1 (10): Compare and contrast the two concepts in each of the following pairs, giving enough detail to make it clear that you are familiar with them (2 points each):

• (a, 2) A back edge and a forward edge in the depth-first search tree of a directed graph.

Both are non-tree edges, meaning that if the edge is (u, v) then node v was discovered and explored before node u. A back edge occurs when v is an ancestor of u, and a forward edge when v is a descendant of u. There is also the possibility of a cross edge, if v is neither an ancestor or descendant.

• (b, 2) The prefix string and the postfix string for an arithmetic expression.

Both are string representaions of the operators and operands of the expression. In the prefix string, the operator comes before the representations of the operands, and in the postfix string it comes after. For example the expression with infix string "a*(b+c)" has prefix string "*a+bc" and postfix string "abc+*".

• (c, 2) The complement of a language and the one's complement of a binary string.

The complement of a language is the set of all strings over the language's alphabet that are not in the language. The one's complement of a binary string w is another binary string, of the same length as w, that has a 1 wherever w has a 0 and vice versa.

• (d, 2) The empty string and the empty language.

The empty string λ is the string with no letters. The empty language ∅ is a set with no strings.

• (e, 2) A Turing decidable language and a Turing recognizable language.

A language X is Turing decidable if there exists a Turing machine that accepts all strings in X and rejects all strings not in X, so that it halts on all strings. A language X is Turing recognizable if there exists a Turing machine that accepts a string if and only if it is in X. The decidable languages are a proper subset of the recognizable languages.

• Question 2 (20): The following are ten true/false questions, with no explanation needed or wanted, no partial credit for wrong answers, and no penalty for guessing. Each one counts two points.

• (a) If Statement V above is true, then Statement VI must be true as well. (Ignore the other statements.)

TRUE. Statement V says that there is a single activity y that Cardie likes with all dogs. So for each dog, there is at least one activity that Cardie likes with then, namely y. As we will see in Question 3, Statement VI can be true without Statement V being true.

• (b) If we want to store the relation L above as a boolean array, without assuming any of the statements, we will need exactly 100 boolean variables.

TRUE. We need a boolean for each triple (x, y, z) where x and z are dogs and y is an activity. By the Product Rule of combinatorics, there are exacctly 5 × 4 × 5 = 100 such triples.

• (c) There is some natural number x such that for any natural number y, the congruence 14xy ≡ y (mod 25) is true.

TRUE. One such natural is 9, since 9 * 14 = 126 is congurent to 1, mod 25. But since 14 and 25 are relatively prime, the Inverse Theorem tells us that 14 must have some inverse modulo 25, and this inverse x satisfies 14x ≡ 1, and thus 14xy ≡ y, modulo 25.

• (d) The operation of concatenation on binary strings is associative but not commutative, and has an identity element.

TRUE. For any three strings u, v, and w, the equation (uv)w = u(vw) is true, but uv need not equal vu. The identity element is λ because for any string w, λw = wλ - w.

• (e) If there is a path from node s to node t in a directed graph G, then a depth-first search of G with start node s and goal node t will always find a path from s to t with a minimum number of edges.

FALSE. Since G is not assumed to be finite and because we are not told that previously seen nodes are recognized, there is no guarantee that the path will be found at all. Even if we were granted those two assumptions, DFS does not always find the path with the minimum number of edges -- it is breadth-first search that has this property.

• (f) In any weighted graph with non-negative edge weights, the function h that has h(x) = 0 for every node x is always an admissible and consistent heuristic for an A* search.

TRUE. This h(x) certainly satisfies 0 ≤ h(x) ≤ d(x), where d(x) is the true distance, which makes it admissible. (If there is no path from x to the goal node, d(x) is considered to be infinity, which is greater than 0.) Consistency requires that for any two nodes x and y, h(x) ≤ h(y) + d(x, y), where d is the single-edge distance, and this must also be true.

• (g) Let G be a finite two-player deterministic game of perfect information, where the game ends with a real number payoff from Black to White. Assume that Black has a strategy that will ensure that he pays at most 3. Then White must have a strategy that guarantees her a payoff of at least 3.

FALSE. The existence of such a strategy does not preclude Black also having a better strategy, with a guaranteed maximum payoff of less than 3. If this strategy were Black's optimal one, then the statement would be true.

• (h) If a language X is not regular, then there exists a Turing machine M such that X = L(M).

FALSE. The language LBS from the text is certainly not regular, but was shown there and in lecture to not be the langauge of any TM.

• (i) Suppose that a single-tape Turing machine M has the transition δ(p, ☐) = (p, a, R) where ☐ is the blank symbol. Then if M reaches the configuration ☐ab☐p☐, it is guaranteed never to halt.

TRUE. Unlike the similar machine in last Fall's COMPSCI 250 final exam, this machine will move right and stay in state p, whereupon it will see another blank, move right and stay in p again, and so on through the infintely many blanks on the tape. (The notation for configurations assumes that there are infinitely many blanks to the right of the cells described.) state again

• (j) If the union of two languages X and Y is Turing decidable, then both X and Y must themselves be Turing decidable.

FALSE. Neither LBS nor its complement are Turing decidable, but the union of these two languages is Σ*, which is certainly decidable because it is the language of a TM that always accepts.

• Question 3 (20+10): This question deals with five statements about a set of dogs D, consisting of exactly the five dogs Arly (a), Cardie (c), Duncan (d), Knight (k), and Nico (n), and a set of four activities A, consisting of "barking" (bar), "chasing" (cha), "swimming" (swi), and "wrestling" (wre). The predicate L(x, y, z) means "dog x likes activity y with dog z". If y is any activity, then the binary relation Ly is defined by Ly (x, z) ↔ L(x, y, z).

• (a, 10) Translate each of these six statements as indicated.

• Statement I (to symbols): For every activity y, the relation Ly is both reflexive and symmetric.

"∀y: ∀x: Ly(x, x) ∧ (∀z: Ly(x, z) → Ly(z, x))" (Many of you left off some or all of the quantifiers.)

• Statement II (to symbols): Cardie likes to wrestle with Duncan, but she does not like to wrestle with any other dog (except herself) and she does not like any other activities with Duncan.

The simplest answer uses no quantifiers at all and just lists the facts given about what Cardie likes: "L(c, wre, d) ∧ ¬L(c, bar, d) ∧ ¬L(c, cha, d) ∧ ¬L(c, swi, d) ∧ ¬L(c, wre, a) ∧ ¬L(c, wre, k) ∧ ¬L(c, wre, n)". It's not clear whether this statement by itself implies L(c, wre, c), though Statement I does imply this. I gave credit either way.

With quantifiers, you can say "∀y:∀z: (L(c, y, d) ↔ (y = wre)) ∧ (L(c, wre, z) ↔ (z = d))". Note that the English Statement II does not tell us anything about which non-wrestling activities Cardie likes with dogs other than Duncan, so the symbolic statement must not say anything about this either. (In this sense Statement II is quite different from Statement IV, which says everything about Arly's preferences.)

• Statement III (to symbols): Cardie likes both swimming and chasing with both Nico and Knight.

This can also be done with or without quantifiers: "L(c, swi, n) ∧ L(c, cha, n) ∧ L(c, swi, k) ∧ L(c, cha, k)" or "∀y:∀z: (((y = swi) ∨ (y = cha)) ∧ (z = n) ∨ (z = k))) → L(c, y, z)". Replacing the "→" with "↔" is a mistake -- the English Statement III is purely a positive statement and says nothing about activities Cardie does not like.

• Statement IV (to English): ∀y: ∀z: L(a, y, z) ↔ ((y = bar) ∨ (z = a))

"Arly likes an activity with a dog if and only if the activity is barking or the dog is Arly himself."

• Statement V (to English) ∃y: ∀z: L(c, y, z)

"There is at least one activity that Cardie likes with all the dogs."

• Statement VI (to English) ∀z: ∃ y: L(c, y, z)

"For each dog, there is at least one activity that Cardie likes with that dog." Of course, as in the situation with Statements I-IV true, there may be different activities for different dogs.

Assuming that Statements I-IV are all true, it turns out that one of Statements V or VI must be true and the other must be false. You must determine which is which.

• (b, 10) Prove either Statement V or Statement VI from Statements I-IV. You may use either English or symbols, but make your use of quantifier proof rules clear.

Statement VI is true, so we will prove it, by Generalization. Let z be an arbitrary dog. We divide into cases. If z = a, we may take y to be barking as Arly likes barking with every dog by IV and thus by I every dog likes barking with him. If z = c, I tells us that any y will do, as Cardie likes every activity with herself. If z = d, II tells us that Cardie likes wrestling with Duncan and so we may take y to be wrestling. If z = k or z = n, III tells us that we take y to be either chasing or swimming. In each case a suitable y exists, and since z was arbitrary we have proved the original universal statement.

• (c, 10) Prove the negation of either Statement V or Statement VI from Statements I-IV. (You will find this easier if you choose the negation of the statement that is actually false.) You may use either English or symbols, but make your use of quantifier proof rules clear.

Statement V is false, so we will prove its negation "∀y:∃z: ¬L(c, y, z)" or "For every activity there is at least one dog that Cardie does not like it with." We prove this by Generalization as well. Let y be an arbitrary activity. We use Proof by Cases with two cases. If y is not wrestling, we may choose z to be Duncan, since II tells us that L(c, y, d) must be false. If y is wrestling, we may choose z to be any dog other than Cardie or Duncan, as II again tells us that L(c, wre, z) is false for those dogs. Since y was arbitrary, we have proved the universal statement. Note that Statement II was sufficient to prove V to be false, though we could have used IV instead to deal with all the non-barking activities at once -- we would still need II to find a dog with whom Cardie does not like barking.

• Question 4 (30): This question uses the functions f, p, q, and r from N to N, and the DFA D, defined above. Recall that for any natural n, p(n) is the number of strings w of length n such that δ*(p, w) = p, and that q(n) and r(n) are defined similarly (with p still used as the start state).

• (a, 10) Recall that f is defined by the rules f(0) = 0 and, for all naturals n, f(n+1) = 2f(n) + n. Prove by induction that for every natural n, f(n) = 2n - n - 1.

P(n) is the statement "f(n) = 2n - n - 1". The base case must be n = 0, since we are asked to prove P(n) for all naturals n. To prove P(0), we must show that the statement "f(0) = 20 - 0 - 1" is true. (It is not sufficient to observe that f(0) = 0 -- we must compute the value for f(0) given by the formula and show that it is the same.)

The inductive hypothesis is P(n), and the inductive goal is P(n+1). The latter says that f(n+1) = 2n+1 - (n+1) - 1 = 2n+1 - n - 2. To prove this we take f(n+1), expand it by the given rule to 2f(n) + n, apply the inductive hypothesis to get 2(2n - n - 1) + n, and use algebra to get 2n+1 - 2n - 2 + n = 2n+1 - n - 2, equaling what the formula gives us for f(n+1). We have completed the inductive step and thus completed the proof.

• (b, 5) Compute the value of p(n), q(n), and r(n) for all n with 0 ≤ n ≤ 4.

Since λ is the only string of length 0 and it goes to p, we have p(0) = 1, q(0) = 0, and r(0) = 0.

Of the two strings of length 1, a goes to p and b goes to q, so we have p(1) = 1, q(1) = 1, and r(1) = 0.

Of the four strings of length 2, aa goes to p, ab and bb go to q, and ba goes to r, given us p(2) = 1, q(2) = 2, and r(2) = 1.

Of the eight strings of length 3, aaa goes to p, aab, abb, and bbb go to q, and the other four go to r, giving us p(3) = 1, q(3) = 3, and r(3) = 4.

Of the sixteen strings of length 4, aaaa goes to p, aaab, aabb, abbb, and bbbb go to q, and the other eleven go to r, giving us p(4) = 1, q(4) = 4, and r(4) = 11.

• (c, 5) Explain why for any natural n, r(n+1) = 2r(n) + q(n). Your explanation should refer specifically to the DFA D.

Let w be an arbitrary string of length n+1. By inspection of D, we can see that there are exactly three possibilities for the edge followed by the last letter of w: w could be va for some string v that goes to q, or w could be either va or vb for some string v that goes to r. There are exactly q(n) v's that go to q, causing exactly q(n) w's of the form va for such v's, and exactly r(n) v's that go to r, causing exactly 2r(n) w's of the form va or vb for such v's. In all we have q(n) + 2r(n) strings of length n+1 that go to r, so r(n+1) = 2r(n) + q(n).

• (d, 10) Let Z(n) be the statement "(p(n) = 1) ∧ (q(n) = n) ∧ (r(n) = 2n - n - 1)". Prove that Z(n) is true for all naturals n, by ordinary induction on n.

The base case Z(0) says that p(0) = 1, q(0) = 0, and r(0) = 20 - 0 - 1 = 0, and we verified these three claims in part (b) above.

The IH Z(n) tells us that p(n) = 1, that q(n) = n, and that r(n) = 2n - n - 1. Our inductive goal Z(n+1) is that p(n+1) = 1, q(n+1) = n+1, and r(n+1) = 2n+1 - (n+1) - 1. We can prove these three statements separately. The only way a string w of length n+1 can go to p is to be of the form va where v is a string of length n that goes to p. There is exactly one such v by the IH, so there is exactly one such w as our goal says. For a string w of length n+1 to go to q, it must be va where v goes to p or vb where v goes to q. This gives us p(n) + q(n) w's of length n+1 going to q, and by the IH this is 1 + n, equaling the n+1 of our goal. The analysis in part (c) tells us that r(n+1) = 2r(n) + q(n), which by the IH is 2r(n) + n. But the proof in (a) tells us that this rule, with the base case r(0) = 0, gives us the desired expression for r(n+1).

• Question 5 (40): This question is the usual one about Kleene's Theorem constructions, using both the DFA D and the λ-NFA N given above.

• (a, 10) Use the construction from lecture and the text to obtain a regular expression denoting the language of D. Do not simplify your expression -- I want the one from the construction.

We first add both a new start state i and a new final state f to D, giving it five states and the new transitions (i, λ, p), (p, λ, f), and (q, λ, f). (Many of you replaced the latter two with the incorrect (r, λ, f).) Now we can eliminate state r, adding no new transitions because we have no transitions out of r. Next we can eliminate either p or q. If we eliminate p first, we create transitions (i, a*b, q) and (i, a*, f), making the final regular expression from eliminating q a* + a*bb*. If instead we eliminate q first, the only new transition created is (p, bb*, f), which merges with the existing (p, λ, f) to make (p, λ + bb*, f), and the final regular expression is a*(λ + bb*). Of course these two regular expressions are equivalent by the distributive law, but I wanted to see which one you produced based on the order of your state elimination.

• (b, 5) Use the construction from lecture and the text to find a λ-NFA whose language is denoted by the regular expression from part (a). (N is such a λ-NFA, but I want the one from the construction.)

Taking the simpler expression a*(λ+bb*), we get a λ-NFA with eight states {1, 2, 3, 4, 5, 6, 7, 8}, start state 1, final state 8, and transitions (1, λ, 2), (2, a, 3), (2, λ, 3), (3, λ, 2), (3, λ, 4), (4, λ, 8), (4, b, 5), (5, λ, 6), (6, b, 7), (6, λ, 7), (7, λ, 6), and (7, λ, 8). There are smaller λ-NFA's with loops, but our construction does not produce them and they did not get full credit.

• (c, 10) Using the construction given in lecture and in the text, find an ordinary NFA N' such that L(N') = L(N). (Use N rather than your answer from (b) == it will be easier.)

The transitive closure of the λ-moves includes moves (i, λ, j) for all i and j with i ≤ j. Each of the two letter-moves in N gives rise to six letter-moves in N': (2, a, 2) gives rise to (1, a, 2), (1, a, 3), (1, a, 4), (2, a, 2), (2, a, 3), and (2, a, 4), while (3, b, 3) gives rise to (1, b, 3), (1, b, 4), (2, b, 3), (2, b, 4), (3, b, 3), and (3, b, 4). State 1 becomes final in N', along with state 4, because λ ∈ L(N), but states 2 and 3 do not become final even though doing so would not change the language.

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

Start state {1} is final, and has an a-arrow to {2, 3, 4} and a b-arrow to {3, 4}. Final state {2, 3, 4} has an a-arrow to itself and a b-arrow to {3, 4}. Final state {3, 4} has an a-arrow to ∅ and a b-arrow to itself. Non-final state ∅ has a-arrow and b-arrow to itself. We complete the construction with four states -- note that leaving out the death state makes an invalid DFA.

• (e, 5) Either prove that your D' is minimal or prove that it is not minimal. (If it is not minimal, you are not required to minimize it.)

States {1} and {2, 3, 4} are both final, both have a-arrows to {2, 3, 4}, and both have b-arrows to {3, 4}. Thus every possible string w takes these two states either both to final states, or both to non-final states. Merging {1} and {2, 3, 4} thus has no effect on the language of the DFA D', and so D' cannot be minimal. The Myhill-Nerode Theorem tells us that the minimal DFA has a number of states equal to the number of equivalence classes, so you could also show that D' is not minimal by describing the three equivalence classes.

The three-state DFA obtained by merging those two states is actually isomorphic to the given DFA D, and this DFA is minimal. The two final states cannot be merged because an a takes p to a final state but takes q to a non-final state.