268. The factors and the execution time of the algorithm related

1. deciding factor

1.1 algorithm selection policy

1.2 size of the problem

1.3 programming language of

Quality of the machine code generated by the compiler 1.4

Computer-executable instructions of speed 1.5

 

 

2. Other influence elements

 

 

3. The scale of the problem (time complexity)

3.1 Definitions

  "Run the workload," the size of a particular algorithm, depends only on the scale of the problem (usually expressed as a whole number n), or that it is a function of problem size.

  If, as the problem size n grows, the same algorithm execution time of growth and f (n) growth rates can be recorded as: T (n) = O ( f (n)), known as T (n) for the (asymptotic) time complexity of the algorithm.

  An algorithm is a control structure (sequential, branch and loop 3 types), and the original operation (referring to the inherent data type of operation) configuration, running time depends on the combined effect of both.

 

 

 

3.2 Time estimation algorithm complexity

(Time Complexity)​

3.2.1 definitions

  For selection of operating a primary problem is studied from the basic operation of the algorithm, the number of times the basic operation in the algorithm is repeatedly performed as a measure of running time criterion.

  "Basic operation" means the operation is repeated the number of times proportional to the running time and the algorithm.

 

  = Σ execution time of the algorithm the original operation (i) × number of executions of the original operation (i) the execution time

  Detailed algorithm execution time and the number of operations performed and is proportional to the original

 

  = + Original algorithm operating control structure (inherent data type of operation)

 

1 two matrix multiplication 
eg1: two matrix multiplication 
void Mult (INTA [], int B [], int & C []) {
 // a two-dimensional memory matrix array elements, c is a, and the product of b 
    for (I = . 1 ; I <= n-; ++ I) { 
         for (J = . 1 ; J <= n-; ++ J) { 
            C [I, J] = 0 ; 
             for (K = . 1 ; K <= n-; ++ K) 
                C [I, J] + = A [I, K] * B [K, J]; 
            } 
    } 
} basic operations: multiplication time complexity: O (n- ^ . 3 )    

2 Select Sort

3 bubble sort

4 has the following recursive function fact (n), the time complexity analysis
 int FACT ( int n-) { IF (n-<= . 1 ) return ( . 1 ); ( . 1 ) 
 the else  return (n-* FACT (N- . 1 )) ; ( 2 ) 
} 
solution: Let fact (n) is a complex function of running time T (n), 
     the function statement ( 1 ) of the running time is O ( 1 ), 
     statements ( 2 ) the running time: T (N- . 1 ) + O ( . 1 ), 
     wherein O ( . 1 ) as the basic operation time, 
thus: T (n-) = O ( . 1 ) + T (N- . 1)
             = O ( . 1 ) O + ( . 1 ) + T (N- 2 )
             = ...... 
             = (N- . 1 ) * O ( . 1 ) T + ( . 1 ) 
             = O n-* ( . 1 ) 
             = O (n-) is fact (n) time complexity is O (n). 

 

3.2.2 Analysis of the general steps of the algorithm time complexity 

 

 

 

 

3.2.3 Progressive symbol

  Let n be the size of the problem algorithm, usually indicates a relationship between growth and the execution time of the algorithm with a large n O, large Ω and Θ, three progressive symbol.

 

3.2.3.1 Big O notation

definition

  Definition 1 (Big O notation), f (n) = O (g (n)) (read as "f (n) is g (n) is large O") if and only if the presence of normal amounts c and n0, so that when n≥n0 time, f (n) ≤cg (n), i.e., g (n) is f (n) upper bound.

 

 The 3n + 2 = O (n) , when because when n≥2, 3n + 2≤4n.
 10n2 + 4n + 2 = O ( n4), because the time when n≥2, 10n2 + 4n + 2≤10n4.

 

  Big O notation used to describe the rate of the upper bound, represents f (n) growth at most as fast as g (n) growth, that is to say, when the input size is n, the maximum time consuming algorithms. The lower bound on the order, the more valuable results, therefore, to 10N 2 + 4N + 2, O (n- 2 ) than O (n- . 4 ) value.

 

  When an algorithm with a large time notation O, always using the most valuable g (n) represents, called "upper bound compact" or "upper bound really tight."

  In general, if there is

 

 

 

The relationship between several kinds of time complexity

Description:

1. In the case where it is difficult to accurately calculate the basic operation execution times (or frequency statements), just to find out its growth or on the order of n to the time complexity of the algorithm can be specifically a 2 being the best, the worst (also known as the worst) and average three cases discussed.

