Time and Space Complexity


foreword

This article explains time and space complexity


提示:以下是本篇文章正文内容,下面案例可供参考

1. Algorithm efficiency

1. How to judge the efficiency of the algorithm?

If we take the performance of the computer as the efficiency of the algorithm

What will happen?

for example:

We use the computer CPU that the United States landed on the moon

Run the following code with the current i9-13900CPU:

Who is faster?

long long Fib(int N)
{
if(N < 3)
return 1;
return Fib(N-1) + Fib(N-2);
}

 There is no need to question that it must be the current CPU

2. The complexity of the algorithm

After the algorithm is compiled into an executable program, it needs to consume time resources and space (memory) resources when running. Therefore, to measure the quality of an algorithm, it is generally measured from two dimensions of time and space , that is, time complexity and space complexity.
Time complexity mainly measures how fast an algorithm runs , while space complexity mainly measures the extra space required for an algorithm to run . In the early days of computer development, computers had very little storage capacity. So I am very concerned about the space complexity. However, with the rapid development of the computer industry, the storage capacity of computers has reached a very high level. So we no longer need to pay special attention to the space complexity of an algorithm.

2. Time complexity

1. Definition of time complexity

Definition of Time Complexity: In computer science, the time complexity of an algorithm is a function that quantitatively describes the running time of that algorithm. Theoretically speaking, the time it takes for an algorithm to execute cannot be calculated. Only when you put your program on the machine and run it can you know it. But do we need to test each algorithm on the computer? It is possible to test them all on the computer, but it is very troublesome, so there is an analysis method of time complexity. The time spent by an algorithm is proportional to the number of executions of the statements in it, and the number of executions of the basic operations in the algorithm is the time complexity of the algorithm.

Please calculate how many times the ++count statement in Func1 has been executed in total? 

void Func1(int N)
{
int count = 0;
for (int i = 0; i < N ; ++ i)
{
for (int j = 0; j < N ; ++ j)
{
++count;
}
}
for (int k = 0; k < 2 * N ; ++ k)
{
++count;
}
int M = 10;
while (M--)
{
++count;
}
printf("%d\n", count);
}

Number of basic operations performed by Func1:


N = 10 F(N) = 130
N = 100 F(N) = 10210
N = 1000 F(N) = 1002010
In practice, when we calculate the time complexity, we do not have to calculate the exact number of executions, but only The approximate number of executions is required, so here
we use the asymptotic notation of big O.

2. Asymptotic representation of big O 


Big O notation (Big O notation): is a mathematical notation used to describe the asymptotic behavior of a function.
Derive the Big O method:
1. Replace all additive constants in run time with the constant 1.
2. In the modified running times function, only the highest order term is kept.
3. If the highest order item exists and is not 1, remove the constant multiplied by this item. The result is big O order.
After using the asymptotic representation of Big O, the time complexity of Func1 is:
N = 10 F(N) = 100
N = 100 F(N) = 10000
N = 1000 F(N) = 1000000
Through the above we will find Big O The progressive representation of removes those items that have little influence on the result , and expresses the number of executions concisely and clearly.
In addition, the time complexity of some algorithms has the best, average and worst cases:
worst case: the maximum number of runs (upper bound) of any input size
average case: the expected number of runs of any input
size best case: any input size of the Minimum number of runs (lower bound)
For example: search for a data x in an array of length N
Best case: 1 find
Worst case: N finds
Average case: N/2 finds
In practice, the general case is concerned with the algorithm The worst running case, so the time complexity of searching data in the array is O(N)

3. Space complexity

Space complexity is also a mathematical expression, which is a measure of the amount of storage space temporarily occupied by an algorithm during operation .
Space complexity is not how many bytes the program occupies, because this is not very meaningful, so space complexity is calculated by the number of variables .
The space complexity calculation rules are basically similar to the practical complexity, and the big O asymptotic notation is also used .
Note: The stack space (storage parameters, local variables, some register information, etc.) required by the function at runtime has been determined during compilation, so the space complexity is mainly determined by the additional space explicitly requested by the function at runtime.

 4. Complexity comparison chart


 

Summarize

Introduction and understanding of algorithm time and space complexity, and how to use it

Guess you like

Origin blog.csdn.net/qq_45591898/article/details/129182704