# Solutions to Second Midterm Exam Fall 2018

### Directions:

• Answer the problems on the exam pages.
• There are four problems, each with multiple parts, for 100 total points plus 10 extra credit. Actual scale was A = 90, C = 60.
• 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.

Exam text is in black, solutions in blue.

```  Q1: 10+10 points
Q2: 20 points
Q3: 40 points
Q4: 30 points
Total: 100+10 points
```

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

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

The Fibonacci function is recursively defined by the rules F(0) = 1, F(1) = 1, and (for all n > 0) F(n+1) = F(n) + F(n-1).

Let E(n) be recursively defined by the rules E(0) = 0, E(n+1) = 3E(n) + 6 for even n, and E(n+1) = 2E(n) + 3 for odd n.

Let V(n) be recursively defined by the rules V(0) = 1, V(n+1) = 3V(n) + 1 for odd n, and V(n+1) = 2V(n) + 1 for even n.

The set S of strings over the alphabet {a, b, c} is defined as follows:

• (1) λ is in S
• (2) if w is an even-length string in S and x is a letter, then wx is in S
• (3) if w is an odd-length string in S, x is a letter, and x ≠ last(w), then wx is in S
• (4) nothing else is in S

There are exactly ten strings in S with length ≤ 2: λ, a, b, c, ab, ac, ba, bc, ca, and cb.

For any natural n, we define an undirected graph Gn whose nodes are labeled by the strings in S of length ≤ n. If w is a string in S, x and y are letters, wx and wy are both in S, and |wx| = |wy| ≤ n, then there are edges (w, wx), (w, wy), and (wx, wy) in G.

Equivalently, we can define the graphs Gn inductively as follows.

• (1) G0 and G1 are as depicted below.
• (2) If n ≥ 0, Gn+2 is obtained by taking G2 below and replacing each of the six bottom nodes with a copy of Gn in which eveyr label is prefixed with the two-letter label of the replaced node.

``````
Undirected graphs G_0, G_1, and G_2, G_3. "L" means "lambda".
Numbers 1 or 2 within edges are edge weights for Question 3 (c) and (d).
In G_2, the edge (a, c, 1) is drawn crossing (b, ba, 2) and (b, bc, 2).

G_0   L     G_1     L          G_2                L
/|\                           /|\
/ | \                         / | \
2  2  2                       2  2  2
/   |   \                     /   |   \
/    |    \                   /    |    \
a--1--b--1--c                 a--1--b--1--c
\         /                 /|\   / \   /|\
\---1---/                 / | --/-1-\-- | \
2  2  2     2  2  2
/   |  |     |  |   \
/    |  |     |  |    \
ab-1--ac ba-1-bc ca--1-cb
``````

G2 is a graph with ten nodes and fifteen edges.

The alphabetical order on strings is defined as follows:

• If u is a proper prefix of v, it comes before v in the order.
• If u and v first differ at letter r, and the rth letter of u comes before the rth letter of v in the alphabet, then u comes before v in the order.

• Question 1 (10+10):

• (a, 10) Prove that for any natural n, the Fibonacci number F(n) is even if and only if n is divisible by 3. (Hint: Use strong induction.)

First base case: For n = 0, 0 is divisible by 3 and F(n) = 0 is even.

Second base case: For n = 1, 1 is not divisible by 3 and F(1) = 1 is odd.

Strong inductive case: Assume that for all k with k ≤ n, F(k) is even if and only if k is divisible by 3. We must show that F(n+1) is even if and only if n+1 is divisible by 3. If n+1 is divisible by 3, neither n nor n-1 is, and by the SIH, both F(n) and F(n-1) are odd. F(n+1) is their sum, so it is even as it should be. If n+1 is not divisble by 3, then one of n or n+1 is divisible by 3 and one is not. So by the SIH, one of F(n) and F(n-1) is odd and the other even, making their sum F(n+1) odd, as it should be.

Comments: I gave four points for the base case, but to get those points you needed the two base cases for strong induction, and you had to say exactly what facts about n = 0 and n = 1 you need for the base case. For the inductive case, you needed to deal both with the case where n+1 is divisble by 3 and the case (or two cases) where it is not. Many people correctly computed from the definition that F(n+3) = 3F(n) + 2F(n-1), so that F(n+3) is guaranteed to be even if F(n) is. This can be the inductive step of a proof that for every n divisible by 3, F(n) is even. But in order to solve this entire problem with this fact, you need to also note that F(n+3) is odd if F(n) is odd, so that P(n) → P(n+3) holds in both the odd and even cases. If you do this, you need three base cases for n = 0, 1, and 2, because the inductive case only starts being useful to prove P(3).

