[Algorithm] Algorithm complexity

1. Algorithm analysis

• Algorithm complexity is a measure of algorithm difficulty.

• The more resources the algorithm requires, the higher the complexity. The most important of computer resources is the time required for calculations and the space resources required for storing programs and data.

• Algorithm complexity includes time complexity and space complexity.

• For complex problems or efficient algorithms, algorithm analysis is generally not done, but benchmark testing methods are used.

• Algorithms that can be analyzed clearly, generally simple or inefficient algorithms;

• Difficulties (such as the salesman burden problem) and efficient algorithms are difficult to analyze clearly.

2. Difficulty in calculating algorithm complexity

• Algorithm complexity is related to the size of the problem;
• The distribution of input data will also affect the algorithm complexity.

Algorithm complexity evaluation:
• Best, worst, average;
• Usually focus on the algorithm complexity in the worst case.

Difficulties in calculating the complexity of the algorithm accurately:
(1) It takes a lot of effort to write the program by the algorithm;
(2) The quality of the algorithm will be affected because of the quality of the program;
(3) The test data is difficult to compare each algorithm. Fair;
(4) Good algorithms need to be improved and tested repeatedly, which requires a lot of work.

3. Representation of algorithm time complexity

• Algorithm time complexity refers to the time required for the program to run from the start to the end.
• When the problem size is n and the time required by the algorithm is T(n), T(n) is called the "time complexity" of the algorithm.
• Algorithm time complexity is often expressed as big O (read: big circle, Order, big-O).
• The time complexity of the algorithm is related to the size of the input data.
• For example, the complexity of the binary search algorithm is O(log n), which means that the binary search requires an operation step of the order of log n to find an array of size n.
• For example, the algorithm complexity is O(f(n)), which means that when n increases, the running time increases at the speed of f(n) at most. Also called progressive complexity.

Common algorithm complexity level
Insert picture description here
algorithm time complexity growth trend curve
Insert picture description here

4. Calculation case of algorithm time complexity

[Case] ​​When the time complexity is T(n)=O(1), such as:
• temp=i;
• i=j;
• j=temp;
• The frequency of the above statements is 1, and the program execution time is not The problem model n is an irrelevant constant.
• When the algorithm time complexity is a constant order, mark T(n)=O(1).
• If the execution time of the algorithm does not increase with the increase of the problem model n, even if the algorithm has thousands of statements, its execution time is a relatively large constant.
Denote as T(n)=O(1)
[Example] The time complexity T(n)=O(n).
Insert picture description here
The time complexity of the above algorithm is: T(n)=2+n+3(n-1)=4n-1=O(n).
[Example] The time complexity T(n)=O(log n).
Insert picture description here
Suppose the frequency of sentence 2 is: f(n), f(n)<=n, f(n)<=log n, take the maximum value f(n)=log n;
the time complexity of the algorithm is: T( n)=2log n +1=O(log n)
【Example】 The time complexity T(n)=O(n2).
Insert picture description here
The time complexity of the above algorithm is: T(n)=2n2+n+1 =O(n2)

5. The space complexity of the algorithm

• Algorithm space complexity refers to the storage space required by the program from the beginning to the end.
• Algorithm space complexity includes:
(1) Fixed parts
• For example, program code, constants, simple variables, etc. occupy space.
(2) Variable part
• For example, the size of the processed data is not related to the problem.
• Space complexity and time complexity calculation methods are similar;
• Space complexity analysis is relatively simple, so time complexity is generally discussed.

Guess you like

Origin blog.csdn.net/qq_44762986/article/details/108070475