CMPSCI 250: Introduction to Computation

Solutions to Second Midterm Exam

David Mix Barrington

31 March 2014

Directions:

Correction in orange added 23 March 2015.

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

Question text is in black, solutions in blue.

Corrections in green made 2 April 2015.

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


Last modified 2 April 2015