Lab 4 The Knapsack Problem solution

$14.99

Original Work ?
Category:

Description

5/5 - (2 votes)

Problem Definition
Your job is to write a program to help a thief choose which items to steal1. The input is a set of items that the thief can steal. Each item has a weight and a value associated with it. Of course, the thief would like to steal items whose collective value is maximum, but since the thief has planned an arduous escape route, he can only carry a certain maximum weight. Thus our input includes a total weight that the thief can carry. Your program should output the items that he must steal such that
1. the total value is maximized, but
2. the total weight is not more than the weight that the thief can carry.
Please follow the steps given below.
0.1 Command Line Arguments
Your program should be written such that the name of the input file can be given as a command line argument. To set the command line argument from within Netbeans2, go to Run → Set Project Configurations → Customize. Choose Run on the left panel. You will see a Run Command on the right panel and “${OUTPUT PATH}” against the Run Command. Modify it to 1This is just a programming exercise. Please do not attempt this in real life. If you do, ensure that the solution is absolutely correct — you don’t want an angry thief coming after you. 🙂 2If you use something other than Netbeans, you can use the traditional command line interface.
1
“${OUTPUT_PATH}” CLG
Where CLG is the command line argument. In our case, CLG must be the full path and name of the input file. One thing to note is that CLG must come outside the quotations. When no input file name is given, then the input must be read from the console.
0.2 The Input Format
Regardless of whether your input is read from a file or from the console, it should have the following format. (Note that all numbers are integers.)