# 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 = 108, C = 72.
• 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 points
Q4: 30+10 points
Q5: 40 points
Total: 120+10 points

Question text is in black, solutions in blue.

Correction in green made 20 April 2016.

Question 3 deals with a set D of dogs, containing the three distinct dogs Ali (a), Cardie (c), and Duncan (d), and possibly others, and a set of exactly four commands C, consisting of "come here" (ch), "fetch" (f), "roll over" (r), and "sit" (s). The predicate O(x, y) means "dog x obeys command y".

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

• Statement I: No dog obeys all the commands.

• Statement II: ∀x: O(c, x) ↔ (O(a, x) ∨ O(d, x))

• Statement III: Every command is obeyed by at least one dog.

• Statement IV: ∃y: O(d, y) ∧ ¬O(a, y)

• Statement V: Ali obeys "sit" and "fetch" but not "roll over".

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

Question 4 uses a recursive function from strings to integers. Let Σ be the alphabet {a, b, c}. Define a function f from Σ* to Z recursively by the rules f(λ) = 0, f(wa) = 3f(w) - 1, f(wb) = 3f(w), and f(wc) = 3f(w) + 1.

Question 5 begins with the following λ-NFA N. The alphabet is {a, b}, the state set is {1, 2, 3}, the start state is 1, the final state set is {1}, and the transition relation Δ is

{(1, a, 2), (2, b, 3), (3, a, 1), (3, λ, 1)}.

Here is a diagram of N, with "L" meaning λ. Note that there are two parallel edges from 3 to 1. There is no state at the southeast corner of the diagram -- the edge from 2 to 3 turns a corner there.

a
>((1)) - - - - - > (2)
^                |
|                |
|                |
|a, L            |
|                | b
|                |
(3)< - - - - - - <

• 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) The input alphabet and the tape alphabet of a Turing machine

The input alphabet is the letters that can occur in a string input to the machine. The tape alphabet is the set of all letters that can occur on the machine's tape -- it includes the input alphabet and the blank symbol, plus possibly other letters.

• (b, 2) uniform-cost search and A* search

These are two methods of finding the shortest path from a start node s to a goal node g in a weighted graph. Each uses a priority queue of nodes -- in UCS the weight of a node x is the length of the best path found from s to x, while in A* the weight is the sum of this distance and an estimate of the distance from x to g (the heuristic).

• (c, 2) the multiplicative inverse and additive inverse of a number, modulo m

The multiplicate inverse of a number x, modulo m, is a number y such that xy is congruent to 1, modulo m. The additive inverse is a number z such that x + z is congruent to 0, modulo m.

• (d, 2) a partial order and a total order

A partial order is a binary relation that is reflexive, anti-symmetric, and transitive. A total order is a partial order that has the additional property of being total, meaning that for any elements x and y of the set, either R(x, y) or R(y, x) is true. (Thus in a total order there cannot be incomparable elements.) (Lots of people mixed up "total order" with the "total relation" that is part of the definition of a function.)

• (e, 2) the commutative and associative properties of a binary operation

A binary operation f is commutative if for any elements x and y, f(x, y) = f(y, x). It is associative if for any elements x, y, and z, f(f(x, y), z) = f(x, f(y, z)).

• 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 Black and White play a game with a finite game tree, and the game always ends with a victory by one player, then either Black or White must have a winning strategy.

TRUE. This is easily proved by induction on game trees.

• (b) The regular expressions (aba)*ba and ab(aba)* denote the same language.

FALSE. The string ba, for example, is in the language of the first expression but not that of the second.

• (c) If G is a finite directed graph and we either use a closed list or mark nodes that have been seen, then a depth-first search and a breadth first search of G with the same start node and the same goal node will return the same boolean answer.

TRUE. Each will return true if and only if there is a path from the start node to the goal node in the graph. Each is guaranteed to be correct on a finite graph, as with a closed list or marking of nodes they cannot get stuck in an infinite loop.

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

FALSE. On the step following this one, the machine will be in state q looking at another ☐, and we have no idea what δ(q, ☐) is. The machine could perfectly well halt in that case.

• (e) If X and Y are each Turing recognizable languages, then the languages X ∪ Y and X ∩ Y must each be Turing recognizable.

TRUE. If M and N are machines with X = L(M) and Y = L(N), we can recognize each of these languages with a machine that runs M and N in parallel. For X ∪ Y we accept if either M or N accepts, and for X ∩ Y we accept if both accept.

