How to optimize solution to the 0/1 knapsack?

630 views Asked by At

The standard knapsack problem solution is O(nW) where we will increment the weight +1 at a time to get to the solution.

Is there any approach to the knapsack problem that does not require incrementing weight +1 at a time.

e.g. One way that I can think of is to divide all the numbers by its common denominator

Capacity = 100 weights = [5, 10, 20] -> Capacity = 20 weights = [1, 2, 4]

1

There are 1 answers

0
Jordi Vermeulen On

Incrementing the weight in steps of 1 is only necessary for a bottom-up dynamic programming implementation. If you implement it top-down, you can just do a recursive call while subtracting the weight of the current item from the remaining capacity.