Unless otherwise stated, refer to the time the normal worst-case complexity.

 

example

1 two matrix multiplication 
eg1: two matrix multiplication 
void Mult (INTA [], int b [], int & C []) { 
// a two-dimensional memory matrix array elements, c is a, and b product 
    for (I =. 1; I <= n-; ++ I) { 
        for (J =. 1; J <= n-; ++ J) {  C [I, J] = 0 ;  for (K =. 1 ; K <= n-; ++ K) C [I, J] + = A [I, K] * B [K, J];}}} basic operations: multiplication time complexity: O (n ^ 3)

2 Select Sort

3 bubble sort

4有如下递归函数fact(n),分析其时间复杂度
int fact(int n){ 
​if(n<=1) return(1);                (1) 
else return(n*fact(n-1));        (2)
}
解:设fact(n)的运行时间复杂函数是T(n),​
     该函数中语句(1)的运行时间是O(1),​
     语句(2)的运行时间为:T(n-1)+O(1), 其中O(1)为基本运算时间,​ 因此: T(n) = O(1)+T(n-1) = O(1)+O(1)+T(n-2) = …… = (n-1)*O(1)+T(1) = n*O(1) = O(n)则fact(n)的时间复杂度为O(n)。​​

 

 

3.2.3.2 大Ω符号

  定义2(大Ω符号),f(n)= Ω(g(n))(读作“f(n)是g(n)的大Ω”)当且仅当存在正常量c和nθ,使当n≥n0时,f(n)≥cg(n),即g(n)为f(n)的下界。

 

  如3n+2=Ω(n),因为当n≥1时,3n+2≥3n。
  10n2+4n+2=Ω(n2),因为当n≥1时,10n2+4n+2≥n2。  

 

  大Ω符号用来描述增长率的下界,表示f(n)的增长最少像g(n) 增长的那样快,也就是说,当输入规模为n时,算法消耗时间的最小值。
与大O符号对称,这个下界的阶越高,结果就越有价值,所以,对于10n2+4n+2,Ω(n2)比Ω(n) 有价值。一个算法的时间用大Ω符号表示时,总是采用最有价值的g(n)表示,称之为“紧凑下界”或“紧确下界”。

  一般地,如果,有

 

 

3.2.3.3大Θ符号

  定义3(大Θ符号),f(n)= Θ(g(n))(读作“f(n)是g(n)的大Θ”)当且仅当存在正常量c1、c2和n0,使当n≥n0时,有c1g(n)≤f(n)≤c2g(n),即g(n)与f(n)的同阶。

 

  如3n+2=Θ (n),10n2+4n+2=Θ(n2)。


  一般地,如果,有f(n)=Θ(nm)。

  大Θ符号比大O符号和大Ω符号都精确,f(n)=Θ(g(n),当且仅当g(n)既是f(n)的上界又是f(n)的下界。

 

 

3.2.3.4关系

 

 

 

3.3算法的最好、最坏和平均情况

  设一个算法的输入规模为n,Dn是所有输入的集合,任一输入I∈Dn,P(I)是I出现的概率,有ΣP(I) =1,T(I)是算法在输入I下所执行的基本语句次数,则该算法的平均执行时间为:A(n)=。  

  也就是说算法的平均情况是指用各种特定输入下的基本语句执行次数的带权平均值。

  算法的最好情况为:G(n)=,是指算法在所有输入I下所执行基本语句的最少次数。

  算法的最坏情况为:W(n)=,是指算法在所有输入I下所执行基本语句的最大次数。

 

 

4.算法选用的策略

4.3非递归算法的时间复杂度分析

  对于非递归算法,分析其时间复杂度相对比较简单,关键是求出代表算法执行时间的表达式。
  通常是算法中基本语句的执行次数,是一个关于问题规模n的表达式,然后用渐进符号来表示这个表达式即得到算法的时间复杂度。

 

 

4.2递归算法的时间复杂度分析

  递归算法是采用一种分而治之的方法,把一个“大问题”分解为若干个相似的“小问题”来求解。
  对递归算法时间复杂度的分析,关键是根据递归过程建立递推关系式,然后求解这个递推关系式,得到一个表示算法执行时间的表达式,最后用渐进符号来表示这个表达式即得到算法的时间复杂度。

 

Guess you like

Origin www.cnblogs.com/ZanderZhao/p/11490735.html