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:
-
FIB (5)
FIB (4) + fib (3)
(FIB (3) + fib (2) + (FIB (2) + fib (1 ))
(FIB (2) + fib (1) + (FIB (1) + fib (0) + (FIB (1) + fib (0 )) + fib (1 ))
(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)