1. Theory and Overview
Macro description: For a problem to be solved, when it is confined to a certain boundary, a small area, or a certain special situation, the answer is often known. If the application scenario of the solution can be expanded to the state space of the original problem, and each step of the expansion process is similar, then recursive and recursive solutions can be considered.
- Taking the known "problem boundary" as the starting point to extend the forward deduction to the "original problem" is recursion
- When the deduced route is difficult to determine, the “original problem” is used as the starting point to try to find a route that reduces the state space to the known “problem boundary”, and then the traversal method of backtracking through this route is recursion
The use of recursion and recursion requires that each transformation step between the "original problem" and the "problem boundary" be similar, so that we can design a program to implement this step, repeating its effect and interval. That is: the program should face the same kind of problems at each step. These problems are a sub-problem of the original problem, which may differ only in scale or certain restrictions, and can use the "program to solve the original problem" "To solve.
For recursive algorithms, with the above premise, we can let the program perform the following three operations in each transformation process:
- Reduce the size of the problem state space. This means that the program tries to find a route of change between the "original problem" and the "problem boundary" and takes a step toward the route being explored.
- Try to solve the problem after the scale is reduced, the result may be success or failure.
- If it succeeds, that is, the answer to the problem after the scale is reduced, then the answer is extended to the current problem, if it fails, then the current problem is returned, and the program may continue to find other alternative routes to the current problem until it is finally determined that the current problem cannot be Solve.
Two key points:
- "How to try to solve the problem of downsizing": Because the problem after the downsizing is a sub-problem of the original problem. So we can regard it as a new "original problem" with the same procedure to solve. This is the so-called "self calls itself"
- If the solution to the sub-problem fails, the program needs to return to the current problem to find other alternative routes. Therefore, all the things that have an impact on the current problem state when the current problem is reduced to a sub-problem should be invalidated. This is the so-called "backtracking time". Restore the scene
The basic unit of recursion is a transformation step composed of "shrink", "solve", and "expand".
2. Simple application of recursion and recursion
Common enumeration forms and traversal methods
Enumerated form | State space scale | General traversal method |
---|---|---|
Polynomial | n^k, k is a constant | Loop (for), recursion |
index | k^n, k is a constant | Recursion, bit operation |
arrangement | n! | Recursion, C++ next_permutation |
combination | Cn (m) | Recursion + pruning |
Divide and conquer
The divide-and-conquer method divides a problem into several smaller sub-problems, solves these sub-problems recursively, and then uses them to derive the solution of the original problem during backtracking.
Fractal
Stay in...