DP solving the optimization principle of complete knapsack problem

1. The formal description of complete knapsack problem

  A knapsack problem is completely classical class DP (Dynamic Programming, dynamic programming) problem, the problem is described as follows:

  There are n kinds of weight and value respectively, W I , V I article, selected from these items does not exceed the total weight W of the articles, require that all programs selected value V I maximum sum. It is worth noting that each item can choose unlimited.

  Suppose the i-th item selected K i th, K i ∈N , the

  The objective function is:

max{Σkivi}

  Constraints are:

Σkiwi <= W

 

2. trivial solution complete knapsack problem

  Firstly numbers 1 ~ n for all types of goods.

  Recursion bottom-up method.

  Set function F. ( I , j ) represents the number ~. 1 I selected article type, the maximum permitted weight of charged j maximum total value backpack, can be obtained.

 

  First, consider the case where the reference: an article selected from 0, i.e., i = 0, this case:

F(i,j) = F(0,j) = 0

 

  Recursive up. From this number ~. 1 I selected article class, the optimal solution is generated from the section is dependent on ~. 1 ( I. 1- optimal solution to the current generated decision).

  To make decisions, to be selected from the k i items (k can be zero), divided into the following two situations:

  1 . If the weight of an article W I does not exceed the permissible weight of the backpack j, may be independently selected from 0, ..., k of the article a, K W I does not exceed the permissible weight j. Finally, choose Select All from the maximum total value of the results, as the current optimal solution.

F(i,j) = max{ F(i-1,j - k × wi) + k × vi      | (k>=0,kwi<=j) 

  2 . If the weight of an article W I exceeds the permissible weight of the backpack j, can not be placed in the current article:

F(i,j) = F(i-1,j)

  Above reveals two recursive transfer relationship between the current state and the previous state. Coupled with the base case, we find that the algorithm has been closed, you can write a program to achieve.

 

The core program

Input: Number Item type n, the maximum allowed weight of the backpack t, the i-th item weight w [i], the value of v [i].

Output: Maximum value dp [n] [t].

Initialization: Clear dp Array

for(int i=1;i<=n;++i) {
    for(int j=0;j<=t;++j) {
        if(j<w[i]) {
            dp[i][j]=dp[i-1][j];
        } else {
            for(int k=0;k*w[i]<=j;++k) {
                dp[i][j]=max(dp[i][j],  max(dp[i-1][j], dp[i-1][j-k*w[i]]+k*v[i]));
            }
        }
    }
}

  Easy to see that the asymptotic complexity of O (n × t × w), three of complexity, so the program can solve the problem of very limited size. But this problem is not unsolvable, as the title of this section suggests, ordinary solution means that there is a large space that can be optimized.

  

3. The use of the data dependency - time complexity optimization

  Observed for ( J ) for the circulation and ( k ) of the cycle, there is data speculation two cyclic correlation, for example: F (i-1, j ) in the case of k selected article, and F (i-1, j - W I ) selecting a k-1 are identical, the recursive estimation F (i-1, j) of the k> = 1 in the calculation portion F. (I-1, J- W I time) has been seeking a, k cycle will continue to repeat the calculation, it is possible to merge into the circulating loop j k.

  To test this idea, the state transition equation is derived as follows:

Title design:

  F (i, j) = max {F (i-1, j - k ×  w i ) + k ×  v | (k> = 0) }   ...... ①

When considering == 0 K, F. (. 1-I, J - K ×  W I ) + K ×  V I reduced to F (i-1, j) , so

  F(i,j) = max(F(i-1,j), max{F(i-1,j - k × wi) + k × vi   |(k>=1)} )

In order to verify the previous speculation, substitution k = k + 1, is equivalent to the modification k> = 0:

  F(i,j) = max(F(i-1,j), max{F(i-1,j - (k+1) × wi) + (k+1) × vi   |(k>=0)})

     = Max (F (i-1, j) max {F (i-1, (j -  w i )  - k ×  w i ) + k ×  v i v i | ( k> = 0) }) ... ... ②  

Conclusion is still not clear, the underlined portion of the formula ② mentioned later max {}, found:

  F (i, j) = max (F (i-1, j) max {F (i-1, (j -  w i )  - k ×  w i ) + k ×  v i   | ( k> = 0) }  v i  ) 

① known contrast, the blue portion of the n-type equivalent to F. (I, J -  W I ).

  F (i, j) = max (F (i-1, j),  F (i, j -  w i )  +  v i )

This is the ultimate expression, you can see the success erased k.

 

  According to the state derived transfer programming equation, the time complexity from three straight down to the second, we see once again the power of DP.

  It is noted that since F (i-1, j) depends on the F. (. 1-I, J- W I calculation result), j to be incremented traverse, in order to ensure the correctness of the calculation.

for(int i=1;i<=n;++i) {
    for(int j=0;j<=t;++j) {
        if(j<w[i]) {
            dp[i][j]=dp[i-1][j];
        } else {
            dp[i][j]=max(dp[i-1][j], dp[i][j-w[i]]+v[i]);
        }
    }
}

 

4. dp reduce array dimensions - spatial complexity optimization

  Before optimization, dp array space complexity is O (n × t) is.

  Noting the array dp, dp [i] of each element depends only on the value of the row before the row dp [i-1], without depending on the dp [i-2], ......, dp [0] lines. This inspired us to only one line dp storage array, and then update the data in the line "in situ."

  There are still two cases:

    1 . For j <w [i], dp is not necessary to update the line.

    2 . For other non-1 . Case, the line needs to be updated dp, dp [J] = max ( dp [J] , dp [JW [i]] + v [i]); bold reflect the "in situ" update strategy.

  

for(int i=1;i<=n;++i) {
    for(int j=<w[i];j<=t;++j) {
        dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
    }
}

  Because the situation 1 . Presence, j values must w [i] is the starting point of the interval.

  Thus a reduced space complexity O (n)).

 

5 Conclusion

  First described the knapsack problem completely, and then analyzes the most intuitive trivial solution, solution found by the ordinary data correlation between the loop and the state transition equation derived optimized, so that the conclusions derived reduced time complexity of O (n × t). Next, from another angle - dp space complexity analysis array dimensionality reduction optimize the spatial complexity is reduced to O (n). Integrated spatial and temporal aspects, come to DP optimization method for solving such problems.

 

Guess you like

Origin www.cnblogs.com/sci-dev/p/11907478.html