When analyzing the time complexity of algorithms based on Recursive equations, the following equations are common,
T (n) = A * t (N/B) + f (N)
A between 1, B> 1, F (n) is generally a simple function.
There are two ways to calculate the time complexity. First, the original formula is substituted into the recursive tree layer by layer to form a series,
Then we use a function to express it and get T (n ).
The second is to apply the master method of the main term theorem. In fact, the main term theorem is an induction of the recursive tree method, forming
Fixed computing methods, which can be calculated in three situations.
In these three cases, nlogba and F (n) are compared. Why do we need to compare these two functions?
Observing the original formula, we can see that nlogba is actually equivalent to the first item on the right of the recursive equation solved using the recursive tree method,
F (n) is the second item on the right of the recursive equation. In this way, the main term theorem is actually comparing the two function items that constitute the result,
In addition, this comparison is based on the order of magnitude (or the variation range), that is, for example, 2n and 28n are
The magnitude (magnitude of change) is equivalent.
In this way, there are three different scenarios:
-
F (n) <nlogba
That isF(N) = O (nlogba-E), E> 0 is a constant of any small number.
Or, F (n) is equal to n.LogSlow change of Ba, slow nE
So, T (n) î Q (nlogba)
-
F (n)> nlogba
That isF(N) = W (nlogba + E), E> 0 is a constant of any small number.
Or, F (n) is equal to n.LogBa changes fast and fast nE
Then, T (n) î Q (F(N ))
Simply put, T (n) is the order of magnitude of the two items on the right of the recursive equation, the blocks of which changes
-
F (n) = nlogba
That is, the order of magnitude of the two items is equivalent, so we can multiply this order by an LG n
T (n) î Q (nlogba * lg n)
Examples (The following example is from the network ):
T (n) = 5 T (n/2) + q (N2)
Case 1: IfF(N) = O (nlogba-E) for some constant E> 0 Then T (n) î Q (nlogba)
Determine: A, B, F (N) and logba
A = 5
B = 2
F (n) = Q (N2)
Logb A = log2 5 ≈ 2.32
Is F (n) î O (NLG 5-E) for E> 0?
Yes. F (n) = Q (N2) î O (NLG 5-e) = O (n2.32-E) for e ≈ 0.32
T (n) î Q (nlog25)
2. t (n) = 2 T (n/2) + n
Determine: A, B, F (N) and logb ()
Case 3: IfF(N) = Q (nlogba) Then T (n) î Q (nlogba lg n)
F (n) = n î Q (nlog22) = Q (N1)
T (n) î Q (nlog22 LG n) = Q (n lg n)
3. t (n) = 5 T (n/2) + q (N3)
Determine: A, B, F (N) and logb ()
A = 5
B = 2
F (n) = Q (N3)
Logb A = log2 5 ≈ 2.32
Case 2: IfF(N) = W (nlogba + E) for some constant E> 0
F (n) = Q (N3) drawing W (nlog25 + e) = W (n2.32 + E) for e ≈ 0.68
AndAF (N/B) ≤ CF (N)For some constant C <1 and all sufficiently large N
5 (n/2) 3≤ CN3
5n3/8≤ CN3
C = 5/8 <1
Then T (n) then q (N3)