• (f) If an integer x satisfies the congruences x ≡ 13 (mod 15) and x ≡ 4 (mod 7), then x - 210 also satisfies those two congruences.

TRUE. Since 210 is a multiple of both 15 and 7, changing x by 210 does not change its remainder modulo either of these numbers.

• (g) If p is a prime number, then p! + 1 must also be a prime number. (Here p! is the factorial of p.)

FALSE. What we proved is that p! + 1 must be divisible by a prime greater than p. If p = 5, for example, p! + 1 is 121, which is not prime.

• (h) The compound proposition ((p ∨ q) → r) ∨ ¬(p → q) ∨ (q ∨ ¬p) is a tautology.

TRUE. In fact the second clause is the negation of the third, so one or the other must always be true.

• (i) Suppose that state p of an r.e.-NFA is not the start state and not the final state. The only transitions involving p are (q, ab, p), (p, ba*), and (p, ba, q). Then I may eliminate p and replace it with one new transition, (q, ab(ba*)*ba, q), merging this new transition with any existing transition from q to q.

TRUE. This is exactly the result of the state elimination algorithm in this case.

• (j) If every dog has another dog that likes it, then there must be a dog that likes every other dog.

FALSE. This implication is only valid in the other direction. For example, there might be three dogs A, B, and C, where A likes only B, B likes only C, and C likes only A.

• Question 3 (20): This question deals with five statements about a set of dogs D, consisting of the three dogs Ali (a), Cardie (c), Duncan (d), and perhaps others, and a set C of exactly four commands, consisting of "come here" (ch), "fetch" (f), "roll over" (r), and "sit" (s). The predicate O(x, y) means "dog x obeys command y".

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

• Statement I (to symbols): No dog obeys all the commands.

This is directly translated as "¬∃x:∀y:O(x, y)", or equivalently "∀x:∃y:¬O(x, y)", meaning "every dog has a command that it doesn't obey". Since the set of commands is known and finite, the quantifier over commands may be replaced by an AND in the first version or an OR in the second.

• Statement II (to English): ∀x: O(c, x) ↔ (O(a, x) ∨ O(d, x))

"For any command, Cardie obeys it if and only if either Ali or Duncan obeys it". Many people said "Cardie obeys all commands if and only if Ali or Duncan obeys all commands", which is wrong because there is a single universal quantifier whose scope is the entire statement.

• Statement III (to symbols): Every command is obeyed by at least one dog.

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

• Statement IV (to English): ∃y: O(d, y) ∧ ¬O(a, y)

There is a command that Duncan obeys and Ali does not obey.

• Statement V (to symbols) Ali obeys "sit" and "fetch" but not "roll over".

O(a, s) ∧ O(a, f) ∧ ¬O(a, r)

• (b, 5) Based on Statements I-V, which of the three dogs might obey "sit"? That is, of the eight possible sets of truth values for the three propositions O(a, s), O(c, s), and O(d, s), which are consistent with the statements?

We know that O(a, s) is true by separation on V. Specifying II to s, we get "O(c, s) ↔ (O(a, s) ∨ O(d, s)". Since the right-hand side of this is true because O(a, s) is true, O(c, s) must be true. None of the statements tell us whether O(d, s) is true, so the two possible settings of the three variables are (1, 1, 0) and (1, 1, 1).

• (c, 10) Assuming that Statements I-V are all true, prove that there exists a dog in the set D other than Ali, Cardie, and Duncan.

There are several approaches to this. I think the simplest is to let x be the command that Cardie does not obey (using specification on the second version of I) and then let y be the dog that obeys x (using specification on III). If this dog y were equal to a, c or d, then by II we would have that O(c, x) is true, but it is not. So y is the desired fourth dog.

Another approach is to assume that there are only three dogs and derive a contradiction. Many people used IV, though the desired result follows from I, II, and III alone. The command y referred to in IV might be either r or ch -- I took off three points for incorrectly claiming that it had to be r. Whichever it is, Cardie must obey it, and cannot obey the other because she already obeys s and f and can't obey all four. But then the other two dogs cannot obey the command that Cardie doesn't obey, and we have a contradiction.

• Question 4 (30+10): Let Σ be the alphabet {a, b, c}. Define a function f from Σ* to Z recursively by the rules f(λ) = 0, f(wa) = 3f(w) - 1, f(wb) = 3f(w), and f(wc) = 3f(w) + 1.

