So for a practice question, we are supposed to design a Dynamic Programming algorithm which is a variation of 0/1 knapsack problem... Basically each item comes from 4 different source, and that item can be taken from only one of the sources..
Namely,
S1={(d_k, b_k) | 1 ≤ k ≤ n},
S2={(d_k, b_k) | n + 1 ≤ k ≤ 2n},
S3={(d_k, b_k) | 2n + 1 ≤ k ≤ 3n},
S4 = {(d_k, b_k) | 3n + 1 ≤ k ≤ 4n}
for n = 10
, if you select i = 16
to put, that means you won't select 6, 26 or 36
...
Can you help me to solve this problem and devise the recurrence equation?
The 0-1 Knapsack problem can be solved using Greedy algorithm.
Select items from X such that it maximizes the profit and the collective weight of selected items does not exceed the knapsack capacity. The knapsack problem has two variants. 0/1 knapsack does not allow breaking up the item, whereas a fractional knapsack does. 0/1 knapsack is also known as a binary knapsack.
Knapsack Question Variants Another common variant is the constrained knapsack problem that restricts your program so you cannot select any item more than once.
If we pick the 2kg item then we cannot pick 1kg item from the 2kg item (item is not divisible); we have to pick the 2kg item completely. This is a 0/1 knapsack problem in which either we pick the item completely or we will pick that item. The 0/1 knapsack problem is solved by the dynamic programming.
We have 4n elements.
Notation:
V[k]
- value of element k (1 <= k <= 4n)W[k]
- weight of element k (1 <= k <= 4n)B
- The boundf(k,B)
- The value of the optimal solution when the bound is B and you have 4k elements.For the kth element we have five possible choices:
f(k-1,B)
. Why? because we have k-1 more elements and the bound is still B.V[k] + f(k-1, B - W[k])
. Why? We've earned V[k] for the kth element and waisted W[k]. So for the rest of the elements we're going to earn f(k-1, B - W[k]).V[k+n] + f(k-1, B - W[k+n])
.V[k+2n] + f(k-1, B - W[k+2n])
.V[k+3n] + f(k-1, B - W[k+3n])
.Your objective is to maximize f. Hence the recurrence equation is:
f(k, B) =
max {
f(k-1,B), //you don't take item n
V[k] + f(k-1, B - W[k]), //you take item k from S1
V[k+n] + f(k-1, B - W[k+n]), //you take item k from S2
V[k+2n] + f(k-1, B - W[k+2n]), //you take item k from S3
V[k+3n] + f(k-1, B - W[k+3n]) //you take item k from S2
}
What left is to find the initial conditions.
Standard 0/1 knapsack problem: For each item, either you don't take it, or you do.
Your problem: For each item, either you don't take it, or you take it from source 1, or ..., or you take it from source 4.
Now look at the usual dynamic programming algorithm and recurrence relation for the 0/1 knapsack problem. Look at where each bit of the RHS in the recurrence relation comes from; it corresponds to the first statement above. Adapt to use the second statement above instead.
(If I'm being a little cryptic, it's because this is homework and you're meant to be learning :-).)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With