Time complexity of recursive algorithm

This article is reproduced to:
Original link | The end of the time complexity of recursive algorithms - Blog Park - python27

In the analysis of the algorithm, when an algorithm contains recursive calls, the analysis of its time complexity will be transformed into the solution of a recursive equation. There are various methods for solving recursive equations. This paper mainly introduces the current mainstream methods: substitution method, iterative method, formula method, generating function method, and difference equation method.

  [Substitution method] The substitution method first predicts the time complexity of the problem, and then brings the prediction into the original recursive equation. If there is no contradiction, it is a possible solution, and finally proves it by mathematical induction.

  [Example] We have the following recursive problem: T(n)=4T(n/2)+O(n), we first predict that the time complexity is O(n2), we might as well set T(n)=kn2 (where k is a constant), put this result into the equation to get: left=kn2, right=4k(n/2)2+O(n)=kn2+O(n), since the order of n2 is higher than that of n, Therefore, the left and right sides are equal, and then it can be verified by mathematical induction.

  [Iterative method] The iterative method is to iterate the right side of the expansion equation until there are no items that can be iterated. At this time, the solution of the equation is estimated by estimating the sum of the right side. It is more suitable for solving the divide and conquer problem. For the convenience of discussion, the general form of its recursive equation is given:
write picture description here

  [Example] Let's take a simple example to illustrate: T(n)=2T(n/2)+n2, the iterative process is as follows:
write picture description here

  It is easy to know that the recursive process ends when n/2^(i+1)=1, then we calculate as follows:
write picture description here

  Here we know that the time complexity of the algorithm is O(n^2). In the above calculation, we can directly use the formula of the infinite proportional sequence without considering the constraint of the number of items i. In fact, these two methods calculate The results are completely equivalent, and interested students can verify by themselves.

  [Formula method] This method is for recursive equations of the form: T(n) = aT(n/b) + f(n). This recursive equation is a recursive relation satisfied by the time complexity of the divide-and-conquer method, that is, a problem of size n is divided into a sub-problems of size n/b, the a sub-problems are solved recursively, and then the The synthesis of the solutions of a sub-problems leads to the solution of the original problem. This method is the best solution to the divide and conquer problem. We first give the following formula:
write picture description here

  Formula memory: We are actually comparing the orders of n^logba and f(n). If they are not equal, then T(n) takes the larger of them. If their orders are equal, then we will use any of them. One multiplied by logn will do. According to this formula, we can calculate the example mentioned in [Iterative method]: O(f(n))=O(n^2), it is easy to calculate another order is O(n), they are not equal, so take Larger order O(n2). Too easy, isn't it?

  It should be noted that the above formula does not include all cases. For example, the first and second cases do not include the following cases: f(n) is less than the former, but not the polynomial less than the former. Likewise, the latter two cases are not all inclusive. In order to understand and apply the formula better, the author expresses the formula as above, but it is not very rigorous. For a rigorous discussion of the formula, please see here . However, we rarely encounter the situation that the formula does not contain, and the above formula has a wide range of applications.

  In particular, for what we often encounter, when f(n)=0, we have:

write picture description here

  [Generating function method] The generating function is used for power series corresponding to an infinite sequence. The recursive problem we solve here is of the form: T(n)=c1T(n-1)+c2T(n-2)+c3T(n-3)+…+ckT(nk)+f(n). For the sake of simplicity, we choose the time complexity of the Fibonacci sequence as an example for discussion.

  [Example] The Fibonacci sequence recursive formula: T(n)=T(n-1)+T(n-2). Here we assume that F(n) is the operation amount of the nth item. Then it is easy to get: F(n)=F(n-1)+F(n-2), where F(1)=F(2)=1. We construct the following generating function: G(x)=F( 1) x+F(2)x2+F(3)x3+..., we can deduce as follows:
write picture description here

  The calculation of the above method is relatively simple. The key lies in the understanding of the generating function, which may not be well understood at the beginning. For the generating function, please refer to here and Wikipedia .

  [Difference equation method] Some recursive equations can be regarded as difference equations, and the recursive equations can be solved by solving the difference equations, and then an asymptotic order estimate of the solution can be made. Here we only consider the longest common recursive form, such as: T(n)=c1T(n-1)+c2T(n-2)+c3T(n-3)+…+ckT(nk)+f(n ), where c1,c2,…ck are constants and not equal to 0; we solve this equation as follows:
write picture description here

  The characteristic equation corresponding to the above homogeneous equation is:
write picture description here

  If the solution t=r is the m multiple roots of the characteristic equation, then the m solutions are in the form: {rn n*rn n2rn ... nm-1rn}, and the rest are in the form of complex solutions and ordinary linear equations. similar, and will not be repeated here. Next, we ask for the general solution of the corresponding inhomogeneous system of equations of this equation, here we give the following general solution form without proof for the special form of this equation:
write picture description here

  Then, like the solution in linear algebra, the solution of the original equation is equal to the general solution + specific solution of the homogeneous system of equations, namely:
write picture description here

  Finally, the value of a(i) can be determined by the initial conditions.

  To help understand, let's take a look at two examples to understand what's going on.

  [Example 1] The recursive equation is as follows:
write picture description here

(1) Write the characteristic equation corresponding to the homogeneous equation:
write picture description here

The basic solution is obtained as: {t1n, t2n}

(2) Calculate the special solution. For this problem, the special solution directly observed is: -8

(3) The form of the solution to the original equation is: T(n)=a0t1n+a1t2n-8

(4) Substitute in the case of n=0, n=1, get a0, a1, and finally get:
write picture description here

  It can be seen that this equation form differs from the Fibonacci sequence discussed above by only a constant 8, so the time complexity of both is the same. Interested students can follow this method to calculate the time complexity of the Fibonacci sequence again to verify.

  [Example 2] The recursive equation is as follows:
write picture description here

(1) Calculate the basic analysis of the corresponding homogeneous equation:

The characteristic equation is: C(t)=t^2-4t-4=0, and a double root t=2 is obtained. Therefore, its basic solution is: {2n n*2n}

(2) Since f(n)=n*2n, corresponding to the last case of the above table, the special solution form is obtained: T(n)=n2(p0+p1n)2n is substituted into the original recursive equation to obtain: p0=1 /2,p1=1/6

(3) The form of the original equation solution is: T(n)=a0*2n+a1*n*2n+n2(1/2+n/6)2n, substitute T(0), T(1) to get: a0 =a1=0

(4) In summary: T(n)=n2(1/2+n/6)2n

So the time complexity is: O(n^3 * 2^n)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326269781&siteId=291194637