Solving the Fractional Knapsack Problem The fractional knapsack problem as follows. A thief robbing a store wants to take the most valuable load that can be carried in a knapsack capable of carrying at most W pounds of loot. The thief can choose to take any subset (including fractional amounts) of n items in the store. The w item is worth v dollars and weighs w pounds, where v and ware integers. Which and how much of each item should the thief take? The idea is to take the item that costs the most per pound first and then proceed on to the item that costs the second most per pound, and so on until you have reached the capacity that you can carry in the knapsack. Remember you can take a fractional part of an item. public static ArrayList fractional Knapsack (ArrayList items, double maxWeight) This method should return how much of each item (as a fraction) from the ArrayList of Items that you should take. The entries in the ArrayList should be values between 0.0 and 1.0. • If I take all of an item, the corresponding entry in the returned ArrayList should be 1.0 • If I take none of an item, the corresponding entry in the returned ArrayList should be 0.0. You can assume that the value of items is already ordered by cost per pound with the largest cost per pound in the first entry of the items. This assumption is for the inputs I will test your code on. When you are testing your code, you will need to make sure that items are ordered by cost per pound with the largest item in the first entry of the ArrayList.

icon
Related questions
Question

This is method signature class below:

package greedy;

import java.util.ArrayList;


public class Knapsack {
    
    static class Item {
        String name;
        int value;
        int weight;
        double costPerPound; //keeping track of this for sorting purposes;
        
        Item(String name,int value,int weight) {
            this.name = name;
            this.value = value;
            this.weight = weight;
            costPerPound = (double)value/weight;
            
        }
        
        public String toString() {
            return name +", value: "+value + ", weight: " + weight;
        }

    }
    
    static ArrayList<Double> fractionalKnapsack(ArrayList<Item> items,double maxWeight){
        
       
    }
    

}

 

Solving the Fractional Knapsack Problem
The fractional knapsack problem as follows. A thief robbing a store wants to take the most
valuable load that can be carried in a knapsack capable of carrying at most W pounds of loot. The
thief can choose to take any subset (including fractional amounts) of n items in the store. The w
item is worth v dollars and weighs w pounds, where v and w are integers. Which and how much
i
of each item should the thief take?
i
The idea is to take the item that costs the most per pound first and then proceed on to the item
that costs the second most per pound, and so on until you have reached the capacity that you can
carry in the knapsack. Remember you can take a fractional part of an item.
public static ArrayList<Double> fractionalKnapsack (ArrayList<Item> items, double maxWeight)
This method should return how much of each item (as a fraction) from the ArrayList of Items that
you should take. The entries in the ArrayList should be values between 0.0 and 1.0.
If I take all of an item, the corresponding entry in the returned ArrayList should be 1.0
● If I take none of an item, the corresponding entry in the returned ArrayList should be 0.0.
You can assume that the value of items is already ordered by cost per pound with the largest cost
per pound in the first entry of the items. This assumption is for the inputs I will test your code on.
When you are testing your code, you will need to make sure that items are ordered by cost per
pound with the largest item in the first entry of the ArrayList.
Transcribed Image Text:Solving the Fractional Knapsack Problem The fractional knapsack problem as follows. A thief robbing a store wants to take the most valuable load that can be carried in a knapsack capable of carrying at most W pounds of loot. The thief can choose to take any subset (including fractional amounts) of n items in the store. The w item is worth v dollars and weighs w pounds, where v and w are integers. Which and how much i of each item should the thief take? i The idea is to take the item that costs the most per pound first and then proceed on to the item that costs the second most per pound, and so on until you have reached the capacity that you can carry in the knapsack. Remember you can take a fractional part of an item. public static ArrayList<Double> fractionalKnapsack (ArrayList<Item> items, double maxWeight) This method should return how much of each item (as a fraction) from the ArrayList of Items that you should take. The entries in the ArrayList should be values between 0.0 and 1.0. If I take all of an item, the corresponding entry in the returned ArrayList should be 1.0 ● If I take none of an item, the corresponding entry in the returned ArrayList should be 0.0. You can assume that the value of items is already ordered by cost per pound with the largest cost per pound in the first entry of the items. This assumption is for the inputs I will test your code on. When you are testing your code, you will need to make sure that items are ordered by cost per pound with the largest item in the first entry of the ArrayList.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer