"Dynamic Planning Algorithm"

Source: Internet
Author: User

I recently studied various online experiences and found that many companies' interview questions are about dynamic planning. So I would like to summarize my understanding about dynamic planning. I have always admired those who have participated in ACM/ICPC. They often can quickly analyze the questions and give algorithms to cope with them. I have done some ACM questions, and there are not a few questions about dynamic planning, and ACM's questions have the following characteristics: most of the time, when you think it is a graphic topic, it is exactly a question that needs to be answered through dynamic planning, just like the mathematical problem of the mathematician Shi Shen in the book "The dedication of Suspect X" in dongye Qiwu, on the surface, the geometric problem is actually a function problem. I just want to tell you that, compared with reciting the answer to a question, the method for analyzing the question is the most important.

Before introducing dynamic planning, let's take a look at a very familiar example.


Recursive definition of the Fibonacci sequence:

F0 = 0

F1 = 1

Fn = Fn-1 + Fn-2

It is easy to write the code:

int fibonacci(int n)
{
if(n==0) return 0;
else if(n==1) return 1;
else return fibonacci(n-1)+fibonacci(n-2);
}

However, recursion is not efficient. if you draw a recursive tree, you will find that many values are computed repeatedly. A good idea is to save the computed results and obtain them directly when performing repetitive tasks in the future to reduce unnecessary recursion. The improved code is as follows:

# Include <iostream>
# Include <memory. h>
# Define n 100
Using namespace STD;

Int array [N];

Int Fibonacci (int n)
{
If (array [N]! =-1) return array [N];
If (n = 0) return 0;
Else if (n = 1) return 1;
Else
{
Array [N] = maid (n-1) + maid (n-2 );
Return array [N];
}
}

Int main ()
{
Memset (array,-1, sizeof (array); // initialize the memo array to-1
Cout <maid (8 );
Return 0;
}

So in my opinion, the basic idea of dynamic planning is to save the results of recursion, so it will not take time to solve the same problem. Dynamic Planning is usually used to solve the optimization problem. Similar to the splitting algorithm, it is used to break down the problem to be solved into several subproblems, first solving the subproblem and then obtaining the solution of the original problem. Different from sub-governance, the sub-Problems of the sub-governance algorithm are independent of each other, while the sub-Problems of the dynamic planning class are often interrelated.

The difference between greedy algorithms for solving optimization problems and dynamic planning is that greedy algorithms strive to achieve the best in each step, while dynamic planning is the pursuit of the best overall.

To use a dynamic planning algorithm, the optimization principle and no aftereffect must be met. The optimization principle is: the sub-strategy of an optimization strategy is always optimal. In short, no aftereffect is that the current decision has nothing to do with the past state.

Steps for using the dynamic planning algorithm: (1) analyze the nature of the optimal solution and portray its structural features. (2) recursively define the optimal value. (3) Use the bottom-up or top-down memory method to calculate the optimal value.

There are some classic questions about dynamic planning: the knapsack problem, matrix chain multiplication, the longest common subsequence ...... You can use it to make your impression better.


[Reference] ACM Program Design Competition basic tutorial

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.