Complexity and space time complexity issues: PubMed notes data structure 408 of (a)

  1. A good algorithm to ensure efficiency and low memory requirements. Where the efficiency of the algorithm execution time, demand for storage capacity is the maximum storage space during the execution of the algorithm needed. That is a measure of the efficiency of the algorithm is described by the time complexity and space complexity degrees.

  2. A statement of frequency is the number of times the statement is executed in the algorithm, and the time complexity is the analysis of the frequency and magnitude of all statements.

  3. N is the size of the problem algorithm, the time complexity of n related.

  4. So why the time complexity of the algorithm to calculate it beforehand? Some problems can not be calculated after the event, such as missile control algorithm. And there are several other factors affecting the time cost: ① machine performance. ② programming language (the more advanced the lower the efficiency of the implementation language). ③ machine instructions generated by the compiler quality. We can not let the algorithm run first, then the statistical time overhead.

  5. Examples of computation time complexity
    void loveyou {
    int I =. 1;
    the while (I <= n-) {
    I ++;
    the printf ( "the I Love you% D \ n-", I);
    }
    the printf ( "I Love you More Within last% D \ n-", I);
    }
    time complexity of the algorithm is the most primitive way of determining the number of times each statement. Apparently 1+ (n + 1) + ( n) + (n) + 1 = 3n + 3 [parentheses are performed in order to distinguish which is the number of statements]

  6. The above algorithm is too simple, you can use a method of computing a statement. And often face tens of thousands of lines of code lines in the development process, it should be how to calculate it?
    The following two rules time complexity analysis program
    ① addition rules: adding a number, only the most items, and becomes a factor. T (n-) = Tl (n-) + T2 (n-) = O (F (n-)) + O (G (n-)) = O (max (F (n-), G (n-)))
    ② product rule: multiplying the number of reservations are
    T (n) = T1 (n ) xT2 (n) = O (f (n)) + O (g (n)) = O (f (n) xg (n))
    comparing the magnitude The method, in general:
    (! n-) O (. 1) <O (log2n) <O (n-) <O (nlong2n) <O (N²) <O (n³) <O (2ⁿ) <O <O (nⁿ ) [order] often refers to power

  7. By the above reasoning, it is found constant term is negligible, and the execution order of the code executed only once, also generated constant term. Analysis calculated so that the time complexity to pick just one cycle of the basic operation. Also a piece of code in the program (without any recycle) does not affect the time complexity. If there are multiple layers circulation, just deepest concern.

  8. Some events are not the time complexity of computing time, need to determine the best-case and worst-case and average-case, respectively.

  9. Step computational complexity with respect to time: ① the basic operation to find a (innermost loop). ② Relationship between the number of executions n t and the size of the problem of the basic operations, draw t = f (n). ③ magnitude comparison, removing the constant term and the coefficients, i.e. to obtain time complexity O (n).

  10. Space complexity: algorithm storage space consumed.
    S (n) = O (g (n))

  11. A need to store program instructions when executed by itself, constants, variables, and data input unit of work operations on data, and the like. Therefore, the input data belongs to the space occupied by the problem itself, regardless of the algorithm, so the space complexity analysis only extra space in addition to the input and procedures. Space complexity still follow the rules of addition and multiplication rules.

Released two original articles · won praise 3 · views 64

Guess you like

Origin blog.csdn.net/qq_38652677/article/details/105188050