1.2 Algorithms and algorithm evaluation (time complexity and space complexity)

1. Basic Concepts of Algorithms

An algorithm is a description of the steps in solving a particular problem, which is a finite sequence of instructions, each of which represents one or more operations.

Features of the algorithm:

1. Finiteness: An algorithm must always end after executing finite steps, and each step can be completed in finite time.
2. Deterministic: Each instruction in the algorithm must have a definite meaning, and only the same output can be obtained for the same input.
3. Feasibility: The operations described in the algorithm can all be implemented by performing a limited number of basic operations that have been implemented.
4. Inputs: An algorithm has zero or more inputs, which are taken from a specific set of objects.
5. Output: An algorithm has one or more outputs, which are quantities that have a certain relationship to the input.

What a good algorithm achieves:

Correctness: The algorithm should be able to correctly solve the problem.
Readability: Algorithms should be well readable to help people understand.
Robustness: When illegal data is input, the algorithm can react or process appropriately without producing inexplicable output results.
Efficiency and low storage requirements: Efficiency refers to the time the algorithm takes to execute, and storage requirements refer to the maximum storage space required during the execution of the algorithm, both of which are related to the size of the problem.

2. Measures of Algorithmic Efficiency

Algorithm efficiency is described by time complexity and space complexity

2.1 Time complexity of the algorithm

Does time complexity compare the running time of programs?

No, because differences in hardware can lead to time differences, a bad program might run in less time on today's computer than a good program on a ten-year-old computer, so it's not very timeless without a comparison If it is accurate, then it is much fairer for us to compare by the number of times the statement is executed.

In general, the number of repetitions of the basic operations in the algorithm is a function f(n) of the problem size n, and the time measurement of the algorithm is denoted as T(n)=O(f(n)), which means that with the problem size n The growth rate of the algorithm execution time 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.

For example: the input parameter is n, the number of statements is f(n), the complexity is O(f(n)), and the O() function is a function that removes the coefficients of high-order terms and low-order terms

int count=0;

The time complexity is O(0)

int n=8, count=0;

for (int i=1; i<=n; i++){
    
    
    count++;
}

The time complexity is O(n)

2.2 The space complexity of the algorithm

Does the space complexity consider the size of the program (executable file)?

The space complexity is to consider the size of the memory occupied by the program when it is running, not the size of the executable file. However, it is not accurate to calculate the memory occupied by the program when it is running. Many factors will affect the actual memory usage of the program, such as the memory alignment of the compiler, the underlying implementation of the programming language container, etc., which will affect the program memory overhead. So the space complexity is to roughly estimate the size of the program memory usage in advance.

The space complexity S(n) of an algorithm is defined as the storage space consumed by the algorithm, which is a function of the problem size n. Denote it as S(n)=O(g(n)).

int j = 0;
for (int i = 0; i < n; i++) {
    
    
    j++;
}

In the first piece of code, we can see that with the change of n, the memory space to be opened does not change with the change of n,
that is, the space complexity of this algorithm is a constant, so it is expressed as big O(1)

int* a = new int(n);
for (int i = 0; i < n; i++) {
    
    
    a[i] = i;
} 

We have created a new array. The size of this data is n.
Although there is a for loop, no new space is allocated. Therefore, the space complexity of this code mainly depends on the first line, which can
increase with n. , the size of the opened memory grows linearly, that is, O(n)

Guess you like

Origin blog.csdn.net/m0_50991874/article/details/123292693