Review notes data structure "a"

Data Structure Review notes

First, the basic concepts and terminology of data structures

① element data structure usually has 4 basic categories:

1.集合:结构中的数据元素之间除了“同属于一个集合”的关系外,别无其他关系。
2.线性结构:结构中的数据元素之间存在一个对一个的关系。
3.树形结构:结构中的数据元素之间存在一个对多个的关系。
3.图状结构或网状结构:结构中的数据元素之间存在多个对多个的关系

Data structures

② time and space complexity of the algorithm:

First, the concept:
time complexity is the total number of the n operation expression changes that affect a maximum (excluding coefficient)
, such as: total number of calculations general expressions like this:
A 2 n * n + B 3+ C n-2 + D ^ n- LG (n-) + E * + F n-
A! = 0, the time complexity is O (2 ^ n-);
A = 0, B <> 0 => O (n-^. 3);
A, B = 0, C <> 0 => O (n-^ 2) and so on
example:

for(i=1;i<=n;i++) {//循环了n*n次,当然是O(n^2)
    for(j=1;j<=n;j++) {
        s++;
    }
}   
for(i=1;i<=n;i++) {//循环了(n+n-1+n-2+...+1)≈(n^2)/2,因为时间复杂度是不考虑系数的,所以也是O(n^2)
    for(j=i;j<=n;j++) {
        s++;
    }
}   
for(i=1;i<=n;i++) {//循环了(1+2+3+...+n)≈(n^2)/2,当然也是O(n^2)
    for(j=1;j<=i;j++) {
        s++;
    }
}
i=1;k=0;
while(i<=n-1){ //循环了n-1≈n次,所以是O(n)
    k+=10*i;
    i++;
}
for(i=1;i<=n;i++) { //循环了(1^2+2^2+3^2+...+n^2)=n(n+1)(2n+1)/6(这个公式要记住哦)≈(n^3)/3,不考虑系数,自然是O(n^3)
    for(j=1;j<=i;j++) {
        for(k=1;k<=j;k++) {
            x=x+1;
        }
    }
}
i=1;  
while (i<=n) {
    i=i*2;
}
/*解:语句1的频度是1,  
    设语句2的频度是t,  则:nt<=n;  t<=log2n
    考虑最坏情况,取最大值t=log2n,
    T(n) = 1 + log2n
    f(n) = log2n
    lim(T(n)/f(n)) = 1/log2n + 1 = 1
    T(n) = O(log2n)*/

Further, the time complexity, log (2, n) (base 2) and lg (n) (base 10) are equivalent, because the number of soled formula:
log (A, B) = log (c, b) / log (c, a)
Therefore, log (2, n) = log (2,10) * lg (n), the coefficient ignored, of course, the two are equivalent
two, calculated as follows:
concrete steps to solve the time complexity of the algorithm is:
  ⑴ identify basic statements algorithm;
  the highest number of the statement that the implementation of the algorithm is the basic statements, usually the innermost loop cycle.
  ⑵ calculate the number of the number of executions of basic statement level;
  just count the number of performing basic statement of magnitude, which means that as long as a function of the number of executions of basic statement of the right to the highest power, you can ignore all low power and maximum times power coefficient. This can simplify the analysis of algorithms, and the focus on the most important point: the growth rate.
  ⑶ represents time performance of the algorithm with a large Ο mark.
  The basic statement is executed into a large number of orders of magnitude Ο notation.
  If the algorithm contains nested loops, the basis statement is usually the innermost loop, if the algorithm loop comprising parallel, then the cycle time complexity of parallel addition. For example:
  for (I =. 1; I <= n-; I ++)
  X ++;
  for (I =. 1; I <= n-; I ++)
  for (J =. 1; J <= n-; J ++)
  X ++;
  first for loop the time complexity is Ο (n), for the second cycle of the time complexity is o (n- 2), the time complexity of the algorithm is o (n-+ n- 2) = o (^ n-2).
Common time complexity:
the common time complexity of the algorithm is in ascending order:
  o (. 1) <o (log2n) <o (n-) <o (Nlog2N) <o (n- 2) <o (n- . 3) <... <Ο (2 ^ n ) <Ο (n!)

Wherein,
1.Ο (. 1) represents the number of a statement is executed substantially constant, in general, does not exist as long as the loop algorithm, the time complexity is Ο (1).
2.O (n), O (n ^ 2), the cubic order O (n ^ 3), ... , k th order O (n ^ k) is the time complexity of the polynomial order, called a first order time complexity , second time complexity. . . .
3.O (2 ^ n), the time complexity is exponential order, the kind is not practical
4. order O (log2n), the order of the linear O (nlog2n), in addition to constant order, the kind of maximum efficiency
Example: Algorithm :

for(i=1;i<=n;++i)
  {
     for(j=1;j<=n;++j)
     {
         c[ i ][ j ]=0; //该步骤属于基本操作 执行次数:n^2
          for(k=1;k<=n;++k)
               c[ i ][ j ]+=a[ i ][ k ]*b[ k ][ j ]; //该步骤属于基本操作 执行次数:n^3
     }
  }

There are T (n) = n- 2 + n- . 3, according to the above parentheses the same order, we can determine n ^ 3 is T (n) of the same order of magnitude
there is f (n) = n ^ 3 , and according to T ( n) / f (n) of limit constant c can be obtained
, the time complexity of the algorithm is: T (n) = O ( n ^ 3)

Released five original articles · won praise 1 · views 256

Guess you like

Origin blog.csdn.net/zhanghongbin159/article/details/102862025