Computation of time complexity

Source: Internet
Author: User

First, the concept
The time complexity is the one that is most affected by the change in the total number of operations in the expression of n (without coefficients) such as: General total number of operations the expression is similar to this: A*2^N+B*N^3+C*N^2+D*N*LG (n) +e*n+fa! =0, Time complexity is O (2^n), a=0,b<>0 =>o (n^3); a,b=0,c<>0 =>o (n^2) and so on
eg
(1)   for (i=1;i<=n;i++)   //Loop n*n times, of course O (n^2) for            (j=1;j<=n;j++)                 s++;(2) for   (i=1;i<=n;i++) //cycle (n+n-1+n-2+...+1) ≈ (n^2)/2, because time complexity is not considered factor, so is O (n^2) for            (j=i;j<=n;j++)                 s++;(3) for   (I=1 ; i<=n;i++)//cycle (1+2+3+...+n) ≈ (n^2)/2, of course also O (n^2) for            (j=1;j<=i;j++)                 s++;(4)   i=1;k=0;  
      while (i<=n-1) {
           K+=10*i;

i++; }

It's circulating.
N-1≈n times, so is O (n)

(5) for (i=1;i<=n;i++)

                 for (k=1;k<=j;k++)
                       x=x+1;
//

Loop (1^2+2^2+3^2+...+n^2) =n (n+1) (2n+1)/6 (This formula to remember OH) ≈ (n^3)/3, regardless of coefficient, naturally is O (n^3)

In addition, in time complexity, log (2,n) (base 2) is equivalent to LG (N) (base 10) because of the logarithmic base-change formula:
Log (A, b) =log (c,b)/log (c,a) So, log (2,n) =log (2,10) *LG (n), ignoring the coefficients, which are of course equivalent
Second, the calculation method 1. The time spent in an algorithm execution is theoretically not calculated, and must be run on the machine to know. But we can not and do not need to test each algorithm, just know which algorithm spends more time, which algorithm spends less time on it. And the time that an algorithm spends is proportional to the number of executions of the statement in the algorithm, which algorithm takes more time than the number of statements executed. 
The number of times a statement is executed in an algorithm is called a statement frequency or time frequency. Note as T (N).
2. In general, the number of times the basic operation of the algorithm is repeated is a function f (n) of module N, so the time complexity of the algorithm is recorded: T (n) =o (f (n)). As the module n increases, the growth rate of the algorithm execution is proportional to the growth rate of f (n), so the smaller the F (n), the lower the complexity of the algorithm, the higher the efficiency of the algorithm. 
In the calculation of time complexity, first find the basic operation of the algorithm, and then according to the corresponding statements to determine its execution times, and then find T (n) The same order of magnitude (its same order of magnitude has the following: 1,LOG2N, N, nlog2n, n Squared, N of three square, 2 N-Square, n! ), when found, f (n) = The order of magnitude, if T (n)/f (n) the limit can be obtained a constant C, the time complexity of T (n) =o (f (n)).

3. Common time complexity
In order of magnitude increments, common time complexity is:

Constant order O (1),  logarithmic order O (log2n),  linear order O (n),  linear logarithmic order O (nlog2n),  Square order O (n^2), Cubic O (n^3),..., K-Order O (n^k), exponential order O (2^n ) 。
Wherein,1.O (n), O (n^2), Cubic O (n^3),..., K-Order O (n^k) is a polynomial order time complexity, respectively known as the first-order time complexity, second-level time complexity .... 
2.O (2^n), exponential order time complexity, this kind of not practical
3. Logarithmic order O (log2n),   linear logarithmic order O (nlog2n), in addition to the constant order, the efficiency of the most
Example: algorithm: for  (i=1;i<=n;++i)  {for     (j=1;j<=n;++j)     {         //This step belongs to the number of basic operation executions: N^2
          for (k=1;k<=n;++k)               c[I [J]+=a[i] [k]*b[K] [j]; //The step is a basic operation execution: N^3     }  }  there is T (n) = n^2+n^3, root According to the same order of magnitude in the brackets above, we can determine that n^3 is the same order of magnitude of T (N)  with f (n) = n^3, then the time complexity of the algorithm is obtained by calculating the limit of T (n)/f (n) to obtain the constant C  : t (n) =o (n^3)

Four

Definition: If the scale of a problem is n, the time required for an algorithm to solve this problem is T (n), which is a function of n (n) called the "time complexity" of the algorithm.

When the input n gradually increases, the limit of time complexity is called the asymptotic time complexity of the algorithm.

We often use large o notation to represent time complexity, noting that it is the time complexity of an algorithm. Large O means only that there is an upper bound, by definition if f (n) =o (n), that obviously establishes F (n) =o (n^2), it gives you an upper bound, but not the upper bounds, but people are generally used to express the former.

In addition, a problem itself has its complexity, if the complexity of an algorithm reaches the lower bounds of the complexity of the problem, it is called such an algorithm is the best algorithm.

"Big O notation": The basic parameter used in this description is N, the size of the problem instance, the function of expressing complexity or running time as N. Here the "O" denotes the magnitude (order), for example "binary retrieval is O (Logn)", that is, it needs "to retrieve an array of size n by logn steps" notation O (f (n)) indicates that when n increases, the run time will grow at a rate proportional to f (n).

This kind of progressive estimation is very valuable to the theoretical analysis and the approximate comparison of the algorithm, but in practice the details may also cause differences. For example, a low-cost O (N2) algorithm may run faster than an O (NLOGN) algorithm with a high additional cost in the case of n smaller. Of course, with n large enough, an algorithm with a slower rise function must work faster.

O (1)

Temp=i;i=j;j=temp;

The frequency of the above three individual statements is 1, and the execution time of the program segment is a constant independent of the problem size n. The time complexity of the algorithm is the constant order, which is recorded as T (N) =o (1). If the execution time of the algorithm does not grow with the increase of the problem size n, even if there are thousands of statements in the algorithm, the execution time is only a large constant. The time complexity of such an algorithm is O (1).

O (n^2)

2.1. Exchange of the contents of I and J
sum=0; (once)
For (i=1;i<=n;i++) (N-Times)
For (j=1;j<=n;j++) (n^2 times)
sum++; (n^2 times)
Solution: T (n) =2n^2+n+1 =o (n^2)

2.2.
for (i=1;i<n;i++)
{
y=y+1; ①
For (j=0;j<= (2*n); j + +)
x + +; Ii
}
Solution: The frequency of statement 1 is n-1
The frequency of Statement 2 is (n-1) * (2n+1) =2n^2-n-1
F (n) =2n^2-n-1+ (n-1) =2n^2-2
The program has a time complexity of T (n) =o (n^2).

O (N)

2.3.
a=0;
B=1; ①
for (i=1;i<=n;i++) ②
{
S=a+b; ③
B=a; ④
A=s; ⑤
}
Solution: Frequency of Statement 1:2,
Frequency of statement 2: N,
Frequency of Statement 3: N-1,
Frequency of Statement 4: n-1,
Frequency of Statement 5: N-1,
T (n) =2+n+3 (n-1) =4n-1=o (n).

O (LOG2N)

2.4.
I=1; ①
while (i<=n)
i=i*2; Ii
Solution: The frequency of statement 1 is 1,
The frequency of setting statement 2 is f (n), then: 2^f (N) <=n;f (n) <=log2n
The maximum value f (n) = log2n,
T (n) =o (log2n)

O (n^3)

2.5.
for (i=0;i<n;i++)
{
for (j=0;j<i;j++)
{
for (k=0;k<j;k++)
x=x+2;
}
}
Solution: When the i=m, J=k, the number of times the inner loop is k when I=m, J can take 0,1,..., m-1, so here the most internal cycle of 0+1+...+m-1= (m-1) m/2 times So, I from 0 to N, then the cycle has been carried out: 0+ (1-1) *1/2+ ... + (n-1) n/2=n (n+1) (n-1)/6 So time complexity is O (n^3).


We should also distinguish between the worst-case behavior of the algorithm and the expected behavior. For example, the worst-case run time for fast sorting is O (n^2), but the expected time is O (Nlogn). By carefully selecting the benchmark value each time, it is possible to reduce the probability of the squared condition (i.e. O (n^2)) to almost equal to 0. In practice, a well-implemented quick sort can generally be run at (O (NLOGN) time.
Here are some common notation:


Accessing an element in an array is a constant-time operation, or an O (1) operation. An algorithm that removes half of the data elements in each step, such as a binary search, usually takes O (LOGN) time. Using strcmp to compare two strings with n characters requires O (n) time. The regular matrix multiplication algorithm is O (n^3), as it is calculated that each element needs to multiply n on the elements and add them together, and the number of all elements is n^2.
The exponential time algorithm usually comes from the need to require all possible results. For example, the collection of n elements has a 2n subset, so the algorithm that requires all subsets will be O (2n). The exponential algorithm is generally too complex, unless the value of n is very small, because adding an element to the problem results in a doubling of elapsed time. Unfortunately, there are many problems (such as the famous "tour salesman Problem"), and the algorithms that have been found so far are exponential. If we do encounter this situation, we should usually replace it with an algorithm that looks for the best results.

Computation of time complexity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.