• (b, 10) State and prove a theorem that tells for exactly which values of n the Fibonacci nunber F(n) is divisible by 8.

The theorem is "∀n: F(n) is divisible by 8 if and only if n is divisible by 6:. I gave three points for a statement of this, but only two points if your statement was not an "if and only if" statement because you were asked "for exactly which values" the fact holds.

The appropriate base cases depend on how the induction works. There are two main approches:

First Proof: By repeated use of the Fibonacci definition, you can prove that F(n+6) = 8F(n+1) + 5F(n). If we know that F(n) is divisible by 8, this equation proves that F(n+6) is also divisible by 8. Thus if we prove a base case of n = 0, by observing that F(0) = 0 is divisible by 8, we can prove half the theorem by using induction on all n that are divisible by 6, to prove that they all have F(n) divisible by 8. But we can also use this same equation to prove that if F(n) is not divisible by 8, then neither is F(n+6). This is because F(n+6) is congruent modulo 8 to 5F(n), and 5 has an inverse modulo 8 as it is relative prime to it. Using this, we can carry out a proof of the entire theorem, using six separate base cases for n = 0 through n = 5 and observing the F(1), F(2),..., F(5) are not divisible by 8.

Second Proof: We can augment our statement P(n) to say exactly what F(n) % 8 is for every possible value of n % 12. We have to view n modulo 12 because the sequence of values of F(n) % 8 has a period of 12: it is 0, 1, 1, 2, 3, 5, 0, 5, 5, 2, 7, 1, 0, 1, 1, 2,... and so on. So our P(n) gives a conditional statement about F(n) with twelve cases: "F(n) % 8 is 0 if n % 12 = 0, is 1 if n % 12 = 1, is 1 if n % 12 = 2,..." and so forth. We can then do a strong induction for this new P(n), with just two base cases for n = 0 and n = 1 (more base cases don't hurt) and an inductive step that breaks into twelve cases. The first case would be "Suppose that (n + 1) % 12 = 0. Then n % 12 = 11 and (n - 1) % 12 = 10, so by the SIH we know that F(n) % 8 = 1 and F(n - 1) % 8 = 7. By the Fibonacci definition, F(n + 1) % 8 = 0 and P(n + 1) is true in this case." The other eleven cases are similar. (I didn't insist that you spelled them all out if you indicated how they would go.)

I gave six points for a complete proof of "(n % 6 = 0) → (F(n) % 8 = 0)".

• Question 2 (20): Recall that E(n) is defined so that E(0) = 0, E(n+1) = 3E(n) + 6 for even n, and E(n+1) = 2E(n) + 3 for odd n. Also V(n) is defined so that V(0) = 1, V(n+1) = 3V(n) + 1 for odd n, and V(n+1) = 2V(n) + 1 for even n.

• (a, 10) Prove that for any natural n, E(2n) = 3×6n - 3. (Hint: In your inductive step, use the definition to put E(n+2) in terms of E(n).)

For the base case, we must check that the claimed formula matches the definition for n = 0, which it does because 3×60 - 3 = 0. We only need one base case because this is an ordinary induction, but that base case must be 0 because we are asked to prove the statement "for any natural".

