Time complexity analysis of recursive algorithm

Source: Internet
Author: User

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 many, the more often used in the following four ways:

(1) Substituting law (Substitution method)

The basic step of substituting method is to guess the explicit solution of the recursive equation first, 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, make it a non-recursive sum, and then achieve the prediction of the solution of the Cheng equation by the prediction 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, the recursive equation is solved by solving the difference equation, and then the asymptotic order of the solution is predicted.

Here are some examples of the methods described above.

First, substituting law

The recursive equation for calculating time for large integer multiplication is: t (n) = 4T (N/2) + O (n), in which t (1) = O (1), we are pushing 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 (2n), because it is a low-order term, does not affect the asymptotic behavior of n is 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

Among them, C is the normal number, E takes 1, the upper type conforms to the definition of T (n) ≤cn2, then can think O (N2) is a solution of T (N), and then use the mathematical induction method to prove.

Second, iterative method

The calculation time of an algorithm is: t (n) = 3T (N/4) + O (n), where T (1) = O (1), iteration twice 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, 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, i<log4n, and thus

3i+1≤3log4n+1 = 3log3n*log43+1 = 3nlog43

Substituting:

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

Three, the method of applying formula

This method is expected to be shaped like:

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 prediction of T (N):

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

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

3. If f (n) = O (nlogba+ε), and for a constant c>1 and fully 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 Nlogba = Nlog24 = N2, and f (n) = n = O (n2-ε), at this time ε= 1, we get t (n) = O (n2) According to the 1th case.

The three types of cases involved are comparing F (n) with Nlogba, while 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 Nlogba is larger, then T (n) =o (Nlogba), 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 (NLOGBA*LOGN), That is, the logarithm of n is used as a factor to multiply the same order of F (n) and T (N).

However, 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) is less than polynomial less than nlogba, and there is a case between the second and third classes, when the formula 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.