[Data Structure] Algorithms and Algorithm Evaluation


foreword

This article includes the algorithm, the characteristics of the algorithm, the measurement of the efficiency of the algorithm, and the calculation of the algorithm.
insert image description here


Algorithm definition

An algorithm is a description of the steps to solve a specific problem, a finite sequence of instructions, and each instruction represents one or more operations.

properties of the algorithm

  • Finiteness: An algorithm must always end after executing a finite number of steps, and each step is completed in finite time.Algorithms must be finite, while programs can be infinite.

  • Determinism: Each instruction in the algorithm must have an exact meaning, forsame inputcan only drawsame output

  • Feasibility: The operations described in the algorithm can be realized by executing the basic operations that have been realized for a limited number of times.

  • Inputs: An algorithm has zero or more inputs, which are taken from a specific set of objects.

  • Outputs: An algorithm has one or more outputs, which are quantities that have some specific relationship to the inputs.
    insert image description here
    “好”算法的特质:

  • correctness

  • readability

  • robustness

  • High efficiency and low memory requirements.
    insert image description here

Measures of Algorithmic Efficiency

time complexity

The frequency of a statement refers to the statement in the algorithmThe number of times it was repeated. all in the algorithmthe sum of the frequencies of the sentencesDenoted as T(n), it is a function of the problem size n of the algorithm, and the time complexity is mainly analyzedOrder of magnitude of T(n). The basic operations in the algorithm (statement inside the deepest loop) is of the same order of magnitude as T(n), so the frequency f(n) of basic operations in the algorithm is usually used to analyze the time complexity of the algorithm. So the time complexity of the algorithm is:T(n)=O(f(n))

一般总是考虑在最坏情况下的时间复杂度,以保证算法的运行时间不会比它更长。
Analysis time complexity rules:

  • Addition rule: T(n)=T1(n)+T2(n)=O(f(n))+O(g(n))=O(max(f(n),g(n))),Only higher order items are kept
  • Multiplication rule: T(n)=T1(n)*T2(n)=O(f(n))*O(g(n))=O(f(n)*g(n))

Normal speed recovery:常对幂指阶
O(1)<O(log2n)<O(n)<O(n(log2n))<O(n^2) <O(n^3) <O(2^n) <O(n!)< O(n^n)

focus

  • Sequentially executed code will only affect the constant term and can be ignored
  • Just pick a basic operation in the loop and analyze the relationship between its execution times and n
  • If there are multiple levels of nested loops, just focus on the deepest loop looping a few times
  • Best case: element n is in the first position,最好的时间复杂度T(n)=O(1)
  • Worst case: element n is in the last position,最坏的时间复杂度T(n)=O(n)

three levels of complexity

  • Worst Time Complexity: Consider the "worst" case of the input data
  • Average time complexity: consider the case where all input data are equally likely to occur
  • Best Time Complexity: Consider the case where the data input is "best"

calculation method

  1. find a primitive operation (deepest loop)
  2. Analyze the relationship between the execution times x of the basic operation and the problem size n, x=f(n)
  3. The magnitude of x O(x) is the algorithm time complexity T(n)

insert image description here
insert image description here

space complexity

The space complexity S(n) of the algorithm is defined asConsumed storage space,it isproblem sizefunction of n, denoted asS(n)=O(g(n))
When a program is executed, in addition to storage space to store the instructions, constants, variables and input data used by itself, it also needs some work units to operate on the data and auxiliary space to store some information required for calculation. If the space occupied by the input data only depends on the problem itself and has nothing to do with the algorithm, then only the extra space other than the input and the program needs to be analyzed.
The algorithm works in situ: the auxiliary space required by the algorithm is constant, that is, O(1)

calculation method

Ordinary program:
1. Find the variables related to the size of the occupied space and the scale of the problem
2. Analyze the relationship between the occupied space x and the scale of the problem n, x=f(n)
3. The order of magnitude of x is O(x) space complexity of the hormone algorithm S(n)

Recursive program:
1. Find the relationship between the depth x of the recursive call and the problem size n, x=f(n)
2. The order of magnitude of x is O(x) and the space complexity of the hormone algorithm is S(n)
注意:有的算法各层函数所需存储空间不同,分析方法略有区别

Analyze space complexity rules:

  • Addition rule: T(n)=T1(n)+T2(n)=O(f(n))+O(g(n))=O(max(f(n),g(n))),Only higher order items are kept
  • Multiplication rule: T(n)=T1(n)*T2(n)=O(f(n))*O(g(n))=O(f(n)*g(n))

Normal speed recovery:常对幂指阶
O(1)<O(log2n)<O(n)<O(n(log2n))<O(n^2) <O(n^3) <O(2^n) <O(n!)< O(n^n)
insert image description here


Summarize

The above is today's learning content~
If you are interested, you can subscribe to the column and continue to update~
See you next time~
insert image description here

Guess you like

Origin blog.csdn.net/m0_55394328/article/details/131682657
Recommended