Question: This is using JAVA. Given n items with weights w1, w2,, and wn, and a bag with a weight capacity of w, the problem of
This is using JAVA. Given n items with weights w1, w2,, and wn, and a bag with a weight capacity of w, the problem of finding the items with the maximum total weight to store in the bag is called the o/1 knapsack problem. Let m(i, w) denote the total weight of the best solution of placing the first i items into a bag with weight capacity w. The problem can be solved using the following recursion: m(0, weightLimit) 0; m(i, w)- max(m(i-1, weightLimit), wi+ m(i-1, weightlimit- wi): Write a recursive method for computing mli, w) using following method header: public static double m(int i, double weightLimit, doublel] w) where w is an array of the weights for items. Write a test program that prompts the user to enter the number of the items and weight for each item and the weight capacity of the bag, and displays the maximum total weight of the items that can be placed in the bag. Your output should look like that below Enter the number of items:6 Enter the eights for each item: 238495 Enter the weight limit for the bag:7 The maximun weight of the items placed in the bag is 7
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
