Algorithm (a): The time complexity of the algorithm complexity and space complexity

table of Contents

concept

Complexity Analysis

Time complexity analysis

Space complexity analysis

to sum up


concept

Complexity of the algorithm : Algorithm refers to the preparation of resources into an executable program, the runtime required, including time resources (operation time-consuming algorithm) and memory resources (memory size run). It is an important indicator to measure the pros and cons of the algorithm is divided into time complexity and space complexity subdivided in accordance with the required resources.

Time Complexity : The time required to run the algorithm with the data amount relationship, denoted T (n), n is the size of the algorithm.

Space complexity : an algorithm during operation temporary occupation measure of the size of storage space, denoted S (n), n is the size of the algorithm.

Big O notation : indicates algorithm time and symbolic complexity space is typically expressed as denoted S (n) = O (f (n)), T (n) = O (f (n)), n is the data scale, f (n) is typically a function, such as f (n) = 2n + 1 ; f (n) = n ^ 2; f (n) = logn like. And T (n) is also generally a function mathematically, the relationship between running time and the corresponding curve is a function of n, n is gradually increased to as very big, the number of factors to remove the main trend of the curve, such function constants, coefficients, extracts a core element power, the square root of the number, the form of the function represented by the core elements become asymptotic time and space complexity of the algorithm or progressive time complexity of, and time complexity is also known space complexity.

Complexity Analysis

Data structures and algorithms to solve the problem itself, "fast" and "province", namely how to make your code run faster, cheaper storage space. Therefore, the efficiency of the algorithm is a very important consideration indicators. How to measure the efficiency of the algorithm that the code you write it? Here it is necessary to perform complex analysis.

Later statistics: a different environment to run the test code in practice, the method of comparing the test results. However, there are some limitations, one test result depends on the test environment: hardware environment affect the test results, such as Intel Core i9 processor and Intel Core i3 processor difference, and second, test results affected by the size of data: the test may not be too small a true reflection of the performance of the algorithm.

Therefore, do not produce a specific test data may need only a rough method for estimating algorithm performance, this is what we want to say today, space complexity and time complexity, the corresponding analytical methods and time complexity analysis can be divided law, space complexity analysis.

Time complexity analysis

In the front we have said, the time complexity of Big O notation is T (n) = O (f (n)), then the question is, how this formula is too out of it, following on to the next analysis.

Asymptotic time complexity official definition : if there exist a function f (n), such that when n approaches infinity, T (n) / f (n) is not equal to zero limit constant, called f (n) is a T (n) is a function of the same order. Denoted by T (n) = O (f (n)), said O (f (n)) is a progressive time complexity of the algorithm, referred to as time complexity . Asymptotic time complexity of O is represented by capital, it is also known as big-O notation.

So, the question is, in general we can roughly calculate the amount of data as a function of n-type and calculate the required time T, based on how the introduction of a known functional relationship time complexity of it, the inference rule focused on the following:

  1. If the running time is constant magnitude, it is represented by a time complexity;
  2. Retaining only the highest order term function of time;
  3. If the highest order term presence in front of the highest order coefficient is omitted.

 

The time complexity of O (. 1) : a method, there are four statements, assumed to be performed for each statement of the time t, the time relationship between data n and T = 4t. That no matter how n increases, computation of the time 4t. The same statement in a method for the k number of methods, T = kt, kt value is necessarily a constant, and n does not matter, as this is called a constant level, in accordance with the first derivation formula , computation time is constant, the function of the same order of T = kt f (n) = 1, then the time complexity T (n-) O = (. 1) ;

    public void eat(int n){
       System.out.println("1111");
       System.out.println("2222");
       System.out.println("3333");
       System.out.println("4444");
    }

In the following figure shows the function, Y-axis is the time complexity, X n data change axis

 Time complexity of O (n) : Suppose the execution time of a statement is t, perform a cycle time 3t, n times the cycle time 3TN, time to execute a method for T = 3tn + t, according to formula 2, the presence of the highest order term n, thus leaving only the highest order term, T = 3tn; according to the formula III removing the highest order term coefficient 3t, which is of the same order finally to obtain the function f (n) = n, the time complexity formula T ( n) = O (n);

