Converting from a problem solving algorithm with a 1-restrictive backpack with dynamic programming to solve a problem with two constraints is very simple. For someone to draw a three-dimensional image that shows you what is happening there, I think it is difficult. The algorithm, on the other hand, is pretty simple:
I assume that you need an exact solution, and you want to minimize the cost, rather than maximize the value (which I got from your example). Earlier, I did not do any of these options, so I can not guarantee that there was no error.
1-restriction
The matrix for the task with a 1-restrictive backpack (item x weight) stores the value in each position.
The algorithm is basically:
2-restriction
Now, to extend this to include, add another restriction: (let's say size is another restriction)
The matrix will be (item x weight x size) .
// WL = backpack weight limit // SL = backpack size limit for w = 0, 1, ..., WL // weight for s = 0, 1, ..., SL // size A[0, w, s] = INFINITY A[0, 0, 0] = 0 for i = 1, 2, ..., n // items for w = 0, 1, ..., WL // weight for s = 0, 1, ..., SL // size // wi, si and ci are the weight, size and cost of the item respectively // A[i-1, w-wi, s-si] + ci = 0 when wi > w or si > s A[i, w, s] = min(A[i-1, w, s], A[i-1, w-wi, s-si] + ci)