Time complexity analysis of the algorithm

Source: Internet
Author: User
Tags square root

In the previous article, the time complexity and its examples are described briefly, this article will analyze the algorithm time complexity and related methods.

1. Methods of pre-analysis and estimation

Because the post-mortem method relies more on the computer hardware, software and other environmental factors, it is sometimes easy to conceal the merits and demerits of the algorithm itself. so people often use the method of pre-analysis and estimation. This article does not describe the post-mortem statistics method. before the program is written, the algorithm is estimated according to statistical method. The time that a program written in a high-level language runs on a computer depends on the following factors:

(1). The strategy and method adopted by the algorithm; (2). The code quality generated by the compilation; (3).  The input scale of the problem; (4). The speed at which the machine executes instructions.

An algorithm consists of a control structure (order, branch, and Loop 3) and the original operation (refers to the operation of the intrinsic data type), then the algorithm time depends on the combined effect of the two. To facilitate the comparison of different algorithms for the same problem, it is common practice to choose from the algorithm a primitive operation that is basic to the problem (or algorithm type) being studied, with the number of repetitions of the basic operation as the time metric of the algorithm.

2. Complexity of Time

(1) Time frequency   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) Time complexity   in the time frequency mentioned earlier, n is called the scale of the problem, and when N is constantly changing, the time frequency t (n) will also change constantly. But sometimes we want to know what the pattern is when it changes. To do this, we introduce the concept of time complexity. In general, the number of iterations of the basic operation of the algorithm is a function of the problem size n, denoted by T (n), if there is an auxiliary function f (n), so that when n approaches infinity, the limit value of T (n)/f (n) is a constant that is not equal to zero, then f (n) is the same order of magnitude function of T (N). As T (n) =o (f (n)), is called O (f (n))   is the progressive time complexity of the algorithm, referred to as the complexity of time.

T (n) =0 (f (n))   indicates that there is a constant C, so that there is always  t (n) ≤c * f (n) when n tends to infinity. Simply put, T (N) is almost as large as f (n) when n tends to infinity. That is, when n approaches positive infinity, the upper bound of T (n) is C * f (n). The is not specified for f (n), but it is generally the simplest possible function. For example, O (2 n 2+n + 1) = O (3 n 2+n+3) = O (7 n 2 + N) =  O (  n 2 ) &NBSP, typically only O ( n 2) is allowed. Notice that a constant c is hidden in the large O symbol, so there is generally no coefficient in f (n). If T (n) is used as a tree, then O (f (n)) expresses the trunk, only cares about the trunk, and all the other details are discarded.
in various algorithms, if the algorithm has a constant number of statements executed, the time complexity is O (1), in addition, the time frequency is different, the time complexity may be the same, such as t (n) = n 2+3n+4 and T (n) =4 n 2+2n+ 1 They vary in frequency, but have the same time complexity as O ( n 2). Increments by order of magnitude, common time complexity: constant Order O (1) , logarithmic order O (log 2n ), linear order O (n),   Linear logarithmic Order O (nlog 2n ), Square order O ( n 2), cubic Order O ( n 3) ,..., K-Order O ( n k), exponential order O ( 2 N). with the increasing of the problem size n, the complexity of the time is not short and the efficiency of the algorithm is less.

Common algorithm time complexity from small to large in order:0 (1) <0 (log2n) <0 (n) <0 (nlog2n) <0 (n2) <0 (n3) ... <0 (2N) <0 (n!)

3, solve the algorithm time complexity of the specific steps:

Find out the basic statements in the algorithm;

The most frequently executed statement in the algorithm is the basic statement, usually the loop body of the most inner loop.

⑵ the order of the number of executions of the base statement;

Simply calculate the order of magnitude of the base statement execution, which means that all coefficients of the lower and highest powers can be ignored as long as the highest power in the function that guarantees the execution of the base statement is correct. This simplifies algorithmic analysis and focuses attention on the most important point: growth rates.

⑶ represents the time performance of the algorithm with a large 0 notation.

Place the order of magnitude of the base statement execution into the large 0 notation.

If the algorithm contains nested loops, the base statement is usually the inner loop body, and if the algorithm contains a parallel loop, the time complexity of the parallel loop is added. For example:

for (I=1; i<=n; i++)
x + +;
for (I=1; i<=n; i++)
for (j=1; j<=n; j + +)
x + +;

The time complexity of the first for loop is 0 (n) and the time complexity of the second for loop is 0 (n2), the time complexity of the entire algorithm is 0 (n+n2) =0 (n2).

0 (1) indicates that the execution number of the base statement is a constant, in general, as long as there is no circular statement in the algorithm, its time complexity is 0 (1). where 0 (log2n), 0 (N), 0 (nlog2n), 0 (n2), and 0 (n3) are called polynomial times, and 0 (2N) and 0 (n!) Called Exponential time . Computer scientists generally believe that the former (that is, polynomial time complexity algorithm) is an effective algorithm, this kind of problem is called P(polynomial, polynomial) class problem , and the latter (that is, exponential time complexity algorithm) called NP (non-deterministic polynomial, non-deterministic polynomial) problem .

