'0/1 Knapsack problem - Need solution for float/double values
Looking for solution/code for 0/1 Knapsack problem where the input weights and values are float/double instead of int.
Eg:
double val[] = { 8.2, 6.8, 6.5, 6.2, 5.9, 5.5, 5.4, 5.2, 5.1, 5 };
double wt[] = { 13, 7.3, 6.7, 10.7, 7, 8.5, 12.1, 8, 10.7, 7.5 };
Solution 1:[1]
First of all, if only values (but not weights) are doubles then it should be no problem (assuming you are using the typical 2D table method). If we have weights as doubles then one workaround is to treat these doubles as integers, by multiplying all weights by 10 to the power of (the min number of digits needed to make all weights integers), which is 1 in your example because all numbers use at most one digit for precision. The overall time complexity of this solution becomes O(N.C.10^(D)) where N is the number of items, C is the max possible capacity, and D is the max number of digits needed as precision.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | subspring |