• (a, 10) Prove by induction that for every natural n, f(an) = -(3n - 1)/2 and f(cn) = (3n - 1)/2.

For the base case, where a0 and c0 are each λ, we must verify that f(λ) = 0 satisfies both the formulas. Substituting 0 for n, we find that -(30 - 1)/2 and (30 - 1)/2 are both 0 as they should be.

For the inductive case, we first assume that f(an) = -(3n - 1)/2 and prove that f(an+1) = -(3n+1 - 1)/2. Since an+1 = ana, we have that f(an+1) = 3f(an) - 1 = (by the IH) 3(-(3n - 1)/2) - 1 = -3n+1/2 + 3/2 - 1 = -(3n+1 - 1)/2.

Similarly, we assume that f(cn) = (3n - 1)/2 and calculate that f(cn+1) = f(cnc) = 3f(cn) + 1 = (by the IH) 3(3n - 1)/2) + 1 = 3n+1/2 - 3/2 + 1 = (3n+1 - 1)/2.

• (b, 10) Prove by induction that for any natural n and for and for any string w of length n, f(an) ≤ f(w) and f(w) ≤ f(cn). (Here an and cn are strings.)

For the base case, both a0 and c0 are equal to λ, which is the only string of length 0, so our claims both simplify to f(λ) ≤ f(λ), which is true. For the inductive case of the first claim, we assume that for any w of length n, f(an) ≤ f(w). Then let x be an arbitrary string of length n+1 and write x as wd for some letter d, so that w has length n. By the rule, f(x) is either f(wa), f(wb), or f(wc), all of which are at least 3f(w) - 1. Since f(an+1) = 3f(an) - 1 and f(an) ≤ f(w), we know that f(an+1) ≤ f(x). The other inductive case is similar, with f(x) = f(wd) ≤ f(wc) = 3f(w) + 1 ≤ (by the IH) 3(cn) + 1 = f(cn+1).

• (c, 10) Prove by induction on strings that for any string w, f(aw) = -3n + f(w), f(bw) = f(w), and f(cw) = 3n + f(w). (In each case n is the length of w.) (Hint: Your induction step should be to assume P(w) and prove P(wa), P(wb), and P(wc). This will involve talking about numbers like f(bwc), for example.)

We first prove P(λ), which says that f(a) = -30 + f(λ), f(b) = f(λ), and f(c) = 30 + f(λ). These claims follow from using the definition of f to calculate that f(a) = -1, f(b) = 0, and f(c) = 1.

The inductive step breaks down into nine cases. For each letter d and letter e taken from {a, b, c}, we need to show that f(dwe) has the proper value in relation to f(we), using the assumption that f(dw) is properly related to f(w). The nine arguments are very similar, and if you did one or more convincingly I was usually sympathetic if you left off the proofs of the others.

We must show that f(awa) = -3n+1 + f(wa), where the "n+1" is because the length of wa is n + 1. Here f(awa) is defined to be 3f(aw) - 1, which by the IH is 3(-3n + f(w)) - 1. This is -3n+1 + 3f(w) - 1, which is -3n+1 + f(wa).

Similarly f(awb) = 3f(aw) = (by IH) 3(-3n + f(w)) = -3n+1 + 3f(w) = -3n+1 + f(wb).

f(awc) = 3f(aw) + 1 = (by IH) 3(-3n + f(w)) + 1 = -3n+1 + 3f(w) + 1 = -3n+1 + f(wc).

f(bwa) = 3f(bw) - 1 = (by IH) 3f(w) - 1 = f(wa).

f(bwb) = 3f(bw) = (by IH) 3f(w) = f(wb).

f(bwc) = 3f(bw) + 1 = (by IH) 3f(w) + 1 = f(wc).

f(cwa) = 3f(cw) - 1 = (by IH) 3(3n + f(w)) - 1 = 3n+1 + 3f(w) - 1 = 3n+1 + f(wa).

f(cwb) = 3f(cw) = (by IH) 3(3n + f(w)) = 3n+1 + 3f(w) = 3n+1 + f(wb).

f(cwc) = 3f(cw) + 1 = (by IH) 3(3n + f(w)) + 1 = 3n+1 + 3f(w) + 1 = 3n+1 + f(wc).

