Project Three due Monday 19 March, 1PM

MCS 275 Project Three : Knapsack, due Monday 19 March 2001, at 1PM.

Purpose : write a program to enumerate all solutions of the knapsack problem.

A universal problem in life is the knapsack problem. Imagine a traveler who needs to pack his belongings for a journey and who has to decide which items to put in a suitcase of limited capacity. Think of a person on a strict diet who can choose from many items to eat, but may not exceed a maximal daily amount of calories. And of course, our pocket money is never large enough for all the things we want to buy. As there is no escape from this dilemma, the purpose of this project is to write a program that can present all valid alternative solutions.

The heart of your program should be a recursive routine to enumerate all possible subsets of a set of n elements. This enumeration is done with divide and conquer. We divide all subsets in two blocks of subsets: those sets that do not and those that contain the first element. Each block of subsets is again divided in two blocks: those sets that do not and those that contain the second element. And each block is again divided in two, until we reach the last element of the set.

After launching your program, the user should first be asked to give in the number of items. Then, for each item, the user should give the corresponding weight. Finally, the user must provide a maximal admitted total weight. Here is a example of an input:

    Solving the knapsack problem.
    Give number of items : 5
    Give weight of item 1 : 2   
    Give weight of item 2 : 3
    Give weight of item 3 : 1
    Give weight of item 4 : 4
    Give weight of item 5 : 5
    Give maximal admitted total weight : 7
Do not set any upper bound on the number of items the user can enter or on the maximal admitted total weight.

The output consists of two parts: a listing of all possible configurations and a frequency table classifying all admitted solutions according to their weight.

The output must start with a line showing the number of items and the maximal admitted total weight. Furthermore, the output must contain 2^n lines, where n is the number of items. Each line represents one potential solution of the knapsack problem, as an array of bits: 1 if an element belongs to the knapsack, 0 otherwise. Then the total weight of all items in the knapsack is printed. Finally, and on the same line, a message whether the solution is admitted or not must be printed, each time comparing with the maximal admitted total weight.

The second part of the output is the frequency table. It starts with a line that contains the number of admitted solutions and the maximal admitted total weight. Then, for each admitted total weight, one line is written on output with that total weight and the number of solutions that attained that weight.

For the input example above, the output is as follows:

    All ways to pack 5 items, with total weight <= 7 :
     0 0 0 0 0 total weight : 0 admitted, less than or equal to 7.
     0 0 0 0 1 total weight : 5 admitted, less than or equal to 7.
     0 0 0 1 0 total weight : 4 admitted, less than or equal to 7.
     0 0 0 1 1 total weight : 9 is heavier than 7, not admitted!
     0 0 1 0 0 total weight : 1 admitted, less than or equal to 7.
     0 0 1 0 1 total weight : 6 admitted, less than or equal to 7.
     0 0 1 1 0 total weight : 5 admitted, less than or equal to 7.
     0 0 1 1 1 total weight : 10 is heavier than 7, not admitted!
     0 1 0 0 0 total weight : 3 admitted, less than or equal to 7.
     0 1 0 0 1 total weight : 8 is heavier than 7, not admitted!
     0 1 0 1 0 total weight : 7 admitted, less than or equal to 7.
     0 1 0 1 1 total weight : 12 is heavier than 7, not admitted!
     0 1 1 0 0 total weight : 4 admitted, less than or equal to 7.
     0 1 1 0 1 total weight : 9 is heavier than 7, not admitted!
     0 1 1 1 0 total weight : 8 is heavier than 7, not admitted!
     0 1 1 1 1 total weight : 13 is heavier than 7, not admitted!
     1 0 0 0 0 total weight : 2 admitted, less than or equal to 7.
     1 0 0 0 1 total weight : 7 admitted, less than or equal to 7.
     1 0 0 1 0 total weight : 6 admitted, less than or equal to 7.
     1 0 0 1 1 total weight : 11 is heavier than 7, not admitted!
     1 0 1 0 0 total weight : 3 admitted, less than or equal to 7.
     1 0 1 0 1 total weight : 8 is heavier than 7, not admitted!
     1 0 1 1 0 total weight : 7 admitted, less than or equal to 7.
     1 0 1 1 1 total weight : 12 is heavier than 7, not admitted!
     1 1 0 0 0 total weight : 5 admitted, less than or equal to 7.
     1 1 0 0 1 total weight : 10 is heavier than 7, not admitted!
     1 1 0 1 0 total weight : 9 is heavier than 7, not admitted!
     1 1 0 1 1 total weight : 14 is heavier than 7, not admitted!
     1 1 1 0 0 total weight : 6 admitted, less than or equal to 7.
     1 1 1 0 1 total weight : 11 is heavier than 7, not admitted!
     1 1 1 1 0 total weight : 10 is heavier than 7, not admitted!
     1 1 1 1 1 total weight : 15 is heavier than 7, not admitted!
    Admitted solutions : 16, all with total weight <= 7 :
      number of solutions with total weight = 0 : 1
      number of solutions with total weight = 1 : 1
      number of solutions with total weight = 2 : 1
      number of solutions with total weight = 3 : 2
      number of solutions with total weight = 4 : 2
      number of solutions with total weight = 5 : 3
      number of solutions with total weight = 6 : 3
      number of solutions with total weight = 7 : 3
Note that you can easily get around the recursion by summing all numbers from 0 to 2^n in the binary representation. However, a (correct) solution that uses this summation will only count for half of the total points (i.e., 20 out of 40). You must use divide and conquer. Mind spelling mistakes.

The listings of your program will be collected at the start of the lecture on Monday 19 March, at 1PM. Also send the code by e-mail to jan@math.uic.edu. The first line of your program should be like

                     /* MCS 275 Project Three by <Author> */
where you replace the < Author > by your name.

Write appropriate documentation inside the code to comment on your subroutines. The gcc compiler will be used to test your program. So even if you have developed your program in a Windows environment, it may be good to run your final version on icarus.cc.uic.edu where the gcc compiler is installed. A final note to Windows users: with the Cygwin tools you can use the gnu tools from Windows.

If you have questions, comments, or difficulties, feel free to come to my office for help.