Question: Implement this algorithm in Java. This must be the output: Algorithm: Given a list of items, pack a bag that doesn't go over weight with
Implement this algorithm in Java.
This must be the output:

Algorithm: Given a list of items, pack a bag that doesn't go over weight with the most valuable items. Input: List Items represented by tuples that have value and weight. Bag storage shown by positive int. Output: A list representing the items placed in the bag. Constraints: The list of items can't be empty. The weight and value must be positive of each item. Bag capacity can't be negative. Sort items in descending order with value/weight ratio. Make empty list named bag to show optimal list of items that can be placed. Initialize totalW variable to 0 . Loop through length of the list of items Add item to bag if it did not exceed capacity, and update totalW. Compute total value in bag and return it with total value. Enter the backpack capacity: 12 optimal sub-list of items to place in the backpack: Name Weight Value ItemItem1225$20$30 Item 45$10 Total value: $60
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