• (d, 10XC) Prove by induction that for every natural n, f restricted to strings of length n is a one-to-one function. That is, if x and y are two different strings of length n, then f(x) ≠ f(y). (Hint: In the inductive step, break into two cases depending on whether x and y have the same first letter. If they do, use the inductive hypothesis and part (c). If they do not, use parts (a), (b), and (c).)

For the base case of n = 0, we need merely note that since λ is the only string of length 0, there cannot be two different strings of length 0 and our claim is true vacuously. We don't need to do the case of n = 1, but the claim is true there as well because f(a), f(b), and f(c) are -1, 0, and 1 respectively.

Now we assume that f is one-to-one on strings of length n, and consider two different strings of length n+1. If these two strings share the same first letter, for example ax and ay, we know by part (c) that f(ax) = f(ay) if and only if f(x) = f(y), and similarly for bx and by or cx and cy. The IH tells us that if x ≠ y, then f(x) ≠ f(y), which is what we need.

The other case is where the two strings have different first letters. Let's take ax and by as an example. Then by part (c), f(ax) = -3n + f(x), and f(by) = f(y). We need to show that these two numbers cannot be equal. By parts (a) and (b), we know that the absolute values of f(x) and f(y) cannot exceed (3n - 1)/2, so the sum of their absolute values cannot exceed 3n - 1. This means that the difference 3n + f(x) - f(y) cannot equal 0, and so f(ax) and f(by) cannot be equal. A similar argument works for f(ax) and f(cy), or f(bx) and f(cy).

A few people were close to this argument for the case of different first letters, but no one got it quite right.

• Question 5 (40): This question is the usual one about Kleene's Theorem constructions, starting with the λ-NFA N given above. The transition relation of that machine is {(1, a, 2), (2, b, 3), (3, a, 1), (3, λ, 1)}, its start state is 1, and its final state set is {1}.

• (a, 10) Use the construction from lecture and the text to find a λ-NFA whose language is (ab + aba)*. (N is such a λ-NFA, but I want the one from the construction.)

There are seven states which I will call {1, 2, 3, 4, 5, 6, 7}, with 1 the start state and 7 the only final state. The transitions are (1, λ, 2), (2, a, 3), (2, a, 4), (2, λ, 6), (3, b, 6), (4, b, 5), (5, a, 6), (6, λ 2), and (6, λ, 7). There are simpler λ-NFA's that have the correct language, including N itself, but for full credit you needed exactly this one.

• (b, 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 (a) -- it will be easier.)

The state set, start state, and final state set of N' are the same as for N. (Since the start state of N is already final, the construction gives no change to the final state set. Changing 3 to a final state did not change the language of the machine, but you were told to follow the construction so there was a small penalty for this.)

The set of λ-moves is already transitively closed as it contains only one move. Transition (1, a, 2) gives rise to itself and (3, a, 2). Transition (2, b, 3) gives rise to itself and (2, b, 1). Transition (3, a, 1) gives rise only to itself. So N' has five transitions in all.

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

The start state is {1}, which is final. We have δ({1}, a) = {2} (non-final) and δ({1}, b) = ∅ (non-final), and of course δ(∅, a) = δ(∅, b) = ∅. Then δ({2}, a) = ∅ and δ({2}, b) = {1, 3} (final). Then δ({1, 3}, a) = {1, 2} (final) and δ({1, 3}, b) = ∅. Finally δ({1, 2}, a) = {2} and δ({1, 2}, b) = {1, 3}. We have finished the process with only five of the possible eight states reachable.

• (d, 10) Find a minimal DFA with the same language as D. (Either run the state minimization algorithm or prove that your D is already minimal.)

The minimization algorithm shows in only one round that D is already minimal. We set N = {∅, {2}} and F = {{1}, {1, 2}, {1, 3}}. The behaviors of each state are NN for ∅, NF for {2}, NN for {1}, NF for {1, 2}, and FN for {1, 3}. (Here "NF", for example, means that the a-arrow is to a state in N and the b-arrow is to a state in F.) Since the two non-final states each have a different behavior, and the three final states each have a different behavior, the next partition of the algorithm will have each state in a separate class, indicating that the original DFA is minimal.

To prove directly that D is minimal, we must show strings separating each pair of non-final states and each pair of final states. There is only one pair of non-final states, ∅ and {2}, and these are separated by b. There are three pairs of final states: {1} and {1, 2} are separated by b, {1} and {1, 3} are separated by a, and {1, 2} and {1, 3} are separated by either a or b.