In general, polynomial level complexity is acceptable, many problems have polynomial-level solutions-that is, the problem, for a size is n input, in n^k time to get results, called P problem. Some problems are more complicated, there is no polynomial-time solution, but you can verify that a certain guess is correct in polynomial time. Like asking if 4294967297 is prime? If you want to start directly, then you have to take the square root of less than 4294967297 of all the primes to see if we can divide evenly. Fortunately Euler tells us that this number equals 641 and 6700417 of the product, not prime, well verified, by the way trouble to tell the horse his conjecture is not tenable. Large number decomposition, Hamilton circuit and other problems, can be polynomial time to verify that a "solution" is correct, such problems are called NP problem.

4, the Simple Procedure Analysis law:

(1). For some simple input-output statements or assignment statements, it is approximate that an O (1) time is required

(2). For sequential structures, the time it takes to execute a series of statements in sequence can be done by using the "summation law" under the Big O

summation rule : means that if the 2-part time complexity of the algorithm is T1 (n) =o (f (n)) and T2 (n) =o (g (n)), then T1 (n) +t2 (n) =o (max (f (n), g (n)))

In particular, if T1 (m) =o (f (m)), T2 (n) =o (g (n)), then T1 (m) +t2 (n) =o (f (m) + g (n))

(3). For a selection structure, such as an if statement, its main time spent is the time spent executing then or else words, it should be noted that the test condition also requires O (1) time

(4). For the loop structure, the running time of the circular statement is mainly embodied in the execution of the loop body in several iterations and the time consuming of verifying the cycle condition, generally can use the "multiplication law" under the Big O.

multiplication Rule : means that if the 2-part time complexity of the algorithm is T1 (n) =o (f (n)) and T2 (n) =o (g (n)), then T1*t2=o (f (n) *g (n))

(5). For complex algorithms, it can be divided into several easy-to-estimate parts, and then use the summation and multiplication rules to technique the time complexity of the whole algorithm

The following 2 algorithms are also available: (1) if G (n) =o (f (n)), then O (f (n)) + O (g (n)) = O (f (n)), (2) O (Cf (n)) = O (f (n)), where C is a normal number

5, the common time complexity analysis explains:

(1), 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). Note: 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).

(2), O (n2)

2.1. Exchange of the contents of I and J

    1. sum=0; (one time)
    2. For (i=1;i<=n;i++) (n+1 times)
    3. For (j=1;j<=n;j++) (n2 times)
    4. sum++; (N2 times)

Solution: because θ (2n2+n+1) =n2 (θ is: to go to the lower order, remove the constant term, remove the common parameter of the higher order), so t (n) = =o (n2);

2.2.

    1. For (i=1;i<n;i++)
    2. {
    3. y=y+1; ①
    4. For (j=0;j<= (2*n); j + +)
    5. x + +; Ii
    6. }

Solution: The frequency of statement 1 is n-1
The frequency of Statement 2 is (n-1) * (2n+1) =2n2-n-1
F (n) =2n2-n-1+ (n-1) =2n2-2;

Also Θ (2n2-2) =n2
The program has a time complexity of T (n) =o (n2).

In general, the step Loop statement only takes into account the number of executions of the statement in the loop body, ignoring the steps in the statement, the step plus 1, the final value, control transfer and other components, when there are several loop statements, the time complexity of the algorithm is the maximum number of nested layers in the loop statement of the most internal statement frequency f (n).

(3), O (n)

    1. a=0;&NBSP;&NBSP;
    2.   b= 1;                       ①  
    3.   for  (i=1;i<=n;i++)  ②  
    4.       {    
    5.      s=a+b;     ③  
    6.      b=a;     ④    
    7.      a=s; ⑤  
    8.   }  

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).
(4), O (log 2n )

    1. i=1; ①
    2. Hile (I<=n)
    3. i=i*2;②

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 )

(5), O (n3)

    1. for (i=0;i<n;i++)   
    2.    {    
    3.        for (j=0;j<i;j++)      
    4.       {  
    5.           for (k=0;k<j;k++)   
    6.             x=x +2;&NBSP;&NBSP;&NBSP;&NBSP;
    7.        }  
    8.    }  

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 (n3).

6. Time complexity and space complexity of common algorithm

a rule of thumb: where C is a constant, if the complexity of an algorithm is C, log2n , n, and nlog2n , then the algorithm time efficiency is higher, if it is 2n ,3n , n!, then a slightly larger n will make the algorithm unable to move, in the middle of the few are passable.

Algorithm time complexity analysis is a very important problem, any programmer should master its concept and basic method, and be good at the mathematical level to explore its essence, can accurately understand its connotation.

Time complexity analysis of the algorithm

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.