For the inductive step, we assume that E(2n) = 3×6n - 3, and use the formulas to evaluate E(2n+2). Since 2n+1 is odd, E(2n+2) = 2E(2n+1) + 3, and because 2n is even, E(2n+1) = 3E(2n) + 6, so E(2n+2) = 2(3E(2n) + 6) + 3 = 6E(2n) + 15. By the IH, this is 6(3(6n - 3) + 15 = 3×6n+1 - 18 + 15 = 3×6n+1 - 3 as desired.

I gave four points for a correct base case, but only one more if you attempted to use the formula to go directly from E(2n+2) to E(2n), because the formula only relates the E values for two consecutive integers and you have to use it twice to relate E(2n+2) to E(2n). The definition depends on the parity of the argument of E on the right-hand side, not that on the left-hand side -- look at what value fits into the role of "n" in the definition. (It doesn't matter whether n itself is even or odd.) As it turns out, in this case if you get the two formulas backward and compute 3(2E(2n) + 6) + 3, you also get 6E(2n) + 15 and can complete the induction, but if you did this I took off two points for the error in interpreting the formulas.

• (b, 10) Prove that for any natural n, V(2n) = (9×6n - 4)/5. (Hint: In your inductive step, use the definition to put V(2n+2) in terms of V(2n).)

This is very similar to part (a). For the base case, we must verify that the value of V(0) given by the claimed formula matches the definition, which it does because (9×60 - 4)/5 = 5/5 = 1 and V(0) is defined to be 1. For the inductive case, we assume that V(2n) is as claimed and compute V(2n+2) as 3V(2n+1) + 1 (because 2n+1 is odd) = 3(2V(2n) + 1) + 1 (because 2n is even) = 6V(2n) + 4. Using the IH, we get that V(2n+2) = 6((9×6n - 4)/5 + 4 = (9×6n+1 - 24)/5 + 20/5 = (9×6n+1 - 4)/5 as desired.

Again I gave four points for a correct base case and only one more for an argument that tried to use the formula to go directly from V(2n) to V(2n+2). This time if you got the two formulas backwards, you got V(2n+2) = 6V(2n) + 3 insteaad of 6V(2n) + 4, and the arithmetic should not work. I took off two points for an error plus an additional point if you fudged the arithmetic to make it work anyway.

• Question 3 (40): In the picture above, G2 has been made into a weighted graph by labeling each edge with 1 or 2. (The edge fron node u to node v, where u and v are strings in S, has weight 1 if u and v have the same length, and weight 2 otherwise.)

• (a, 10) Carry out a depth-first search of G2 with start node a and no goal node. Your search should recognize nodes that have previously come off the stack. When nodes enter the stack at the same time, they should come off in alphabetical order. Draw the resulting DFS tree, indicating tree and back edges.

• λ starts on the stack
• λ comes off, a, b, and c go on
• a comes off, ab, ac, b, and c go on
• ab comes off, ac goes on
• ac (the one from ab) comes off, nothing goes on
• ac (the one from a) comes off and is discarded
• b (the one from a) comes off, ba, bc, and c go on
• ba comes off, bc goes on
• bc (the one from ba) comes off, nothing goes on
• bc (the one from b) comes off and is discarded
• c (the one from b) comes off, ca and cb go on
• ca comes off, cb goes on
• cb (the one from ca) comes off, nothing goes on
• cb (the one from c) comes off and is discarded
• c (the one from a) comes off and is discarded
• b (the one from λ) comes off and is discarded
• c (the one from λ) comes off and is discarded
• the stack is empty and we are done

The tree has root λ with only child a. Node a has two children, ab and b. Node ab has only child ac. Node b has two children, ba and c. Node ba has only child bc. Node c has only child ca which has only child cb. These are the nine tree edges -- the other six edges (corresponding to the six discards in the search) are (λ, b), (λ, c), (a, ac), (a, c), (b, bc) and (c, cb). These are back edges in the tree since they are discovered from the descendent and go to the ancestor, as do all non-tree edges in a DFS tree of an undirected graph.

• (b, 10) Carry out a breadth-first search of G2 with start node a and no goal node. Your search should recognize nodes that have previously come off the queue. When nodes enter the queue at the same time, they should come off in alphabetical order. Draw the resulting BFS tree, indicating tree and non-tree edges.

To draw the tree, when placing a node on the queue, we'll indicate the neighbor from which it was visited; when first taken off the queue, that neighbor will be the parent in the tree. Taking a node off the second time results in a non-tree edge.

• Put a on the queue.
• Take off a, put on λa, aba, aca, ba, ca.
• Take off λa (tree edge). Put on bλ and cλ: (aba, aca, ba, ca, bλ, cλ)
• Take off aba (tree edge). Add acab.
• Take off aca (tree edge). Add nothing. (ba, ca, bλ, cλ, acab)
• Take off ba (tree edge). Add cb, bab, bcb: (ca, bλ, cλ, acab, cb, bab, bcb)
• Take off ca (tree edge). Add cac and cbc: (bλ, cλ, acab, cb, bab, bcb, cac, cbc).
• Take off bλ and cλ (non-tree edges). Add nothing. All children of a (nodes at depth 1) are visited now.
• Take off acab (non-tree edge). Add nothing.
• Take off cb (non-tree edge). Add nothing. Queue: (bab, bcb, cac, cbc)
• Take off bab (tree edge). Add bcba.
• Take off bcb (tree edge). Add nothing: (cac, cbc, bcba)
• Take off cac (tree edge). Add cbca.
• Take off cbc (tree edge). Add nothing: (bcba, cbca).
• Take off bcba and cbca (non-tree edges). Add nothing. Queue is empty, search is done.

The tree has five children of a at level 1, with four non-tree edges at that level: between ab and ac, and between λ, b and c. Node b has two children at level 2, ba and bc, with a non-tree edge between them, and c has two children, ca and cb, linked by a non-tree edge.

• (c, 10) Carry out a uniform-cost search of the weighted version of G2, with start node a and goal node bc. Assume that when the priority queue has more than one node tied for minimum priority, it returns the one that is first in alphabetical order.

• Add a0 to the queue
• Take off a0. Put on b1, c1, λ2, ab2, ac2.
• Take off b1. Put on c2, λ3, ba3, bc3. (c1, λ2, ab2, ac2, c2, λ3, ba3, bc3)
• Take off c1. Put on λ3, ca3, cb3. (c1, λ2, ab2, ac2, c2, λ3, λ3, ba3, bc3, ca3, cb3)
• Take off λ2. Add nothing.
• Take off ab2. Add ac3. (ac2, c2, λ3, λ3, ac3, ba3, bc3, ca3, cb3)
• Take off ac2. Add nothing.
• Take off and discard c2 and λ3 (twice).
• Take off and discard ac3. (ba3, bc3, ca3, cb3)
• Take off ba3. Add bc4. (bc3, ca3, cb3, bc4)
• Take off bc3 and declare victory, with cost 3.

• (d, 10) For any node w in G2, define the heuristic function h(w) to be the number of edges (ignoring edge costs) in the shortest path from w to bc. Using h as a heuristic, carry out an A* search of the weighted version of G2, with start node a and goal node bc. Assume that when the priority queue has more than one node tied for minimum priority, it returns the one that is first in alphabetical order.

We compute the heuristic function for all nodes in the graph. h = 0 for bc. h = 1 for b and ba. h = 2 for λ, a, c. h = 3 for the remaining nodes: ab, ac, ca, cb.

• Take off (a, 0+2). Add (b, 1+1), (c, 1+2), (λ, 2+2), (ab, 2+3), (ac, 2+3).
• Take off (b, 1+1). Add (bc, 3+0), (ba, 3+1), (c, 2+2), (λ, 3+2). Queue: [(bc, 3+0), (c, 1+2), (λ, 2+2), (ba, 3+1), (c, 2+2), (λ, 3+2), (ab, 2+3), (ac, 2+3)]
• Take off (bc, 3+0). Declare victory with cost 3.

• Question 4 (30): The following are fifteen true/false questions, with no explanation needed or wanted, no partial credit for wrong answers, and no penalty for guessing. Some of them refer to the scenarios of the other problems, and/or the entities defined above. Each one counts two points.

• (a) The following is not a valid recursive definition: Define f(0) = 0, and for n > 0, f(n) = 1 + f(n/2) if n is even, and f(n) = 3n + 1 if n is odd.

FALSE. The value of f for every odd number is defined directly, and for any even number the recursion will continue until an odd number is reached. For example, f(12) = 1 + f(6) = 2 + f(3) = 2 + 10 = 12. This is somewhat similar to the definition of the Collatz sequence, the subject of a famous unsolved problem which you may look up on your own if you are interested.

• (b) Define g(x, 0) = 1 and g(x, 2k) = (g(x, k))2 for k > 0, g(x, 2k + 1) = x(g(x, 2k))2 for k ≥ 0. Then for all naturals k, g(x, n) = xn.

TRUE. This is the repeated squaring algorithm for computing powers -- we could prove the claim by induction on n, letting x be arbitrary.

• (c) If P(0) is true, and for all n ≥ 1, (P(n) → P(n+1)) ∧ (P(n-1) → P(n-1)), then ∀n: P(n) must be true.

FALSE. P(1) does not follow from the given statements. We could let P(n) be "n ≠ 1" and the premises would all be true, but the conclusion false.

• (d) Define the relation G on naturals such that ∀x: G(S(x), 0) and ∀x: G(x, y) → G(S(x), S(y)), where S is the successor function. Then ∀x:∀y:∀z: G(x, y) → G(x+z, y+z).

TRUE. These rules define G to be the "greater than" predicate. We could prove the claim by induction on z, letting x and y be arbitrary and using only the second statement of the definition.

• (e) Let P be a property of strings over the alphabet {a, b, c}. If P(λ) is true, and P(w) → P(wx) is true for any string w and any letter x, then it is possible that P(w) is false for some w ∈ S.

FALSE. These premises are enough to prove P(w) for all strings w, not just those in S.

• (f) In the graph Gn, there exist two nodes x and y such that the shortest path from x to y has 2n edges.

FALSE. Every node has a path of at most n-1 edges to a node on the first level (a, b, or c) and these nodes all have edges to one another. So any two nodes have a path between them of at most (n-1) + (n-1) + 1 = 2n - 1 edges.

• (g) If u and v are both strings of odd length in S, then the strings uv and vu are both in S.

FALSE. If u and v are both "a", they are both in S but "aa" isn't.

• (h) If u and v are both strings of even length in S, then the strings uv and vu are also both in S.

TRUE. A string is in S if and only if it not of the form uaav, ubbv, or uccv where u is a string of even length. IF we concatenate two strings x and y each of even length, a double letter in in a bad position in x or y if and only if it is in a bad position in xy.

• (i) For any n > 1, there exists an undirected graph with n nodes and at least 2(n!) different simple cycles.

TRUE. Let G be the complete graph. Any of the n! orderings of the n nodes leads to two distinct simple paths that are not cycles, one with n-1 edges and one of n-2 edges. For example, with n = 4 we can assign the ordering a-b-c-d to the path a-b-c-d and the path a-b-c (leaving out d). Neither of these are cycles because they do not begin and end at the same point. We should be a bit careful of the n = 2 case: there the four (2(2!)) paths are a-b, b-a, a, and b. The latter two are simple paths (since they don't reuse a vertex) and are not cycles because a cycle in an undirected graph must have at least three edges.

• (j) An undirected graph with 6 nodes, 8 edges, and no isolated nodes must be connected.

TRUE. If it were not connected and had no isolated nodes, the connected components could be size 2 and 4, or size 3 and 3. (A graph with more than two connected components has fewer edges than one with two components, and we'll show these have too few edges.) A graph with 2 and 4 nodes in its components could have at most seven edges, and one with 3 and 3 could have at most six.

• (k) Consider a directed graph with five nodes, no self-loops, and an antisymmetric edge relation. It is possible for such a graph to contain two different strongly connected components, each with more than one node.

FALSE. If x is a node in a directed graph with an asymmetric edge relation, and its component has more than one node, it must have an edge to another node y. Since y cannot have an edge back to x, there must be a least one other node in the component. So a graph with two such components must have at least six nodes.

• (l) There is exactly one way to add a + operator to the string "2 + 3 * 4 5" to make it a valid prefix expression string.

TRUE. A prefix expression string must begin with an operator, so the only place to put the + is at the beginning.

• (m) For all n > 1, there exists an undirected graph G and a node v in G such that the BFS tree from v has depth 1 and the DFS tree from v has depth n - 1.

TRUE. For n = 2, let the graph have one edge -- both trees have depth 1 which is fine because n = 1 = 1. For n = 3, let the graph be a triangle, so the DFS has depth 2 and the BFS has depth 1. For n > 3, let the graph be a cycle of n - 1 nodes together with a node v that shares an edge with every node of the cycle. (This is called a wheel graph and has 2n - 2 edges.) The BFS tree of the wheel clearly has depth 1 because every other node is distance 1 from v and thus goes on the first level. The DFS will have depth n - 1 because each node we encounter will have an unseen neighbor, until we have seen them all. (The DFS tree consists of a single path.)

• (n) In the BFS tree of a directed graph, there can be an edge from a node at level t to a node at level t + 2.

FALSE. If this happened, there would be a directed path of length t + 1 from the start node to the second node (using the path to the first node and the alleged edge from the first to the second), but this node would be on level t + 2. The level in a BFS tree is the length of the shortest path from the start node.

• (o) In a two-player game represented by a finite game tree, changing the value at any leaf will change the value of the game.

FALSE. If we take any outcome that one player will avoid, and make it worse, that player will still avoid it and the optimal play of the game will be exactly the same.