Introduction to Divide and Conquer Algorithm

Source: Internet
Author: User

Let's take a look at the definition from wikipedia:Http://en.wikipedia.org/wiki/Divide_and_conquer_algorithmDivide and conquer (D & C) is an important algorithm design paradigm based on multi-branched recursion. A divide and conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same (or related) type, until these become simple enough to be solvdirectly. the solutions to the sub-problems are then combined to give a solution to the original problem. splitting Algorithm It is an algorithm design mode based on multi-branch recursion. The grouping algorithm recursively Splits a major problem into multiple subproblems of the same type until these subproblems are simple enough to be directly solved. Finally, we can combine the solutions of these subproblems to obtain the solutions to the original problems. According to this definition, we can clearly understand that for the D & C problem, we need to solve it in two steps. One is to find the recursion; the second is to find the final answer based on recursion. First, what is recursion. The following is a short article on copy from the Introduction to Algorithm (Introduction to Algorithm Edition 3), which explains quite clearly. Recurrences go hand in hand with the divide-and-conquer paradigm, because theygive us a natural way to characterize the running times of divide-and-conquer algorithms. A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs. for example, T (n) = 4 T (n/2) + n. This expression is our recursive formula. For different problems, the recursive formula we get is different. This is usually because of the problem. But the general idea is how to break down the original question. The focus of this article is to introduce how to find the answer to the question in the case of recursion. Such as analyzing the time complexity of a problem. Generally, there are three ways to solve this type of problem (always recursive, find the answer): 1. mathematical Induction 2. the Recursion Tree draws the recursive Tree finding rule 3. master Theorem Main Theorem (as if the Chinese version of the introduction to algorithms is translated in this way, it feels really frustrated) is followed by a simple description of the above three methods with a specific example.1. Mathematical InductionThe mathematical induction is basically clear to everyone. It is assumed that a conclusion was established at n, proving that the conclusion was also established at n + 1. Of course, here we are, slightly changed. For example. T (n) = T (n/2) + T (n/4) + T (n/8) + n now we want to use the above expression T (n ), now we should first guess T (n) = Theta (n ). Of course we know that to prove T (n) = Theta (n), we need to prove T (n) = O (n) and T (n) = Omega (n) respectively ). Obviously, T (n) = Omega (n) Here, because T (n) = T (n/2) + T (n/4) + T (n/8) + n> n, obviously, T (n) = Omega (n ). the following mathematical induction is used to prove T (n) = O (n) suppose T (n) <= cn, So c is a constant. So T (n) <= c * n/2 + c * n/4 + c * n/8 + n = (7c/8 + 1) n we only need to let (7c/8 + 1) n <= cn, obviously we can find a normal number c to make this formula true. So T (n) = O (n ). In summary, T (n) = Theta (n ). The above proof is no problem, but some friends may ask, why did you get guess T (n) = Theta (n) from the very beginning? That's right. make a good guess is the key to this method. Let's briefly talk about the intuition of make this guess. First, we can simply draw the following tree. Of course, we can find that the total cost of each layer decreases with the deepening of recursion, so we can speculate that when the layers are large, the total cost of that layer is very small and cannot be compared with the top layer. That is to say, the T (n) of this problem is mainly determined by the top layer, and the top layer is n, so we have a reason for guess to answer cn. C is a constant. Next we will introduce two small tricks1 of guess. When T (n) = aT (n/B + d) + f (n), in most cases, we can directly ignore d for guess, because when n is large enough, n/B + d and n/B are almost the same. 2) When guess is used, a constant can be added. For example, when guess T (n) = O (n) is used, T (n) <= cn, sometimes, you can try T (n) <= cn-d to get rid of the interference of some constant terms.2. Recursion Tree draw recursive Tree lookup rulesIn fact, this method is very similar to the previous method for tree painting. Here is a simple example to illustrate the problem. Now let's assume that our recursive formula is T (n) = aT (n/B) + n ^ k. It is troublesome to draw a tree here (to steal a lazy (* ^__^ *)...) I drew it into a table. In fact, the principles are the same, but the presentation methods are different. Of course, we know that the final level is log _ {B} ^ n. Then we add the last column to get our results. Finally, let's say a few more. When looking for the height of the tree, sometimes we may encounter T (n) = T (n/3) + T (2n/3) + O (n) similar problems: the height of the tree may not be obtained at once. In fact, if we plot a recursion tree, we can know that the height of the tree is determined by the longest branch, that is, n --- (2/3) n --- (2/3) ^ 2n... --- 1 this branch, so the height k should meet (2/3) ^ k * n = 1, So k = log _ {3/2} ^ {n }.
3. Master Theorm Master TheoremIn fact, there is nothing to talk about. The proof of theorem is complicated. If you are interested, you can directly refer to the last several sections in the fourth chapter of Introduction to algorithms. Here we will post the conclusion.Generally, when the conditions are met, you can draw a conclusion directly.

========================================================== ======================================

Related Article

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.