Algorithm's time and space complexity notes

Xiaobai, I used a blog to record what I learned. Today, I learned about the complexity of time and space and searched a lot, but I didn't have a detailed explanation that I thought was very detailed. After I finished learning, I sorted out my notes. (It’s the first time to write a blog, please point out the shortcomings, give me more advice and help~)

Please watch it slowly and carefully pick the wrong one (* _ *)

Algorithm refers to a set of methods used to manipulate data and solve program problems. For the same problem, using different algorithms may have the same result, but the resource consumption and time in the process will be very different.

So in what way do you consider it? ?
Answer: It is mainly measured from the two dimensions of time and space occupied by the algorithm.

Time complexity: measure the length of the execution time of the algorithm.
Space complexity: measure the size of the storage space required by the algorithm

^ _ ^, then we have to learn to calculate the time complexity and space complexity of the programs we write

1. Calculation of time complexity :
First give a chestnut:

计算时间复杂度例子
for i in range(0,n):
	print("hello")
	for j in range(0,n):
		print("byebye~")	

The number of executions of statements in an algorithm is called statement frequency or time frequency. Recorded as T(n)
The number of repeated executions of the basic operation of the algorithm n is recorded as a certain function f(n), therefore, the time complexity of the algorithm is recorded as:
T(n)=O(f(n))
as n The increase of the algorithm execution time is proportional to f(n), so the smaller the f(n), the lower the time complexity of the algorithm and the higher the efficiency of the algorithm.
Steps
When calculating the time complexity
1. First find out the basic operation of the algorithm, and then determine its execution times according to the corresponding sentences, and then find out the same order of magnitude of T(n) (its same order of magnitude: 1<log2n <n <nlog2n <n 2 <n 3 <2 n <n! These kinds),
2. After finding out, f(n) = this order of magnitude, if T(n)/f(n) is the limit, a constant can be obtained c, then the time complexity T(n)=O(f(n))

Steps to calculate the time complexity (take the above code as an example):
1. T(n) = n + n 2 has the order of n 2 , f(n) = n 2
2. When n approaches positive infinity, lim T (n)/f(n) If the result is a constant c that is not equal to zero, then the time complexity T(n)=O(f(n)) When
f(n) is calculated, ignore the constants in T(n),
Simplified version of low-power and high-power coefficients :
Let’s analyze it again. It can be seen that the most frequently executed statement determines the complexity of the algorithm, and it is generally the statement with the innermost loop. And, usually whether the solution limit is a constant is also omitted.
Therefore, the above steps can be simplified as:
1. Find the most executed statement
2. Calculate the order of magnitude of the statement execution
3. Use a big O to indicate the
complexity of the result : O (1)<O(log2n) <O(n) <O(nlog2n) <O(n 2 )<O(n 3 )<O(2 n )<O(n!)

Second, the calculation of space complexity

1. Constant space

When the storage space of the algorithm is fixed and has no direct relationship with the input scale, the space complexity is recorded as O(1).

2. Linear space

When the space allocated by the algorithm is a linear set, and the set size is proportional to the input size n, the space complexity is denoted as O(n)

The space complexity will not be calculated, and the memory is not well understood, so let it go (QTQ)

3. The choice
between space and time should be weighed against the pros and cons, and the two should be the less powerful.
If the two are almost the same, choose the shorter one (also consider the actual requirements, conditions, etc.)

PS: This article is not used for any commercial purposes, only for learning. Some of the content is excerpted from the Internet. If the original author is different, please contact me.
PS: There are errors, please be sure to point out
PS: Space complexity will be updated later

Guess you like

Origin blog.csdn.net/qq_51182221/article/details/111648975