1. Recursive execution process
(1) Example: Seeking n!.
This is a simple "multiplicative" problem, which can be solved by recursive algorithm.
N! = n * (n-1)! n > 1
0! = 1, 1! = 1 N = 0,1
Therefore, the recursive algorithm is as follows:
Java code
1 fact (int2if(n = = 0 | | n == 13return 1< c13>4Else5return N * Fact (n-16
Take n=3 as an example to see how the process works:
Fact (3)-----Fact (2)-----Fact (1)------fact (2)-----fact (3)
------------------------------>------------------------------>
Recursive backtracking
Recursive algorithm constantly calls itself to reduce the scale of the process, when the scale reduced to 1, that is, recursive to the fact (1), meet the Stop condition stop recursion, start backtracking (return to call algorithm) and calculate, from fact (1) =1 calculation returned to fact (2); calculation 2*fact (1) = 2 returns to fact (3), calculates 3*fact (2) = 6, ends recursion.
The starting module of the algorithm is also the terminating module.
(2) Recursive implementation mechanism
Each recursive call, with a special data structure "stack" records the execution state of the current algorithm, specifically set the address stack, used to record the current algorithm execution position, in case of backtracking back to normal return. The formal parameters of a recursive module are normal variables, and each recursive call gets a different value, and they are also stored by "stacks".
(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 invocation: 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 of recursive algorithm analysis, and the most common one is iterative method.
The basic step of the iterative method is to simplify the recursive algorithm to the corresponding recursive equation, and then through iterative iteration, the right end of the recursive equation is transformed into a series, and then the sum of the series and the asymptotic order of the second estimate.
<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 assumes the K-square 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) sub-square * T (N/2 (i-1)) + $ (i:1~ (k-1)) 2 I-party
= 2 (K-1) (2 of K-square)-2
= (3/2) * (2 of the K-th square)-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 assumes the K-th-square 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)//base 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)//base 2
O (NLOGCA) (a>c && c>1)//n (LOGCA) sub-square, base C
The 3 recursive invocation forms described above are commonly used in the first case, and the second form sometimes occurs, while the third form (indirect recursive invocation) uses less, and the algorithm analyzes
More complex. Here is a second form of recursive invocation example.
<4>The recursive equation is: t (n) = t (N/3) + t (2N/3) + N
For a better understanding, first draw the recursive tree corresponding to the recursive process:
n--------> N
N/3 2N/3--------> N
N/9 2n/9 2N/9 4N/9--------> N
...... ...... ...... ....... ......
--------
Total O (Nlogn)
The values of the non-recursive items of each layer of the cumulative recursive tree, each layer equals n, and the longest path from the root to the leaf is:
----(2/3)-----(4/9) N--------1
Set the longest path to K, you should have:
(2/3) k-th-square * n = 1
Get k = log (2/3) n//With (2/3) as Bottom
So T (n) <= (K + 1) * n = N (log (2/3) n + 1)
That is, T (n) = O (Nlogn)
The example shows that for the second recursive form invocation, it is simple to use iterative method to analyze the algorithm by using recursive tree.
Time complexity analysis of recursive functions (RPM)