Divide governance: literally, divide a problem into two or more identical or similar subproblems, and divide the subproblems into smaller subproblems... knowing that the final sub-problem can be solved simply and directly, the solution of the original problem is the merge of the sub-problem solutions.
Recursion: a recursive algorithm is called directly or indirectly. There are two elements of recursion: The ending condition (boundary condition) and the recursive equation. Only when these two conditions are met can the results be obtained after a finite number of computations.
The execution efficiency of recursive algorithms is usually poor. Therefore, you need to convert recursive algorithms into non-recursive algorithms. There are two methods: Direct conversion and indirect conversion.
1. Direct conversion method:
The direct conversion method requires some variables to save the intermediate results. Direct conversion is usually used to eliminate tail recursion and unidirectional recursion. Instead, recursive structures are replaced by cyclic structures.
Tail recursion means that in a recursive algorithm, there is only one recursive call statement at the end of the algorithm, such as a factorial algorithm:
Long fact (int n)
{
If (n = 0) return 1;
Else return N * fact (n-1 );
}
For example, n = 3, that is, return 3 * fact (3-1), continue decomposition: return 3*2 * fact (2-1), continue return 3*2*1.
When a recursive call is returned, it is returned to the next statement of the previous recursive call, and the return position is exactly the end of the algorithm. Therefore, it is not necessary to use the stack to save the returned information. Recursive Algorithms in the form of tail recursion can be replaced by cyclic structures. Therefore, you can write the following non-recursive algorithms:
Long fact (int I)
{
Int S = 1;
For (INT I = 1; I <= N; I ++)
S = S * I; // use s to save the intermediate result
Return S;
}
One-way recursion means that, although multiple recursive call statements exist in a recursive algorithm, the parameters of each recursive call statement are irrelevant, and these recursive call statements are at the end of the recursive algorithm. Apparently, tail recursion is a special case of unidirectional recursion. For example, the recursive algorithm for finding the Fibonacci sequence is as follows:
Int F (int n)
{
If (n = 1 | n = 0) return 1;
Else return F (n-1) + f (n-2); // the parameters of the two recursive call statements are not mutually dependent and there is no relationship between them.
}
For unidirectional recursion, you can set some variables to save the intermediate results and replace the recursive structure with the cyclic structure. For example:
Int F (int n)
{
Int I, S;
Int S1 = 1, S2 = 1;
For (I = 3; I <= N; I ++)
{
S = S1 + S2;
S2 = S1; // Save the value of F (n-2)
S1 = s; // Save the F (n-1) Value
}
Return S;
}
2. Indirect Conversion Method
This method uses the stack to save the intermediate results. It is generally obtained based on the stack changes in the execution process of recursive functions. The general process is as follows:
The initial status S0 is pushed to the stack.
While (Stack is not empty)
{
Stack rollback: grant the top element of the stack to S;
If (S is the result to be searched) returns;
Else
{
Locate S-related status S1;
Add S1 to stack
}
}
There are many examples of indirect conversions in data structures, such as non-Recursive Implementation of Binary Tree Traversal Algorithms and non-Recursive Implementation of deep graph optimization Traversal Algorithms.
Therefore, when solving some problems, we hope to use recursive algorithms to analyze the problem and use non-recursive algorithms to solve the problem.
There are three basic methods for conversion from recursive algorithms to non-recursive algorithms:
1. Through analysis, skip the decomposition process and directly use the cyclic structure algorithm to evaluate the process.
2. Use the stack to simulate the runtime stack of the system, and save only the information that must be saved through analysis, so as to replace the recursive algorithm with a non-recursive algorithm.
3. Using the stack to save parameters, because the post-first-in-first-out feature of the stack matches the execution process of the recursive algorithm, the recursive algorithm can be replaced by the non-recursive algorithm.