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

```  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.)

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

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

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

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

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

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

• (d, 10) For any node w in G2, define the heuristic function h(w) to be the number of edges (ignoring edge weights) 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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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