Time complexity analysis of recursive algorithm

Source: Internet
Author: User

Transfer From address http://blog.csdn.net/metasearch/article/details/4428865

In algorithm analysis, when a recursive call is included in an algorithm, the analysis of its time complexity is transformed into a recursive equation solution. In fact, this problem is a mathematical solution to the asymptotic order of the problem, and the recursive equation of the form of various, the solution is also a variety of methods, more commonly used in the following four ways:

(1) Substituting law (Substitution method)

The basic step of substituting method is to first speculate the explicit solution of the recursive equation, and then use the mathematical induction method to verify whether the solution is reasonable.

(2) Iterative methods (iteration method)

The basic step of iterative method is to expand the right end of the recursive equation iteratively, to make it a non-recursive sum, and then to estimate the solution of the Cheng equation by the estimation of the sum formula.

(3) Apply Formula Method (Master method)

This method is for a recursive equation that is shaped like "T (n) = at (n/b) + f (n)". This recursive equation is the recursive relation that satisfies the time complexity of the divide-and-conquer method, that is, a problem of scale n is divided into a sub-problem of scale n/b, the problem of a is solved recursively, and then the solution of the problem is obtained by synthesizing the solution of the A-sub-question.

(4) Difference equation (difference Formula method)

Some recursive equations can be considered as difference equations, and the recursive equation is solved by the method of solving difference equation, and then the asymptotic order of the solution is estimated.

Here are some examples of how to do this.

First, substituting law

The recursive equation of the large integer multiplication computation time is: t (n) = 4T (N/2) + O (n), where T (1) = O (1), we guess a solution t (n) = O (N2), according to the definition of the symbol O, to n>n0, there is T (n) < Cn2-eo (2n) (note, subtract O (2n), because it is a low-order term, does not affect the asymptotic behavior of n large enough, the solution into the recursive equation, to obtain:

T (n) = 4T (N/2) + O (n)
≤4C (N/2) 2-eo (2N/2)) + O (n)
= Cn2-eo (n) + O (n)
≤cn2

where c is the normal number, E takes 1, the upper type conforms to the definition of T (n) ≤cn2, it can be considered that O (N2) is a solution of T (N), and then proved by the mathematical induction method.

Second, iterative method

The calculation time of an algorithm is: t (n) = 3T (N/4) + O (n), where T (1) = O (1), iteration Two can expand the right side to:

T (n) = 3T (N/4) + O (n)
= O (n) + 3 (O (N/4) + 3T (N/42))
= O (n) + 3 (O (N/4) + 3 (O (N/42) + 3T (N/43))

As can be seen from the above, this is a recursive equation, we can write the equation after the iteration i:

T (n) = O (n) + 3 (O (N/4) + 3 (O (N/42) + ... + 3 (n/4i + 3T (n/4i+1)))

When n/4i+1 = 1 o'clock, T (n/4i+1) = 1, the

T (n) = n + (3/4) + (32/42) n + ... + (3i/4i) n + (3i+1) T (1)
< 4n + 3i+1

and by n/4i+1 = 1 known, I<log4 N, thus

3i+1≤3log4 n+1 = 3log3 n*log4 3 +1 = 3nlog4 3

Substituting:

T (n) < 4n + 3nlog4 3, i.e. t (n) = O (n).

Three, the method of applying formula

This method is for estimating the shape such as:

T (n) = at (n/b) + f (n)

Among them, a≥1 and b≥1 are constants, and f (n) is a definite positive function. In the three categories of f (n), we have the asymptotic estimation of T (N):

1. If for a constant ε>0, there is f (n) = O (nlogb a-ε), then T (n) = O (Nlogb a)

2. If f (n) = O (nlogb a), then T (n) = O (nlogb a *logn)

3. If f (n) = O (Nlogb a+ε), and for a constant c>1 and all full large positive integer n, there is AF (n/b) ≤CF (n), then T (n) =o (f (n)).

Set T (n) = 4T (N/2) + N, then a = 4,b = 2,f (n) = n, calculated nlogb A = nlog2 4 = N2, and f (n) = n = O (n2-ε), at this time ε= 1, according to the 1th case, we get t (n) = O (n2 )。

The three types of cases involved are the comparison of F (n) with Nlogb A, and the asymptotic order of the solution of the recursive equation is determined by the larger of the two functions. In the first case, the function nlogb A is larger, then T (n) =o (Nlogb a), and in the third case, the function f (n) is larger, then T (n) =o (f (n)), and in the second case, the two functions are the same size, then T (n) =o (Nlogb a *logn), That is, the logarithm of n is used as a factor to multiply the same order of F (n) and T (N).

But the above three types of cases do not cover all possible f (n). There is a gap between the first and second types of situations: F (n) less than polynomial less than nlogb a, and between the second and third classes, where the equation method does not apply

Time complexity analysis of recursive algorithm

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.