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...
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.
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)
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...
There are n items in a store. For each item i=1,2,...,n the weight of the item is wi and the value of the item is vi. A thief is carrying a knapsack of weight W. In this version of a problem the items can be broken into smaller pieces, so the thief may decide to carry only a fraction xi of object i, where 0≤xi ≤1. Item i contributes xiwi to the total weight in the knapsack, and xivi to...
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...
Fulcrum def can_balance(items): Each item in the list of items is now considered to be a physical weight, and therefore guaranteed to be a positive integer. Your task is to find and return a fulcrum position in this list so that when balanced on that position, the total torque of the items to the left of that position equals the total torque of the items to the right of that position. (The item on the fulcrum is assumed to be...
Formulate the followings into optimization problems. While you could use the integer constraints, the linear structure should be maintained. However, you shouldn’t use the integer constraints if you can formulate the problem without them. Describe the decision variables, objective function and constraints carefully. You don’t need to solve this problem. (b) (5 pts) A KAIST student is planning a back-pack trip to Europe for this summer. There are 11 items (1 through n) that she is considering to carry to...
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...
[Recursive Cost] [ALGORITHM] Improving Efficiency PLEASE explain in DETAIL the following question in detail. The algorithm is also given below. Thank You! 1.a) Define recursively the worst case cost Kn of the Knapsack function for n items. Remember that you need to provide both the base case and the recurrence relation. Also do not forget to include the cost of the function Worth in your cost. Justify your answer (i.e. explain what each component of the formula represents). [5points] 1.b) Use...