1. Dynamic planning
The dynamic planning method is similar to the method. The English "dynamic programming", the programming here is not the meaning of the program, but a table method. The original problem is solved by combining the sub-problem solutions. The sub-problem is divided into subproblems that are separated by different sub-problems and solved recursively, then they are combined to find the solution to the original problem. On the contrary, dynamic planning is used to overlap subproblems, that is, different subproblems have common subproblems, and the subproblems are solved recursively, divide it into smaller subproblems, dynamic planning,Each subproblem is solved only once and saved in the table.So that you do not need to re-calculate each time you solve the sub-problem. Two equivalent implementation methods: top-down and bottom-up methods with memos.
Dynamic planning is often used to solve optimization problems. This type of problem can have many feasible solutions. Each solution has a value. We hope to find the optimal value. We usually design a dynamic planning algorithm in the following four steps:
1. Portray the features of an optimal solution.
2. Recursively define the value of the optimal solution.
3. Calculate the value of the optimal solution. Generally, the bottom-up method is used.
4. Use the calculated information to construct the left and right solutions.
2. Steel strip cutting
The following describes several different algorithms.
This algorithm has a high time complexity and can be expressed using a recursive tree:
<Span style = "font-size: 18px;">/*** @ param p: price array corresponding to each length * @ param n: the length of the steel bars to be split * this split method has a lot of repetitive calculations not as good as 1 3 and 3 1. This should be a split method, but * splitting twice will reduce the running efficiency, especially when n is long, the time increases a lot * every kind of top-down recursion, can all be written as a cycle for solving the problem starting from the petty Capital. Here we will not overwrite */public int cutRod (int [] p, int n) {if (n = 0) return 0; int price =-100; for (int I = 1; I <= n; I ++) {if (price <p [I] + cutRod (p, n-I) price = p [I] + cutRod (p, n-I);} return price ;}</span>
The following two types of motion regulation algorithms are used:
/** Use the dynamic planning method to solve * to solve the problem that the calculated values in the previous layer are still calculated, * When an optimal length is obtained, it is recorded in the array. If an existing value exists in the array is checked, it is returned. ** @ Param p: price of the steel bars of each length * @ param n: length of the steel bars to be cut * @ param r: save the value of the optimal solution for the obtained subproblem (a memorandum) */public int memoizedCutRod (int [] p, int n, int r []) {if (n = 0) {r [0] = 0; return r [0];} if (r [n]> = 0) return r [n]; int price =-100; for (int I = 1; I <= n; I ++) if (price <p [I] + memoizedCutRod (p, n-I, r) price = p [I] + memoizedCutRod (p, n-I, r); r [n] = price; return price ;} /** The second solution of dynamic planning is the bottom-up method, that is, recursive loop implementation. The * optimal solution is obtained from the bottom-down order. This method is more intuitive and recursive, the current solution depends on the solution of the preceding problem * Compared with the previous two methods, the method divides the array s for the optimal solution and the optimal solution when * a is required for each cut. * @ Param p * @ param n * @ param r * @ param s: storage optimal solution */public int bottomUpCutRod (int p [], int n, int r [], int s [], int a) {r [0] = 0; int I; for (int j = 1; j <= n; j ++) {int price =-100; for (I = 1; I <= j; I ++) {if (price <p [I] + r [j-I]) {price = p [I] + r [j-I]; // segmentation problem. The left half side is I, and the right half side is n-is [j] = I ;}} if (s [j]! = J) // In fact, it is split only once at a time, separated from I, followed by the n-I optimal solution, n-iprice-= a; // It is a subproblem, the cut cost has been deducted. This is the advantage of recursion and r [j] = price; // bottom-up} return r [n];} public void printSolution (int [] s, int n) {while (n> 0) {System. out. print (s [n] + ""); n = n-s [n] ;}}
Beauty Of algorithms (7) Dynamic planning Of strip segmentation Matrix chain multiplication longest common subsequence Optimal binary tree