Haloo , i have java program ,
Java Program , dynamic program
Given a knapsack with capacity B∈N and -n- objects with profits
p0, ..., p n-1 and weights w0, ..., wn-1. It is also necessary to
find a subset I ⊆ {0, ..., n-1} such that the profit of the
selected objects is maximized without exceeding the capacity.
However, we have another limitation: the number of objects must not
exceed a given k ∈ N
Example: For the items in the following table, B = 10 and k = 3 ,4
is the maximum possible profit (without restriction of the items we
could have achieved a profit of 6).
objects - Profit - weights
0 - 1 - 3
1 - 2 - 2
2 - 1 - 2
3 - 1 - 2
4 - 1 - 1
5 - 1 - 2
Create in the package ads.set1.knapsack a class
MaxItemsKnapsackSolver. Implement the algorithm in the following
method:
/**
* Solves the limited-items knapsack problem using a dynamic
programming
* algorithm based on the one introduced in the lecture.
*
* @param items (possibly empty) list of items. All items have a
profit
* {@code > 0.}
* @param capacity the maximum weight allowed for the knapsack
({@code >= 0}).
* @param maxItems the maximum number of items that may be
packed
* ({@code >= 0}).
* @return the maximum profit achievable by packing at most {@code
maxItems}
* with at most {@code capacity} weight.
*/
public static int pack(Item[] items, int capacity, int maxItems)
{
// Implement me
return -1;
}
The available entries are described by instances of the Item class,
which you can download here.
https://drive.google.com/file/d/15IAoYJQWc7kImRlYFOsEHP6oKER0QO2-/view?usp=sharing
Your tax should only consist of the class MaxItemsKnapsackSolver.
Solution for this problem is to consider all subsets of objects and calculate the total weight and profits of all subsets. Include only subsets whose total weight is smaller than maxWeight. From all such subsets, get the maximum value subset and return the same.
Code:
public class MaxItemsKnapsackSolver
{
public static void main(String[] args) {
int profit[] = new int[]{1, 2, 1,1,1,1};
int weight[] = new int[]{3, 2, 2,2,1,2};
int maxWeight = 10;
int lenProfit = 3;
System.out.println(pack(maxWeight, weight, profit,
lenProfit));
}
//function to return max of two numbers
static int maxNumber(int x, int y) { return (x >
y)? x : y; }
// Returns the maximum value that can be put in a knapsack of
capacity maxWeight
public static int pack(int maxWeight, int weight[], int profit[],
int lenProfit)
{
int i, w;
int knapsack[][] = new int[lenProfit+1][maxWeight+1];
// Build table Knapsack[][] in bottom up manner
for (i = 0; i <= lenProfit; i++)
{
for (w = 0; w <= maxWeight; w++)
{
if (i==0 || w==0)
knapsack[i][w] = 0;
else if (weight[i-1] <= w)
knapsack[i][w] = maxNumber(profit[i-1] +
knapsack[i-1][w-weight[i-1]], knapsack[i-1][w]);
else
knapsack[i][w] = knapsack[i-1][w];
}
}
return knapsack[lenProfit][maxWeight];
}
}
Haloo , i have java program , Java Program , dynamic program Given a knapsack with capacity B∈N and -n- objects with profits p0, ..., p n-1 and weights w0, ..., wn-1. It is also necessary to find a su...
Recall that in the "Knapsack Problem", there are n items having respective values V1..n) and weights W1..n), all greater than 0 and one needs to maximize the total value of the subset of the items placed in the knapsack limited by a weight capacity of W In the 0-1 Knapsack Problem, each item must be either be included or excluded in its entirety, in light of the fact that this problem is to be "NP-Complete", how can one solve the...
1. Apply the dynamic programming algorithm discussed in class to solve the knapsack problem. (10 points) a. Show the completed table. b. Which items are included in the final configuration of the knapsack? c. What is the maximum value that can fit in the knapsack using a configuration of these items? item 1 2. 3 4 weight 3 2 value $25 $20 $15 1 capacity W = 6. 4 5 $40 $50 5
"Greedy, but Better": Given a knapsack problem with a weight capacity C, and n items, and each item has a weight W[1:n] and monetary value P[1:n]. You have to determine which items to take so that the total weight is C, and the total value (profit) is maximized. In this case we are considering an integer problem, so you can either take an item, or not take an item, you cannot take it fractionally. If you recall, the greedy algorithm...
I'm having trouble with my Java Homework in which my professor wants us to solve the 0-1 Knapsack problem with Dynamic Programming. The code below is what she provided and she requested that we not change any of her existing code but simply add to it. As you can see she gave us the stub file for the knapsack class and the Item class. You are a thief with a knapsack with a carrying capacity of knapsackCapacity pounds. You want...
Fill out the table for the knapsack problem, where the objects, weights, and values are as given, and the overall weight limit is 10 Next, circle the entries in the table that are used when backtracking to find objects to use in the solution Then list the object numbers that can be used for an optimal solution .Also list the weights and values of those objects Verify that the values of your solution objects add up to the optimal number...
2 Knapsack Problem In a Knapsack problem, given n items {11, I2, -.., In} with weight {wi, w2, -.., wn) and value fvi, v2, ..., vn], the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity W. Tt i=1 In this question, we will consider two different ways to represent a solution to the Knapsack problem using an array with size...
solution is required in pseudo code please. 2 Knapsack Problem În al Knapsack problem. given n items(11-12. . . . . 1"} with weight {w1·W2. . . . . ux) and value (n 2, .., nJ, the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity In this question, we will consider two different ways to represent a solution to the...
In a Knapsack problem, given n items {I1, I2, · · · , In} with weight {w1, w2, · · · , wn} and value {v1,v2, ···, vn}, the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity W . i-1 In this question, we will consider two different ways to represent a solution to the Knapsack problem using . an...
Please help with question 2 (c). (2) Suppose, the game is updated so that every item, i, now has weight, wi], in kilograms (you can assume you are passed the item weights in an array, w, of size m). You can only carry n kilograms of weight. (a) (1 point) Example: Let n Ξ 15, m 5. If the item values are v (5.30, 17, 32, 40) and the item weights are w - 2,4, 3,6,15} which should you choose...
Please explain step by step, thank you so much! 0-1 Knapsack (N=6, W=10) Item Weight Value (lb) ($) 8 1 0 10 Weight limit w(lb) 4 5 6 7 2 2 2 2 2 2 3 2 #2 2 1 2 43 33 3 w #3 0 2 3 3 #4 56 w 2 a #6 7. (10%) (Cont.) Unbounded Knapsack Problem (1-Dim Dynamic Programming) Weight limit w 0 : 6 Weight limit w F(w) 7 Unbounded Knapsack (N=6, W=10)...