- Answer the problems on the exam pages.
- There are four problems for 100 total points. Approximate scale would be A=90, C = 60.
- If you need extra space use the back of a page.
- No books, notes, calculators, or collaboration.

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

**Question 1 (20):**Let G be the following undirected graph. The vertex set is {a,b,c,d,e,f} and the edge set is {(a,b), (a,d), (a,f), (b,c), (c,d), (d,e), (e,f)}. Draw depth-first and breadth-first search trees for G, including the non-tree edges in your picture and distinguishing them from the tree edges. (When adding vertices to the open list, add them in alphabetical order.)The DFS graph is a single branch, with a at the root with child b, grandchild c, followed by d, e, and f. The two non-tree edges are from f to a and from d to a, because these edges are discovered while processing f and d respectively, before they are seen from a.

(a)-->(b)-->(c)-->(d)-->(e)-->(f) ^ / / |\_____________/ / |__________________________/

The BFS graph has a as its root, has b, d, and f as children of the root on level 1, and c and e on level 2 -- c is a child of b and e is a child of d. There are two non-tree edges, from d to c and from f to e.

(a) / | \ / | \ v v v (b) (d) (f) | / | / | / | / v v (c) (e)

**Question 2 (35):**Recall that a rooted directed tree is defined to be either (a) a single node, a leaf, or (b) a root node with edges to the roots of one or more other rooted directed trees. Suppose we are given a rooted directed tree where each leaf is labeled with a natural number, and we want to find the maximum value of all these leaf values.- (a,15) Describe a recursive algorithm to find this maximum value, using
English or pseudo-Java or some combination. Your algorithm should follow the
definition of rooted directed trees, having a base case for leaves and using
recursion on subtrees if the given tree has subtrees.
We need a method that takes a subtree (in the form of its root node) and returns the maximum leaf value in the subtree. If the node in question is a leaf we return its value. Otherwise we recurse on each of the subtrees of this tree (there must be one or more if we are not at a leaf) and return the maximum of these values.

