algorithm
- 1. The basic concept of the algorithm
- 2. Measurement of algorithm efficiency
-
- 2.1 Time complexity
-
- 2.1.1 How to evaluate the algorithm time cost
- 2.1.2 The time complexity of the algorithm
- 2.1.3 Question 1: Can some parts of the expression be omitted?
- 2.1.4 The operation rules of the time complexity of the algorithm
- 2.1.5 Question 2: If there are thousands of lines of code, do you need one line per line according to this method?
- 2.1.6 Analysis and conclusions of several different algorithms
- 2.1.7 Best, worst, average time complexity
- 2.2 Space complexity
Note: Please study in combination with specific books; recommend "Data Structure and Algorithm", "Wang Dao Data Structure", "Tianqin Data Structure", etc.
Offline notes: https://download.csdn.net/download/qq_38454176/12352560
1. The basic concept of the algorithm
Knowledge structure overview
1.1 What is an algorithm
Program = data structure + algorithm
Data structure: how to informationize real-world problems and store the information in the computer. At the same time, basic operations on the data structure must be implemented
Algorithm: How to process this information to solve practical problems
Example: Analysis of priority dining problems for customers with children:
1.2 5 characteristics of the algorithm
The characteristics of the algorithm: 必须具备
the characteristics of the algorithm
-
有穷性
: An algorithm must always end after executing finite steps, and each step can be completed in finite time.
Note: The algorithm must be infinite (using finite steps to solve a specific problem), and the program can be infinite (queuing system is a program that can never stop) -
确定性
: Each instruction in the algorithm must have an exact meaning, which相同的输入
can only be obtained相同的输出
. -
可行性
: The operations described in the algorithm can all be implemented by already基本运算执行有限次
implemented. -
输入
: An algorithm has零个或多个输入
(such as hello world is no input), these inputs are taken from a collection of specific objects. -
输出
: An algorithm has一个或多个输出
, these outputs are quantities that have a certain specific relationship with the input.
1.3 The characteristics of a "good" algorithm
Goals to be pursued when designing algorithms
-
正确性
: The algorithm should be able to solve the problem correctly. -
可读性
: The algorithm should have good readability to help people understand.
Note: The algorithm can be described in pseudo-code or even in text. It is important to describe the steps to solve the problem "unambiguously" -
健壮性
: When inputting illegal data, the algorithm can react or process appropriately without producing inexplicable output results. -
高效率
With低存储量需求
: Fast execution speed. Time complexity is low; no memory is required. Low space complexity
2. Measurement of algorithm efficiency
2.1 Time complexity
Structure overview
Note: The performance problem of the algorithm will only be exposed when n is large
2.1.1 How to evaluate the algorithm time cost
- Let the algorithm run first and count the running time afterwards?
What's the problem?
- Related to machine performance, such as: supercomputer vs single-chip microcomputer
- Related to the programming language, the higher the level of the language, the lower the execution efficiency
- Related to the quality of machine instructions generated by the compiler
- Some algorithms cannot be counted after the fact, such as: missile control algorithm
2.1.2 The time complexity of the algorithm
The time complexity of the algorithm: predict the relationship between the time cost of the algorithm T(n) and the problem size n in advance (T stands for "time")
Example: Use algorithm to confess-
two questions about loving you N times in Complex 4 will be discussed and analyzed below
2.1.3 Question 1: Can some parts of the expression be omitted?
Analysis and conclusion:
- Conclusion 1: You can only consider the high-order part
- Conclusion 2: When the problem scale is large enough, the coefficient of the constant term can also be ignored
2.1.4 The operation rules of the time complexity of the algorithm
-
加法规则
T (n) = T1 (n ) + T2 (n) = O (f (n)) + O (g (n)) = O (max (f (n), g (n)))
number of the addition , Only the highest order term is retained, and the coefficient becomes 1 -
乘法规则
T(n) = T1(n)×T2(n) = O(f(n))×O(g(n)) = O(f(n)×g(n))
Eg: T3(n)=n^3 +n^2 log2n =O(n^3)+O(n^2 log2n) =???
Multiply multiple items and keep them
The time complexity is
listed as follows: The time complexity of the two algorithms are as follows, which is of higher order (higher time complexity)?
2.1.5 Question 2: If there are thousands of lines of code, do you need one line per line according to this method?
Analysis and conclusion:
2.1.6 Analysis and conclusions of several different algorithms
- Nested loop type:
- Exponentially increasing
void loveYou(int n) {
//n 为问题规模
int i = 1; // 爱你程度
while (i <= n) {
//外层循环执行n次
i*2; // 每次翻倍
printf("I Love You %d", i);
}
printf("I Love You More Than %d", n);
}
Calculate the time complexity T(n) of the above algorithm: Suppose the sentence frequency of the deepest loop (the total number of loops) is x, then it can be known from the loop condition that 2^x>n
x=log2n+1 is met at the end of the loop
T(n)=O(x)=O(log2n)
- Search by number
void loveYou(int flag[], int n) {
//n 为问题规模
printf("I anm Iron Man %d");
int i = 1; // 爱你程度
for(int i=0;i<n;i++{
//从第一个元素开始查找
if(flag[i] == n) {
//找到元素n
printf("I Love You %d", n);
break; //找到后立即跳出循环
}
}
}
int flag[n] = {
1...n};
Calculate the time complexity of the above algorithm T(n)
最好
Case: Element n is in the first position- 最好时间复杂度
T(n)=O(1)
最坏
Case: Element n is in the last position- 最坏时间复杂度
T(n)=O(n)
平均
Situation: Assume that the probability of element n at any position is the same as 1/n —— 平均时间复杂度
T(n)=O(n)
2.1.7 Best, worst, average time complexity
Worst time complexity: the time complexity of the algorithm in the worst case
Average time complexity: the expected running time of the algorithm when all input examples appear with equal probability.
Best time complexity: the time complexity of the algorithm in the best case
2.2 Space complexity
Knowledge framework:
2.2.1 Memory requirements (space complexity) when the program is running
Case analysis 1:
Case analysis 2:
Case analysis 3:
2.2.2 Memory overhead caused by recursive function calls
Case study 1:
Case study 2: