【Postgraduate entrance examination or self-study data structure 1.2】——Algorithm (basic concept of algorithm, measurement of algorithm efficiency [time complexity, 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
Insert picture description here

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:
Insert picture description here

1.2 5 characteristics of the algorithm

The characteristics of the algorithm: 必须具备the characteristics of the algorithm

  1. 有穷性: 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)

  2. 确定性: Each instruction in the algorithm must have an exact meaning, which 相同的输入can only be obtained 相同的输出.

  3. 可行性: The operations described in the algorithm can all be implemented by already 基本运算执行有限次implemented.

  4. 输入: An algorithm has 零个或多个输入(such as hello world is no input), these inputs are taken from a collection of specific objects.

  5. 输出: 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

  1. 正确性: The algorithm should be able to solve the problem correctly.

  2. 可读性: 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"

  3. 健壮性: When inputting illegal data, the algorithm can react or process appropriately without producing inexplicable output results.

  4. 高效率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
Insert picture description hereNote: The performance problem of the algorithm will only be exposed when n is large

2.1.1 How to evaluate the algorithm time cost

  1. 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-
Insert picture description heretwo 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:
Insert picture description here

  • 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

  1. 加法规则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

  2. 乘法规则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
Insert picture description herelisted as follows: The time complexity of the two algorithms are as follows, which is of higher order (higher time complexity)?
Insert picture description here

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:

Insert picture description here

2.1.6 Analysis and conclusions of several different algorithms

  1. Nested loop type:
    Insert picture description here
  2. 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)

  1. 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:
Insert picture description here

2.2.1 Memory requirements (space complexity) when the program is running

Case analysis 1:
Insert picture description here
Case analysis 2:
Insert picture description here
Case analysis 3:
Insert picture description here

2.2.2 Memory overhead caused by recursive function calls

Case study 1:
Insert picture description here
Case study 2:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_38454176/article/details/105679307