Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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 0/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)m(i-1, weightLimit) if wi> weightLimit Write a recursive method for computing m(i, w) using following method header: public static double m(int i, double weightimit, doublell 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 weights for each item: 2 38495 Enter the weight limit for the bag: 7 The maximum weight of the items placed in the bag is 7.0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions