I. Preface
The main idea of recursive functions is to split a problem into several subproblems (The problem size is reduced), solve these subproblems respectively, and then combine the obtained results. Therefore, the complexity of recursive functions includes the cost of splitting, the cost of combined results (collectively referred to as non-recursive costs), and the cost of solving subproblems (recursive complexity ).
The complexity analysis of recursive functions is mainly based on Recursive equations and recursive trees. First, the complexity of a recursive function is expressed as a recursive equation. Then, the approximate complexity of a recursive function is obtained through mathematical computation or recursive tree analysis. Here we mainly introduce the complexity analysis methods of the two recursive functions. (This mainly refers to the time complexity)
1. divide-and-conquer (divide and conquer)
The complexity of this type of recursive function can be expressed
T (n) = Bt (N/C) + f (n) ------------------------ (1)
Obviously, this method Splits a problem into several subproblems of the same size. F (n) is called a non-recursive price. It mainly refers to the price of the splitting problem and the combination result.
2. Chip and conquer
The complexity of this type of recursive function can be expressed
T (n) = T (n-C) + f (n) ------------------------ (2)
Or, more generally,
T (n) = Bt (n-C) + f (n) ------------------------ (3)
Obviously, this method gradually reduces the problem size and keeps decreasing to the problem size that can be solved (that is, base-case ).
Ii. Recursive tree
The following describes an important tool for analyzing complexity.-----Recursive tree
The node of the recursive tree has two fields, for example:
T (size) indicates the complexity of the function when the problem size is size. Nonrec. Cost indicates the non-recursive cost when the problem size is size.
Each subnode of the root node represents the complexity of a subproblem split. In this way, the problem is recursively decomposed. It reaches the leaf node, that is, base-case. In the previous discussion, we did not involve base-case. When using a recursive tree to analyze the complexity,Let's assume thatBase-caseComplexity is1.
For example, we can clearly explain how to construct a recursive tree.
Example1: Build a recursive tree by the recursive equation T (n) = 2 T (n/2) + n
First, construct the root contact
Its subnode is
......, And so on. Therefore, the final recursive tree is:
Recursive tree rules:
Root Node complexity = non-recursive complexity of all non-leaf nodes + complexity of leaf nodes.
Therefore, in the above example, the non-recursive complexity of each layer is N, while the base-case occurs in approximately lgn layer (n/2 ^ d = 1; D = lgn ). Because the complexity of base-case is 1, t (n) ≈ nlgn, that is
Recursive tree is an important tool for analyzing and calculating recursive equations. It intuitively shows the complexity of recursive functions and makes them easy to understand.
III.Divide-and-conquerSolution
In the above example, we use the recursive tree rule to obtain the approximate time complexity of the recursive function. However, recursive trees can only roughly solve a small number of problems. Here we will introduce a more general solution to divide-and-conquer class problems-master theorem.
However, in order to better understand and use the master theorem, we need to further analyze the recursive tree and introduce an important parameter:Key power
Recall the recursive tree of recursive equation (1.
Each time a layer is added, the problem size of the function is reduced by a factor of C. Assume that base-case appears in layer d, then n/C ^ d = 1; D = 1_c (n ). use the bottoming formula D = lg (N)/lg (c ).
At the same time, the number of nodes on each layer increases by B times. Therefore, the number of nodes in layer D is L = B ^ d (L indicates the number of nodes in layer D ). Take the logarithm of the two sides, get lg (L) = DLG (B); Bring d = lg (N)/lg (c) to the upper formula, get lg (l) = (lg (B)/lg (c) lg (n ). at this time, the coefficient before lg (n) is called the key power. E is generally used for representation.
So E = lg (B)/lg (c), while lg (L) = ELG (N), we can see that l = n ^ e.
The famous master theorem is as follows:
The recursive equation T (n) = Bt (N/C) + f (n) has the following solutions:
If for some constant, then.
If, then.
If for some constant, and if for some constantC<1, and all sufficiently largeN, Then
The master theorem is very simple and convenient in solving the complexity of recursive functions. However, it is difficult to understand the master theorem. We can use the recursive tree to understand it. The proof of the master theorem is also complex, so it is not listed here.
4.Chip-and-conquerSolution
Let's first consider the general situation of the Chip-and_Conquer problem, recursive equation (3)
T (n) = Bt (n-C) + f (N)
Similarly, we use a recursive tree for analysis. The recursive tree is basically the same as the recursive tree for divide-and-conquer problems, so we will not repeat it here. Only analyze the recursive tree.
Obviously, base-case appears in the N/C layer, and the cost of each node in the N/C layer is 1 (hypothesis)
First, calculate the non-recursive cost of all nodes at each layer. The non-recursive cost of all nodes at layer I is (B ^ I) * F (n-ic ).
Therefore, the complexity of the root node (that is, the complexity of the recursive function) is
T (n) = Σ 0... N/C (B ^ I) * F (n-ic). Make H = (N/C)-I, get
T (n) = B ^ (N/C) Σ 0... N/C f (CH)/B ^ hε ⊙ (B ^ (N/C ));
Therefore, the complexity of such recursive functions increases exponentially. Obviously, exponential growth algorithms do not have any significance. The Recursive Method of this problem is not practical.
However, considering its special situation: recursive equation (2)
T (n) = T (n-C) + f (N)
That is, B = 1.
Complexity
T (n) = Σ 0... N/C f (n-ic) = Σ 0... N/C f (CH) ≈ (1/C) limit 0... NF (x) dx (definition of points)
At this time, if F (n) ε ⊙ (N ^ A), T (n) ε ⊙ (N ^ (a + 1 ));
If F (n) ε ⊙ (logn), T (n) ε ⊙ (nlogn); this complexity is acceptable.
V. Summary
In short, when calculating and analyzing the complexity of recursive functions, we mainly use recursive equations and recursive trees. In particular, recursive trees can be applied in more cases to solve the complexity of recursive functions. For divide-and-conquer class problems, there is also a convenient theorem: Master theorem. This theorem is very convenient to use, but it has its limitations. Therefore, it has an extended theorem of the master theorem, but it is limited by space. I will not introduce it here.