Lab 8: Skiing
Sugarloaf Ski Resortw will let you ski for free all winter, in
exchange for helping their ski rental shop with an algorithm to assign
skis to skiers. You have a set of pairs of skis, and a set of skiers.
Each skier needs to get a pair of skis.
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
her pairs of 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.
IN CLASS: The skis and skiers in practice
Collaboration: team programming, Level 0 (everything allowed).
Write code, in a language of your choice, to implement part (3) of
the skis and skiers problem (you will need to read the homework part
of this lab before you do this).
You need to implement TWO functions for finding OPT(i,j):
- One that corresponds to solving the problem recursively without
dynamic programming. You could name it OPT-recursive(i,j)
- One that corresponds to the dynamic programming solution. You
could name it OPT-dynprogr(i,j).
- 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.
- The arrays that hold the heights of the skis and the skiers can be
global variables.
- 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 and recompile to change n and m.
- 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).
- Aim to run your program like this:
g++ marty.cpp -o marty
./marty 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 =
- Test both functions on increasing values of m and n, until you
notice a significant difference in running time between the two
methods.
Homework
Collaboration level: Level 1
- Let's assume that there is the 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 a 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:
- P1 < S1 < P2 < S2:
- P1 < S1 < S2 < P2:
- P1 < P2 < S1 < S2:
- S1 < P1 < P2 < S2:
- S1 < P1 < S2 < P2:
- S1 < S2 < P1 < P2:
- Algorithm for same number of skis and skiers: Using this
observation, 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?
- Algorithm for when there are more skis than skiers:
design an efficient algorithm for the more general case where there
are n skiers and m pairs of skis and n < m (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.
- Sort the skiers and skis
by increasing height.
- Let h[i] denote the height of the i-th skier
in sorted order, and s[j] denote the height of the j-th pair of skis
in sorted order.
- Let OPT(i,j) be the optimal cost (disparity) for matching the
first i skiers with skis from the set 1, 2, ..., j.
- The solution we seek is OPT(n, m).
Define OPT(i,j) recursively.
- Implement your solution (in class) and use it to fill out the
table OPT(i,j) for the following sample data: ski heights {1, 2, 5,
7, 13, 21, 25, 28, 30}. Skier heights { 3, 4, 7, 11, 15, 18}.
Run your code, with and without the table, on values of m and n,
and report your conclusions, specifically around what values the
solution without the table starts to be too slow.
Note: you do not need to submit your code, only a hard copy (your
answers to part 4 and 5 will be proof that you implemented and got
it to work).
Enjoy!