knapsack problem (knapsack problem): Given a set of items, each item has its own weight and price, within a limited total weight, we choose how to make the total price of the item the highest. that is, if the total weight does not exceed W, can the total value reach v?
Basic ideas
This is the most basic knapsack problem, characterized by: only one piece of each item, you can choose to put or not put.
Define the state with sub-question: F[i][v] Indicates the maximum value that the first I item can get in a backpack with a capacity of V. Then its state transition equation is: f[i][v]=max{f[i-1][v], F[i-1][v-c[i]]+w[i]}.
This equation is very important, "put the first I item into a backpack with a capacity of V" This sub-problem, if only consider the article I item strategy (put or not put), then can be converted into a only 1 items in the first I-related issues. If I do not put the article I, then the problem is converted to "the first I-1 items into the backpack of the capacity V", the value of f[i-1][v]; If you put the item I, then the problem translates to "the first I-1 items into the remaining capacity of v-c[i] backpack", the maximum value can be obtained is F [ I-1][v-c[i]] plus the value obtained by placing the item in article I w[i].
Public class knapsack{ public static void knapsack (int[] V,&NBSP;INT[]&NBSP;W,&NBSP;INT&NBSP;C,&NBSP;INT[][]&NBSP;M) { /** v[] w[] c is an array of values, weights, and backpack capacities, respectively &NBSP;&NBSP;M[I][J] Indicates that there are i~n items, the backpack capacity is the maximum value of J. */ int n = v.length-1; int jmax = math.min (w[n]-1, c); for (int j = 0; j <= jmax; j++) m[n][j] = 0; //when w[n]>j has m[n][j]=0 //m[ n][j] indicates that only n items, the capacity of the backpack is J maximum value for (int l = w[n]; l <= c; l++) m[n][l] = v[n]; //when w[n]<=j has m[n][j]=v[n] //recursive call m[][] Other values until m[0][c] is found for (int i = n-1; i >=1; i--) { jmax = math.min (w[i]- 1,C); for (int k = 0; k <=jmax; k++) m[i][k] = m[i+1][k]; &nbSp; for (int h = w[ i]; h <= c; h++) m[i][h] = math.max (M[i+1][h],m[i+1][h-w[i]]+v[i]); } m[0][c] = m[1][c]; if (c >= w[0]) m[0][c] = math.max (m[0][c],m[1][c-w[0]]+v[0]); system.out.println ("bestw =" +m[0][c]); } public static void traceback ( INT[][]&NBSP;M,&NBSP;INT[]&NBSP;W,&NBSP;INT&NBSP;C,&NBSP;INT[]&NBSP;X) {// The optimal solution   based on the optimal value; int n = w.length-1; for (int i = 0; i<n;i++) if (M[i][c] == m[i+1][c]) x[i] = 0; else{ x[i] = 1; c -= w[i]; } x[n] = (m[n][c]>0)?1:0; } public static void main (String[] args) { //Test int[] ww = { 2,2,6,5,4}; int[] vv = {6,3,5,4,6}; int[][] mm = new int[11][11]; knapsack (vv,ww,10,mm); int[] xx =new int[ww.length]; traceback (MM,WW,10,XX); for (int i = 0;i<xx.length;i++) system.out.println (Xx[i]); }}
Test Result: bestw=15 1 1 0 0 1
650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "alt=" J_0003.gif "/>
This article is from a "stroll," blog, please be sure to keep this source http://macxiao.blog.51cto.com/9606147/1588040
0-1 knapsack problem (DP)