# Solutions to Second Midterm Exam Spring 2020

### 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 A = 95, 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.

Question text in black, solutions in blue.

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

• Question 1 (15): Define the function G from naturals to naturals recursively, by the rules G(0) = 0, G(1) = 1, and, for all n with n ≥ 1, G(n+1) = 5G(n) - 6G(n-1). Prove, by strong induction, that for all naturals n, G(n) = 3n - 2n.

Base cases: G(0) is defined to be 0, and 30 - 20 = 1 - 1 = 0. G(1) is defined to be 1, and 31 - 21 = 3 - 2 = 1.

Assume as strong inductive hypothesis that G(i) = 3i - 2i for all i with i ≤ n. Our inductive goal is to prove that G(n+1) = 3n+1 - 2n+1.

We know that G(n+1) = 5G(n) - 6G(n-1). Applying the inductive hypothesis, we get G(n+1) = 5(3n - 2n) - 6(3n-1 - 2n-1) =

3n-1(5(3) - 6) + 2n-1(-5(2) + 6) = 3n-1(9) + 2n-1(-4) = 3n+1 - 2n+1.

• Question 2 (20+10): These questions deal with a language L over the alphabet {a, b, c, d}, and a related set T of rooted trees with leaves labeled by letters from that alphabet. Both are defined recursively. The rules for L are:

1. b is in L.
2. If w is a string in L, then awc is in L.
3. If u and v are both strings in L, then udv is in L.
4. The only strings in L are those made using the first three rules.

The rules for T are:

1. A single node labeled by b is in T.
2. If t is a tree in T, then the tree that has a root with three children: a leaf labeled a, t, and a leaf labeled c, is in T.
3. If t and u are trees in T, then the three that has a root with three children: t, a leaf labeled d, and u, is in T.
4. The only trees in T are those made using the first three rules.

• (a, 5) Prove by induction on all naturals n that the string anbcn is in L. (For example, if n = 3 this string is aaabccc.)

Base case: n = 0, string is “b”, string is in L by the first rule.

Inductive hypothesis: anbcn is in L.

Inductive goal: an+1bcn+1 is in L.

Inductive step: By the second rule, since anbcn is in L, a(anbcn)c = an+1bcn+1 is in L.

• (b, 5) For any positive natural n, define wn to be the string of length 2n - 1 that starts with b and alternates b's and d's. (For example, if n = 3 this string is bdbdbdb.) Prove, by induction on all positive naturals n, that wn is in L.

Base case: n = 1 (since we are proving this for positive naturals), wi = “b”, and b is in L by the first rule.

Inductive hypothesis: wn is in L.

Inductive goal: wn+1 is in L.

Inductive step: In the third rule, we let u and v both be wn, and derive that wndwn is in L.

But wndwn is exactly the same string as wn+1, because its length is 2(2n - 1) + 1 = 2n+1 - 1, and it alternates between b’s and d’s, since the new d goes between the last b of the first wn and the first b of the second wn.

• (c, 10) Prove, by induction on the definition of L, that every string in L has odd length.

Base case: If a string is made by the first rule, it is “b” and its length is 1, which is odd.

Inductive hypothesis: All shorter strings in L have odd length.

Inductive goal: A new string made by the second or third rules has odd length.

Inductive step: If w is a string of odd length and we make awc, we make a string of length 2 greater than w’s length, which is also odd. If u and v are both of odd length and we make udv, we make a string whose length is the sum of two odd numbers, plus one, and thus is an odd number.

• (d, 10XC) Prove, by induction on both definitions in the question, that a string is in L if and only if it is the string formed by reading the leaves of a tree in T from left to right.

We first prove that every string in L is the leaf string of a tree in T. For the base case, b is the leaf string of the base case tree of T. If w is a string in L, w is the leaf string of t, and we make awc, then awc is the leaf string of the tree made from t by the second rule. If u and v are strings in L, and are the leaf strings of t and u respectively, then udv is the leaf string of the tree made from u and v by the third rule.

Now we prove that every tree in T has a leaf string in L. For the base case, the leaf string of the base tree is “b”, which is in L. If t is a tree in T, and we make a tree from t by the second rule, then because the leaf string of T (call it w) is in L we know that awc is also in L. If t and u are trees in T, with leaf strings x and y respectively, and we know that x and y are in L, then if we make a tree from u and v by the third rule, its leaf string is xdy, which is in L by the third rule for L.

