CS 361: Algorithms and Data Structures
Homework 7


This assignment has both a written and a programming section. Both are due at the beginning of class on the 9th. The written portion will be collected at the beginning of the class period as usual. The programming portion should be uploaded to Moodle by the beginning of the class period.

For the programming, you can work by yourself or with a partner. If you do work with a partner, be sure to put both of your names in the comment at the top of the Skiing.java file

For the questions on the homework that ask you to "design an algorithm" or "give an algorithm", please review the "Homework Guidelines" so you know what your answer must include.


Written Questions

  1. Algorithm Design 6.1 parts (a), (b), and (c). Note that part (c) says to "Give an algorithm". You should follow the "Homework Guidelines" when designing your algorithm. Also, your algorithm should return both the value of the independent set with maximum total weight as well as returning the independent set itself. I recommend writing two separate algorithms to do so: one to return the maximum total weight, and one to reconstruct the independent set.

  2. Algorithm Design 6.2 part (b) only. For this problem, you only need to return the value of an optimal solution. You are not required to return the solution itself.

Programming Question

Suppose a set of n people want to organize a ski trip. They go to a ski rental shop which has m pairs of skis (where m > n). We'll let h(i) denote the height of the ith person and s(j) denote the length of the jth pair of skis.

We want to assign each skier a pair of skis that matches their height as closely as possible. In other words, we want to assign each skier a pair of skis such that the following difference in heights is minimized:
In this equation, A[i] is the ski assignment for the ith person.

This question asks you to implement a dynamic programming algorithm that solves this problem. Your program should return the actual solution in the form of an array of length n where the ith entry is the ski assignment for person i. For example, suppose we had the following inputs:

h = [72, 73, 75] // people height in inches
s = [63, 72, 73, 73, 77] // ski length in inches

The optimal solution would be A = [2, 4, 5]. The first person is assigned the 2nd pair of skis, the second person is assigned the 4th pair of skis, and the third person is assigned the 5th pair of skis. In this case, the difference in heights is |72-72| + |73-73| + |75-77| = 2 inches. Please return your solution in base-1 indexing since this is easier to read than base-0.

I have provided you with starter code for this question in the form of a Java file named Skiing.java. Inside, I have provided code for randomly generating arrays of human heights and ski lengths. I tried to generate realistic lengths so you'll notice both human and ski heights range between 61 inches (5 feet 1 inch) and 77 inches (6 feet 5 inches).

You are free to add whatever static methods and variables you'd like however you should make sure that your final solution is implemented in the computeSolution() method. The input to this method will be an array of length n. The computeSolution() method should fill this array with the optimal assignment of skis to skiers. Just like we've done in class, it's a good idea to write one method that computes the value of the optimal solution and another method that uses this to compute the actual solution.

This problem requires you to think carefully about the various base cases that exist.


Submission Instructions

Your Java code should be submitted in a zipped directory that contains your Skiing.java file. You and your partner only need to submit one directory. Please make sure that you put both of your names in the class comment for Skiing.java so I know who you worked with.

Your code should compile with no errors and it should obey the specifications provided above. Please upload your zipped directory to Moodle by the beginning of class on Friday.


Last modified: Fri Jan 24 10:58:47 PST 2014