Recursive Definition
Original address: http://blog.csdn.net/thisinnocence
Recursion and iteration are the most common basic skills in programming, and recursion is often more concise and powerful than iteration. It is defined as directly or indirectly calling itself. Typical problems include power calculation, factorial, combination number, Fibonacci series, and tower of Hanoi. Its algorithm concept:
- The original problem can be decomposed into subproblems (necessary conditions );
- Similar to the subproblem after decomposition (recursive equation );
- Limited number of decomposition times (poor Sub-problems );
- The final problem can be solved directly (recursive boundary );
For recursive application and optimization, it is necessary to estimate the time-space complexity when directly recursion, so as not to take too long or stack overflow. Optimization recursion is based on the principle of no repetition. For common series problems, there are often Recursive formulas, that is, the relationship between F (N) and F (n-1). In fact, recursive formulas can easily write the code of recursive algorithms, here we will repeat the differences between Recursion and Recursion:
- Recursion: the problem where the problem scale is n is degraded into n-1 problems, which are degraded in turn until the problem scale can be obtained, and the solution of the low-level scale is obtained, which is substituted into the high-order problem, until the solution of the problem with a scale of N is obtained (n-> 0 );
- Recursion: Construct a low-level scale (for example, the scale is I, and the general I = 0) problem, find the solution, and derive the problem and solution with the scale of the problem being I + 1, push to N in order (0-> N );
Quick power
Directly converted to code. The time complexity is calculated by O (N)-> O (logn) of the simple power operation ):
/* The power of a to the nth power, C code */INT quickpower (int A, int N) {If (n = 0) return 1; if (N % 2 = 1) return quickpower (A, n/2) * quickpower (A, n/2) * A; elsereturn quickpower (A, n/2) * quickpower (A, n/2 );}
Fibonacci Series
To code, the time complexity is changed from direct recursion of O (N ^ 2)-> O (logn). The following experiment is implemented in Python. If C ++ is used to overload the multiplication operator, you can reuse the quick power code to a large extent:
Import time # recursive computation of the Fibonacci series def fiber 1 (n): If n <= 1: return 1 else: Return fiber 1 (n-1) + fiber 1 (n-2) # Calculate the product def MUL (A, B) of A and B (all 2 × 2 matrices): r = [0, 0], [0, 0] R [0] [0] = A [0] [0] * B [0] [0] + A [0] [1] * B [1] [0]; R [0] [1] = A [0] [0] * B [0] [1] + A [0] [1] * B [1] [1]; R [1] [0] = A [1] [0] * B [0] [0] + A [1] [1] * B [1] [0]; R [1] [1] = A [1] [0] * B [0] [1] + A [1] [1] * B [1] [1]; return R; # recursive acceleration calculation of the Fibonacci series O (N ^ 2)-> O (logn) def fib (n): If n = 0: return [1, 0], [0, 1] If n = 1: return [1, 1], [1, 0] If n % 2 = 0: return MUL (FIB (INT (n/2), FIB (INT (n/2) else: Return MUL (FIB (INT (n/2 )), FIB (INT (n/2), [[1, 1], [1, 0]) if _ name _ = '_ main _': starttime = time. clock () print (fiber 1 (35) endtime = time. clock () print ('use recursion for direct computation: ', endtime-starttime) starttime = time. clock () print (FIB (35) [0] [0]) endtime = time. clock () print ('matrix recursive power acceleration: ', endtime-starttime) ''' experiment running result: 14930352 direct calculation using recursion: 5.51856492966247114930352 matrix recursive power acceleration: 0.0002042703426949899 '''