• Question 3 (35): These questions involve the following weighted undirected graph. The numbers in the middle of the edges give edge weights. The numbers in parentheses at each node are weights for the A* search in part (e).

``````
q(7)
/   \
1     1
/         \
r(6)        s(8)
|           |
1           3
|           |
t(5)        u(6)
\        /  \
2      3    2
\   /        \
v(3)        w(5)
|            |
2            3
|            |
x(1)         y(2)
\       /
2     2
\  /
z(0)

``````

• (a, 5) Carry out a depth-first search of G with start node s and no goal node. Indicate the stack contents at each step. If two nodes enter the stack at the same time, they should come off in alphabetical order. Draw the DFS tree, including non-tree edges.

• s starts on the stack
• s comes off, q and u go on
• q comes off, (s, q) is tree edge, r goes on
• r comes off, (q, r) is tree edge, t goes on
• t comes off, (r, t) is tree edge, v goes on
• v comes off, (t, v) is tree edge, u and x go on
• u comes off, (v, u) is tree edge, w goes on, (u, s) is discovered as a back edge
• w comes off, (u, w) is tree edge, y goes on
• y comes off, (w, y) is tree edge, z goes on
• z comes off, (y, z) is tree edge, x goes on x comes off, (z, x) is tree edge, (x, v) is discovered as a back edge, nothing goes on
• stack is empty

The tree is a single chain s-q-r-t-v-u-w-y-z-x with back edges from u to s and from x to v.

• (b, 5) Carry out a breadth-first search of G with start node s and no goal node. Indicate the queue contents at each step. If two nodes enter the queue at the same time, they should come off in alphabetical order. Draw the BFS tree, including non-tree edges.

• s starts on the queue
• s comes off, q and u go on
• q comes off, (s, q) is tree edge, r goes on
• u comes off, (s, u) is tree edge, v and w go on
• r comes off, (q, r) is tree edge, t goes on
• v comes off, (u, v) is tree edge, t and x go on
• w comes off, (u, w) is tree edge, y goes on
• t (from r) comes off, (r, t) is tree edge, nothing goes on
• t (from v) comes off, (v, t) is non-tree edge, nothing goes on
• x comes off, (v, x) is tree edge, z goes on
• y comes off, (w, y) is tree edge, z goes on
• z (from x) comes off, (x, z) is tree edge, nothing goes on
• z (from y) comes off, (y, z) is non-tree edge, nothing goes on
• queue is empty

Level 0 of the BFS tree has just s.

Level 1 has q and u, each with tree edges from s.

Level 2 has r with tree edge from q, v with tree edge from u, and w with tree edge from u.

Level 3 has t with tree edge from r and non-tree edge from v, x with tree edge from v, and y with tree edge from w.

Level 4 has just z, with tree edge from x and non-tree edge from y.

• (c, 5) Prove that if we conduct either a DFS or a BFS of G, with any choice of start node and no goal node, then the resulting tree will have exactly two non-tree edges.

G has ten nodes and eleven edges. Wherever you start either kind of search, the tree edges will form a tree that includes all ten nodes. Such a tree has nine edges by a theorem proved in lecture, so there will be exactly two edges left to be non-tree edges.

• (d, 10) Carry out a uniform-cost search of G with start node s and goal node z, using the given edge weights. Indicate which nodes are on the priority queue at each stage of the search.

• (s, 0) goes on: (s, 0)
• (s, 0) comes off, (q, 1) and (u, 3) go on: (q, 1), (u, 3)
• (q, 1) comes off, (r, 2) goes on: (r, 2), (u, 3)
• (r, 2) comes off, (t, 3) goes on: (t, 3), (u, 3) [Either could come off now, we’ll break ties alphabetically]
• (t, 3) comes off, (v, 5) goes on: (u, 3), (v, 5)
• (u, 3) comes off, (w, 5) goes on: (v, 5), (w, 5)
• (v, 5) comes off, (x, 7) goes on: (w, 5), (x, 7)
• (w, 5) comes off, (y, 8) goes on: (x, 7), (y, 8)
• (x, 7) comes off, (z, 9) goes on: (y, 8), (z, 9)
• (y, 8) comes off, (z, 10) goes on, (z, 9), (z, 10)
• (z, 9) comes off, search ends with (z, 10) still in the PQ
• (e, 10) Conduct a complete A* search of G with start node s and goal node z, using the given edge weights and the given values for the heuristic function h. Indicate which nodes are on the priority queue at each stage of the search.

