-01 Data Structures and Algorithms, Complexity Analysis

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? Then you need to use the complexity of the analysis, it is divided into "time complexity analysis " and "space complexity analysis " does not indicate the specific code for real time execution, but the description code execution time (or space) with the data trend-scale growth.

Big O notation

All code execution time T (n) n is proportional to the frequency of each line of code. We can summarize this law into a formula.

T(n)=O(f(n))

Wherein, T (n) represents the performance of the script; n-represents the size of data size; f (n) represents the sum of the number of each line of code is executed. Because this is a formula, so with f (n) is represented. Formula from O, the execution time code T (n) and f (n) is proportional to the expression.

According to this notation, the code view.

1  int cal(int n) {
2    int sum = 0;
3    int i = 1;
4    for (; i <= n; ++i) {
5      sum = sum + i;
6    }
7    return sum;
8  }

Second and third lines are needed over a 4, 5, row operation n times, it is necessary to perform the time 2n * O, the total execution time of this code is (2n + 2) * O. It can be seen, T (n) = O (2n + 2).

 

Look at another piece of code

 1  int cal(int n) {
 2    int sum = 0;
 3    int i = 1;
 4    int j = 1;
 5    for (; i <= n; ++i) {
 6      j = 1;
 7      for (; j <= n; ++j) {
 8        sum = sum +  i * j;
 9      }
10    }
11  }

 

2,3,4 lines of code needed for each row perform a pass, 5,6 line loop is executed n times, the execution time required in 2n * O, 7 and 8 lines n2 times the loop is executed, so It requires 2N 2 * O execution time. Therefore, the whole sections of code total execution time T (n-) = (2N 2 + 2N +. 3) * O.

When large n, you can think of it as 10000,100000. The three-part formula of low-order, constant coefficient is not about growth trends, it can be ignored. Only a maximum magnitude recorded on it, the time complexity of the above that the two codes, it can be written as: T (n-) = O (n-); T (n-) = O (n- 2 ) .

 

Time complexity analysis

There are four methods may be more practical analysis time a piece of code complexity :

1 ) only concerned with the most number of cycles execute a piece of code

Such as the above first piece of code, the second and third lines are constants stage execution time, regardless of the size of n, the complexity for no effect. The loop executes the highest number of lines of code are 4,5, so this key code to be analyzed, the overall time complexity is O (n).

2) maximum rule: the complexity of the overall complexity equal to the magnitude of the largest part of the code of

Such as a piece of code have a single cycle and multiple cycle, then the complexity of taking multiple cycles.

 1 int cal(int n) {
 2    int sum_1 = 0;
 3    int p = 1;
 4    for (; p < 100; ++p) {
 5      sum_1 = sum_1 + p;
 6    }
 7 
 8    int sum_2 = 0;
 9    int q = 1;
10    for (; q < n; ++q) {
11      sum_2 = sum_2 + q;
12    }
13  
14    int sum_3 = 0;
15    int i = 1;
16    int j = 1;
17    for (; i <= n; ++i) {
18      j = 1; 
19      for (; j <= n; ++j) {
20        sum_3 = sum_3 +  i * j;
21      }
22    }
23  
24    return sum_1 + sum_2 + sum_3;
25  }

 

The code is divided into three parts, namely, seeking sum_1, sum_2, sum_3. We can analyze the time complexity of each part separately, and then put them together, and then take an order of magnitude as the maximum complexity of the whole code.

The complexity of the three-part code are O (. 1), O (n-) and O (n2). Overall time complexity of the three sections of the code, we take the largest of magnitude. Therefore, the time complexity on the whole code is O (n2). In other words: The total time complexity is equivalent to the time complexity of the order of the largest part of the code.

3) Multiplication Rule: complexity of nested tags is equal to the product of the nested inner and outer code complexity

For example, recursive, multi-cycle, etc.

4) a plurality of adders scale for

The method has two parameters such as frequency and two control cycle, then the time whichever adding complexity.

 1 int cal(int m, int n) {
 2   int sum_1 = 0;
 3   int i = 1;
 4   for (; i < m; ++i) {
 5     sum_1 = sum_1 + i;
 6   }
 7 
 8   int sum_2 = 0;
 9   int j = 1;
10   for (; j < n; ++j) {
11     sum_2 = sum_2 + j;
12   }
13 
14   return sum_1 + sum_2;
15 }

 

As can be seen from the code, m and n represent the two data size. We can not assess in advance the order of m and n whose big, so when we represent complexity, can not simply use the addition rule, one of which is omitted. Therefore, the above time code is the complexity of O (m + n).

 

Some common examples of time complexity analysis

1. (1)

O (1) is only a level representation constant time complexity, not only refers to the line of code is executed. This code example, even if there are 3 rows, its time complexity is O (1), instead of O (3).

1  int i = 8;
2  int j = 6;
3  int sum = i + j;

As long as the execution time of the code does not increase with the increase of n, so that the time complexity of the code we have referred to as O (1). Or, under normal circumstances, as long as the loop, a recursive algorithm statement does not exist, even if there are thousands of lines of code, the time complexity is Ο (1).

2. (logn), (nlogn)

1  i=1;
2  while (i <= n)  {
3    i = i * 2;
4  }

As can be seen from the code value of the variable i is taken from a beginning, once per cycle multiplied by two. When greater than n, the loop ends. In fact, the value of the variable i is a geometric sequence.

2 22 23 24 ... 2x = n

所以,只要知道 x 值是多少,就知道这行代码执行的次数了。x=log2n,所以,这段代码的时间复杂度就是 O(log2n)。实际上,不管是以 2 为底、以 3 为底,还是以 10 为底,在对数阶时间复杂度的表示方法里,我们忽略对数的“底”,统一表示为 O(logn)。

理解 O(logn),那 O(nlogn) 就很容易理解了。如果一段代码的时间复杂度是 O(logn),我们循环执行 n 遍,时间复杂度就是 O(nlogn) 了。而且,O(nlogn) 也是一种非常常见的算法时间复杂度。比如,归并排序、快速排序的时间复杂度都是 O(nlogn)。

3.  O(m+n)、O(m*n)

代码的复杂度由两个数据的规模来决定。代码同时间复杂度分析的“4)

空间复杂度分析

时间复杂度的全称是渐进时间复杂度,表示算法的执行时间与数据规模之间的增长关系。类比一下,空间复杂度全称就是渐进空间复杂度(asymptotic space complexity),表示算法的存储空间与数据规模之间的增长关系

 1 void print(int n) {
 2   int i = 0;
 3   int[] a = new int[n];
 4   for (i; i <n; ++i) {
 5     a[i] = i * i;
 6   }
 7 
 8   for (i = n-1; i >= 0; --i) {
 9     print out a[i]
10   }
11 }

 

跟时间复杂度分析一样,第 2 行代码中,申请了一个空间存储变量 i,但是它是常量阶的,跟数据规模 n 没有关系,所以可以忽略。第 3 行申请了一个大小为 n 的 int 类型数组,除此之外,剩下的代码都没有占用更多的空间,所以整段代码的空间复杂度就是 O(n)。

常见的空间复杂度就是 O(1)、O(n)、O(n2),像 O(logn)、O(nlogn) 这样的对数阶复杂度平时都用不到。而且,空间复杂度分析比时间复杂度分析要简单很多。所以,对于空间复杂度,掌握这些内容已经足够了。

Guess you like

Origin www.cnblogs.com/marxist/p/12047463.html
Recommended