01 knapsack problem-Dynamic programming algorithm

Source: Internet
Author: User

Turn https://www.cnblogs.com/Christal-R/p/Dynamic_programming.html

First , the problem description: There are n items, they have their own weight and value, a given capacity of the backpack, how to make the backpack loaded items have the maximum value of the sum?

Second, the general idea: according to the dynamic planning problem-solving steps (abstract problems, building models, finding constraints, judging whether to meet the optimality principle, finding large problems and small problems of recursive relations, filling out, looking for solutions to the composition) to find the 01 knapsack problem of the optimal solution and composition, and then write code implementation;

Three, the principle and process of dynamic programming:

Eg:number=4,capacity=8

I

1

2

3

4

W (volume)

2

3

4

5

V (value)

3

4

5

6

1 , principle

The dynamic programming is similar to the divide-and-conquer method, which divides the big problems into small problems, solves the problem by finding the recursive relationship between the big problems and the small problems, and finally achieves the effect of solving the original problems. But the difference is, divide and conquer in sub-problem and sub-sub-problem and so on have been repeated calculation many times, and dynamic planning has memory, by filling out the table to all the solved sub-question answer record, in the new problem need to use the sub-problem can be directly extracted, to avoid repeated calculations, thus saving time, So after the problem satisfies the optimality principle, the core of solving the problem with the dynamic plan is to fill in the form, complete the table, and find the optimal solution.

2 , Process

A) The knapsack problem is abstracted (X1,X2,...,XN, where Xi takes 0 or 1, which means I select or not), vi denotes the value of the item I, WI indicates the volume of the item I (weight);

b) Establish a model, that is to ask Max (V1X1+V2X2+...+VNXN);

c) Constraint conditions,w1x1+w2x2+...+wnxn<capacity;

d) Definition V (i,j): Current backpack capacity J, the value of the best combination of the first I items;

E) optimality principle is the basis of dynamic programming, the optimality principle refers to the "multi-stage decision-making process of the optimal decision-making sequence has such a nature: regardless of the initial state and the initial decision, for the previous decision-making of a State, the subsequent stages of the decision sequence must constitute the optimal strategy." Determine whether the problem satisfies the optimality principle, using contradiction proof:

Hypothesis (X1,X2,...,XN) is the optimal solution of the 01 knapsack problem, then there is (X2,X3,...,XN) the optimal solution of its sub-problem,

Hypothesis (Y2,y3,...,yn) is the optimal solution for sub-problems of the above problems, then there should be (V2y2+v3y3+...+vnyn) +v1x1 > (V2X2+V3X3+...+VNXN) +v1x1;

and (V2X2+V3X3+...+VNXN) +v1x1= (V1X1+V2X2+...+VNXN), there is (V2y2+v3y3+...+vnyn) +v1x1 > (V1X1+V2X2+...+VNXN);

This formula (X1,y2,y3,...,yn) is the best solution for the 01 knapsack problem, which is contradictory to the optimal solution of the 01 knapsack problem in the first hypothesis (X1,X2,...,XN), so 01 knapsack problem satisfies the optimality principle;

f) Looking for a recursive relationship, there are two possibilities in front of the current product:

First, the capacity of the package is smaller than the volume of the commodity, can not fit, the value at this time and the value of the former I-1 is the same, that is V (i,j) =v (I-1,J);

Second, there is enough capacity to install the product, but it does not necessarily reach the current optimal value, so in the assembly and not to choose between the best one, that is V (i,j) =max{V (i-1,j), V (i-1,j-w (i)) +v (i)}

where V (i-1,j) is not installed, V (i-1,j-w (i)) +v (i) indicates that the first commodity is installed, the backpack capacity is reduced by w (i) But the value is increased by V (i);

A recursive relationship can be derived from this:

1) j<w (i) V (i,j) =v (i-1,j)

2) j>=w (i) V (i,j) =max{ V (i-1,j),V (i-1,j-w (i)) +v (i) }

g) Fill in, first initialize boundary conditions, V (0,j) =v (i,0) = 0;

h) and then a line of filling,

1) such as, I=1,j=1,w (1) =2,v (1) = 3, there is j<w (1), so V () =v (1-1,1) = 0;

