Q1: 20 points Q2: 35 points Q3: 30 points Q4: 15 points Total: 100 points
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.
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)-->(b)-->(c)-->(d)-->(e)-->(f)
^ / /
|\_____________/ /
|__________________________/
(a)
/ | \
/ | \
v v v
(b) (d) (f)
| / | /
| / | /
v v
(c) (e)
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;
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 = (1 2)
(1 0)
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.
A^2 = (3 2) A^4 = (11 10)
(1 2) ( 5 6)
Since we have already computed A4 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 is
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.
A^4 = (1 0)
(0 1)
Yes, it is. We could calculate this number of paths modulo 5 by finding the matrix A400 over the integers modulo 5, and looking at the (1,2) entry. But since A400 = (A4)100, and we saw above that A4 = I, the identity matrix over the integers modulo 5, we know that A400 = I100 = 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 10100.
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) ≤
2d+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 20+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) ≤ 2n+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) ≤ 2n+1 - 1, we
have that f(n+1) ≤ 1 + 2(2n+1 - 1) = 1 + 2n+2 - 2 =
2n+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) ≤ 2n.
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) ≤ 2n, we get that
R(n+1) ≤ 2(2n) = 2n+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 2i, and since this sum is exactly
2n+1 the desired fact is true.
Last modified 15 November 2007