# Second Midterm Exam

### Directions:

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

Correction in orange added 23 March 2015.

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

#### Definitions:

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

Question 2 uses two binary relations R and S, each subsets of N × N. They are defined by the following recursive pseudo-Java methods: R(x, y) is true (meaning (x, y) ∈ R) if and only if `r(x, y)` returns `true`, and similarly for S. The code for S was changed during the exam -- this is the corrected version.

``````

public static boolean r (natural x, natural y) {
if (x == 0) return true;
if (x == 1) {
if (y == 0) return false;
if (y == 1) return true;
return r(x, y - 2);}
if (y <= 1) return r(x - 2, y);
return r(x - 2, y - 2);}

public static boolean s (natural x, natural y) {
if (x % 2 == y % 2) return true;
if (x % 2 == 0) return true;
return false;}

``````

Questions 3 and 4 use the following recursively defined family of labeled directed graphs. Graph G0 consists of a single node A with no edges. To obtain graph Gn+1 from graph Gn, we add a new row of n + 2 nodes at the bottom, and add 3n + 3 edges, each labeled with the cost (weight) n + 1, to connect this new row to the bottom row of Gn. Here are ASCII pictures of the graphs G1, G2, and G3. There are three kinds of directed edges -- going down and to the left, going horizontally right, and going up and to the left.

``````
A
G1:                                       / ^
A                          G3:     1/   \
/ ^                                 /     \1
1/   \                               V   1   \
/     \1                            B ------> C
V   1   \                           / ^       / ^
B ------> C                        2/   \    2/   \
/     \2  /     \2
V   2   \ V   2   \
A                      D ------> E ------> F
G2:      / ^                    / ^       / ^       / ^
1/   \                 3/   \    3/   \    3/   \
/     \1               /     \3  /     \3  /     \3
V   1   \              V   3   \ V   3   \ V   3   \
B ------> C            G ------> H ------> I ------> J
/ ^       / ^
2/   \    2/   \
/     \2  /     \2
V   2   \ V   2   \
D ------> E ------> F

``````

• 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 connected undirected graph

• (b, 2) the depth of a rooted tree

• (c, 2) the Induction Rule for binary strings

• (d, 2) the prefix string for a boolean expression

• (e, 2) the reversal of a string

• Question 2 (40+10): Here R and S are the two binary relations on N defined by the pseudo-Java methods `r` and `s` above.

• (a, 5) Calculate the truth values of R(2, 3), R(2, 4), R(3, 4), and R(3, 2) from the code.

• (b, 5) Describe R in English, that is, say for exactly which naturals R(x, y) is true. For this part of the problem you do not need to justify your answer if you are correct.

• (c, 15) Prove that R is a reflexive relation, which means proving ∀n: R(n, n). Use induction on naturals, possibly strong induction or separate inductions on the odds and evens. Use only the code of `r`, not any relationship between the relations R and S.

• (d, 15) Prove that if two naturals x and y are both even, then R(x, y) ↔ S(x, y) is true. (Hint: Let P(x) be the statement "For all even y, R(x, y) ↔ S(x, y) is true". Prove P(x) by induction on the evens, using a separate induction on y in each case.)

• (e, 10XC) Complete the proof that R and S are actually the same relation, that is, that R(x, y) ↔ S(x, y) for all naturals x and y.

• Question 3 (15): This question uses the family of labeled directed graphs Gi defined above.

Let T(n) be the total length (cost, weight) of all the directed edges in the graph Gn. Using the recursive definition of Gn, prove (by ordinary induction on n) that T(n) = n(n + 1)(2n + 1)/2.

• Question 4 (35): These questions use the labeled directed graph G2, and the names of its nodes, given above.

• (a, 10) Trace the depth-first search of the directed graph G2 starting from node B, with no goal node and with a closed list to prevent re-exploring a node. Draw the resulting DFS tree, indicating the non-tree edges and identifying them as back, forward, or cross edges. When two nodes are put on the stack at the same time, the first one off should be the one with the lower-cost edge. (This is the only way we consider the edge labels in this part of the problem.)
• (b, 5) Describe what happens if we carry out the search from part (a) without using a closed list.
• (c, 10) Trace a uniform-cost search of the labeled directed graph G2, starting from node B and terminating when the shortest-path distance from B to node F has been determined by the algorithm.
• (d, 5) In order to carry out an A* of G2 with goal node F, we want to define a heuristic function h where h(x) is the minimum number of edges in any directed path from x to F. Why is this an admissible heuristic? Compute (without proof) the values of h(A), h(B), h(C), h(D), and h(E).
• (e, 5) Given an arbitrary directed graph (with positive integer edge labels) and an arbitrary goal node, we want to calculate the value of the function h(x) defined above, for every node x in the graph. What would be the best way to do this, using the search techniques we have learned?