Third Midterm Exam

20 November 2007

The scale was A = 90, C = 54.

```  Q1: 10 points
Q2: 15 points
Q3: 20 points
Q4: 15 points
Q5: 15 points
Q6: 25 points
Total: 100 points
```

• Question 1 (10): Recall that a simple path in a directed graph is one that never visits any node more than once. Prove that for any natural n, any simple path with n edges visits exactly n+1 nodes. (Hint: Use ordinary induction.)

• Question 2 (15): Recall that a rooted directed tree is defined to be either (1) a single node which is both a leaf and the root, or (2) a root node with edges to the roots of each of one or more subtrees. Describe (in English or pseudo-Java) a recursive algorithm that takes a node of a rooted directed tree as inpt and returns the number of leaves in the subtree under that node. You may use any reasonable syntax for looking at all the children of a node: one way to do it is to assum that ou have methods `node firstChild()`, `node nextChild()`, and `boolean isAnotherChild()` in the `Node` class.

Questions 3 and 4 deal with a particular kind of rooted directed trees called foo-trees. They are defined recursively:

• (i) A single leaf is a foo-tree.
• (ii) If the root of a tree has one or more children, the tree is a foo-tree if and only if the last subtree is a foo-tree and all the other subtrees consist of single leaves.
• (iii) These are the only foo-trees, so that if a predicate P(v) is true whenever v is a leaf, and true whenever v is the root of a tree as described in (ii) with P(w) true for v's last child w, then P(v) is true whenever v is the root of a foo-tree.

Here is an example of a foo-tree with eight nodes:

```         (a)
/ | \
/  |  \
/   |   \
/    |    \
/     |     \
/      |      \
(b)      (c)      (d)
/ | \
/  |  \
/   |   \
/    |    \
/     |     \
/      |      \
(e)      (f)      (g)
|
|
|
|
|
|
(h)
```

• Question 3 (20): Prove by induction on all foo-trees T that the breadth-first search of T and the depth-first search of T visit the nodes of T in exactly the same order. (You should assume that the children of each node of T come in an order that is specified as part of the definition of T.)

• Question 4 (15): Prove that if the breadth-first search of T and the depth-first search of T visit the nodes of T in exactly the same order, then T must be a foo-tree. (Hint: Any non-foo tree must have a node where condition (ii) fails to be true. Thus the non-foo-trees can be defined recursively as well -- a tree is non-foo if and only if the root (a) has a child that is not a leaf and is not last, or (b) has a last child that is the root of a non-foo tree. Using this definition, you can prove by induction on non-foo-trees that each of them has nodes that are visited in a different order by the BFS and DFS.)

Questions 5 and 6 deal with the following labeled undirected graph G. The node set is {a,b,c,d} and there are four edges: (a,c) with weight 4, (a,d) with weight 1, (b,c) with weight 1, and (b,d) with weight 1.

• Question 5 (15): Write down the single-step distance matrix A for this graph G. Use min-plus matrix multiplication (Floyd's Algorithm) to compute the shortest-path distance matrix for G. (Hint: Because G is undirected, the matrix A and all of its powers are symmetric -- the (i,j) entry is always equal to the (j,i) entry. You can use this fact to simplify your calculation.)

• Question 6 (25):
• (a,15) Describe in detail the results of a uniform-cost search of G starting from node a. Draw the resulting search tree and indicate the shortest paths from a to each other node.
• (b,10) Carry out a breadth-first search of G from vertex c, ignoring the weights of edges. Let h(v), for each node v, be the level of v in the tree of this BFS. Indicate how this function h could be used in an A* search to find the shortest path from a to c. (It's important that the weight of each edge is at least 1.)