First, let's take a look at the four steps of dynamic planning:
1. Identify the nature of the optimal solution and characterize its structure;
2. Recursive definition of the optimal solution;
3. Describe the optimal value in a bottom-up manner;
4. The optimal solution is constructed according to the information obtained when the optimal value is calculated.
The improved dynamic Programming algorithm: Memo method, is to describe the optimal value in the top-down way, for the dynamic planning method and the memo method, the use of the two are as follows:
In general, using a dynamic programming algorithm is better than using a memo method when all sub-problems of a problem are at least once solved. At this point, the dynamic planning algorithm does not have any redundant computations. At the same time, for many problems, it is often possible to reduce the computational time and space requirement of the dynamic programming algorithm by using the table access method of its rules. When some sub-problems in sub-problem space can not be solved, it is advantageous to use the memo method, because it can be seen from the control structure that the method solves only those problems that really need solving.
For the dynamic programming algorithm, we must clear two basic elements, these two elements for the design of the algorithm to solve specific problems, whether the choice of dynamic programming algorithm is instructive:
1 The validity of the algorithm depends on the optimal substructure property of the problem itself : The first step of the design algorithm is usually to characterize the structure of the optimal solution. When the optimal solution of the problem contains the optimal solution of the sub-problem, it is said that the problem has the optimal substructure property. The optimal substructure property of the problem provides an important clue that the problem can be solved by using the dynamic programming algorithm .
In the problem of optimal order of matrix products, it is noted that if a1a2 ... An optimal full-parenthesis method between AK and Ak+1 is broken, then the sub-chain a1a2a3 can be determined ... The complete parenthesis method of AK and ak+1ak+2...an is also optimal, that is, the problem has the optimal substructure property. In the analysis of the optimal substructure property of the problem, the method used is universal. First of all, it is assumed that the sub-problem derived from the original problem is not the optimal solution, and then we try to show that it can construct a better solution than the original problem in this hypothesis, which leads to the contradiction.
In the dynamic programming algorithm, the optimal solution of the whole problem is constructed by using the optimal sub-structure property of the problem and the optimal solution of the recursive sub-problem from the bottom-up method. The space scale of the sub-problem is smaller in algorithm investigation. For example, in the optimal order problem of the product of the proof, the sub-problem space consists of all the unused sub-chains of the Matrix chain. The number of all unused strands is O (n*n), thus the space size of the sub-problem is O (n*n)
2 A dynamic programming algorithm can be used to solve problems should have another basic element is the overlap of sub-problems. When a recursive algorithm is used to solve this problem from top to bottom, each sub-problem is not always a new problem, and some sub-problems are counted repeatedly. The dynamic programming algorithm takes advantage of the overlapping nature of this seed problem, solves each sub-problem only once, then saves it to a table, and when it needs to solve the problem again, it simply uses the constant time to look at the result. Often, the number of sub-problems increases with the size of the problem. Therefore, the use of dynamic programming algorithm usually only requires polynomial time, so as to obtain a higher efficiency of solving problems.
The following is a Java implementation of solving the 0,1 knapsack problem using the Dynamic programming algorithm:
Package Dynamic_planning;public class Package0_1 {private int c; Backpack Total capacity private int n; Number of items in backpack private int []v; The value of each item in the backpack must be one by one the corresponding private int []w; The weight of each item in the backpack must be one by one the corresponding private int [][]m; Space public package0_1 () {c=20;n=6;v=new int[]{12,45,23,14,5,27};w=new int[]{2,9,8,3,7,6};m=new int[n][c used by dynamic solver ];} Public package0_1 (int c,int n,int []v,int []w,int] {][]m;} Bottom-up solution child problem public void knapsack () {for (int i=0;i<n;i++) m[i][0]=0;for (int. s=1;s<=n;s++) {for (int i=n-s;i>=0; i--) {for (int j=1;j<c;j++) {if (i==n-1) {if (J<w[i]) M[i][j]=0;else if (J>=w[i]) m[i][j]=v[i];} Else{if (J<w[i]) M[i][j]=m[i+1][j];else if (J>=w[i]) m[i][j]=m[i+1][j]> (M[i][j-w[i]]+v[i])? M[I+1][J]:(M[i] [J-w[i]]+v[i]);}}}} The loop solves the 0,1 vector group, 0 means the corresponding position is not loaded in the backpack, 1 is loaded into the backpack public void TraceBack () {for (int k=1;k<=n;k++) System.out.print (k + "); SYSTEM.OUT.PRINTLN (); int j=c-1;for (int i=0;i<n-1;i++) {if (M[i][j]==m[i+1][j]) System.out.print (0+ ""); else{ System.out.print (1+ ""); j-=W[i];}} if (j>=w[n-1]) System.out.print (1); else System.out.print (0);} public static void Main (string[] args) {package0_1 package0_1=new package0_1 ();p ackage0_1.knapsack ();p ackage0_1. TraceBack ();}}
Solving 0,1 knapsack problem with dynamic programming algorithm