NTUEE Spring, 2011 Algorithms Programming Assignment #2 0-1 Knapsack Problem Introduction: The 0-1 Knapsack problem is a famous NP-complete problem (will see in later). Suppose we have a knapsack of space S. We have n items to select, indexed from 1 to n. xi = 1 means the ith item is selected and xi=0 means it is not selected. xi is binary (0 or 1) so we cannot take a fraction of an item. The value of the ith item is vi. The size of the ith item is si. We want to maximize the total value of selected items in the knapsack. For example, we have n= 5 items and the total space S=11 . i 1 2 3 4 5 vi 1 4 18 21 35 si 1 2 3 6 7 We can choose items 1, 3, 5 and the maximum total value is 54. Input/output Files: In the input file (*.in), the first two lines shows the number of total items n and space S. The other two lines show vi and the space of each item si. positive integers between 1 and 1million. All numbers are n = 5 S = 11 vi = 1 4 18 21 35 si = 1 2 3 6 7 The output file(*.out) is as follows. The first line shows the maximum values and the second line shows whether item ith is selected. The last two lines show the runtime (user) in seconds and memory usage (peak) in MB. max = 54 xi = 1 0 1 0 1 runtime = 0.1 sec memory = 1.0 MB Command line parameters: Please implement four algorithms to solve this problem. In the command line, you are required to follow this format 01knapsack –[BF|GD|DP|RC] <input_file_name> <output_file_name> where BF means ‘brute force’, GD means ‘greedy’, DP means ‘dynamic programming’ and ‘RC’ means recursion. For example, ./bin/01kanpsack –DP ./inputs/n5S11.in ./outputs/n5S11.out Questions to answer in your report: 1. Write a brute force algorithm (just pseudo code) in your report that tried all possible NTUEE Spring, 2011 combinations of xi . What is the time complexity of this algorithm? 2. Write a greedy algorithm (just pseudo code) in your report. complexity of this algorithm? What is the time Does your algorithm give the optimal solution? 3. Let (i, s) equal to the maximum value that can be obtained by placing up to i items to the knapsack of size less than or equal to s. Show a recursive solution to this problem in your report. 4 . Write pseudo codes for the recursive algorithm in your report. Analyze the time complexity of this algorithm. 5. Write pseudo codes for the DP algorithm in your report. Analyze the time complexity of this algorithm. Hint: You can use a two dimensional table: one is item, the other is space. Requirements: 1. Your source code must be written in C or C++. The code must be executable on EDA union lab machines. 2. In your report, write all four pseudo codes and analyze their time complexity. 3. In your report, compare the results, running time, and memories different input sizes. (Because BF is very slow so only small cases are needed.) Please fill in the following table and also plot figures showing the memory and running time. Please use –O2 optimization and turn off all debugging message. input size n5S11 n10S100 n20S100 n100S1000 n1KS10K n10KS100K brute force max = runtime (s) 54 0.1 ---- memor y (MB) 1.0 ---- greedy max = runtime (s) memor y (MB) ---- ---- dynamic programming max = runtime memor (s) y (MB) 54 recursion max = runtime (s) 54 memor y (MB) *Please explain if your program cannot solve it in reasonable time or space. Submission: Please submit a hardcopy of your report in class. Also, please submit a single *.tgz file to CEIBA system. Your submission must contain: 1. your source codes and a make file. By simply typing ‘make’, an executable binary is produced under the bin directory. Please use ‘-O2’ optimization. Please turn off all debugging messages. 2. a README file that explains your files. 3. a report in the doc directory. Please also submit a printed report in class. 4. We will use our own test cases so do NOT include the input files. The submission filename should be compressed in a single file <student_id>-<pa2>.tgz. (e.g. b90901000-pa2.tgz). If you have a modified version, NTUEE Spring, 2011 please add _v2 as a postfix to the filename and resubmit it (e.g. b9090100 0-pa2-v2.tgz). You can use the following command to compress a whole directory: tar zcvf <filename>.tgz <dir> You are required to run the checksubmitPA2 script to check if your .tgz submission file is correct. To use this script, simply type ./checkSubmitPA2.sh b99901000-pa2.tgz We have so many students in the class so we need automatic grading. Any mistake in the submission will result in cost 20% off your score. Please be very careful in your submission. Grading: 60% correctness (dynamic programming and recursive) 20% file format and location 20% report Bonus : 5% bonus will be given to any extra features that you add to make your program faster or smaller memory. Please write clearly in your report. NOTE: TA will check your source code carefully. zero grade for all students involved. Copying other source code can result in