• (s, 0+8) goes on: (s, 8)
• (s, 8) comes off, (q, 1+7) and (u, 3+6) go on: (q, 8), (u, 9)
• (q, 8) comes off, (r, 2+6) goes on: (r, 8), (u, 9)
• (r, 8) comes off, (t, 3+5) goes on: (t, 8), (u, 9)
• (t, 8) comes off, (v, 5+3) goes on: (v, 8), (u, 9)
• (v, 8) comes off, (u, 8+6) and (x, 7+1) go on: (x, 8), (u, 9), (u, 14)
• (x, 8) comes off, (z, 9+0) goes on: (u, 9), (z, 9), (u, 14)
• (u, 9) comes off, (w, 5+5) goes on: (z, 9), (w, 10), (u, 14)
• (z, 9) comes off, search ends with path of length 9, w(10) and (u, 14) remain on the queue. Note that the search never looked at w or y.

• 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.

• (a) Any undirected graph with no cycles is a tree.

FALSE. It might not be connected.

• (b) Depth-first search is the same as uniform-cost search with costs of 1 on every edge.

FALSE. BFS is the same as UCS with unit edge weights.

• (c) Let g(s) and h(s) be two admissible heuristic functions for an A* search of the same graph with the same target node. Then the average of the two, (1/2)(g(s) + h(s)), is also an admissible heuristic for that graph with that target node.

TRUE. If both g(s) and h(s) are in the range from 0 to d(s, g), then so is their average.

• (d) The path relation on a directed graph may fail to be transitive.

FALSE. We proved that it is transitive.

• (e) If a rooted tree has exactly one node, then its depth is 1.

FALSE. The depth is the length of the longest path from the root to a leaf, which in this case is 0.

• (f) There exists a rooted binary tree T, with a positive even number of nodes, such that every internal node of T has exactly two children.

FALSE. The single node tree has an odd number of nodes, and adding exactly two children to any leaf keeps the number odd, so the number is always odd.

• (g) Let G be a weighted directed graph with nodes s and t. Let C be a positive constant. It is possible that if we add C to the cost of evere edge in G, then we will change the path from s to t found by a uniform-cost search.

TRUE. A simple example has three nodes and edges (s, x, 1), (s, t, 3), and (x, t, 1). The optimal path is two steps, but if we add 10 to every edge cost the one-edge path becomes optimal.

• (h) 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. Black might also have a better strategy that assures him more than 3.

• (i) Let P(n) be a predicate on naturals. If P(0) and P(1) are both true, and ∀n: P(n) → (P(2n) ∧ P(2n+1)) is true, then ∀n: P(n) must be true.

TRUE. We have enough to prove ∀n:P(n) by strong induction.

• (j) Let G be a two-player deterministic game with perfect information, defined by a finite game tree with real-number values at its leaves. It is possible that G has value x, but that none of the leaves of the tree has value x.

FALSE. If both players use their optimal strategies, the result will be the value of the game. But in a finite game the result must be the payoff at some leaf node.

• (k) Let Q(w) be a predicate on binary strings. It is possible that Q(λ) and ∀w: Q(w) → (Q(w0) ∧ Q(w1)) are both true, but that there exists a string v such that Q(v) is false.

FALSE. These premises are enough to prove ∀w:Q(w).

• (l) Let G be a directed graph and let s and t be nodes of G. If we do both a depth-first search and an A* of G with start node s and goal node t, then the A* search is guaranteed to expand no more nodes than the DFS.

FALSE. The DFS could happen upon the shortest path immediately, while if the heuristic is zero and we have unit weights, A* becomes BFS, which will examine all paths from the start that are shorter than the shortest one to the goal.

• (m) Let G be a strongly connected directed graph, and suppose we conduct a DFS of G from start node s. If we remove all the back edges from the resulting DFS tree, the remaining edges form an acyclic graph.

TRUE. Tree and forward edges only go from ancestors to descendants. And no sequence of cross edges can get from a descendant to an ancestor.

• (n) Let (u, v) be a non-tree edge of the BFS tree of a strongly connected directed graph. If u is at level 5 of the tree, then it is possible that v is at level 2 or at level 6, but not possible that v is at level 7.

TRUE. The edge can go to the following level, the same level, or any previous level, but if v were not yet discovered it would be at the next level after u’s.

• (o) The set of integers {0,1,2,3,4,5,6}, with successor, addition, and multiplication all defined modulo 7, does not model the Peano axioms.

TRUE. Zero is the successor of a number (6), which the Peano Axioms forbid.