natural maxValue (node v) {//returns largest leaf value in subtree rooted at v if (v.isLeaf()) return v.value(); natural maxSoFar = 0; for (each child w of v) { natural thisMax = maxValue (w); if (thisMax > maxSoFar) maxSoFar = thisMax;} return maxSoFar;

- (b,20) Prove by induction on trees that your algorithm returns the
correct maximum value whenever the input is a tree. (Recall the induction
method for rooted directed trees -- if P(v) is true whenever v is a leaf, and
P(v) is true whenever P(w) is true for every child w of v, then P(v) is true
for all nodes in the tree.)
We let P(v) denote the predicate "maxValue returns the maximum leaf value in the subtree rooted at v". We must first prove P(v) when v is a leaf. In this case there is only one leaf in the subtree rooted at v, so its value is the maximum leaf value in the subtree. Since this leaf is v itself and we return v's value, we return the maximum value and P(v) is true.

Now we assume each of the calls to maxValue(w), for each child w of v, returns the maximum leaf value in the subtree under that w. Our method returns the maximum of all these values. Since each leaf in v's subtree must be in the subtree of one of the w's, the maximum leaf value in v's subtree is the largest of the maximum values of those subtrees. And this is exactly what we return. So P(v) is true given that P(w) is true for each child w. This completes the inductive case and thus completes the proof.

- (a,15) Describe a recursive algorithm to find this maximum value, using
English or pseudo-Java or some combination. Your algorithm should follow the
definition of rooted directed trees, having a base case for leaves and using
recursion on subtrees if the given tree has subtrees.
**Question 3 (30):**Consider the following directed multigraph G where the node set is {1,2} and the edges have names as follows: edge a goes from 1 to 1, edges b and c both go from 1 to 2, and edge d goes from 2 to 1.- (a,5) Give the natural-number matrix A
for G, where the (i,j) entry says how
many edges go from i to j.
A = (1 2) (1 0)

- (b,10) How many four-step paths go from vertex 1 to vertex 2? Calculate
this number using your matrix, and list the paths as sequences of four edges.
A^2 = (3 2) A^4 = (11 10) (1 2) ( 5 6)

There are ten four-step paths from 1 to 2 by the Path/Matrix Theorem. These paths are aaab, aaac, abdb, abdc, acdb, acdc, bdab, bdac, cdab, and cdac.

- (c,10) Suppose we view the matrix A as being over the integers modulo 5
rather than over the naturals. Find the matrix A
^{4}over this semiring and explain what each of its four entries mean.Since we have already computed A

^{4}over the naturals, we can compute it modulo 5 by taking each entry modulo 5. (We know from Chapter 3 that in taking sums and products, it doesn't matter whether we take results modulo m before or after the operations.) So our matrix isA^4 = (1 0) (0 1)

This means that the number of paths from 1 to 1, and the number of paths from 2 to 2, are each congruent to 1 modulo 5. Also, the number of paths from 1 to 2, and the number of paths from 2 to 1, are each congruent to 0 modulo 5, meaning that these two numbers are each divisible by 5.

- (d,5) Is the number of 400-step paths from 1 to 2 in G divisible by 5?
Explain your answer.
Yes, it is. We could calculate this number of paths modulo 5 by finding the matrix A

^{400}over the integers modulo 5, and looking at the (1,2) entry. But since A^{400}= (A^{4})^{100}, and we saw above that A^{4}= I, the identity matrix over the integers modulo 5, we know that A^{400}= I^{100}= I, and the (1,2) entry of this matrix is 0. Note that the actual number of such 400-step paths is really large, bigger than 10^{100}.

- (a,5) Give the natural-number matrix A
for G, where the (i,j) entry says how
many edges go from i to j.
**Question 4 (15):**Suppose I have a directed graph G where each node has at most two edges out of it and let s be a particular node of G. Prove that for any natural d, the number of nodes that are reachable from s by paths of length at most d is at most 2^{d+1}-1. (Hint: Use ordinary induction on d.)This question turned out to be a little trickier than I intended, but it's quite doable. There are two ways to do it -- let's look at the more straightforward one first. We let f(n) be the number of nodes reachable by paths of length at most n, and let P(n) be the proposition "f(n) ≤ 2

^{d+1}- 1". We first prove P(0), which says that the number of nodes reachable by paths of length at most 0 is at most 2^{0+1}- 1 = 1. This is true because s, and only s, is reachable from s by a path of length 0. Now we assume P(n), which says that f(n) ≤ 2^{n+1}- 1. Then we have to look at the nodes reachable by paths of length n+1. These consist of s itself, plus*at most two*nodes for each node reachable by a path of length n. (This is because each path of length greater than zero consists of a path of length one shorter, followed by an edge.) This means that f(n+1) is at most 1 + 2f(n). Using the IH that f(n) ≤ 2^{n+1}- 1, we have that f(n+1) ≤ 1 + 2(2^{n+1}- 1) = 1 + 2^{n+2}- 2 = 2^{n+2}- 1. But this is exactly what P(n+1) says, so we have proved P(n+1) using the inductive hypothesis of P(n), and completed the induction.Another way to prove this fact uses an auxiliary definition. We let R(n) be the number of nodes reachable by paths of length

*exactly*n. Then we prove by induction that for any natural n, R(n) ≤ 2^{n}. The base case of this induction says that R(0) ≤ 1, which is true because s is the only node reachable by a path of length 0. Then each path of length exactly n can lead to at most two paths of length n+1, so R(n+1) ≤ 2R(n). Using the IH that R(n) ≤ 2^{n}, we get that R(n+1) ≤ 2(2^{n}) = 2^{n+1}.But we aren't done -- we have a bound for R(n), not f(n). But f(n) is at most the sum, for i from 0 through n, of R(i), because any node reachable by a path of at most n must be reachable by a path of length i for some natural i with i ≤ n. The sum of these R(i)'s, by what we just proved, is at most the sum for i from 0 through n of 2

^{i}, and since this sum is exactly 2^{n+1}the desired fact is true.Last modified 15 November 2007