Homework problems B and C

Problem B

Assigned Sept 11, modified Sept 17, due Sept 25.

This problem is based on the heaps discussion in CLR section 6.1

Let A be a min-heap of size n > 4 made from a set of n distinct values. For this problem we'll discuss positions indexed starting at 1, so the range of indices is [1..n]. Position 1 holds the root and the children of node i (if any) are at at positions 2i and 2i + 1. The rank r value in a set of n distinct values is the value that would be in position r if the data were sorted, so r-1 values are smaller and n-r are larger.

  1. (3 points) Which positions might possibly be occupied by the third smallest (rank 3) element? Explain.

  2. (3 points) Which positions might possibly be occupied by the largest (rank n) element? Explain.

  3. (3 points) Which positions might possibly be occupied by the second largest (rank n-1) element? Explain.

  4. (1 point) Do any one part.
    1. Prove for odd n that the middle rank value (the median value, the ceiling(n/2) rank value) may appear in any position beyond the second position.
    2. Prove for even n that the rank n/2 value (the lower median value) may appear in any position beyond the second position.

    3. Characterize those n for which the median value (the element of rank ceiling(n/2)) may appear in the second position.
The previous part d was false, making it a little hard to prove! "Prove that the middle rank value (the median value in the data set) may appear in any position except the first."

Problem C

Assigned Sept 11, due Sept 25.

This problem is based on recurrences chapter 4, specifically on problem 4.3a.

A recursive binary search:

template<class Ptr>
Ptr binsearch(Ptr p, Ptr b, Ptr e)
/* 
p points to desired item. 
b and e define the sorted range in which to search.
Return ptr to occurrence of *p in sorted array *[b..e).
Return e if no such value exists.
We assume standard comparison operators can be used on the 
datatype pointed to by Ptr instances.
*/
{ if (e <= b) return e; // failed search
  if (*p == *b return b; // successful search
  Ptr m = b + (e - b)/2;
  return (*p < *m) ? binsearch(p, b, m) 
		   : binsearch(p, m, e);
}

Professor Stu Bjorn prefers the old method of array and index parameters instead of pointers. Stu writes this equivalent code:

template<class V>
size_t binsearch(V& x, V A[], size_t b, size_t e)
/* 
x is the desired item. A is a sorted array. 
b and e define the sorted range in which to search.
Return location of occurrence of x in sorted array [A[b]..A[e]).
Return 0 if no such value exists (1-based indexing).
We assume standard comparison operators can be used on datatype V.
*/
{ if (e <= b) return 0; // failed search
  if (x == A[b] return b; // successful search
  size_t m = (b + e)/2;
  return (x < A[m]) ? binsearch(x, A, b, m) 
		    : binsearch(x, A, m, e);
}
  1. (3 points) Suppose arrays are passed as pointers (as is done in C++). State a recurrence relation for the time taken by this binsearch, and solve it.

  2. (3 points) Suppose arrays are passed by value (i.e. entire arraye copied) on each call to binsearch, including the recursive calls. State a recurrence relation for the time taken by this binsearch including the time to copy data, and solve it. Assume unit cost to copy one value.

  3. (3 points) Suppose arrays are passed by value (i.e. are copied) on each call to binsearch, including the recursive calls. However the smart compiler is able to see that only the region between index b and e will be accessed. The region is inclusive of A[b], exclusive of A[e], so that only that subarray consisting of e - b elements is copied. State a recurrence relation for the time taken by this binsearch including the time to copy data, and solve it.

  4. (1 point) Student Imso Sharp notices that these binsearches require an index or pointer comparison and two value comparisons, an `==' and a `<', per step. Rewrite the Ptr based binsearch so that only one value comparison is used in the typical step. Will this be better? Why? (i.e. by approximately how much better, do you think?)