[US] data structures and algorithms of the study notes 03 - best, worst, average, amortized time complexity

We often say that never quite work data structures and algorithms, in fact, we are active or unconsciously filter out such opportunities.

Four conceptual complexity

Focus on four knowledge of the complexity of the analysis.

  1. 最好情况时间复杂度(Best Case Time
    Complexity): complexity of the code in the ideal case of time;
  2. 最坏情况时间复杂度(Worst case time complexity): code execution in the worst case complexity;
  3. 平均情况时间复杂度
    (Average case time complexity): with a weighted average number of times of execution codes expressed in all cases;
  4. 均摊时间复杂度(Amortized time complexity): the complexity of the code execution in all cases most of the complexity of low-level, high-level individual situation is complicated having timing relationship, individual high-level complexity can be shared equally to a low level and degree of occurrence the complexity. Results substantially equal to the low-level shared equally complexity.

If you can grasp these concepts, it is for you, the complexity of the analysis of this section no big problem.

Why introduction

  1. The time complexity of the same code will appear on the order of differences in different situations, in order to be more comprehensive, time complexity more accurate description of the code, so the introduction of these four concepts.
  2. When the magnitude of the difference code complexity appear in different situations we need to distinguish between the four complexity. In most cases, the analysis is not necessary to distinguish them .

How to analyze

The best, the worst time complexity

  1. Code Example:
// n 表示数组 array 的长度
int find(int[] array, int n, int x) {
  int i = 0;
  int pos = -1;
  for (; i < n; ++i) {
    if (array[i] == x) pos = i;
  }
  return pos;
}

This code function is to implement, in a disordered array (array), find the variable xposition appears. If not found, returns -1. Analysis method according to the class speaking, the complexity of this code is O(n), which nrepresents the length of the array.

We look for data in the array does not need every time to traverse the entire array all over again, because it is possible to find a way to be ahead of the end of the cycle.

  1. Find optimize the code above
// n 表示数组 array 的长度
int find(int[] array, int n, int x) {
  int i = 0;
  int pos = -1;
  for (; i < n; ++i) {
    if (array[i] == x) {
       pos = i;
       break;
    }
  }
  return pos;
}

The problem comes. After we finish optimizing the time complexity of this code or O(n)do? Obviously, an analysis of the talk, can not solve this problem.

  1. Because the variable you want to find xmay appear anywhere in the array. If the first element of the array is just to look for variables x, there is no need to continue to traverse the rest of the n-1data, that is the time complexity O(1).
  2. However, if the variable does not exist in the array x, then we need to have to traverse the entire array again, the time complexity has become O(n).

Therefore, under different circumstances, the time complexity of this code is not the same

  1. The above O(1)i.e., the best-case time complexity ; O(n)i.e., the worst case time complexity .

The average time complexity

Best-case and worst-case time complexity corresponding time complexity is an extreme case of code complexity of the case, the probability of occurrence is not large.
In order to better represent the complexity of the average case, we need to introduce another concept: the average time complexity of the situation, I referred back to the average time complexity .

  1. With the above variables to find xan example to explain it to you. To find the variable xposition in the array, there are n+1kinds of situations: at the array of 0~n-1locations and not in the array . In each case we look for the number of elements required to traverse the cumulative up, then divided n+1, you can get the number of elements required to traverse the average value , namely:

1
2. We know that the time complexity of Big O notation can be omitted factor, low-level, constant, so, after we put just simplify this formula, the average time complexity is obtained O(n).

  1. Although this conclusion is correct, but the calculation process a little bit problem. What is the problem? We just talked about this n+1case, the probability is not the same.

Here we use a little bit 概率论of knowledge, but very simple, do not worry. )

  1. We know that the variables you want to find x, either in the array or not in the array. In both cases the corresponding probability statistics a lot of trouble, in order to facilitate your understanding, we assume that the probability is not in the array and the array are 1/2. In addition, the data appear to be looking at 0~n-1the probability of the n position is the same as 1/n. Therefore, according to the law of probability multiplication, the data appear to be looking at 0~n-1the probability that anywhere 1/(2n).

  2. Therefore, the biggest problem the previous derivation is that there is no probability of the occurrence of a variety of circumstances into account. If we put the probability of occurrence of each case is also taken into account, and that the average time complexity of the computation process becomes this:

2

This value is the probability theory weighted average , also called the expected value , the average time complexity of the full name should be called the weighted average time complexity or time complexity expectations .

  1. After the introduction of the probability, a weighted average of the front part of the code (3n+1)/4. O notation represented by a large, and remove the constant coefficient, the weighted average time complexity of this code remains O(n).

You might say, the average time complexity analysis of good complex ah, but also involves knowledge of probability theory. In fact, in most cases, we do not need to distinguish between the best and the worst, the average case time complexity of the three cases . Examples like those on a move as, very often, we use a complexity to meet the demand.

Only the same piece of code in different situations, there is the time complexity of the order of the gap, we will use these three complexity notation to distinguish.

Summary : code complexity magnitude differences occur in different cases , with the number of times of execution code for all possible cases at a weighted average of FIG.

Amortized time complexity

Used when two conditions are met:

  1. Code is the low-level complexity in most cases, only a handful of cases of high-level complexity;
  2. Low-level and high-level complexity appears with a timing rule.

Specific analytical reference blog: https://github.com/foreverZ133/Beauty-of-Data-Structure-and-Algorithms/issues/5

Results are shared equally generally equal to the low level of complexity, i.e. the best case time complexity .

Actual analysis

Code:

// 全局变量,大小为 10 的数组 array,长度 len,下标 i。
int array[] = new int[10]; 
int len = 10;
int i = 0;
// 往数组中添加一个元素
void add(int element) {
  if (i >= len) { // 数组空间不够了
    // 重新申请一个 2 倍大小的数组空间
    int new_array[] = new int[len*2];
    // 把原来 array 数组中的数据依次 copy 到 new_array
    for (int j = 0; j < len; ++j) {
      new_array[j] = array[j];
    }
    // new_array 复制给 array,array 现在大小就是 2 倍 len 了
    array = new_array;
    len = 2 * len;
  }
  // 将 element 放到下标为 i 的位置,下标 i 加一
  array[i] = element;
  ++i;
}
  1. Best-case time complexity of O(1)
  2. Worst case analysis:
    number of worst-case code is executed with the length of each of the array related to
    the number of executions of the first invocation insert is n,
    the number of executions of the second invocation of the insert is 2n,
    the third call to the insert a the number of executions is 2 ^ 2 * n
    number of executions of the k-th call to the insert is 2 ^ (k-1) * n
    the worst time complexity is O(n).
  3. Analysis of the average case
    when each encounter the worst case the array will be performed when 2倍扩容the original is introduced into the new array the array, although the length of the array is increased, but the fall of the insertion section is the same length, respectively 0 ~ len-1, len ~ (2len-1), ...;
    insert case is still len+1kind: 0~len-1and after poking O(len); so the probability of each insert is: p = 1 / ( l e n + 1 ) p = 1 / (referred to as +1) ,
    and finally obtains the weighted average time complexity1*p + 2*p+ ... + len*p + len * p = O(1);
  4. Amortized time complexity O(1)
    and shared equally due to the complexity of each O(len)of the occurrence followed by lentimes O(1), is coherent, and therefore will O(len)equal shares to
    the front lenon the second, draw amortized complexity is O(1).
Published 20 original articles · won praise 3 · Views 4535

Guess you like

Origin blog.csdn.net/qq_34246646/article/details/87656597
Recommended