Dynamic Programming-Algorithm optimization for 0-1 knapsack problem

Source: Internet
Author: User

Simple description

The 0-1 knapsack problem is described as follows:

There is a backpack with a capacity of V, and some items. These items have two properties, Volume W and value V, and only one for each item. Ask for the maximum value of the item that is worth as much as possible with this back, and the backpack may not be filled. Because there are two possible cases for each item in the optimal solution, either in the backpack or absent (there are 0 items or 1 in the backpack), we call this problem the 0-1 knapsack problem.

0-1 knapsack Problem state transition equation

Use dp[i][j] to indicate that the top I items in the overall product does not exceed J, the maximum value placed in the backpack. Thus, the state transfer equation can be introduced:

DP[0][J] = 0;

DP[I][J] = Max{dp[i-1][j-v[i]] + w[i],dp[i-1][j]};

The above formula should be well understood, when the volume of item I is less than the current remaining volume, it can be loaded into the backpack, then dp[i][j] = Dp[i-1][j-v[i]]+w[i]. On the other hand, can not be transferred to the backpack, dp[i][j] = dp[i-1][j].

0-1 knapsack problem Implementation algorithm 1
#include <iostream>using namespace std; #define MAXSIZE 100int  w[maxsize];int  v[maxsize];int  maxv;int  n;int  dp[ Maxsize][maxsize];int Max (int a, int  b) {if (A;  b) return  A; else return  B;} int  Main () {CIN >> n >>  maxv; for (int i = 1; I <= n; i++ ) {cin >> w[i] >> V[i]; } for (int i = 0; I <= maxv; i++ ) Dp[0][i] = 0 , for (int i = 1; I <= n; i++ ) {///only if J >= w[i],d P[I][J] To select the maximum value for (int j = MAXV; J >= W[i]; j--) {dp[i][j] = max (Dp[i-1][j], dp[i-1][j-w[i] +  v[ I]); }//When J < W[i], stating that the item I is not able to transfer to the backpack, so dp[i][j] = Dp[i-1][j] for (int j = W[i]-1; J >= 0; j--) dp[i][j] = Dp[i-1][J]; } cout << DP[N][MAXV] <<  Endl; return 0 ;}         

See here, not yet, haha! The following describes the optimization of DP, we found that the DP is a two-dimensional array, in fact, the DP can be fully represented by a one-dimensional array. Why???

0-1 knapsack problem Implementation algorithm 2

Look here: The state transfer equation of the 0-1 backpack can be found dp[i][j] = max{dp[i-1][j-w[i]]+v[i],dp[i-1][j]}, the current state depends only on the remaining volume of the previous state and the current item volume V[i] relationship. According to this feature, we can reduce the DP to one-dimensional dp[j] = max{dp[j],dp[j-w[i]]+v[i]}. From this equation we can find that there are two dp[j], but to differentiate. The dp[j on the left of the equals sign] is the state of the current I, and the dp[j in parentheses on the right is the value in the I-1 state.

So in order to ensure the correct transfer of the state, we need to update the DP[J in the left side of the equals sign] (Dp[j of the current state).

#include <iostream>using namespace std; #define MAXSIZE 100int w[maxsize];int v[maxsize];int MAXV ; int n;int dp[maxsize];int max (int a, int b) {if (a > b) return A; else return B;} int Main () {cin >> n >> maxv; for (int i = 1; I <= n; i++) {cin >> w[i] >>  V[i]; } for (int i = 0; I <= maxv; i++) Dp[i] = 0, for (int i = 1; I <= n; i++) {///only if J >= W[i],dp[j] can Select the maximum value, otherwise dp[j] will not be updated, equal to Dp[i-1][j]. for (int j = MAXV; J >= W[i]; j--) {dp[j] = max (Dp[j], Dp[j-w[i]] + v[i]);} cout << DP[MAXV] < < Endl; return 0;}               

Comparing the above two algorithms can be found that their time complexity is O (N*MAXV), only the spatial complexity of the change. The spatial complexity of the latter has been optimized.

Expand 0-1 knapsack problem

Haha, not yet, continue to 0-1 knapsack problem, if the above problem plus a restriction, the selected item must be filled with a backpack, otherwise output-1.

Given the same two algorithms, their time complexity is the same, but the complexity of space is different.

An O (N*MAXV) algorithm for spatial complexity
#include <iostream>using namespaceSTD; #define MAXSIZE 100intW[maxsize];intV[maxsize];intMaxv;intN;intDp[maxsize][maxsize];int Max (int A, intb) {if (A >b) returnA else return b;} int  Main () {CIN >> n >>  maxv; for (int i = 1; I <= n; i++ ) {cin >> w[i] >> V[i]; }//initialization, when the volume is 0 o'clock, that cannot be loaded, the maximum value is 0 for (int i = 1; I <= n; i++ ) {dp[i][0] = 0 ;}//initialized to-1, indicating that no fill for (int i = 0; I <= N; i++ ) for (int j = 1; J <= Maxv; j + + ) dp[i][j] = 1 ; for (int i = 1; I <= n; i++ ) {for (int j = MAXV; J >= W[i]; j--) {//dp[i-1][j-w[i]]! = 1 means that the volume is j-w[i] is not filled, so when the volume is J, install w[i] must not fill//dp[i-1][j-w[i]] + v[i] > dp[i-1][ J] means the item I is loaded and the total value is greater than the total value of the previous i-1 items if (dp[i-1][j-w[i]]! =-1 && dp[i-1][j-w[i]] + v[i] >= dp[i-1  ][J]) dp[i][j] = Dp[i-1][j-w[i]] +  v[i];} for (int j = W[i]-1; J >= 1; j--) dp[i][j] = Dp[i-1][J]; } cout << DP[N][MAXV] <<  Endl; return 0 ;}         
An O (MAXV) algorithm for spatial complexity
#include "stdafx.h"#include <iostream>using namespace std; #define MAXSIZE 100int w[maxsize];int  V[maxsize];int maxv;int n;int dp[maxsize];int max (int a, int b) {if (a > b) return A; else Retu RN b;} int Main () {cin >> n >> maxv; for (int i = 1; I <= n; i++) {cin >> w[i] >>  V[i]; }//initialization, when the volume is 0 o'clock, that cannot be loaded, the maximum value is 0 dp[0] = 0;//initialized to-1, indicating that no full for (int j = 1; J <= Maxv; j + +) dp[j] = 1; for (int i = 1; I <= n; i++) for (int j = MAXV; J >= W[i]; j--) {if (Dp[j-w[i]]! =-1 && dp[j-w[i ]] + v[i] >= dp[j]) dp[j] = Dp[j-w[i]] + v[i];} cout << DP[MAXV] << Endl; return 0;}  

From the above algorithm, we found that the state transfer equation and the 0-1 knapsack problem state transfer equation is the same drop, just a little change in the initial state of the preliminary.

Oh, to here 0-1 knapsack problem is finished first, will continue to introduce more complex knapsack problem in the back.

PS: Take a step, learn a step, summed up a step, the road will not be too far, the goal will not be unreachable.

Dynamic Programming-Algorithm optimization for 0-1 knapsack problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.