# Solutions to Final Exam Spring 2017

### Directions:

• Answer the problems on the exam pages.
• There are four problems, each with multiple parts, for 120 total points. Actual scale was A = 105, C = 70.
• 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: 30 points
Q2: 30 points
Q3: 25 points
Q4: 35 points
Total: 120 points
```

Question text is in black, solutions in blue.

Here are definitions of some terms, sets, predicates, and statements used on this exam.

Remember that the scope of any quantifier is always to the end of the statement it is in.

Remember that a natural is a non-negative integer, so that the set N or all naturals is {0, 1, 2, 3,...}.

Question 2 deals with the following scenario. All of the dogs in my neighborhood are avid birdwatchers. One day, a set of five dogs met after their morning walks to compare their observations of five possible bird species. The set D of dogs consists exactly of Arly (a), Cardie (c), Duncan (d), Mia (m), and Whistle (w). The set S of species consists exactly of Bluebird (B), Crow (C), Heron (H), Mallard (M), and Woodpecker (W). The relation R ⊆ (D × S) is defined so that (x, y) ∈ R means "dog x observed a bird of species y".

Question 2 also refers to the following five statements, where the variables are of type "dog" or of type "bird species".

The statements are:

• Statement I: There is a species that was observed by all the dogs.

• Statement II: ∀z: R(a, z) ↔ (z = C)

• Statement III: Every dog other than Arly observed at least two different species of bird.

• Statement IV: ∀x: [(x ≠ a) ∧ (x ≠ c)] → [(∀y:R(c, y) → R(x, y)) ∧ (∃z: R(x, z) ∧ ¬R(c, z))]

• Statement V: Duncan was the one and only dog who observed a Woodpecker, and Whistle was the one and only dog who observed a Bluebird.

Question 3 uses a recursive function f from N to N defined by the rules f(0) = 1, f(1) = 1, and for n ≥ 1, f(n+1) = f(n) + 2f(n-1).

Questions 3 and 4 both use a language X over the alphabet {a, b, c}, which is denoted by the regular expression (a+ab+ac)*.

Question 3 uses the function g from N to N, defined so that g(n) is the number of strings of length n in the language X.

Question 4 also uses the following ordinary NFA N, which has state set {p, q, r}, start state p, final state set {p}, and transition relation Δ = {(p, a, p), (p, a, q), (p, a, r), (q, b, p), (r, c, p)}.

``````
The ordinary NFA N:

---
|   | a
|   V    a
>((p)) -----> (q)
^ |  <-----
| |     b
c| |a
| |
| V
(r)
``````

• Question 1 (30): Identify each of the following fifteen statements as true or false. There are two points for each correct answer, with no explanation needed or wanted. Note that there is no penalty for guessing. Some of these statements refer to terms on the supplementary sheet.

• (a) Let x, y, and z be three positive integers such that there is no positive integer d, with d > 1, that divides all three. Let a, b, and c be any three integers. Then the congruences n ≡ a (mod x), n ≡ b (mod y), and n ≡ c (mod z) must have a common solution.

FALSE. This would be true if x, y, and z were pairwise relatively prime. We could have x = 2, y = 3, and z = 4, which have no factor common to all three but are not pairwise relatively prime. Then if a were 0 and c were 1, there would be no common solution.

• (b) Let p, q, and r be three propositions. If p → q, q → r, and r → p are all true, then p, q, and r must all be true.

FALSE. If p, q, and r are all false, those three implications are all true.

• (c) Statement IV above implies that the species observed by Cardie are a proper subset of the species observed by Duncan.

TRUE. The statement says that Duncan saw all of Cardie's species plus at least one more, which is the definition of Cardie's species being a proper subset.

• (d) Let Q(x, y) be a binary predicate on naturals. If Q(0, 0) and ∀x: ∀y: Q(x, y) → Q(x+1, y+1) are both true, then ∀x: ∀y: Q(x, y) must be true.

FALSE. Q(x, y) might true when x = y and false otherwise, and all the given conditions would still be true.

• (e) Let P(x) be a unary predicate on naturals. If P(1), ∀x: P(x) → P(x+2), and ∀x: P(x) → P(2x) are all true, then P(x) must be true for all positive x, but P(0) may be false.

TRUE. The given conditions suffice to prove P(x) for all positive naturals, either by odd/even induction or by string induction, once we use the last condition to prove P(2).

• (f) Let G be a directed graph with finitely many nodes and let s and t be nodes of G. If we perform a breadth-first search of G starting from s, without recognizing previously seen nodes, and there is a path from s to t, then the search will discover node t.

TRUE. BFS always finds a path if one exists, when the graph has only finitely many nodes, even if we don't recognize previously seen nodes.

• (g) Let G be a directed graph. Suppose we carry out a depth-first search of G from some start node s, recognizing previously seen nodes, and the open list becomes empty after we have seen a proper subset S of the nodes of G. If we restart the search at a node outside of S, then any edges we find with destinations in S will become cross edges.

TRUE. Such edges will have destinations that are neither ancestors nor descendants of their sources in the DFS forest. This is because the destinations will be in the first tree of the forest, and the sources will be in subsequent trees.

• (h) There exist weighted directed graphs, and admissible and consistent heuristic functions, for which A* search produces a path that is no shorter than the path produced by a uniform-cost search with the same start and goal node.

TRUE. In fact, whenever the heuristic is admissible and consistent, and A* returns a path, it will be the same length as the path found by uniform-cost search.

• (i) Consider the following two-player deterministic zero-sum game. White chooses to move Right or Left, then Black chooses to move Right or Left. If the sequence of moves is LL, White gets a payoff of 2. If it is LR, White gets -1. If it is RL, White gets 3, and if it is RR, White gets -2. Then if both players play optimally, White will get -1.

TRUE. An optimal White wil move Left on the first move, whereupon an optimal Black will move Right to get payoff for White. If White moved Right on the first move, Black would move Left to force a payoff smaller than -1, so an optimal White will not do this.

• (j) Let X be the language denoted by the regular expression (a+ab+ac)*. Then the strings acba and acbac are X-equivalent in the sense of the Myhill-Nerode Theorem.

TRUE. Neither string can be made into a member of X by appending any string at all.

• (k) A two-way DFA, when started on any string over its alphabet, always either accepts, rejects, or hangs after a finite number of steps.

FALSE. It might get stuck in a loop, where it continues to compute forever without ever halting or hanging.

• (l) Let M be a Turing machine with transition function δ, and let δ(q, a) be (p, b, L). Then if the configuration of M is aqab on one time step, it will be pabb on the next time step.

TRUE. The machine is in state q and sees an a, so by the given rule it changes to state p, prints a b over the a it was looking at, and moves left. The tape contents change from aab to abb, and the machine is looking at the first character after the step, in state p.

• (m) Let Y and Z be any two Turing recognizable languages. Then the language Y ∪ Z is also Turing recognizable.

TRUE. We could build a new TM that would run recognizers for Y and Z, in parallel, on the given input. If either recognizer accepts, it will accept. If both reject, the new TM may reject, but otherwise it will run forever, which is fine because the input is not in Y ∪ Z.

• (n) Let M be a Turing machine with exactly two final states, one accepting and one rejecting. Recall that for such a TM, L(M) is the set of strings accepted by M. Let M' be the Turing machine made from M by switching the roles of the accepting and rejecting states, Then L(M') is the complement of L(M).

FALSE. If M never halts on some string, it will be in neither L(M) nor L(M'). So L(M) and L(M') will not be complements in that case because their union will fail to include all possible strings.

• (o) Let BT be the set of strings representing Turing machines that accept on blank input, that is, BT = {(M): λ ∈ L(M)|. Then there cannot exist an enumerator that outputs exactly the strings in BT, in order of length, with strings of the same length in alphabetical order.

TRUE. We proved in lecture that this language BT is not Turing decidable. But if this enumerator existed, we could decide membership in BT for an arbitrary string w. We would run the enumerator until it either output w (whereupon we would accept) or output a string longer than w (whereupon we would reject). One of these has to happen because there are arbitrarily long strings in BT and the enumerator must get to all of them.

• Question 2 (30): This question deals with the scenario described above, and with the five statements about a set of dogs D, consisting of exactly the five dogs Arly (a), Cardie (c), Duncan (d), Mia (m), and Whistle (w), and a set of five bird species S, consisting of Bluebird (B), Crow (C), Heron (H), Mallard (M), and Woodpecker (W). The relation R ⊆ (D × S) is defined so that for any dog x and any species y, (x, y) ∈ R if and only if dog x observed a bird of species y.

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

• Statement I: (to symbols) There is a species that was observed by all the dogs.

∃y: ∀x:R(x, y)

• Statement II: (to English) ∀z: R(a, z) ↔ (z = C)

For every species, Arly observed that species if and only if it was Crow. Equivalently, Arly observed crows and only crows.

• Statement III: (to symbols) Every dog other than Arly observed at least two different species of bird.

∀x: (x ≠ a) → (∃y:∃z:R(x, y) ∧ R(x, z) ∧ (y ≠ z)). It's important to specify that the two species observed are not the same.

• Statement IV: (to English) ∀x: [(x ≠ a) ∧ (x ≠ c)] → [(∀y:R(c, y) → R(x, y)) ∧ (∃z: R(x, z) ∧ ¬R(c, z))]

If any dog is neither Arly nor Cardie, then it observed all the species that Cardie observed, and observed at least one species that Cardie did not.

• Statement V: ( to symbols) Duncan was the one and only dog who observed a Woodpecker, and Whistle was the one and only dog who observed a Bluebird.

∀x: (R(x, W) ↔ (x = d)) ∧ (R(x, B) ↔ (x = w))

• (b, 20) Assuming that Statements I-V are all true, prove that some dog observed a Heron. You may use either English or symbols, but make your use of quantifier rules clear.

The simplest proof is to note that Mia must have seen at least three species, since Cardie (who is not Arly) observed at least two by III and Mia (who is neither Arly nor Cardie) observed those two plus at least one more by IV. Since by V those three species cannot have included Woodpecker or Bluebird, they must have been Crow, Heron, and Mallard, and thus Mia saw a Heron.

If we just gather information from the earlier statements, we can get to the same conclusion. The species observed by all the dogs in I must have been Crow by II, since Arly saw no other species. Specializing III to Cardie, we find that she has seen some species other than Crow. This cannot have been Woodpecker or Bluebird by V, so it must have been Heron or Mallard. If it was Heron, we are done. If it was Mallard, we can specify III to Mia and look at the species she saw that Cardie did not. This cannot have been Crow or Mallard (which Cardie saw) or Woodpecker or Bluebird (which Mia did not see) so it must have been the Heron. In either case we get ∃x:R(x, H) by Existence.

• Question 3 (25): This question uses the language X and the functions f and g defined above. X is the language of the regular expression (a+ab+ac)*. The rules defining f are f(0) = 1, f(1) = 1, and for any n with n ≥ 1, f(n+1) = f(n) + 2(f(n-1). The function g is defined so that g(n) is the number of strings of length n in the language X.

• (a, 10) Prove by strong induction on all naturals n that f(n) = (2n+1 + (-1)n)/3. You will need base cases for n = 0 and n = 1.

Base case n = 0: (20+1 + (-1)0)/3 = (2+1)/3 = 1.

Base case n = 1 (21+1 + (-1)1)/3 = (4-1)/3 = 1.

Inductive case for strong induction: We are given that f(n+1 = f(n) + 2f(n-1). By the strong IH, since n ≥ 1, f(n) = (2n+1n)/3 and f(n-1) = (2n-1+1 + (-1)n-1)/3. So f(n+1) = (2n+1 + (-1)n + 2(2n + (-1)n-1))/3 = (2n+2 + (-1)n+1)/3.

• (b, 5) Compute the values of f(n) for all n with 0 ≤ n ≤ 4. By listing strings in X, show that f(n) = g(n) for these values of n.

n = 0: f(n) = 1 given, only string is λ.

n = 1: f(n) = 1 given, only string is a.

n = 2: f(2) = f(1) + 2f(0) = 1 + 2 = 3, strings are aa, ab, ac.

n = 3: f(3) = f(2) + 2f(1) = 3 + 2 = 5, strings are aaa, aab, aac, aba, aca.

n = 4: f(4) = f(3) + 2f(2) = 5 + 6 = 11, strings are aaaa, aaab, aaac, aaba, aaca, abaa, abab, abac, acaa, acab, acac.

• (c, 10) Prove by strong induction on all naturals n that f(n) = g(n). You may use part (b) for your base cases. For the general case, note that your argument must refer to strings and the regular expression defining X.

The base cases of n = 0 and n = 1 were verified in part (b). Assume that f(n) = g(n) and that f(n-1) = g(n-1), using the strong IH. Then g(n+1) is the number of strings of length n+1 in X. These strings may be formed either by appending an a to a string in X of length n, or by appending an ab or an ac to a string in X of length n-1. There is no double counting since the first set of strings each end in a and the second does not. So we have g(n) = f(n) strings of the first kind and 2g(n) = 2f(n) of the second kind. By definition, f(n+1) is equal to f(n) + 2f(n-1), and by this analysis g(n) is also equal to that.

• Question 4 (35): This question is the usual one about Kleene's Theorem constructions, using both the regular expression (a+ab+ac)* and the NFA N given above.

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

The λ-NFA from the construction has six states. There is a start state i with λ-arrow to state p. State p has a-arrows to states q, r, and s, and a λ-arrow to s. State q has a b-arrow to s, and state r has a c-arrow to s. State s has λ-arrows to p and to state f. There are no other arrows, and f is the only final state.

• (b, 5) Explain informally (but convincingly) why L(N) = X.

A string is in L(N) if and only if it can label a path in N from state p to itself. Such a path must consist of zero or more phases between visits to p, and thus of zero or more subpaths labeled by a, ab, or ac. A string is in (a+ab+ac)*, by definition, if and only if it is the concatenation of zero or more strings, each of which is either a, ab, or ac.

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

We have start state {p}, which is final since p is final in N.

The a-arrow from {p} goes to {p, q, r}, which is a final state.

The b-arrow and c-arrow from {p} both go to ∅, a non-final state, since there is no b-arrow or c-arrow from p in N.

From {p, q, r}, the a-arrow goes to {p, q, r} and the b-arrow and c-arrow both go to {p}.

From ∅, all three arrows go to ∅.

The process is complete with three states in the DFA.

• (d, 5) Is your D minimal? Prove your answer. If it is not minimal, you are not required to minimize it, but just to show that it is not minimal.

This DFA is minimal, because the two final states are separated by b (since b takes {p} to a non-final state but takes {p, q, r} to a final state). So they cannot be merged, and any correct DFA must have at least two final states and at least one non-final state.

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

We add a new start state i and a new final state f, adding &-moves from i to {p}, from {p} to f, and from {p, q, r} to f. We eliminate ∅, which produces no new arrows since there are no arrows out of ∅.

We then eliminate {p}, which produces moves (i, λ, f), (i, a, {p, q, r}), ({p, q, r}, (b+c)a, {p, q, r}), and ({p, q, r}, b+c, f).

Then eliminating {p, q, r} produces the final regular expression λ + a(a+ba+ca)*(λ+b+c).

Though it looks a bit different, this is equivalent to (a+ab+ac)*, since any non-empty string in the latter can be divided into an a, zero or more a's, ba's and ca's, and an optional b or c.