Knapsack with continuous (non distinct) constraint

后端 未结 7 542
南方客
南方客 2021-02-03 12:05

I watched Dynamic Programming - Kapsack Problem (YouTube). However, I am solving a slightly different problem where the constraint is the budget, price, in double, not integer.

7条回答
  •  有刺的猬
    2021-02-03 12:56

    Have your looked at this. Sorry, I don't have comment privilege.

    Edit 1

    Are you saying constraint is the budget instead of knapsack weight? This still remains a knapsack problem.

    Or are your saying instead of Item Values as Integers(0-1 knapsack problem) your have fractions. Then Greedy approach should do fine.

    Edit 2

    If I understand your problem correctly.. It states

    We have n kinds of items, 1 through n. Each kind of item i has a value vi and a price pi. We usually assume that all values and pricess are nonnegative. The Budget is B.

    The most common formulation of the problem is the 0-1 knapsack problem, which restricts the number xi of copies of each kind of item to zero or one. Mathematically the 0-1-knapsack problem can be formulated as:

             n
    maximize E(vi.xi)
             i=i
    
               n
    subject to E(pi.xi) <= B,         xi is a subset of {0,1}
               i=1
    

    Neo Adonis's answer is spot on here.. Dynamic programming wont work for arbitrary precision in practice.

    But if you are willing to limit the precision say to 2 decimal places.. then carry on as explained in video.. your table should look something like this..

    +------+--------+--------+--------+--------+--------+--------+
    | Vi,Pi| 0.00   | 0.01   | 0.02   | 0.03   | 0.04 ...    B   |
    +------+--------+--------+--------+--------+--------+--------+
    |4,0.23|        |        |        |        |        |        |
    |2,2.93|        |        |        |        |        |        |
    |7,9.11|        |        |        |        |        |        |
    | ...  |        |        |        |        |        |        |
    | Vn,Pn|        |        |        |        |        | answer |
    +------+--------+--------+--------+--------+--------+--------+
    

    you can even convert real numbers to int as you have mentioned.

    Yes, the range of values is very large, and you also have to understand knapsack is NP-complete, i.e, there is no efficient algorithm to solve this. only pseudo polynomial solution using DP. see this and this.

提交回复
热议问题