Spatial complexity

An indicator of algorithm quality
Collection
zero Useful+1
zero
Space Complexity is a measure of the amount of storage space temporarily occupied by an algorithm during its operation, recorded as S (n)=O (f (n)). Such as direct Insert Sort Of Time complexity It is O (n ^ 2), and the spatial complexity is O (1). And general recursive algorithm There will be O (n) space complexity, because every recursion will storage Return information. The advantages and disadvantages of an algorithm are mainly from two aspects: the execution time of the algorithm and the required storage space measure
Chinese name
Spatial complexity
Foreign name
space complexity
Record
S(n)=O(f(n))。
Measurement
Execution time and required storage space

Introduction to Measurement

Announce
edit
be similar to [1] Time complexity The spatial complexity S (n) of an algorithm is defined as the storage space consumed by the algorithm, which is also a function of the problem size n. Asymptotic space complexity is also often referred to as space complexity. Space Complexity is a measure of the amount of storage space temporarily occupied by an algorithm during operation. An algorithm in the computer storage The storage space occupied by the algorithm includes the storage space occupied by the algorithm itself, the storage space occupied by the input and output data of the algorithm, and the storage space temporarily occupied by the algorithm during operation. The storage space occupied by the input and output data of the algorithm is determined by the problem to be solved. It is transferred from the calling function through the parameter list, and it does not change with the difference of the algorithm. The storage space occupied by the storage algorithm itself is proportional to the length of the algorithm writing. To compress the storage space in this area, a shorter algorithm must be written. The storage space temporarily occupied by the algorithm during operation varies with the algorithm. Some algorithms only need to occupy a small number of temporary work units, and it does not change with the size of the problem, We call this algorithm "in place", which is a memory saving algorithm. The number of temporary work units that some algorithms need to occupy is related to the size of the problem. It increases with the increase of n. When n is large, it will occupy more storage units, such as fast sorting and Merge sort algorithm This is the case.

analysis

Announce
edit
The analysis of the storage space occupied by an algorithm should be considered comprehensively from all aspects. For example, for recursive algorithm Generally speaking, they are relatively short. The algorithm itself occupies less storage space, but an additional stack , thus occupying more temporary work units; If it is written as a non recursive algorithm, it may be relatively long, and the algorithm itself takes up more storage space, but it may need fewer storage units at runtime.
The spatial complexity of an algorithm only considers local variable The size of the allocated storage space, which is included in the parameter table Formal parameter The storage space allocated for variables and Function body The storage space allocated by the local variable defined in. If an algorithm is [2] recursive algorithm Its space complexity is the size of the stack space used by recursion, which is equal to the size of the temporary storage space allocated by a call times the number of calls (that is Recursive call The number of times is increased by 1, and this 1 represents a non recursive call started). The spatial complexity of the algorithm is also given in the form of order of magnitude. For example, when the spatial complexity of an algorithm is constant , that is, when it does not change with the size of the amount of data to be processed n, it can be expressed as O (1); When the spatial complexity of an algorithm is proportional to the logarithm of n with base 2, it can be expressed as O (log2n); When the spatial complexity of an algorithm is linearly proportional to n, it can be expressed as O (n). If the formal parameter is array , you only need to allocate a storage Real parameter An address transmitted Pointer Space, that is, a Machine word length Space; If the formal parameter is in reference mode, it only needs to allocate an address space for it to store the address of the corresponding actual parameter variable, so that the system can automatically reference the actual parameter variable.

Time space complexity

Announce
edit
For an algorithm, its [1] Time complexity And spatial complexity often interact. When pursuing a better time complexity, the performance of space complexity may become worse, which can lead to taking up more storage space; On the contrary, when pursuing a better spatial complexity, the performance of time complexity may become worse, which can lead to a longer running time. In addition, all the performances of the algorithm have more or less interaction. Therefore, when designing an algorithm (especially a large algorithm), we should comprehensively consider the performance of the algorithm, the use frequency of the algorithm, the size of the data processed by the algorithm, the characteristics of the algorithm description language, the machine system environment where the algorithm runs, and other factors to design a better algorithm. The time complexity and space complexity of the algorithm are called the complexity of the algorithm.