2) also such as i=1,j=2,w (1) =2,v (1) = 3, there is j=w (1), so V () =max{V (1-1,2), V (1-1,2-w (1)) +v (1)}=max{0,0+3}=3;

3) So go on, fill in the last One, I=4,j=8,w (4) =5,v (4) = 6, with J>w (4), so V (4,8) =max{V (4-1,8), V (4-1,8-w (4)) +v (4)}=max{9,4+6}=10; So fill out the table as:

1 void Findmax ()//Dynamic planning 2 {3     int i,j; 4     //fill     out 5 for (i=1;i<=number;i++) 6     {7 for         (j=1;j<= capacity;j++) 8         {9             if (J<w[i])//packaging does not enter the                 v[i][j]=v[i-1][j];12             }13             else//can be installed 14             {                 if (V[i-1][j]>v[i-1][j-w[i]]+v[i])//No value large (                     v[i][j]=v[i-1][j];18                 }19                 The optimal solution of i-1 items before else//and the sum of the value of the item I is greater.                 {                     v[i][j]=v[i-1][j-w[i]]+v[i];22                 }23             }24         }25     }26}

i) Fill out the form, the optimal solution is V (number,capacity) =v (4,8) = 10, but do not know what the solution is composed of products, so according to the optimal solution backtracking to find the composition of the solution, according to the principle can be the following method of seeking solutions:

1) v (i,j) =v (I-1,J), the description does not select the item I, then return to V (I-1,J);

2) V (i,j) =v (i-1,j-w (i)) +v (i) in real time, stating that I have a commodity, which is part of the optimal solution composition, and then we have to go back to the product before loading, that is, back to V (I-1,j-w (i));

3) until the end of the i=0, the composition of all the solutions will be found.

j) As in the example above,

1) The optimal solution is V (4,8) = 10, while V (4,8)!=v (3,8) has V (4,8) =v (3,8-w (4)) +v (4) =v (3,3) +6=4+6=10, so the 4th item is selected and returned to V (3,8-w (4)) =v (3,3);

2) There is V (3,3) =v (2,3) = 4, so the 3rd item is not selected, back to V (2,3);

3) while V (2,3)!=v (1,3) has V (2,3) =v (1,3-w (2)) +v (2) =v (1,0) +4=0+4=4, the 2nd item is selected and returned to V (1,3-w (2)) =v (1,0);

4) There is V (1,0) =v (0,0) = 0, so the 1th item is not selected;

K) to this, 01 knapsack problem has been solved, the use of dynamic planning to solve this problem is the efficiency of filling out this table efficiency, so the dynamic planning time efficiency of O (number*capacity) =o (n*c), due to the use of two-dimensional array storage sub-problem solution, so the space efficiency of dynamic planning is O (n *C);

1 void FindWhat (int i,int j)//Find the composition of the solution 2 {3     if (i>=0) 4     {5         if (V[i][j]==v[i-1][j])//Equal description not installed 6         {7             I tem[i]=0;//global variable, tag not checked 8             FindWhat (I-1,J); 9         }10         else if (j-w[i]>=0 && V[i][j]==v[i-1][j-w[i]] +v[i]) Each         {             item[i]=1;//mark has been selected             for FindWhat (I-1,j-w[i]);//back to the location before the package         }15}16     }

3. Space optimization

L) space optimization, the value of each V (i) (j) Change is only related to V (i-1) (x) {X:1...J}, V (i-1) (x) is the value of the previous I-loop preservation;

Therefore, V can be reduced to a one-dimensional array, so as to achieve the purpose of optimizing space, the state transition equation to b (j) = Max{b (j), B (J-w (i)) +v (i)};

Also, the state transition equation, each derivation of V (i) (j) is deduced by V (i-1) (J-w (i)), so the scanning order of J in a one-dimensional array should be from large to small (capacity to 0), the value of the previous cycle will be modified, resulting in errors.

m) is also illustrated in the above example by i=3:

1) i=3,j=8,w (3) =4,v (3) = 5, with J>w (3), B (8) =max{b (8), B (8-w (3)) +v (3)}=max{b (8), B (4) +5}=max{7,4+5}=9;

2) J-i.e. J=7, with J>w (3), then B (7) =max{b (7), B (7-w (3)) +v (3)}=max{b (7), B (3) +5}=max{7,4+5}=9;

