Like normal knapsack, this too is a dynamic programming problem. The code above solves it in top down manner with memoization. To accommodate for the lowerbound, we need to change the base case a little and add an extra condition to invalidate some paths.
C++ Code With Comments:
#include <bits/stdc++.h>
using namespace std;
int n, lowerbound, cost[100], weight[100], dp[100][100];
int solve(int index, int filled) // recursive fn to calculate
minimum cost
{
if(index == n && filled >= lowerbound) return 0; // base
case
if(index == n) return 100000; // return high value because invalid
path
if(dp[index][filled] != -1) return dp[index][filled]; // if this
path is already calculated
// before no need to calculate it again
return dp[index][filled] =
min(cost[index] + solve(index + 1, filled + weight[index]),
solve(index + 1, filled));
// two choices, whether to take the object at current index or
not
// recursively trying all paths and storing in dp table to avoid
repeated calculations
}
int main()
{
memset(dp, -1, sizeof dp); // setting dp table to one
cin >> n; // number of objects
for(int i = 0 ; i < n ; i++){
cin >> cost[i] >> weight[i]; // cost of item i and
value of item i
}
cin >> lowerbound; // lowerbound weight
cout << solve(0, 0); // call function and output
}
Consider the following variant of the knapsack problem. Given are n items with costs ci and...
The decision version of the Knapsack problem is as follows: Given a set of n items {1, 2, …, n}, where each item j has a value v(j) and a weight w(j), and two numbers V and W, can we find a subset X of {1, 2, …, n} such that Σj∈X v(j) ≥ V and Σj∈X w(j) ≤ W? Prove formally that the Knapsack problem is NP-complete.
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)
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...
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...
"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...
Solve the 0-1 knapsack problem given the following items, each labeled with weight and value. Assume the total weight limit W is 8 lbs. Item 1 Value ($) 8 Weight (lb) 1 23 4 40 30 54 2 6 3
Consider the following greedy algorithm for the knapsack problem: each time we pick the item with the highest value to weight ratio to the bag. Skip items that will make the total weight exceeded the capacity of the bag. Find a counterexample to show that this approach will not work, and the result could be 100 times worse than the optimal solution. That is, construct a table of set of items with weight and values and find a bag capacity...
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...
Need help on Problem 7 & 8 Vi CI 7. A 10 mL volume of a 5 mM lactic acid solution is mixed with 5 mL of water. What is the concentration of the resulting mixture in uM? (Remember, CV,=C2V2, where C and V represent the concentrations and volumes of each solution, respectively.) Finaca C2 5avi = Cava (5x10-3) Clomi) = (Ca)(151) 15ml 15ml 8. What volume of water should be added to 7 mL of a 10 mM riboflavin...
Consider the Producer-Consumer problem Assume that there are 2 producers (P1 and P2) and 1 consumer (C1). The maximum number of items in the queue is 100. Consider the following solution The functions qfull enque and deque are the standard functions discussed in class Assume they have been implemented correctly by one of the methods Semaphore ni=0, mutex=1; /* ni= number of items currently in the queue.*/ /* mutex is used to provide critical section. */ Code of a Producer...