# Solutions to Final Exam

### Directions:

• Answer the problems on the exam pages.
• There are five problems, some with multiple parts, for 120 total points plus 10 extra credit. Actual scale was A = 100, C = 65.
• 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: 30 points
Q3: 30 points
Q3: 20+10 points
Q4: 30 points
Total: 120+10 points
```

Exam text is in black, solutions in blue.

#### Definitions:

Question 2 refers to the following five statements, where the variables are of type "dog", the set of dogs includes the dog Duncan (d) and possibly others, and we use the following predicates:

• F(x) means "dog x is in the forest"

• I(x) means "dog x is illegal", ¬I(x) means "dog x is legal"

• L(x) means "dog x is on a leash"

• V(x) means "dog x is under voice control"

• Y(x) means "dog x is in the yard"

The statements are:

• Statement I: A dog in the forest is legal if and only it is either on a leash or under voice control, or both.

• Statement II: ∀x: ¬(Y(x) ∨ F(x)) → (L(x) ⊕ I(x))

• Statement III: Duncan is on a leash unless he is in the yard.

• Statement IV: ¬∃z: I(z) ∧ Y(z)

• Statement V: No dog on a leash is illegal.

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

Question 3 uses a function f from N to N. The function arises in a popular video game where tiles are labeled by integers that are powers of two. The smallest tiles are labeled 2, and they are created without any points being scored. (We will assume for this exam that only tiles with label 2 are created, though actually the game also creates some with label 4.) When two tiles each with label 2k are merged, a single tile with label 2k+1 is created and the player gets 2k+1 points.

For any positive natural k, f(k) is defined to be the number of points scored in the process of creating a tile with label 2k. If k > 1, this includes the 2k points for the last merge creating the tile, plus all the points scored in the process of creating the tiles that were merged.

Question 4 uses the following two formal languages over the alphabet {a, b}. The language X is the set of all strings that do not have both two a's in a row and two b's in a row. The language Y is defined inductively as follows. Note that the text in green was omitted when the test was given.

1. The string λ is in Y.

2. If the string w in Y can be written as uaa for some string u, then wa = uaaa is in Y.

3. If the string w in Y can be written as ubb for some string u, then wb = ubbb is in Y.

4. If the string w in Y cannot be written as either uaa or ubb, then both wa and wb are in Y.

5. The only strings in Y are those that are forced to be by rules (1)-(4).

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

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

Here is a diagram of N, with "L" meaning λ:

``````
a         a
>(1) ----> (3) ----> ((5))
|         ^         /
|         |        /
|         |       /
|         |      /
|a        |L    / a
|         |    /
|         |   /
|         |  /
|         | /
V    b    |V
(2) ----> (4)
<----
L
``````

• Question 1 (10): Identify each of the following five concepts, giving enough detail to make it clear that you are familiar with them (2 points each):

• (a, 2) a Turing recognizable language

A language X is TR if some Turing machine M exists such that X = L(M), that is, if on any string w, M halts on X if and only if w ∈ X.

• (b, 2) the shortest path from node x to node y in a weighted directed graph

The length of a path in a weighted graph is the sum of the weights of its edges. A shortest path from x to y is one whose length is smaller than or equal to the weight of any other path from x to y.

• (c, 2) a partial order on a set X

A partial order is a binary relation R ⊆ X × X that is reflexive (∀a: R(a, a)), antisymmetric (∀a:∀b: (R(a, b) ∧ R(b, a)) → a = b), and transitive (∀a:∀b:∀c: (R(a, b) ∧ R(b, c)) → R(a, c)).

• (d, 2) the transition function of a deterministic one-tape Turing machine

The transition function δ of a Turing machine has domain Q × Γ and range Q × Γ × {L, R}, where Q is the state set, Γ is the tape alphabet, and L and R indicate left and right moves on the tape, resspectively. If δ(p, a) = (q, b, R), for example, then if the machine is in state p and reading an a, it will change to state q, print a b over the a, and then move right.

• (e, 2) a pairwise relatively prime set of naturals

The set S is pairwise relatively prime if for any two distinct naturals x and y in the set, the GCD of x and y is 1. That is, no natural greater than one divides any two distinct naturals in the set.

• Question 2 (30): This question deals with five statements about a set of dogs that includes the named dog Duncan (d) and possibly others. The predicate F(x) means "dog x is in the forest", I(x) means "dog x is illegal", L(x) means "dog x is on a leash", V(x) means "dog x is under voice control", and Y(x) means "dog x is in the yard". Don't draw inferences from the English meanings -- for example, F(x) ∧ Y(x) might be true.

• (a, 5) Translate the five statements as indicated:

• Statement I (to symbols): A dog in the forest is legal if and only if it is either on a leash or under voice control, or both.

∀x: F(x) → [¬I(x) ↔ (L(x) ∨ V(x))]. Many of you had statements that incorrectly asserted things about dogs not in the forest.

• Statement II (to English): ∀x:¬(Y(x) ∨ F(x)) → (L(x) ⊕ I(x))

If a dog is in neither the forest nor the yard, then it is either on a leash or is illegal, but not both. Equivalently, such a dog is legal if and only if it is on a leash.

• Statement III (to symbols): Duncan is on a leash unless he is in the yard.

¬L(d) → Y(d), or equivalently L(D) ∨ Y(d), among several other formulations.

• Statement IV (to English): ¬∃z: I(z) ∧ Y(z)

There is no illegal dog in the yard. Equivalently, all dogs in the yard are legal.

• Statement V (to symbols): No dog on a leash is illegal.

∀x: L(x) → ¬I(x), or equivalently ¬∃x: L(x) ∧ I(x).

• (b, 5) For each of the statements I, II, and IV, give a propositional statement about Duncan that is implies by the statement. Which predicate proof rule did you use?

• (I): F(d) → [¬I(d) ↔ (L(d) ∨ V(d))]

• (II): ¬(Y(d) ∧ F(d)) → (L(d) ⊕ I(d)).

• (IV): Using the form ∀x: ¬(I(x) ∧ Y(x)), we derive ¬(I(d) ∧ Y(d)) or equivalently ¬I(d) ∨ ¬Y(d).

In each case we use the Rule of Specification, also called Universal Instantiation.

• (c, 10) Using Statements I-IV (but not Statement V!) prove by propositional logic that Duncan is legal. (Statement III is a propositional statement about Duncan, and you should use the three propositional statements about Duncan derived in part (b).) You may use a truth table or a propositional proof, and if you choose the latter you may use any format that makes your arguument clear.

We use Proof by Cases.

Case 1, Y(d): IV specifies to ¬I(d) ∨ ¬Y(d) as in (b) above. This is equivalent to Y(d) → ¬I(d), from which the premise gives us ¬I(d) by Modus Ponens.

Case 2, ¬Y(d) ∧ F(d): I specifies as in (b) above, and since F(d) is true Modus Ponens on that statement gives us ¬I(d) ↔ (L(d) ∨ V(d)). III says ¬Y(d) → L(d), which by Modus Ponens gives us L(d), since ¬Y(d) is assumed for this case. We get L(d) ∨ V(d) by Right Joining, and this is equivalent to ¬I(d) by a statement that we have derived.

Case 3, ¬Y(d) ∧ ¬F(d): II and Modus Ponens now give us L(d) ⊕ I(d). III, and the statement ¬Y(d) from the premise, give us L(d), from which ¬I(d) follows.

• (d, 10) Using Statements I-IV, prove Statement V. Make clear the predicate proof rules that you are using.

Let x be an arbitrary dog. Assume that L(x) is true, and we will prove ¬I(x).

Case 1, Y(x): IV specified to x gives us ¬I(x) ∨ ¬Y(x), and thus ¬I(x) follows from Y(x).

Case 2, ¬Y(x) ∧ F(x): I specified to x, F(x), and Modus Ponens together give ¬I(x) ↔ (L(x) ∨ V(x)). L(x) is assumed, so L(x) ∨ V(x) is true by Right Joining, and thus ¬I(x) is true.

Case 3, ¬Y(x) ∧ ¬F(x): II specified to x, the premise for the case, and Modus Ponens together give us L(x) ⊕ I(x). Since L(x) is assumed true, I(x) must be false.

Since we proved L(x) &arr; ¬I(x) in each case, we may conclude ∀x: L(x) ↔ ¬I(x) by Generalization (also called Universal Generalization).

• Question 3 (30): This question uses the function f from N to N, defined above.

• (a, 5) State a rule giving f(k+1) in terms of f(k), with a base case for f(1). Use this rule to compute f(1), f(2), f(3), f(4), and f(5).

We have f(1) = 0, since we are told no points accrue from the creation of a 21 = 2 tile. For a merger, the points to make a 2k+1 tile are 2k+1 plus the points to make two 2k tiles, so f(k+1) = 2k+1 + 2f(k). So f(2) = 4 + 2(0) = 4, f(3) = 8 + 2(4) = 16, f(4) = 16 + 2(16) = 48, and f(5) = 32 + 2(48) = 128.

• (b, 10) Prove by induction, on all naturals n such that n ≥ 3, that f(n) ≥ 2n+1.

The base case of n = 3 is given above, since 16 = 23+1. If we assume f(n) ≥ 2n+1, then f(n+1) = 2n+1 + 2f(n) ≥ 2n+1 + 2(2n+1) = 3(2n+1) ≥ 2n+2.

• (c, 15) Find a formula that describes f(n) for all positive naturals n. Prove your formula correct by induction. (You should use ordinary induction starting with n = 1.)

The formula is f(n) = (n-1)2n. For the base case of n = 1, the formula gives f(1) = (0)21, which is true because f(1) = 0 as above. Assume that f(n) = (n-1)2n. By the definition, f(n+1) = 2n+1 + 2f(n) = (by the IH) 2n+1 + 2(n-1)2n = n2n+1 by arithmetic.

• Question 4 (20+10): This question uses the definition of the languages X and Y above. The language X is the set of all strings that do not have both two a's in a row and two b's in a row. The language Y is defined inductively as follows (Note that the text in green was omitted when the test was given):

1. The string λ is in Y.

2. If the string w in Y can be written as uaa for some string u, then wa = uaaa is in Y.

3. If the string w in Y can be written as ubb for some string u, then wb = ubbb is in Y.

4. If the string w in Y cannot be written as either uaa or ubb, then both wa and wb are in Y.

5. The only strings in Y are those that are forced to be by rules (1)-(4).

• (a, 15) Prove Y ⊆ X. I think it is easiest to use induction on the definition of Y, but you might also use ordinary induction on the length of strings in Y.

Base case of induction on defintion of Y: λ ∈ X because λ has no aa or bb, much less both.

Assume that a string w in Y does not have both aa and bb. We must prove that none of the additional strings forced to be in Y have both an aa and a bb.

If w = uaa, then w must not have any bb. Adding an a cannot create a bb.

If w = ubb, then w must not have any aa. Adding a b cannot create an aa.

To solve the remaining case, we need an additional fact: If w has an aa, it ends in aa, and if w has a bb, it ends in bb. We prove this fact by induction: Both statements are true vacuously for λ, the added letters in clauses 2 and 3 of the definition preserve both properties, and if clause 4 applies then w cannot have either an aa or a bb, and so if the added letter creates an aa or a bb then the new string ends with that pair.

Now we can observe that in the last case, the string w has neither an aa nor a bb, and the new letter can create only one of them and the new string is still in X.

Note: Without the clauses in green above, the desired statement Y ⊆ X is false, because we can use the version of clause 4 with w = aab to show that aabb is in Y, and aabb is clearly not in X. If you pointed this out, you got full credit.

This is not true, since aab is certainly not in Y (since it can't be made from aa) and it is still in X because it has no bb.

• (c, 10XC) Find a set of pairwise Y-inequivalent strings that is as large as possible. (If there is any such set that is infinite, then any such infinite set is as large as possible.)

Using the Myhill-Nerode Theorem, a minimal DFA for Y will have one state for each string in such a set. Consider the DFA with state set {1, 2, 3, 4, 5, 6}, start state 1, final state set {1, 2, 3, 4, 5}, and transition function δ(1, a) = δ(3, a) = 2, δ(1, b) = δ(2, b) = 3, δ(2, a) = 4, δ(3, b) = 5, δ(4, a) = 4, δ(5, b) = 5, δ(4, b) = δ(5, a) = δ(6, a) = δ(6, b) = 6.

Each string in the set {λ, a, b, aa, bb, aab} goes to a different state of the DFA. Following the definition, it is easy to see that the language of this DFA is Y -- the first aa in a string must take it to 4, and the first bb in a string must take it to 5.

It remains to show that this DFA is minimal. On the first phase we have a set F = {1, 2, 3, 4, 5} containing all the final states. States 1, 2, and 3 go to F on either a or b, so we put them into a new set X. State 4 goes to F on a and to N on b, and state 5 goes to N on a and to F on b, so they each go into a set of size 1 and may be ignored. Analyzing X with respect to the new partition, we find that 1 goes to X on both a and b, that 2 goes to state 4 on a and to X on b, and that 3 goes to X on a and to sttae 5 on b. Each of the six states has a distinct behavior, and so the DFA is minimal.

• Question 5 (30): 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), (1, a, 3), (2, b, 4), (3, a, 5), (4, λ, 2), (4 λ, 3), (5, a, 4)}, its start state is 1, and its final state set is {5}.

• (a, 10) Using the construction given in lecture and in the text, find an ordinary NFA N' such that L(N') = L(N).

The set of two λ-moves is already transitively closed. We create our N' with the same five states, and 5 is still the only final state since there is no λ-path from 1 to 5. The letter moves (1, a, 2) and (1, a, 3) causes only themselves.

The move (2, b, 4) causes six moves in all, because 2 can be reached from 4 and 4 can go to 2 or 3 on λ. The six moves are (2, b, 2), (2, b, 3), (2, b, 4), (4, b, 2), (4, b, 3), and (4, b, 4).

The move (3, a, 5) causes itself and (4, a, 5).

The move (5, a, 4) causes itself, (5, a, 2), and (5, a 3).

Our N' thus has thirteen total letter moves.

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

Our nonfinal start state is {1}, which has an a-move to {2, 3} and a b-move to the nonfinal death state ∅, which of course has an a-move and a b-move to itself. State {2, 3} has an a-arrow to a final state {5} and a b-move to the nonfinal state {2, 3, 4}. State 5 has an a-move to {2, 3, 4} and a b-move to ∅. State {2, 3, 4} has an a-move to {5} and a b-move to itself. We have completed the construction, finding a DFA D with five states.

• (c, 10) Find a regular expression R such that L(R) = L(N). You may begin with any of the machines N, N', or D.

Starting from D, we need not add a start state but we must add a final state f, make {5} nonfinal, and add a transition (5, λ, f). We can delete the death state ∅, giving us five states to start.

Eliminating state {2, 3} creates two new transitions: ({1}, aa, {5}) and ({1}, ab, {2, 3, 4}).

Eliminating state {5} creates four new transitions: ({1}, aa, f), ({1}, aaa, {2, 3, 4}), ({2, 3, 4}, a, f), and ({2, 3, 4}, aa, {2, 3, 4}). Two of these combine with existing transitions to form ({1}, ab + aaa, {2, 3, 4}) and ({2, 3, 4}, b + aa, {2, 3, 4}).

Eliminating {2, 3, 4}, the last intermediate state, gives us a regular expression of

aa + (ab + aaa)(b + aa)*a