Detailed explanation of recursive time complexity Calculation

Source: Internet
Author: User

Time Complexity Analysis of Recursive Algorithms

In algorithm analysis, when an algorithm contains a recursive call, the analysis of its time complexity is converted into a recursive equation. In fact, this problem is a mathematical solution to the gradual order, and the recursive equation has a variety of forms, the solution method is also not enough, the more commonly used are the following four methods:

Method 1: replacement method

The replacement method requires the following two steps:

1. to guess the answer, you do not need to completely guess the answer. Instead, you do not need to know the exact value of the Changshu coefficient. Instead, you only need to guess its form, for example, assume that the time complexity of a recursive formula is O (n2), that is, its running time should be a Changshu multiplied by N2, and there may be some low-level items.

 

Method 2: recursive tree

The recursive tree method mainly finds the answer by expanding the recursive tree, and then uses the substitution method to prove it, because the recursive tree method is not rigorous.

For example, use the recursive tree method to calculate T (n) = T (n/2) + N2, and use the recursive tree method to expand the recursive formula.

In this way, the recursive tree is expanded and extended, and only T (1) is left at the leaf node. The height of the recursive tree is logn, because it starts from vertex N, to n/2, to N/4 ,...... From the last to 1, the number of folds from N to 1 is logn, that is, the height is logn (it should be a constant multiplied by logn, but it doesn't matter much ). The number of leaf nodes at the bottom is N, because from the first layer down, the number of nodes changes to 1, 2, 4, 8 ......, If the height of the fruit tree is h, there will be 2 h leaf nodes, and the height is logn, then 2 logn = n. The overall work is T (n ).

T (n) = [1 + 1/2 + 1/4 +…] N2 = 2n2, so we can see that the time complexity is T (n) = O (n2 ).

For example, if we use the recursive tree method to calculate T (n) = T (N/4) + T (n/2) + N2, we use the recursive tree method to expand the recursive formula.


Finally, finding the number of leaf nodes is a little troublesome because the recursive speed of the branches is different. When the left side is reduced to N/16, the right side is reduced to N/4, the height of the Left subtree is smaller than that of the right subtree. We can see that the number of leaf nodes must be less than N, because the initial problem is N, and then recursion is a subproblem of N/4 and N/2, until the end of The Recursion TO 1 stop, and N/4 + n/2 <n, so the number of leaf nodes does not exceed n, the sum of each layer will get T (N ), after observation, we found a proportional sequence, so mathematical induction began to come in handy.

T (n) = (1 + 5/16 + 25/256 + ......) N2 ≤ 2n2 = O (n2), so the time complexity of this recursive formula is O (n2). Because it is a guessed proportional sequence, it must be proved by mathematical induction, then it becomes a replacement method in method 1 for verification.

Method 3: Master Method)

This method is only applicable to recursive expressions in specific formats.


At the same time, F (n) is required to gradually become positive, that is, when n-> infinity, F (n)> 0. (I think logn is missing in step 2 and 3. For details, refer to the time complexity discussion in the introduction to algorithms)

For example:

T (n) = 4 T (n/2) + N, then a = 4, B = 2, F (n) = N, calculate nlog (B,) = N2> F (N), satisfying Mode 1, so t (n) = nlog (B, A) = O (n2)

T (n) = 4 T (n/2) + N2, T (n) = O (n2logn)

T (n) = 4 T (n/2) + N3, satisfying Mode 3, T (n) = O (N3)

For the correctness of this method, we can use the recursive tree method to prove that it is lazy to draw a sketch in the brain:

In the first layer, F (n) is decomposed into a sub-problem. Each sub-problem is F (N/B). In the second layer, each sub-problem is decomposed into a sub-problem, every problem is F (N/b2 )...... In this way, the final leaf is O (1), and the height of the entire tree is the logarithm of N at the bottom of log with B as the base, the number of leaf nodes is a. The log base B is the logarithm power of N (alog (B, n), that is, nlog (B, A) leaf nodes. The decreasing speed of each branch is the same. When each layer is added together, T (n) is obtained)
In this case, we need to discuss the F (n) situation (that is, the above 1, 2, 3 ).

 

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.