public void eat1(int n){
    System.out.println("000");
    for(int i=0; i<n; i++){;
        System.out.println("111");
        System.out.println("222");
        System.out.println("333");
    }
}

In the following figure shows the function, Y-axis is the time complexity, X n axis data changes, any changes in the data, the time constant is fixed at

Time complexity O (N²) : Suppose the time execution of a statement is t, the time of a process T = t + n (3t + 2tn) is performed, i.e. T = t + 3tn + 2tn², according to the formula 2 out t 3tn, The three formulas removal coefficient 2t, of the same order finally to obtain the function f (n) = n²; i.e., time complexity T (n) = O (n² )

public void eat1(int n){
    System.out.println("000");
    for(int i=0; i<n; i++){;

        System.out.println("111");
        System.out.println("222");
        System.out.println("333");

        for(int k=0; k<n; k++){;
            System.out.println("444");
            System.out.println("555");
        }
    }
}

Corresponding to the function curve as follows:

Similarly, the other types as a function of time complexity of O (logn), O (2 ^ n), O (n³) will not repeat, etc., directly on the map:

 

Space complexity analysis

The time complexity is not used to calculate the specific procedures and time-consuming, space complexity is not used to calculate the actual space occupied by the program. Space complexity of the algorithm is a measure of a temporary occupation of storage space during operation, also reflects a trend. Commonly used spatial complexity O (1), O (n), O (n²), the relative time complexity, less common species. In most cases, the efficiency of the algorithm running time is the most important. As long as the storage space algorithms do not reach the computer unacceptable extent. So, often in exchange for a more efficient algorithm run-time efficiency and space complexity sacrifice.

Algorithm takes on computer memory space comprises three parts:

O : the algorithm input and output data storage space is transferred from the calling function by the parameter list, it will not change with different algorithms.

算法本身:存储算法本身所占用的存储空间与算法的长短成正比,要压缩这部分存储空间,就必须编写出较短的算法。然而,算法想要实际应用需要根据需求采取不同的编程语言来实现,不同编程语言实现的代码长短差别很大,然而存储空间都在可接受范围之内。

临时内存:临时内存是运行算法时临时占用的内存空间,分为两类。一类是占用固定内存大小,不随着问题规模变化而变化,一种是随着问题规模变化而变化。

空间复杂度O(1):如下代码执行所需要的临时空间不随着变量n的大小而变化,即此空间复杂度为一个常量,代码中的 i、j、m 所分配的空间都不随着处理数据量变化,因此它的空间复杂度 S(n) = O(1);

int i = 1;
int j = 2;
++i;
j++;
int m = i + j;

空间复杂度O(n):这段代码中,第一行new了一个数组出来,这个数据占用的大小为n,这段代码的2-6行,虽然有循环,但没有再分配新的空间,因此,这段代码的空间复杂度主要看第一行即可,即 S(n) = O(n)

int[] aar = new int[n]
for(i=1; i<=n; ++i)
{
   j = i;j++;
}

总结

算法的复杂度分为时间复杂度和空间复杂度,大多数情况下,更加注重时间复杂度。

通常时间复杂度近似于算法运行时间时间曲线,在数据量或者说问题规模变得很大的时候,时间复杂度高的算法耗时就高,时间复杂度低的耗时就低,从最简答到最复杂依次为:O(1)<O(logn)<O(n)<O(n²)。时间复杂度的高的通常伴随着深层次的循环和递归。减少代码的循环层次可以降低时间复杂度。

而复杂的程序代码,使用多层级的方式可以减少代码量,降低空间复杂度,层级少了较大可能需要多写代码,导致空间复杂度的提高,另外以我个人理解,对于空间的需求一般在算法初始化时就进行了预估和分配,在后续代码中虽然会产生少量临时变量,但是关键还是看初始化部分,这可能就是空间复杂度的分类比时间复杂度分类少得多。

辛辛苦苦敲了几天,拼拼凑凑加理解也算告一段落了,感谢读者的阅读,如有出入,请指正,谢谢!

发布了6 篇原创文章 · 获赞 6 · 访问量 660

Guess you like

Origin blog.csdn.net/huangggf/article/details/103793009