Dynamic Planning of algorithm notes

Source: Internet
Author: User

Dynamic Planning of algorithm notes

1To tell the story about dynamic planning.

After reading the MIT algorithm introduction tutorial video, I still have little idea about dynamic planning. Until I see such an article, such a story. A wise king with a gold mine story.

Http://www.cnblogs.com/sdjl/articles/1274312.html

 

Summary:

Reference Baidu encyclopedia's definition of dynamic planning --

The multi-stage process is transformed into a series of single-stage problems, and the relationship between each stage is used to solve them one by one. A new method to solve these process optimization problems is created..

 

2, Differentiated by divide and conquer Law

  • In common: both of them require the original problem to have the optimal sub-structure. They are all sub-problems that divide and conquer the original problem into several small (small to easy-to-solve programs. then combine the solutions of the sub-problem to form the solution of the original problem.

 

  • Implementation methods of separation and Control Law and dynamic planning:

① Divide and conquer methods are usually solved using recursion.

② Dynamic planning is usually usedBottom-up Iteration Method, But can also be usedRecursive top-down solution of Memory Function.

 

  • Main differences between the Division and management method and dynamic planning:

① Divide and conquer Methods regard the subproblems after decomposition as independent of each other.

② Dynamic planning refers to the subproblems after decomposition as interrelated and overlapping parts.

 

3Key Points of Dynamic Planning

(1) optimal sub-structure: when the sub-problem is the best, the parent problem must be the best after optimization and selection, which also becomes the optimization principle;

(2) subproblems overlap-the reason why dynamic planning is used is because complicated problems need to be subdivided and iterated continuously. The subproblems are similar but not identical, so it is overlapping.

(3) Clearly define borders-in the story, the Kings rely on ministers, and the ministers rely on the following officials to constantly resolve the issue of relying on their children, thus, the effect is the same as that of the Multi-bonuo brand. Therefore, the final sub-problem must be solved. Otherwise, the parent problem will never be solved.

 

4Use Dynamic Planning to solve the longest Public String Problem

Finally, I finished the MIT video tutorial and explained the longest Public String problem. Therefore, the source code for dynamic planning to solve the problem is provided for your reference.

 

<pre name="code" class="java">
public class CommonSubsequence{public static void main(String[] args){String[] x = {"A", "B", "C", "B", "D", "A", "B"};String[] y = { "B", "D", "C", "A", "B", "A"};int[][] tempArray = calStringLength(x, y);printString(tempArray , x, x.length-1, y.length-1);}public static int[][] calStringLength(String[] x, String[] y){int[][] temp1 = new int[x.length][y.length];int[][] temp2 = new int[x.length][y.length];for(int i=1; i<x.length; i++){for(int j=1; j<y.length; j++){if( x[i] == y[j]){temp2[i][j] = temp2[i-1][j-1] + 1;temp1[i][j] = 1;}else if(temp2[i-1][j] >= temp2[i][j-1]){temp2[i][j] = temp2[i-1][j];temp1[i][j] = 0;}else                        {                        temp2[i][j] = temp2[i][j-1];                        temp1[i][j] = -1;                          }}}return temp1;}public static void printString(int[][] b, String[] x, int i, int j){if(i == 0 || j == 0)return;if(b[i][j] == 1){printString(b, x, i-1, j-1);System.out.print(x[i] + " ");}else if(b[i][j] == 0){printString(b, x, i-1, j);}else if(b[i][j] == -1){printString(b, x, i, j-1);}}}


 




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.