CS302 Final Exam, December 11, 2012 - James S. Plank - Page 2

Question 4: Sorting

Part 1: Suppose I need to sort 1,000,000 integers that are uniformly distributed between -50,000,000 and 50,000,000. Explain to me how do to this in linear time using a temporary vector that holds 2,000,000 integers. Be precise in your explanation.

Part 2: Your friend Harvey is implementing quicksort. He tells you that he is going to simply choose the first element of the array as a pivot. How do you explain to Harvey that doing so is a bad idea?

Part 3: When we implemented quicksort and merge sort, we found that quicksort was faster, even though they are both O(n log(n)) algorithms. Why was quicksort faster?


Question 5: Dijkstra, Prim & Kruskal

All of the questions pertain to the graph on the right. In case you're wondering, there are 17 edges with weights that are multiples of 5, from 5 up to 85, inclusive.

Part 1: Suppose you are using Dijkstra's algorithm to determine the shortest path from A to I. Tell me the order in which the nodes are visited, and tell me the shortest path.

Part 2: Suppose you are using Prim's algorithm to determine the minimum spanning tree of the graph. Suppose you start with node G. Tell me order in which the nodes are visited.

Part 3: Suppose you are using Kruskal's algorithm to determine the minimum spanning tree of the graph. Tell me the edges of the minimum spanning tree in the order in which Kruskal's algorithm determines them.


Question 6: Dynamic Programming

Your colleague Khloe has written a hash function which she calls "KHash." It hashes strings to integers, and it's supposed to run reasonably fast. She's bundled it up into a C++ class whose definition is to the right.

Khloe has implemented her Get_Hash() method, which you can also see to the right. In case you don't remember, s.substr(i, j) returns the substring of s composed of j characters starting at index i.

Khloe's problem is that her program is taking forever once the size of the string exceeds 25. She has come to you for help, and since you've taken CS302, you are in a good position to help her.

Turn her program from one that is exponential in the size of the string to one that is quadratic in the size of the string. Do this by performing step #2 of dynamic programming. You may assume that Get_Hash() is only called once after an instance of KHash is created (in other words, if you want to put some junk into the constructor, go for it).

Extra Credit: This is going to be roughly 5 points of extra credit -- don't do it unless you have time: Perform step #3 of dynamic programming on this problem.

class KHash {
  public:
    KHash();
    int Get_Hash(string s);
    int Shift(int n);
  protected:
    vector <int> Base;
};

int KHash::Get_Hash(string s)
{
  int i;

  i = s.size();
  if (i == 1) return Base[s[0]];

  return (Base[s[0]] ^ 
          Shift(Base[s[i-1]]) ^ 
          Shift(Get_Hash(s.substr(1, i-1))) ^
          Get_Hash(s.substr(0, i-1)));
}

    Note -- you don't have to understand what's in Base or what Shift() does to solve this problem.