Complexity of time and space

Source: Internet
Author: User

The measure method of algorithm efficiency

* Post-mortem method: This method is mainly through the design of good test procedures and data, the use of computer timers to different acid prevention program running time comparison, so as to determine the efficiency of the algorithm.

This method has a lot of flaws, must be based on the algorithm prior to the preparation of test procedures, usually need to spend a lot of time and effort, if the test is found to be bad algorithm, it will fall short.

Different test environment side is not generally large.

* Prior analysis and estimation method: Before the computer program is written, the algorithm is evaluated according to the statistical method.

In summary, we found that the time it takes for a program written in a high-level language to run on a computer depends on the following factors:

1. The strategy adopted by the algorithm, the scheme

2. Code quality generated by compilation

3. Input scale of the problem

4. Speed of instruction performed by the machine

Note: We study the complexity of the algorithm, focusing on the algorithm as the input scale expands the amount of an abstraction, rather than exactly how many times need to be executed, because if so, we have to consider the compiler optimization and other issues.

When we analyze the run time of an algorithm, it is important to correlate the number of basic operations with the input pattern.

Operations such as increment and cyclic termination conditions, variable declarations, print results, and so on for cyclic indexes are not counted.

Progressive growth of functions

Example 1:

Two of the input size of the algorithm is n, the algorithm A to do 2n+3 operations (it can be understood that: first execute n Cycles, then perform an n cycle, and finally 3 operations), the algorithm B to do 3n+1 operations. Which one is faster?

Progressive growth of functions: given two functions f (n) and g (n), if there is an integer n, so that for all n>n,f (n) is always greater than g (n), then we say that f (n) growth is faster than g (n).

As the input size increases, the addition constants can be ignored.

Example 2:

Algorithm C is 4n+8, and algorithm D is 2n^2 + 1.

Constants that are multiplied by the highest times are not important or can be ignored.

Example 3:

The algorithm e is 2n^2+3n+3, and the algorithm F is 2n^3+3n+1.

The higher the index of the highest item, the faster it grows.

Time complexity of the algorithm

Definition: When performing an algorithm analysis, the total number of executions of the statement T (N) is a function of the problem size n, which then analyzes the change of T (n) with N and determines the order of magnitude of T (N). The time complexity of the algorithm, which is the time measurement of the algorithm, is recorded as: T (n) =o (f (n)). It is the increase of the problem size n, the algorithm execution time growth rate and F (n) The same growth rate, called the algorithm's progressive time complexity, referred to as time complexity. where f (n) is a function of the problem size n.

In general, with the increase of the input size n, the algorithm with the slowest growth of T (n) is the optimal algorithm.

The strategy of Time complexity:

1. Replace all the addition constants in the run time with constant 1.

2. In the modified run function, only the highest order is preserved.

3. If the highest order exists and is not 1, the constant multiplied by the item is removed.

4. The final result is the Big O-note.

Constant order O (1).

Linear order O (n).

Square Order O (n^2).

Logarithmic order

int i = 1,n = 100;

while (I<n)

{

i = i*2;

}

O (LOGN)

Analysis of time complexity of function call

The time complexity is often spent from small to large in order: O (1) <o (LOGN) <o (n) <o (NLOGN) <o (n^2) <o (n^3) <o (2^n) <o (n!) <o (N^n)

The spatial complexity of the algorithm

The conversion of time and space to each other, such as judging is not a leap year algorithm.

Method one is to write a judgment statement;

Method Two is to list an array of 2050, to check the table.

Complexity of time and space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.