Lab 8: Dynamic Programming in practice

The skis and skiers problem

You've decided to become a ski bum, and hooked up with Sugarloaf Ski Resort. They'll let you ski for free all winter, in exchange for helping their ski rental shop with an algorithm to assign skis to skiers.

Ideally, each skier should obtain a pair of skis whose heigth matches his or her own height exactly. Unfortunately, this is generally not possible. We define the disparity between a skier and his/her skis as the absolute value of the difference between the height of the skier and the height of the skis.

The objective is to find an assignment of skis to skiers that minimizes the sum of the disparities.

  1. First, let's assume that there are m skiers and n pairs of skis (that is, same number of pairs of skis and skiers). One day, while waiting for the lift, you make an interesting discovery: if we have a short person and a tall person, it would never be better to give to the shorter person aa taller pair of skis than were given to the taller person. Prove that this is always true.

    Hint: Let P1, P2 be the length of two skiers, and S1, S2 the lengths of the skis. We assume P1 < P2 and S1 < S2. Prove that pairing P1 with S1 and P2 with S2 is better than pairing P1 with S2 and P2 with S1. Basically we'd like to show that |P1 - S1| + |P2 - S2| <= |P1 - S2| + |P2 - S1|. To prove this consider all possible cases:

    1. P1 < S1 < P2 < S2:
    2. P1 < S1 < S2 < P2:
    3. P1 < P2 < S1 < S2:
    4. S1 < P1 < P2 < S2:
    5. S1 < P1 < S2 < P2:
    6. S1 < S2 < P1 < P2:

  2. Describe a greedy algorithm to assign the (pairs of) skis to skiers, and argue why this algorithm is correct. What is the time complexity of your algorithm?

  3. Your next task is to design an efficient algorithm for the more general case where there are m skiers and n pairs of skis and m < n (i.e. more skis than skiers). The greedy solution above does not work in this case (can you come up with a simple example?), so you'll come up with a dynamic programming solution.

    Here is some notation that may help you.

    Define OPT(i,j) recursively.

  4. Illustrate your algorithm by explicitly filling out the table OPT(i,j) for the following sample data: ski heights {1, 2, 5, 7, 13, 21}. Skier heights {3,4, 7, 11, 18}.

    Note: This requires a lot of patience but it makes you really understand what's going on. You may consider skipping this part and going to the programming part below.

The skis and skiers in practice

Write code, in a language of your choice, to implement part (3) of the skis and skiers problem above. You need to implement TWO functions for finding OPT(i,j):
  1. Each function should take i,j as parameters, and should return the optimal cost of the assignment (not the actual assignment of skis to skiers; as usual we claim that it can be added easily). In this assignment the goal is to get an understanding of the running time with and without dynamic programming.

  2. The arrays that hold the heights of the skis and the skiers can be global variables.

  3. The number of skiers and skis, m and n, need to be read from the command line using argv and argc (if you use c/c++); use something similar if you use python, but the idea is that the user does not need to edit your code to change n and m.

  4. Initialize the skis and skiers height arrays with random values that are reasonable for skis and skiers heights (assume that heights are integers, in inches, so come up with a reasonable range).

  5. We will run your program like this:
    g++ bullock.cpp -o bullock
    ./bullock 100 200
    you entered m=100 n=200
    running opt_dynprogr  with m=100, n=200: total time = ...
    running opt_recursive with m=100, n=200: total time = 
    

  6. Test both functions on increasing values of m and n, until you notice a significant difference in running time between the two methods.

  7. Run your code on the heights in part (4) above, and print the table that you got. Include this table in your findings section at the top of your file.

  8. At the top of your file write your names, date, the table, and a brief summary of your observations running the program (such as when does one method start to be faster than the other, and if the difference is significant).

  9. What to turn in: email me the .cpp file. Please don;t create a separate report. The brief report shoudl be all in the header, at the beginning of the .cpp file.

  10. Drive to Sugarloaf, take a picture of yourself in front of the lift, and email it to me (yes, this is a joke. Just checking that you are still reading).

  11. You can work with a partner. If you do, please write clearly both names at the beginning of your file.

  12. In addition to email, print hard-copy of your code and hand it in.

Enjoy!