CMPSCI 250: Introduction to Computation

Solutions to Third Midterm Exam

David Mix Barrington

20 November 2007

Question text is in black, solutions in blue. 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 

In English, we return 1 in the case of a single node, since it is a leaf. If the root node has children, we recursively compute the number of leaves under each child and add these numbers together (since a leaf under the root must be under exactly one of the root's children).

In pseudo-Java:


          natural numLeaves (node v)
          {// returns number of leaves in subtree under v
             if (v.isLeaf()) return 1;
             node w = v.firstChild();
             natural temp = numLeaves(w);
             while (v.isAnotherChild())
                temp += numLeaves(v.nextChild());
             return temp;}

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

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

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

We prove the contrapositive of the given statement -- that if a rooted directed tree T is not a foo-tree, then the BFS and DFS of T do not visit the nodes of T in the same order. We do this by induction on all non-foo trees T. The base case is when T's root has a child v that is not last and not a leaf. In this case the BFS visits v's next sibling right after v, while the DFS visits v's first child after v, so the order of the two searches is different.

The recursive case is when the last child of T is a non-foo tree U. Here the IH tells us that the BFS and DFS of U visit the nodes of U in different order, and so the BFS and DFS of T, since they simulate this search, must also visit nodes in different order.

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.

Last modified 26 November 2007