# Second Midterm Exam

### Directions:

• Answer the problems on the exam pages.
• There are three problems for 100 total points plus 10 extra credit. Actual scale is A = 93, C = 65.
• If you need extra space use the back of a page.
• No books, notes, calculators, or collaboration.
• In case of a numerical answer, an arithmetic expression like "32× 26 + 13 × 41" need not be reduced to a single integer.

Q1: 35 points
Q2: 25+10 points
Q3: 40 points
Total: 100+10 points

Here are several definitions used on the exam.

Remember that natural always means "non-negative integer".

Generalized Fibonacci Numbers:: If a and b are any two naturals, we define the function Ga,b by the rules (1) Ga,b(0) = a, (2) Ga,b(1) = b, and (3) (for n ≥ 1) Ga,b(n+1) = Ga,b(n) + Ga,b(n-1). Note that the ordinary Fibonacci numbers, as defined in Discussion #7, are defined by F(n) = G0,1(n).

A Function From Binary Strings to Integers: Let Σ = {0, 1}, so that Σ* is the set of all binary strings, including the empty string λ. We define the function f from Σ* to the integers, recursively by the rules (1) f(λ) = 0, (2) for any string w, f(w0) = 1 - f(w), and (3) for any string w, f(w1) = 1 + f(w). (Here "w0" is the string made by appending a 0 to w, and similarly for "w1".)

A Labelled Directed Graph: We will look at several searches of the following labelled directed graph G. Each edge goes in only one direction and is labelled by its cost.

1           2
[a] -----> [b] -----> [c]
^          |          |
| 1        | 1        | 3
|          |          |
|    5     V    4     V
[s] -----> [d] -----> [g]
|          |          ^
| 6        | 1        | 1
|          |          |
V    3     V    1     |
[e] -----> [f] -----> [j]

A Heuristic: For an A* search of G with goal node g, we will use the following heuristic. For any node x in g, the function h(x) will give the smallest number of edges on any path from x to g. Thus h(a) = h(e) = 3, h(b) = h(s) = h(f) = 2, h(c) = h(d) = h(j) = 1, and h(g) = 0.

• Question 1 (20): These questions all deal with the generalized Fibonacci numbers defined above.

• (a, 5) Compute the value of G4,2(5). (Hint: If you want to do some arithmetic, compute G4,2(10). If you don't get 246, your method is wrong. But only G4,2(5) is required for the answer.

• (b, 10) Let a and b be any two naturals. Prove that for any natural n, Ga,b(n) is an natural (that is, it is an integer and is not negative). (Hint: Use strong induction on n as in the Fibonacci proofs on Discussion #7.)

• (c, 10) Again letting a and b be arbitrary naturals, prove that for all n with n ≥ 1, Ga,b(n+1) ≥ Ga,b(n). (Hint: There are two ways to do this. You could either use strong induction starting with n = 1, or use a direct proof from the definition of Ga,b and the result of part (b).)

• (d, 10) Again for arbitrary naturals a and b, prove that if n is any natural with n ≥ 2, then Ga,b(n+3) ≤ 5Ga,b(n). (Hint: You don't need induction, but use the result of part (c).)

• Question 2 (25+10): These questions use the definition of the function f: Σ* → Z defined above. (Here Z is the integers -- it is conceivable that f(w) may be a negative integer.)

• (a, 5) Compute the integers f(0110) and f(01010) according to the recursive definition.

• (b, 10) Write a pseudo-Java method public static int f (string w) to compute f(w) for any pseudo-Java binary string w. Remember that unlike real Java String objects, pseudo-Java string values are primitives. Use the methods public static boolean isEmpty, public static char last, and public static string allButLast as given in the book. Each of these three methods takes a single argument of type string.

• (c, 10) Prove by induction on all binary strings w that f(0w) = f(1w). Here "0w" is the string obtained by prepending a 0 to w. Remember that the Law of Induction for binary strings says that to prove ∀w:P(w), you must prove P(λ), ∀w: P(w) → P(w0), and ∀w: P(w) → P(w1). Note that "P(w1)", for example, means "f(0w1) = f(1w1)".

• (d, 10XC) Prove that for any string w, if f(w) = 0, then the length of w cannot be odd.

• Question 3 (40): These four questions involve four searches of the directed labelled graph G from above, each with start node s and goal node g. Note that the edge labels are only used in parts (c) and (d). In every case, when two nodes are eligible to come off the open list at the same time, the one whose name comes first in the alphabet comes off first. Also note that in all four searches, we do not put a node onto the open list if it has already come off the list. (Whether a node can go on the open list, when another copy of it is already on the open list, depends on which search you are doing.)

• (a, 10) Describe the action of a depth-first search of G, with start node s and goal node g. Which nodes go on the open list, and what path from s to g is found by the search?
• (b, 10) Describe the action of a breadth-first search of G, with start node s and goal node g. Which nodes go on the open list, and what path from s to g is found by the search?
• (c, 10) Describe the action of a uniform-cost search of G, with start node s and goal node g. Which nodes go on the open list, and what path from s to g is found by the search?
• (d, 10) Consider an A* search of G with start node s and goal node g. You do not have to carry out the entire search if you can answer the following three questions: (1) Explain how h meets the conditions for an admissible heuristic, (2) What path from s to g is returned by the search, and (3) Why does the node e never come off the open list in the A* search, while it does come off of it in the uniform-cost search?