The basic idea of divide and conquer law:A n-scale problem is divided into k smaller subproblems. These subproblems are independent of each other and are the same as the original problems. Recursively solve these problems, and then combine the solutions of each subproblem into the solutions of the original problem. Its general algorithm design pattern is as follows:
Divide-and-conquer(P)
{
If(| P | <= N0)Adhoc(P); // solves small-scale problems
DivideP into smaller subinstancesp1, P2,..., PK; // Decomposition Problem
For(I = 1, I <= K, I ++)
Yi =Divide-and-conquer(PI); // recursive solution to each subproblem
ReturnMerge (Y1,..., yk); // merge the solutions of each subproblem into the solutions of the original problem.
}
People have found from a large number of practices that it is best to make the size of sub-problems roughly the same when designing algorithms using the divide and conquer method. It is effective to divide a problem into k sub-Problems of the same size. This method roughly equals the size of sub-problems.Balancing sub-ProblemsIdea, it is almost always better than the sub-problem scale.
Governing conditions of the division and Control Law:
The problems solved by the Division and control law generally have the following characteristics:
- This problem can be easily solved if the scale is reduced to a certain extent, because the computing complexity of the problem generally increases with the increase of the scale of the problem, so most problems satisfy this feature.
- This problem can be broken down into several small-scale identical problems, that is, the problem has the optimal sub-structure nature. This feature is the prerequisite for applying the division and control method, and is also feasible for most problems, this feature reflects the application of recursive thinking.
- The solutions of subproblems decomposed by this problem can be combined into the solutions of this problem. Whether or not the division and control method can be used depends on whether the problem has this feature. If the problem has the first two features, if you do not have the third feature, considerGreedy AlgorithmOrDynamic Planning.
- Each subproblem identified by the problem is independent of each other, that is, the subproblem does not include a public subproblem. This feature involves the efficiency of the Division and control law. If the sub-problems are not independent, the division and Control Law should do a lot of unnecessary work to solve the public sub-problems repeatedly, at this time, although it can also be used, but generally usedDynamic PlanningBetter.
Complexity Analysis of the division and Control Law:
A division and control method divides the problem of N into k subproblems of N/m. The decomposition threshold value N0 = 1, and the ADHOC solution scale is 1, which consumes 1 unit of time. Then, it takes F (n) to resolve the original problem into k sub-problems and merge the solutions of K sub-problems into the original problem using merge. T (n) indicates the calculation time required to solve the problem of the division and Control Method | p | = n:
Concept of recursion:
Directly or indirectly, calling an algorithm is calledRecursive Algorithms. The function defined by the function itself is calledRecursive functions.
Sub-problems produced by the Division and control method are often small models of the original problems, which provides convenience for the use of recursive technology. In this case, the sub-problem type can be consistent with the original problem type, but its scale is constantly reduced. In the end, the sub-problem can be easily solved directly. This naturally leads to the generation of recursive processes.
Grouping and Recursion are like twins. They are often applied to algorithm design at the same time, and many efficient algorithms are generated.
Advantages:The structure is clear and readable, and it is easy to use mathematical induction to prove the correctness of the algorithm. Therefore, it brings great convenience for designing algorithms and debugging programs.
Disadvantages:The Running Efficiency of recursive algorithms is low. The computing time consumed and the storage space occupied are much higher than that of non-recursive algorithms.
Solution:Eliminate recursive calls in recursive algorithms to convert them into non-recursive algorithms.
- A user-defined stack is used to simulate the recursive call stack of the system. This method is versatile, but it is actually recursive in nature. However, the optimization effect is not obvious because it is done manually by the compiler.
- Implement recursive functions using recursion.
- Some Recursion can be converted to tail recursion through transformation, so as to obtain the result iteratively.
The latter two methods have greatly improved the time-space complexity, but their applicability is limited.