analysis of time complexity of recursive function
(1) Recursive execution process
Example: Seek n!.
This is a simple "tired multiply" problem, with recursive algorithm can also be solved.
N! = n * (n-1)! n > 1
0! = 1, 1! = 1 N = 0,1
Therefore, the recursive algorithm is as follows:
Java code
Fact (int n) {
if (n = = 0 | | n = = 1)
return 1;
Else
return n * fact (n-1);
}
Take n=3 as an example, look at the running process as follows:
Fact (3)-----Fact (2)-----Fact (1)------fact (2)-----fact (3)
------------------------------>------------------------------>
Recursive backtracking
Recursive algorithm in the operation of the continuous call itself to reduce the size of the process, when the scale is reduced to 1, that is, recursion to fact (1), satisfies the stop condition to stop recursion, starts backtracking (returns the call algorithm) and calculates, returns from fact (1) =1 Computation to Fact (2), calculates 2*fact (1) = 2 returns to fact (3), calculates 3*fact (2) = 6, ends recursion.
The starting module of the algorithm is also the termination module.
(2) Recursive implementation mechanism
Each recursive call uses a special data structure "stack" to record the execution state of the current algorithm, specifically to set the address stack, to record the current algorithm's execution position, in case of backtracking normal return. The formal parameters of recursive modules are ordinary variables, and each recursive call gets a different value, and they are stored by the stack.
(3) Several forms of recursive invocation
There are several forms of general recursive invocation (where A1, A2, B1, B2, K1, and K2 are constants).
<1> Direct Simple Recursive call: F (n) {... a1 * F ((N-K1)/B1);
<2> Direct Complex Recursive invocation: F (n) {... a1 * F (N-K1)/B1); A2 * F ((N-K2)/B2);
<3> Indirect recursive invocation: F (n) {... a1 * F (N-K1)/B1);
G (N) {... a2 * f (N-K2)/B2);
2. Recursive algorithm Efficiency analysis method
There are many methods to analyze the recursive algorithm, the most commonly used is iterative method.
The basic step of the iterative method is to simplify the recursive algorithm to the corresponding recursive equation, then through iterative iteration, the right end of the recursive equation is transformed into a series, and finally the sum of the series is evaluated and the asymptotic order is obtained.
<1> Example: n!
The recursive equation of the algorithm is: t (n) = t (n-1) + O (1);
Iterative expansion: t (n) = t (n-1) + O (1)
= T (n-2) + O (1) + O (1)
= T (n-3) + O (1) + O (1) + O (1)
= ......
= O (1) + ... + O (1) + O (1) + O (1)
= n * O (1)
= O (n)
The time complexity of this example is linear.
<2> Example: The following recursive equation:
T (n) = 2T (N/2) + 2, and assume the K-th side of the n=2.
T (n) = 2T (N/2) + 2
= 2 (2T (n/2*2) + 2) + 2
= 4T (n/2*2) + 4 + 2
= 4 (2T (n/2*2*2) + 2) + 4 + 2
= 2*2*2T (n/2*2*2) + 8 + 4 + 2
= ...
= 2 (k-1) Second Party * T (N/2 (i-1)) + $ (i:1~ (k-1)) 2 i-th side
= 2 (k-1) Second Party + (2 k-th side)-2
= (3/2) * (2 K-th side)-2
= (3/2) * n-2
= O (n)
The time complexity of this example is also linear.
<3> Example: The following recursive equation:
T (n) = 2T (N/2) + O (n), and assume the K-th side of the n=2.
T (n) = 2T (N/2) + O (n)
= 2T (N/4) + 2O (N/2) + O (n)
= ...
= O (n) + O (n) + ... + O (n) + O (n) + O (n)
= k * O (N)
= O (k*n)
= O (nlog2n)//Bottom 2
Generally, when the recursive equation is t (n) = at (n/c) + O (n), the solution of T (N) is:
O (n) (a<c && c>1)
O (nlog2n) (a=c && c>1)//Bottom 2
O (NLOGCA) (a>c && c>1)//n (LOGCA) on the second side, with C as the base
The 3 recursive invocation forms described above are more commonly used in the first case, while the second form is sometimes present, while the third form (indirect recursive invocation) uses less, and the algorithm analyzes
More complex. Here's a second form of recursive invocation examples.
<4> recursive equation is: t (n) = t (N/3) + t (2N/3) + N
To better understand, first draw the recursive tree corresponding to recursive process:
n--------> N
N/3 2N/3--------> N
N/9 2n/9 2N/9 4N/9--------> N
...... ...... ...... ....... ......
--------
Total O (Nlogn)
Cumulative recursive tree each layer of the value of the non recursive items, each layer and are equal to N, from the root to the longest path to the leaf is:
n--> (2/3) n--> (4/9) n--> (12/27) n--> ...--> 1
Set the longest path to K, you should have:
(2/3) k-th square * n = 1
Get k = log (2/3) n//to (2/3) as the base
So T (n) <= (K + 1) * n = N (log (2/3) n + 1)
i.e. T (n) = O (Nlogn)
This example shows that for the second recursive form invocation, with the help of recursive tree, it is simple and easy to analyze the algorithm by iterative method.