Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving a variation of 0/1 Knapsack (multiple source for items, each item can be selected from one of the sources)

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?

like image 839
Tolga E Avatar asked Mar 20 '11 21:03

Tolga E


People also ask

Which of the following methods can be used to solve the 0 1 knapsack problem?

The 0-1 Knapsack problem can be solved using Greedy algorithm.

How 0 1 knapsack problem can be solved using backtracking?

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.

Which knapsack problem restricts the selection of any item more than once?

Knapsack Question Variants Another common variant is the constrained knapsack problem that restricts your program so you cannot select any item more than once.

How solve 0 1 knapsack problem using dynamic programming explain with example?

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.


2 Answers

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 bound
  • f(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:

  1. Not inserting the kth element to the knapsack. Under that constraint, the value of the optimal solution is f(k-1,B). Why? because we have k-1 more elements and the bound is still B.
  2. Taking the kth element from S1. Under that constraint, the value of the optimal solution is 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]).
  3. Taking the kth element from S2. Use the same logic as before to see that the value of the optimal solution under that constraint is V[k+n] + f(k-1, B - W[k+n]).
  4. Taking the nth element from S3. optimum: V[k+2n] + f(k-1, B - W[k+2n]).
  5. Taking the nth element from S4. optimum: 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.

like image 129
snakile Avatar answered Sep 28 '22 08:09

snakile


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 :-).)

like image 20
Gareth McCaughan Avatar answered Sep 28 '22 10:09

Gareth McCaughan