3) J-i.e. J=6, with J>w (3), then B (6) =max{b (6), B (6-w (3)) +v (3)}=max{b (6), B (2) +5}=max{7,3+5}=8;

4) J-i.e. J=5, with J>w (3), then B (5) =max{b (5), B (5-w (3)) +v (3)}=max{b (5), B (1) +5}=max{7,0+5}=7;

5) J-i.e. j=4, with J=w (3), then B (4) =max{b (4), B (4-w (3)) +v (3)}=max{b (4), B (0) +5}=max{4,0+5}=5;

6) J-that is j=3, there is j<w (3), continue to access the array will be out of bounds, so the operation stopped, B (0) to B (3) The value of retaining the last round (i=2) value unchanged, into the next cycle i++;

If J does not reverse order and uses the positive sequence j=0...capacity, as shown, when J=8 should have B (8) =b (8-w (3)) +v (3) =b (4) +5, but at this time B (4) has been modified at the time of J=4, the original B (4) = 4, now B (4) = 5, so the calculation of B (8) =5+5=10, obviously this is not in accordance with the correct answer; So the value after the one-dimensional array requires the previous value of the operation and then change, if the positive sequence is convenient, then the previous value will be modified to cause the subsequent data error; Modify the following data before modifying the previous data, this kind of situation will not be wrong;

1 void Findmaxbetter ()//dynamic planning after optimized space 2 {3     int i,j; 4 for     (i=1;i<=number;i++) 5     {6 for         (j=capacity;j >=0;j--) 7         {8             if (B[j]<=b[j-w[i]]+v[i] && j-w[i]>=0)//Two dimensional variable one-dimensional 9             {ten                 b[j]=b[j-w[i]]+v [i];11             }12         }13     }14}

N) However, although the space of dynamic programming is optimized, the method can not find the solution composition of the optimal solution, because the dynamic planning of the early-searching solution composition must be determined in the premise of the optimal solution to find the composition of the solution, and the optimized dynamic planning only used one-dimensional array, the previous data has been overwritten, so no way to find So the two methods have their advantages.

Four, brute force method test:

1) Brute force method is the simplest and easiest way to solve the 01 knapsack problem, but the efficiency is very low.

2) (X1,X2,...,XN) where xi=0 or 1 means that the commodity of the first I is selected or not selected, there are N (n-1)/2 possibilities;

3) The simplest way is to put all the way to the goods are listed, and finally to determine whether this method satisfies the package conditions, and by comparison and record to find the optimal solution and composition (if satisfied then record the value and the way of loading, the next time the loading method is better than this one, then update the record, So down to the end will find the optimal solution, while the solution composition is also found);

4) n Products, a total of n (n-1)/2 kinds of possible, so the efficiency of brute force method refers to the number of levels, visible efficiency is very low;

5) Brute force method is not recommended for low efficiency, but it can be used to test the correctness and feasibility of a small scale dynamic programming problem, such as the output is visible, the solution of 01 knapsack problem with dynamic programming is feasible:

Five, Summary:

For the 01 knapsack problem, it is consistent with the brute force method and the optimal solution and composition of the solution obtained by dynamic programming, so it is feasible to solve such problems by dynamic programming. Dynamic programming efficiency is linear, brute force method efficiency is exponential, combined with the above content and theoretical knowledge can be drawn, the solution of this problem is more suitable for dynamic programming than brute force method. For the lack of dynamic planning is the space overhead, the data storage is used to two-dimensional array; Well, the solution of the current problem is only related to the solution of the sub-problem of the previous layer, so the dynamic programming space can be optimized to convert the space efficiency from O (n*c) to O (c), unfortunately, although the space is optimized, But the optimal solution can only be obtained after optimization, and the exploration method of the solution composition has been destroyed when the method is running; After all, dynamic planning and optimization of dynamic planning have advantages and disadvantages, can choose different ways according to the needs of the actual problem.

Vi. extension:

What types of problems can dynamic planning solve?

The original problem to be solved is more difficult, but this problem can be broken down into small problems, and the solution of small problems is very easy to obtain, if only by using recursive method to solve the original problem, then the use of the idea of separate treatment, dynamic programming with memory, the solution of sub-problems are recorded, So as not to repeat the calculation in the process of recursion, thus reducing the amount of computation.

01 knapsack problem-Dynamic programming algorithm

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.