java implementation of the knapsack problem dynamic programming

 

Solving steps:

1) model

2) Find constraints : only three commodity, a weight of the backpack 10

3) to find recurrence relations

V (i): Value

W is ( I ) : Weight

V ( i, J ): the current capacity of the backpack J , before the i th best combination value corresponding to the article

For current product there are two cases:

① current product weight is greater than the weight of the backpack remaining, not put into. Then V ( I-. 1, J ) = V ( I, J ) ;

② current product weight is less than the remaining weight backpack, but not necessarily installed can reach the optimal solution, and the refill is not installed between a selected. Max {v (i-1, j), v (i, jw (i)) + v (i)}

Wherein V (I-. 1, J) : indicates not installed; V (I, JW (I)) + V (I) represents installed, backpack remaining weight reduction w (i), but the value increases v (i). It follows the recurrence relation:

j(i)>w(i):  v(i,j)=v(i-1,j)

J(i)<w(i):  max{v(i-1,j),v(i-1,j-w(i))+v(i)}

Note: Why put the case can go to solving such as dynamic programming there is a principle of optimality, v (i-1, JW (i)) is a condition caused by earlier decisions, is simply the present article can be put to, but I need to make a comparison, the previous comparison was already out of the optimal solution will not be because the backpack adds new weight has changed, from a bag and go in and hold the two cases selected maximum value. Behind decisions must constitute an optimal strategy. Two cases were compared, the best results.

 

Guess you like

Origin www.cnblogs.com/had1314/p/11210714.html