You want to put your valuable items into a box. The maximum capacity of the box is 4. The table below shows the values and weights for items 1, 2, 3 and 4 respectively.
Item i |
Value Vi |
Weight Wi |
1 |
15 |
1 |
2 |
10 |
5 |
3 |
9 |
3 |
4 |
5 |
2 |
(i) Use recursion strategy to put the items into the box such that it holds the highest value
(ii) Repeat (i) above using backtracking approach.
class Knapsack {
public static void main(String[] args) throws Exception {
int val[] = {10, 40, 30, 50};
int wt[] = {5, 4, 6, 3};
int W = 10;
System.out.println(knapsack(val, wt, W));
}
public static int knapsack(int val[], int wt[], int W) {
//Get the total number of items.
//Could be wt.length or val.length. Doesn't matter
int N = wt.length;
//Create a matrix.
//Items are in rows and weight at in columns +1 on each side
int[][] V = new int[N + 1][W + 1];
//What if the knapsack's capacity is 0 - Set
//all columns at row 0 to be 0
for (int col = 0; col <= W; col++) {
V[0][col] = 0;
}
//What if there are no items at home.
//Fill the first row with 0
for (int row = 0; row <= N; row++) {
V[row][0] = 0;
}
for (int item=1;item<=N;item++){
//Let's fill the values row by row
for (int weight=1;weight<=W;weight++){
//Is the current items weight less
//than or equal to running weight
if (wt[item-1]<=weight){
//Given a weight, check if the value of the current
//item + value of the item that we could afford
//with the remaining weight is greater than the value
//without the current item itself
V[item][weight]=Math.max
(val[item-1]+V[item-1][weight-wt[item-1]],
V[item-1][weight]);
}
else {
//If the current item's weight is more than the
//running weight, just carry forward the value
//without the current item
V[item][weight]=V[item-1][weight];
}
}
}
//Printing the matrix
for (int[] rows : V) {
for (int col : rows) {
System.out.format("%5d", col);
}
System.out.println();
}
return V[N][W];
}
}
It is in java
You want to put your valuable items into a box. The maximum capacity of the box is 4. The table below shows the values and weights for items 1, 2, 3 and 4 respectively. Item i Value Vi Weight...
You want to put your valuable items into a box. The maximum capacity of the box is 4. The table below shows the values and weights for items 1, 2, 3 and 4 respectively. Item i Value Vi Weight Wi 1 15 1 2 10 5 3 9 3 4 5 2 (i) Use recursion strategy to put the items into the box such that it holds the highest value (ii) Repeat (i) above using backtracking approach.
9-2. Five items are to be loaded in a vessel. The weight w, volume v, and value r for item i are tabulated below. Item i Unit weight, w, (tons)Unit volume, v, (yd) Unit worth, r, ($100) 4 The maximum allowable cargo weight and volume are 112 tons and 109 yd" respectively. Formulate the ILP model, and find the most valuable cargo. hatf fll and 7 empty You would like to 9-2. Five items are to be loaded in a...
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...
Modify the item weights to be [2, 1, 2, 3, 5] and keep the item values the same [25, 20, 15, 40, 50]. How many different optimal subsets does the instance of part (a) have? item weight value $25 $20 $15 $40 $50 capacity W-6 4 4 item weight value $25 $20 $15 $40 $50 capacity W-6 4 4
In weighted knapsack problem, given the knapsack capacity is 16 and the following items (Weight, Value), what is the maximum value we can take away. Explain shortly how and by what approach you arrived at this solution. Item 1 (4, 12) Item 2 (3, 14) Item 3 (7, 22) Item 4 (8, 32) Item 5 (4, 24) Item 6 (6, 20)
Question 1. There are 6 gifts with weights 5, 3, 2, 1, 6 and 4; and values 8, 2, 5, 13, 16, and 1 respectively. Use dynamic programming to find the most valuable subset of gifts subject to the constraint the total weight cannot exceed 10. Show the entire table for bottom-up computation, together with the keep array. (20)
please I would like assistance with this which are question 1 and 2, thank you 2. We have 5 objects, and the weights and values are No. 2 3 4 5 10 20 30 50 V 20 30 66 60 55 W 40 The knapsack can carry a weight not exceeding 90, find a subset items and give the total weight and value for following algorithms: 1) By using the algorithm of greedy of value for 0-1 knapsack problem? By...
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...
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...
Using the same value/weight table as the example presented in the video, i v[i] w[i] 1 1 2 2 3 3 3 5 4 4 07 calculate the result for a knapsack capacity of 23. What is the maximal value of the knapsack? ca. 28 r 6.29 6:30 . e. 32