2
$\begingroup$

Is there any way for solving the Knapsack problem when we are limited by several constraints?

Let's assume that we have a set of items in which we have value v that we want to maximize and we also have two constraints weight and volume that must be lower or equal than the values W and V respectively.

My question is, can this problem be solved using dynamic programming?

$\endgroup$
2
  • 1
    $\begingroup$ Welcome to CS.SE! What have you tried? Have you tried adapting the dynamic programming algorithm for standard knapsack? The answer is yes, you can adapt it, and if you understand how that algorithm works, it should be pretty straightforward. $\endgroup$
    – D.W.
    Oct 30, 2016 at 17:45
  • $\begingroup$ I have not implemented anything yet, I am trying to understand what to do. My initial guess if the solution for 1 constraint is a 2D-Matrix in which the rows(x-axis) indicate the item number and columns(y-axis) the available weight then in order to implement with 2-constraints I should have a 3D-Matrix in which the matrix deep(z-axis) will be the available volume. Do you thing that I am in the good way in order to solve it? $\endgroup$ Oct 30, 2016 at 20:48

2 Answers 2

2
$\begingroup$

Yes. More precisely, for any fixed number of constraints (for example, weight and volume) the problem has a pseudo-polynomial time algorithm based on dynamic programming.

The idea in your comment (add one more dimension to the dynamic programming table) is essentially correct. In the classic knapsack, for any $i=0,\ldots,n$ and $w=0,\ldots,W$, you compute the highest profit that you can get with items of type $\le i$ and total weight $\le w$ (so you can get an $O(nW)$ time algorithm). In the generalization, you also need to iterate on the total volume $v=0,\ldots,V$, which means an $O(n W V)$ time algorithm.

$\endgroup$
0
$\begingroup$

In knapsack algorithm, if any object fits the knapsack, you compare the value of the objects. You still have to compare the objects but you got two parameters to compare for each object. Tricky part will also be backtracking, if required. Refer this: http://user.engineering.uiowa.edu/~dbricker/Stacks_pdf1/MultiDim_Knapsack.pdf

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.