Algorithms and Data Structures [] complexity analysis

       This article documents the blogger analysis of algorithm complexity, the complexity of several common, and the average time complexity, the best summary / complexity worst time.

Complexity Analysis

  About complexity of the algorithm, we usually use the Big O to represent, here we assume that the execution time of each line of code are the same, as a unit of time, and then this time on the basis of assumptions, analysis of the space complex. First analysis of the above code, the time complexity is 2-4 row 3, followed by two for loops, a first loop for the time complexity is n, while the time complexity of the sixth row is also n, the time complexity for the interior of the loop is 2 * n * n. The total time complexity compared to 2 * n * n + 2 * n + 3. Thus, the time complexity of the algorithm is O (2 * n * n + 2 * n + 3). In regard to the complexity of the analysis, there are three points to note:

  1, the overall time complexity equal to the maximum order of complexity that end of the code, the analysis time of the time complexity of the algorithm, is often overlooked constant order , low-level , coefficients. Therefore, the analysis time complexity of an algorithm, we only need to focus on the magnitude of the maximum period can be.

  2, the multiplication rules: Nested nested code equals the product of the code execution time in vivo . We can be found in a nested for loop, basically using this rule. The code below, the complexity for the outer loop is O (3 * n), the complexity for the second cycle was O (2 * n), the time complexity of the whole code is O (2 * n) * O (n) + O (therefore change the code complexity is O (n * n)

 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  }

 

Several common complexity

  Common time complexity of the following: divided into two categories, and two non-polynomial polynomial of the order, the order in which the non-polynomial has two ratings: factorial index and rank-order. Issues of this magnitude complexity is also known as NP-hard problem.

   Constant order o (1)

int a = 0;
int b = a;
int i;

  On the order of O (logn)

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

 

  x = n-th power of 2 can be obtained as in x = log2 n represent an O mark when the time complexity coefficient is ignored, the time complexity of the code present is O (log n); if the first to be noted that 3 into line 2 3, still time complexity is O (log n), since the calculation of the number of rules between different substrates can be converted to each other, log3n = log32 * log2n

  Linear order of O (nlogn)

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

 

  O(m+n)

 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   int sum_2 = 0;
 8   int j = 1;
 9   for (; j < n; ++j) {
10     sum_2 = sum_2 + j;
11   }
12  return sum_1 + sum_2;
13 }

 

  O(m*n)

1 int cal(int m, int n){
2   int sum_2 = 0;
3   int j = 1, i = 1;
4   for (; j < n; ++j) {
5       for(; i<m; ++i)
6       sum_2 = sum_2 + j;
7   }

 

Third, the best / worst, the average time complexity

. 1  // n-array represents an array of length 
2  int Find ( int [] array, int n-, int X) {
 . 3    int I = 0 ;
 . 4    int POS = - . 1 ;
 . 5    for (; I <n-; ++ I) {
 . 6      IF (Array [I] == X) POS = I;
 . 7    }
 . 8    return POS;
 . 9 } 

  The best time complexity

  Best-case time complexity is, in the best case execution time complexity of this code. The above code as an example, the code for the find function, find a digital focus position in the array, the ideal situation is the first position we find an array of digits, the time complexity is O (1).

  The worst time complexity

  The worst time complexity is, in the worst case execution time complexity of this code. Or in the above code, for example, the worst case is, we want to find the number is not in the array, you need to traverse the entire array, the time complexity of the algorithm for this time of O (n).

  The average time complexity

  Average time complexity algorithm is to consider each case, the execution time of each case added up and then divided by the number of all possible cases, it is possible to obtain an average value of the number of elements need to be traversed. The average time complexity of the above analysis of this code, a total of n + 1 case (0 ~ n-1 found or not found n) we can see that the average time complexity of the algorithm (1 + 2 + ...... + n + n) / n + 1 = n (n + 3) / [2 (n + 1)], we can get the average complexity is O (n).

  Similar to the average time complexity as well as the weighted average of the time, the probability of each happening statistics come in. Is the weighted average of the probability theory, the probability of each case occurring is above 1 / (1 + n), on the basis of the average time complexity by multiplying 1 / (1 + n), to obtain the weighted average time complexity O (1) .

Guess you like

Origin www.cnblogs.com/Trevo/p/11983888.html