Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedThis 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions