In the previous article, we explained by an example of the largest and most consecutive sub-arrays, presumably we have already figured out the meaning of the divide-and-conquer strategy and recursion, which may be more vague, but not clearly described in words. But it doesn't matter, I believe that through this blog post, we will be more clear and easy to use their own words to describe.
Through the previous two chapters of the study, we have approached two examples: merge sort and sub-array Max and. These two examples all use the divide to govern the strategy, through the analysis, we can draw the thought of divide and conquer the strategy: As the name implies, divide and conquer is to divide a primitive problem into many sub-problems, and the form of sub-problem is same as the original problem, only the scale is smaller, through the solution of sub-problem, the original problem also To summarize, it can be broadly divided into three steps:
decomposition : The original problem is divided into the same form of sub-problem, scale can be unequal, half or 2/3 of the 1/3 division.
Solve : For the solution of sub-problems, it is clear that the use of recursive solution, if the sub-problem is small enough, stop recursion, direct solution.
Merging : The solution of the sub-problem is merged into the solution of the original problem.
This raises the question of how to solve the sub-problem, which is obviously the way of recursive call stack. Therefore, recursion is closely connected with the Division method, and it is very natural to characterize the running time of the division and treatment by using recursive formula. So, if you want to ask me about the relationship between division and recursion, I will answer this: Divide and conquer relies on recursion, division is a kind of thought, and recursion is a means, recursion can characterize the time complexity of the divide-and-conquer algorithm . So the main point of this chapter is: How to solve the recursive type?
Three ways to solve recursive regression
There are three ways to do this: surrogate, recursive tree, and Main method. (This part of the following combined with some users of the summary and my summary obtained)
Substituting method:
Definition: First guess the existence of a certain bounds, and then use the mathematical induction to prove the correctness of the speculation.
Disadvantage: can only be used in the form of the solution is easy to guess the situation.
Summary: This method requires experience accumulation, which can be solved by converting to a similar recursive type as previously seen.
Recursive Tree method:
Cause: The substitution method is sometimes difficult to get a correct good guess value.
Purpose: Drawing a recursive tree is a direct way to get good guesses.
Analysis (emphasis): In a recursive tree, each node represents the cost of a recursive function calling a sub-problem in a collection. The cost of each layer in the recursive tree is added together to get a set of cost per layer, then add the cost of each layer to the total cost of recursion all levels.
Summary: Recursive trees are best used to generate good guesses and then be validated with substitution methods.
Recursive tree method is very intuitive, the total cost is to add the cost of all levels together to get. But analyzing the size of the total cost is not an easy thing to do, and sometimes it takes a lot of math knowledge.
Main method:
The main method is the best method, the book with "Recipes" to describe the use of this method, it can instantly estimate a recursive algorithm complexity. But we know that there is certainly a strong mathematical proof behind this, and for our users we just have to know how to use it.
Pros: Recursive for shapes such as t (n) = AF (n/b) + f (n)
Disadvantages: And can not solve all the above-mentioned forms of recursion, there are some special cases, see below analysis.
Analysis: Three cases, for example, focus on the section of the Circle Line:
Intuition: Look at the relationship between F (n) and Nlogba, who take who, equal to two multiply, but pay attention to see whether the difference factor nε. For 3), also see if the Condition AF (n/b) <= CF (n) is satisfied.
As mentioned above, the method can not be used in all the form of the recursive type, f (n) and Nlogba relationship must be a polynomial in the sense of less than greater than, that is, asymptotic relationship (asymptotic less than, asymptotic greater than), what is asymptotic, is the difference between the two factors nε. Therefore, between the conditions 1 and 2 there is a certain gap between the same situation 2 and see 3 There is also a certain gap between, for Case 3, but also to see whether the regular conditions are met.
Through the above, I believe that I should be clear about these three methods, you may be a little confused, but it doesn't matter, you just lack of examples of guidance, below we look at a few examples, which fully applied to these three methods.
The act of substituting: (Intuition, experience)
1), exercise 4.3-1:t (n) = T (n-1) + N
2), exercise 4.3-2:t (n) = T (N/2) + 1
Recursive Tree method:
1), to recursive t (n) = 3T (N/2) +n, using recursive tree to determine a good asymptotic upper bound, with the method of generation to verify.
2), to recursive t (n) = t (N/2) + N2, the recursive tree is used to determine a good asymptotic upper bound, which is verified by the method of substituting.
Main method:
1), for the following recursive type, using the main method to find the asymptotic tight certainty bounds.
A, T (n) = 2T (N/4) + 1
b, T (n) = 2T (N/4) + N1/2
C, T (n) = 2T (N/4) + N
D, T (n) = 2T (N/4) + N2
Well, the above is just for warm-up use, about more after-school exercise solution, please see: http://www.cnblogs.com/Jiajun/archive/2013/05/08/3066979.html
Introduction to the algorithm fourth chapter of division and Governance Strategy Programming practice (II.)