1. What is the complexity of time?
Time complexity is the time consumption of an algorithm, which is the function of the algorithm to find the problem size n. The incremental time complexity is the magnitude of the time complexity of the algorithm when the problem scale tends to be infinite. Usually we calculate the time complexity of the algorithm for T (n) =o (f (n)). Usually we always consider the worst-case time complexity.
Common time complexities are: constant O (1), Logarithmic O (logn), linear O (n), O (Nlogn), square O (n^2), K-th square O (n^k), exponent O (2^n)
2. Examples of time complexity
for0; i < n; ++i) //循环次数为n^2次,O(n^2) for0; j < n; ++j) suma[i][j]
1;while (i <= n) //循环次数为1 + logn, O(logn) i = i*2;
3. Calculation method
- Find out the basic operation first;
- Find the execution times of each statement;
- The time complexity of each statement is then added.
- The final conversion to the large O-meter method (similar to infinity and infinity in higher mathematics)
4. Recursive time complexity
Reference
1. http://www.cnblogs.com/jy02414216/archive/2012/08/10/2633071.html
2. http://www.cnblogs.com/wu8685/archive/2010/12/21/1912347.html
When an algorithm contains recursion, the problem of computational complexity or the problem of solving the recursive equation is transformed.
There are several common solutions:
Iterative method:
The recursive equation is expanded iteratively to make it a non-recursive sum, and then the estimation of the solution of the Cheng equation is achieved by the estimation of the sum formula.
Example
The calculation time of an algorithm is: t (n) = 3T (N/4) + O (n), where T (1) = O (1), iteration Two can expand the right side to:
T (n) = 3T (N/4) + O (n)
= O (n) + 3 (O (N/4) + 3T (N/42))
= O (n) + 3 (O (N/4) + 3 (O (N/42) + 3T (N/43))
As can be seen from the above, this is a recursive equation, we can write the equation after the iteration i:
T (n) = O (n) + 3 (O (N/4) + 3 (O (N/42) + ... + 3 (n/4i + 3T (n/4i+1)))
When n/4i+1 = 1 o'clock, T (n/4i+1) = 1, then:
T (n) = n + (3/4) + (32/42) n + ... + (3i/4i) n + (3i+1) T (1) < 4n + 3i+1
T (n) = n + (3/4) + (32/42) n + ... + (3i/4i) n + (3i+1) T (1) < 4n + 3i+1
and by n/4i+1 = 1, I
Recursive tree:
Example 1
T (n) = 2T (N/2) + N2
Iterating 2 times can be done by:
T (N) = n2 + 2 (2T (N/4) + (N/2) 2)
You can also continue the iteration to fully expand it to get:
T (N) = n2 + 2 ((N/2) 2 + 2 ((N/22) 2 + 2 ((N/23) 2 + 2 ((N/24) 2 +...+2 ((n/2i) 2 + 2T (n/2i + 1))))) ...... (1)
And when n/2i+1 = = 1 o'clock, the iteration ends.
Expand the brackets (1) to:
T (N) = n2 + 2 (N/2) 2 + (N/22) 2 + ... + 2i (n/2i) 2 + 2i+1t (n/2i+1)
This happens to be a tree-shaped structure, which leads to recursive tree method.
(a) (b) (c) (d) in the figure is step 1,2,3,n of the recursive tree generation, respectively. In each node, the current free-entry N2 is left in it, and the two recursive items T (N/2) + t (N/2) are amortized over his two child nodes, so loop.
The sum of all the nodes in the graph is:
[1 + + (2) + (3) + ... + (n2) i] = 2n2
The time complexity of O (N2)
The rules that can be used to get a recursive tree are:
(1) The node of each layer is t (n) = KT (n/m) + f (n) in the value of f (n) under the current n/m;
(2) The number of branches per node is k;
(3) The right side of each layer marks the and of all nodes in the current layer.
Example 2
T (n) = t (N/3) + t (2N/3) + N
Its recursive tree is as follows:
Complexity of Time