Dynamic Planning Algorithm

Source: Internet
Author: User

Dynamic PlanningAlgorithmIt is usually used to solve a problem with a certain optimum nature.. In the field of mathematics and computer science, dynamic planning is used to solve the problems that can be decomposed into duplicates (overlapping subproblems,Think about recursive factorial.And has the optimal substructure (optimal substructure,Think about the shortest path algorithm). Dynamic Planning takes less time than normal algorithms. In such problems, there may be many feasible solutions. Each solution corresponds to a value. We hope to find a solution with the optimal value. The dynamic planning algorithm is similar to the divide and conquer method.Basic IdeasIt is also to break down the problem to be solved into several subproblems, first solve the subproblems, and then obtain the original solution from the solutions of these subproblems. Different from the Division and control method, it is suitable for Dynamic Planning and solving problems. After decomposition, subproblems are often not independent of each other. If you use the divide and conquer method to solve this type of problem, the number of subproblems obtained by the decomposition is too large, and some subproblems are repeatedly calculated many times.If we can save the answers to the resolved sub-questions and find the answers we have obtained when necessary, we can avoid a large number of repeated computations and save time.. WeYou can use a table to record the answers to all resolved subquestions. Whether or not this subproblem is used in the future, as long as it has been computed, it will fill in the results in the table.. This is the basic idea of dynamic planning. The specific dynamic planning algorithms are diverse, but they have the same table filling format.

Dynamic Planning is widely used, such as the maximum subsequence sum, the optimal binary query tree, And the Fibonacci sequence.

    • Example:

1. Maximum subsequence and
Here we repost two good articlesArticle:

Maximum subsequences and Problems

Dynamic Programming Method ------- maximum continuous subsequence and

2. Fibonacci Series

Find the nth number in the Fibonacci sequence and implement it directly based on its mathematical definition:

Function fib (N)
If n = 0
Return 0
Else if n = 1
Return 1
Return fib (n-1) + fib (n-2)

If we call fib (5), a call tree is generated for repeated calculation of the same value multiple times:

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 ))

    In particular, FIB (2) is calculated three times. In larger cases, more fib values are repeatedly calculated, which will consume exponential time.

    Now, suppose we have a simple map object.MCreates a ing for each computed fib and its return values, modifies the above function fib, and uses and constantly updates it.M. The new function will only requireO(N), Rather than the exponential time:

    VaR M: = map (0 → 1, 1 → 1)
    Function fib (N)
    If map M does not contain key N
    M [N]: = fib (n-1) + fib (n-2)
    Return M [N]

    This technology is called cache, which stores the calculated values.Top-downMethod: divide the problem into several subproblems, and then calculate and store the value.

    InBottom-upIn this method, we first calculate a smaller fib, and then calculate a larger fib based on it. This method also takes only linear (O(N) Because it containsN-One cycle. However, this method only requires constants (O(1) space, on the contrary,Top-downThe O (N) To store mappings.

    Function fib (N)
    VaR previusfib: = 0, currentfib: = 1
    If n = 0
    Return 0
    Else if n = 1
    Return 1
    Repeat n-1 times
    VaR newfib: = previusfib + currentfib
    Previusfib: = currentfib
    Currentfib: = newfib
    Return currentfib

    In both examples, we only calculate fib (2) once, and then use it to calculate fib (3) and FIB (4), instead of re-computing each time.

    3. Zero currency change

    See: currency change

    To be continued...

    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.