This is the old and famous knapsack problem : Knapsack Problem
Here I have Knapsack problem with one constraint.
I have Knapsack with size W = 100000000 and N = 100 items I wrote the dynamic solution for it the Complexity of my algorithm is O(100000000*100)
and this is too big in both time and space but there is one condition here that either W ≤ 50000 or max 1≤ i ≤ n Vi ≤ 500.
so if my Knapsack size is more than 50000 my maximum value of items is limited.
So now I wonder how can I reduce the time Complexity of my algorithm with this condition I thought Knapsack problem depends on the size of knapsack and the number of items so how the value of items can change the change my algorithm?
Knapsack with one additional constraint
940 views Asked by Daniel.V At
1
Instead of creating a table of size
W*n
, where each entryD[x][i]
indicates the best value (highest) you can get with at mostx
weight using the firsti
items, use the table where nowD[x][i]
is the minimal weight needed to get to value ofx
, using the firsti
elements:When you are done, find
max{ x | D(x,n) <= W) }
- this is the highest value you can get, using at mostW
weight, and is done by a linear scan of the last line of the DP matrix.Checking which variation you need is done by a single scan of the data.