"Dahua Data Structure" ch2 notes

  • An algorithm is a description of the solution steps to solve a specific problem, represented in a computer as a finite sequence of specific instructions, and each instruction represents one or more operators.


  • Algorithms have 5 basic properties: input, output, finiteness, determinism, and feasibility.

1. Inputs: Algorithms have 0 or more inputs

2. Output: The algorithm has at least 1 or more outputs

3. Finiteness: The algorithm ends automatically without an infinite loop after executing a finite number of steps, and each step is completed in an acceptable time

4. Deterministic: each step of the algorithm has a definite meaning, and there will be no ambiguity

5. Feasibility: Each step of the algorithm must be feasible, that is, each step can be completed by executing a finite number of times


  • Algorithm Design Requirements

1. Correctness: no grammatical errors; output results that meet the requirements can be generated for input legal data; description results that meet specifications can be obtained for input illegal data; requirements are met for carefully selected and even difficult test data output result. In general, we take the third as a criterion for whether an algorithm is correct.

2. Readability: Another purpose of algorithm design is to facilitate reading, understanding and communication.

3. Robustness: When the input data is illegal, the algorithm can also make relevant processing, instead of producing abnormal or inexplicable results.

4. High time efficiency and low storage: Time efficiency refers to the execution time of an algorithm. For the same problem, if there are multiple algorithms that can be solved, the algorithm with short execution time is more efficient, and the algorithm with long execution time is inefficient. The storage requirement refers to the maximum storage space required by the algorithm during the execution process, mainly referring to the memory space or external hard disk storage space occupied by the algorithm program running.


  • A measure of algorithm efficiency

1. Post hoc statistical methods

This method mainly uses the computer timer to compare the running time of the programs compiled by different algorithms through the designed test program and data, so as to determine the efficiency of the algorithm.

Defects: The program must be prepared in advance, which requires a lot of energy and time; the comparison of time depends on environmental factors such as computer hardware and software; the test data design of the algorithm is difficult, and the running time of the program often has a great relationship with the scale of the test data , high-efficiency algorithms are often not reflected in the face of small test data.


2. Ex-ante analysis and estimation method

That is, before the computer program is compiled, the algorithm is estimated according to statistical methods.

The time it takes for a program written in a high-level programming language to run on a computer depends on the following factors:

a) The strategy and method adopted by the algorithm (the basis for the quality of the algorithm)

b) The quality of the code produced by the compilation (supported by the software)

c) the input size of the problem (how much input to the algorithm)

d) The speed at which the machine executes instructions (hardware performance)


The most reliable way to test run time is to count the number of executions of basic operations that cost the run time. The running time is proportional to this count.

We don't care what programming language the programs are written in, or what kind of computer those programs will run on, we only care about the algorithms it implements. In this way, ignoring the recursion of loop indices, loop termination conditions, variable declarations, printing results, etc., in the end, when analyzing the running time of a program, it is most important to think of the program as an algorithm or a series of algorithms independent of the programming language. step.

When we analyze the running time of an algorithm, it is important to correlate the number of basic operations with the input size, that is, the number of basic operations must be expressed as a function of the input size.


  • progressive growth of the function

Asymptotic growth of functions: Given two functions f(n) and g(n), if there is a positive integer N such that for all n>N, f(n) is always greater than g(n), then, We say that f(n) grows asymptotically faster than g(n).

To judge whether an algorithm is good or not, we cannot make an accurate judgment with only a small amount of data. We found that if we can compare the asymptotic growth of the key execution times function between algorithms, we can basically analyze: an algorithm, as n increases, it will be more and more better than another algorithm, or more and more worse than another algorithm. This is actually the theoretical basis of the pre-estimation method, and the time efficiency of the algorithm is estimated by the time complexity of the algorithm.


  • Algorithm time complexity

1. Definition: In the algorithm analysis, the total execution times T(n) of the statement is a function of the problem size n, and then analyze the change of T(n) with n and determine the order of magnitude of T(n). The time complexity of the algorithm, that is, the time measurement of the algorithm, is denoted as T(n)=O(f(n)). It means that with the increase of the problem size n, the growth rate of the execution time of the algorithm is the same as the growth rate of f(n), which is called the asymptotic time complexity of the algorithm, or the time complexity for short. where f(n) is some function of problem size n.

2. In general, as n increases, the algorithm with the slowest growth of T(n) is the optimal algorithm.

3. O(1) is a constant order, O(n) is a linear order, and O(n^2) is a square order

4. Derivation of the big-O order method

a) Replace all additive constants in runtime with constant 1

b) In the modified run times function, only the highest order terms are kept

c) If the highest-order term exists and is not 1, remove the constant multiplied by this term

The result obtained at this time is the big O order


  • Common time complexity

O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n) < O(n!) < O(n^n)


  • worst case vs average case

The analysis of the algorithm is similar. We look for a certain number in an array of n random numbers. The best case is that the first number is, then the time complexity of the algorithm is O(1), but it is also possible that this number is Staying in the last position, then the time complexity of the algorithm is O(n), which is the worst case. Worst-case runtime is a guarantee that runtime will not get bad again. In applications, this is the most important requirement, and generally, unless otherwise specified, the runtimes we refer to are worst-case runtimes. And the average running time is that from the point of view of probability, the probability of this number in each position is the same, so the average search time is n/2 times after the target element is found. The average run time is the most meaningful of all cases because it is the expected run time. That is, when we run a piece of program code, we want to see the average running time. But in reality, the average running time is difficult to obtain through analysis, and is generally estimated by running a certain amount of experimental data.

For the analysis of the algorithm, one method is to calculate the average value of all cases. The calculation method of this time complexity is called the average time complexity, and the calculation method of this time complexity is called the average time complexity. Another way is to calculate the worst case time complexity, this method is called worst time complexity. Generally, unless otherwise specified, it refers to the worst time complexity.


  • Algorithm space complexity

When we write code, we can completely trade space for time. The space complexity of the algorithm is realized by calculating the storage space required by the algorithm. The calculation formula of the algorithm space complexity is recorded as S(n) = O(f(n)), where n is the scale of the problem, and f(n) is Statement about the function of the storage space occupied by n.

In general, when a program is executed on a machine, in addition to the instructions, constants, variables and input data of the program itself, it also needs to store the storage unit for data operations. If the space occupied by the input data only depends on the problem itself and has nothing to do with the algorithm, it is only necessary to analyze the auxiliary units required for the implementation of the algorithm. If the auxiliary space required for the execution of the algorithm is constant relative to the amount of input data, the algorithm is said to work in place, and the space complexity is O(1).

Typically, "time complexity" is used to refer to running time requirements, and "space complexity" is used to refer to space requirements. When "complexity" is used without a qualifier, it usually refers to time complexity.







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325938172&siteId=291194637