Algorithm-Dynamic Planning Algorithm

Source: Internet
Author: User

Basic Idea of Dynamic Planning: The original problem is divided into similar subproblems. In the process of solving the problem, the solution of the original problem is obtained through the subproblem solution.
Well-known Application Instances include: Solves Shortest Path Problems, knapsack problems, project management, and network flow optimization.

Personal understanding of dynamic planning,This mainly avoids repeated computing.. That is, when the previously computed values are saved and the same subproblem is encountered again, the saved values are provided directly without calculation.

There is a simple example about the Fibonacci series.

 

What is the Fibonacci series? According to Wikipedia,

Feberatsi Series(Fibonacci sequence), Translated againThe number of feidanes,Fibonacci Series,February Series,Golden split Series.

In mathematics,Feberatsi SeriesIt is defined by recursion:

In terms of words, the feberatsi series starts from 0 and 1, and the feberneatsi coefficient after is added by the previous two numbers.

To write a C language, it is as simple as this:

IntFIB (IntN ){If(N =0| N =1)Return 1;ReturnFIB (n-1) + Fib (n-2);}

When you ask for fib (5), many fib (1) and FIB (0) are simple at the end.

When n = 5, the calculation process of Fib (5) is as follows:

      1. FIB (5)
      2. FIB (4) + fib (3)
      3. (FIB (3) + fib (2) + (FIB (2) + fib (1 ))
      4. (FIB (2) + fib (1) + (FIB (1) + fib (0) + (FIB (1) + fib (0 )) + fib (1 ))
      5. (FIB (1) + fib (0) + fib (1) + (FIB (1) + fib (0) + (FIB (1) + fib (0) + fib (1 ))

Therefore, many computing processes are repeated, which is too wasteful for computers. Therefore, we hope to avoid repeated processes from happening again, just like writing functions, to make them more efficient.

Using the idea of dynamic planning, we first save the calculated values. If we find that there are the same steps, we just need to take out the previously saved values.

 

Therefore, this is what I wrote:

 //  Dynamic Programming  Int M [ 10 ];  Bool BN [ 10  ];  Int Fiber 2 ( Int  N ){  If (! BN [N]) //  Check whether the calculation has been performed  {M [N] = Fiber 2 (n- 1 ) + Fiber 2 (n- 2 ); //  Save the calculated value BN [N] =True  ;}  Return  M [N];}  Int _ Tmain ( Int Argc, _ tchar * Argv []) {  //  Initialize data      For ( Int I = 0 ; I < 5 ; I ++ ) Bn [I] = False ; M [  1 ] = M [ 0 ] = 1  ; Bn [  0 ] = Bn [ 1 ] = True  ; Printf (  "  % D  " , Fiber 2 ( 5  ));  Return   0 ;} 

ComparedAlgorithmThe introduction provides an example for Wikipedia to make it easier to understand. (More and more people are finding that they cannot leave the network)

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.