time complexity and space complexity

time complexity

 

      The same problem can be solved by different algorithms, and the quality of the algorithm will affect the efficiency of the algorithm program. The purpose of algorithm analysis is to select the appropriate algorithm and improve the algorithm.

 

      The time complexity of an algorithm is a function that quantitatively describes the running time of the algorithm. This is a function of the length of the string representing the input value of the algorithm. The time complexity is often expressed in big- O notation, excluding the lower-order terms and leading coefficients of the function. When used this way, the time complexity can be said to be asymptotic, which considers the situation as the magnitude of the input value approaches infinity.

 

      Algorithm complexity is divided into time complexity and space complexity.

 

Its role: time complexity refers to the computational workload required to execute the algorithm; while space complexity refers to the memory space required to execute the algorithm. (The complexity of the algorithm is reflected in the amount of resources required by the computer when running the algorithm. The most important computer resources are time and space (ie register) resources, so the complexity is divided into time and space complexity).

 

 

calculation method

 

      1. In general, the number of repetitions of the basic operations in the algorithm is a function of the problem size n , which is represented 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 not equal to zero, then f(n) is said to be a function of the same order of magnitude as T(n) . Denoted as T(n)=O(f(n)) , O(f(n)) is called the asymptotic time complexity of the algorithm, referred to as the time complexity.

 

      Analysis: As the module n increases, the growth rate of the algorithm execution time is proportional to the growth rate of f(n) , so the smaller f(n) , the lower the time complexity of the algorithm and the higher the efficiency of the algorithm.

 

 

      2. When calculating the time complexity, first find out the basic operation of the algorithm, then determine its execution times according to the corresponding statements, and then find the same order of magnitude of T(n) (its same order of magnitude is as follows: 1 , log2n , n , n log2n , n squared, n cubed , 2 nth power, n! ), after finding out, f(n) = this order of magnitude, if T(n)/f(n) find the limit A constant c can be obtained , then the time complexity T(n) = O(f(n))

 

      Example: Algorithm:

 

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

 

 for(j=1; j<=n; ++j){

 

  c[i][j] = 0;// This step belongs to the number of executions of the basic operation: the square of n

 

  for(k=1; k<=n; ++k){

 

   c[i][j] += a[i][k] * b[k][j];//该步骤属于基本操作执行次数:n的三次方次

 

  }

 

 }

 

}

 

      则有 T(n) = n 的平方+n的三次方,根据上面括号里的同数量级,我们可以确定 n的三次方 为Tn)的同数量级

 

      则有 f(n) = n的三次方,然后根据 T(n)/f(n) 求极限可得到常数c

 

      则该算法的时间复杂度:T(n) = O(n^3) 注:n^3即是n3次方。

 

 

      3.时间复杂度比较简单的计算方法是:看看有几重for循环,只有一重则时间复杂度为O(n),二重则为O(n^2),依此类推,如果有二分则为O(logn),二分例如快速幂、二分查找,如果一个for循环套一个二分,那么时间复杂度则为O(nlogn)

 

 

空间复杂度

 

      一个算法的空间复杂度S(n)定义为该算法所耗费的存储空间,它也是问题规模n的函数。空间复杂度是对一个算法在运行过程中临时占用存储空间大小的量度。

 

 

      一个算法在计算机存储器上所占用的存储空间,包括存储算法本身所占用的存储空间算法的输入输出数据所占用的存储空间算法在运行过程中临时占用的存储空间这三个方面。

 

      一个算法的空间复杂度只考虑在运行过程中为局部变量分配的存储空间的大小,它包括为参数表中形参变量分配的存储空间和为在函数体中定义的局部变量分配的存储空间两个部分

 

 

      算法的空间复杂度一般也以数量级的形式给出。如当一个算法的空间复杂度为一个常量,即不随被处理数据量n的大小而改变时,可表示为O(1);当一个算法的空间复杂度与以2为底的n的对数成正比时,可表示为O(log2n);当一个算法的空间复杂度与n成线性比例关系时,可表示为O(n)。若形参为数组,则只需要为它分配一个存储由实参传送来的一个地址指针的空间,即一个机器字长空间;若形参为引用方式,则也只需要为其分配存储一个地址的空间,用它来存储对应实参变量的地址,以便由系统自动引用实参变量。

 

 

时间与空间复杂度比较

 

      对于一个算法,其时间复杂度和空间复杂度往往是相互影响的。当追求一个较好的时间复杂度时,可能会使空间复杂度的性能变差,即可能导致占用较多的存储空间;反之,当追求一个较好的空间复杂度时,可能会使时间复杂度的性能变差,即可能导致占用较长的运行时间。

 

      另外,算法的所有性能之间都存在着或多或少的相互影响。因此,当设计一个算法(特别是大型算法)时,要综合考虑算法的各项性能,算法的使用频率,算法处理的数据量的大小,算法描述语言的特性,算法运行的机器系统环境等各方面因素,才能够设计出比较好的算法。算法的时间复杂度和空间复杂度合称为算法的复杂度。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326693716&siteId=291194637