One of common algorithm design ideas: Dynamic programming algorithm

Source: Internet
Author: User

The commonly used algorithm design ideas mainly include dynamic programming, greedy method, randomization algorithm, backtracking method and so on, these ideas have overlapping parts, when facing a problem, from these several ideas often can get a good answer.

Originally wanted to put the dynamic planning alone to write three articles, and later found that they learn sparse shallow, it is only to speak some fur, more in-depth things to try to conceive a few times, there is no progress, the intention of each design thought to write an article bar.

Dynamic programming (programming) is a very useful algorithm to solve complex problems by decomposing complex problems into simple sub-problems to obtain the optimal solution. first, top-down and bottom-up

In general, we can divide the solution of dynamic programming into top-down and bottom-up two ways.

A problem if you can use dynamic planning to solve, it must have "optimal substructure", in short, if the problem can be decomposed into multiple sub-problems, and these sub-problems have an optimal solution, then this problem can use dynamic planning. top Down (top-down)

The top-down approach is actually the use of recursion to solve the sub-problem, the final solution only need to call recursive, sub-problem gradually down the layer of recursive solution. We can use the cache to cache each of the sub-problems solved each time, the next time the call will not have to recursive calculation.

For example the computation of the famous Fibonacci sequence:

#!/usr/bin/env python
# coding:utf-8
def fib (number):
    if number = = 0 or Number = = 1:
        return 1
    else:
  return fib (number-1) + fib (number-2)
if __name__ = = ' __main__ ':
    print fib (35)

With a little development experience, it can be seen that fib (number-1) and fib (number-2) cause us to generate a lot of repetitive calculations, the above program executes 14s before the results, now, we have to save the results of each calculation, the next time we need to calculate the time to take the cache directly, Look at the results:

#!/usr/bin/env python
# coding:utf-8
cache = {}
def fib (number):
    If number in cache:
        return cache[ Number]
    if number = = 0 or Number = = 1:
        return 1
    else:
        cache[number] = fib (number-1) + fib (number-2) 
  return Cache[number]
if __name__ = = ' __main__ ':
    print fib (35)

The time-consuming effect of 0m0.053s is significantly improved. Bottom-up (bottom-up)

Bottom-up is another way to solve the dynamic programming problem, which does not use recursion, but uses the loop to calculate all the possible results, and gradually accumulates the solution of the sub-problem.

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.