CSCI 256
Design and analysis of algorithms
Assignment 6

Due Monday, 3/12/2001


Only turn in problems from the second section.

Practice problems:

  1. Suppose that there are two different skylines: One is projected on a screen with a blue color, and the other is superimposed on the first one with a red color. Design an efficient algorithm to compute the shape that will be colored purple. In other words, compute the intersection of the two skylines.

  2. Problem 16-2 on page 325 of the text. Assume no word is longer than will fit into a line.

Problems to be turned in:

  1. Let x1, ..., xn be a sequence of positive real numbers. Design an O(n) algorithm to find the subsequence xi, ..., xj (of consecutive elements) such that the product of the numbers in it is maximum over all consecutive subsequences. The product of the empty sequence is 1.
    Hint: Think inductively, but it is not enough to just consider the maximum consecutive subsequence of the sequence of n-1 numbers in order to find the max for n. The new element can become part of the max subsequence if it contributes sufficiently to a product of some suffix of the sequence. Strengthen the inductive assumption of your algorithm by assuming you can both find the maximum consecutive subsequence and the maximum consecutive sequence that ends with the last element of the sequence (though of course, they might turn out to be the same!).

  2. The following are a sequence of generalizations of the knapsack problem. Each successive one is slightly harder than the previous. You need only turn in the last one you succeed with.

    1. Solve the variation of the knapsack problem where there is an unlimited supply of each item. In other words, the problem is to pack items of given sizes in a given sized knapsack, but each item may appear many times.

    2. Solve the variation of the above where each item has an associated value. E.g. a can of beans may have size 15 and value 3 while a steak may have the same size but value 10. Design an algorithm to find how to pack the knapsack fully, such that the items in it have the maximal value among all possible ways to pack the knapsack.

    3. The Ultimate Knapsack (optional -- for extra credit): Like the previous version but now we are not restricted to filling the knapsack exactly to capacity. we are interested only in maximizing the total value, subject to the constraint that there is enough room for the chosen items in the knapsack.

  3. Write a computer program to solve the most complex variation of knapsack you submitted to the previous problem. The input should begin with an integer k representing the capacity of the knapsack, a number n representing the number of items, then n lines, each with the size of an item. If you do the second or third variant above, you should follow that by n more lines, each with the value of an item. Thus if item i has size si and value vi, si will appear alone on the ith line after n, while vi will appear on the ith line of values. You should print out the actual solution (which items fit into the knapsack and the total value if you are doing the second or third variants above.

    Please follow the guidelines from homework three on how to turn in your program. We will expect a well-documented and well-structured solution that has complexity O(kn).


Back to:

  • CS256 home page
  • Kim Bruce's home page
  • CS Department home page
  • kim@cs.williams.edu