Turn: Spatial complexity of the algorithm

Source: Internet
Author: User

Turn from: The spatial complexity of the algorithm

Similar to the discussion of time complexity, the spatial complexity of an algorithm (space complexity) S (n) is defined as the storage space consumed by the algorithm, and it is also a function of the problem size n. Asymptotic spatial complexity is also often referred to as spatial complexity.

    • Spatial complexity (space complexity) is a measure of the amount of storage space that is temporarily occupied by an algorithm while it is running. The storage space occupied by an algorithm in the computer memory, including the storage space occupied by the storage algorithm itself, the storage space occupied by the input and output data of the algorithm and the storage space occupied by the algorithm in the running process three aspects.

When we write code, we can completely use space for time, for example, to determine if a year is not a leap, you may spend a bit of effort to write an algorithm, and because it is an algorithm, it means that each time to give a year, is to calculate whether it is a leap years results. Another way is to create an array of 2 050 elements in advance (a little more than the actual number of years), and then put all the years in the subscript number corresponding, if it is a leap year, the value of this array item is 1, if it is not a value of 0. Thus, the so-called judge whether a year is a leap years, it becomes the problem of finding the value of an item in this array. At this point, our operations are minimized, but we need to store these 2050 0 and 1 on the hard disk or in memory.

The storage space occupied by the input and output data of the algorithm is determined by the problem to be solved, which is passed by the calling function by the parameter table, and it does not change with the algorithm. Storage algorithm itself occupies the storage space and the length of the algorithm written in proportion, to compress the storage space, you must write a shorter algorithm. Algorithm in the running process of temporary occupied storage space varies with the algorithm, some algorithms only need to occupy a small amount of temporary work units, and does not vary with the size of the problem, we call this algorithm "in-place" is to save the algorithm, as described in this section of the algorithm is so , some algorithms need to occupy the number of temporary working units and solve the problem of the size of N, it increases with the increase of N, when n is large, will occupy more storage units, such as the fast sorting and merging sorting algorithm is the case.

A small amount of space overhead in exchange for a little skill in calculating time. Which is good, in fact, depends on where you use.

The spatial complexity of the algorithm is realized by calculating the storage space required by the algorithm, and the computational formula for the spatial complexity of the algorithm is as follows: S (n) = O (f (n)), where n is the scale of the problem, and F (n) is the function of the statement about the storage space occupied by N.

In general, when a program executes on a machine, it needs to store the storage unit for the data operation in addition to the instructions, constants, variables, and input data stored in the program itself. If the input data occupies space only depends on the problem itself, and the algorithm independent, so only need to analyze the algorithm in the implementation of the necessary auxiliary units. If the auxiliary space required by the algorithm is a constant relative to the amount of input data, the algorithm is said to work in situ and the space complexity is O (1).

    • On the question of O (1), O (1) says that the size of the data is independent of the number of temporary variables, not that it defines only a temporary variable. For example: Regardless of the size of the data, I define 100 variables, which is called data size independent of the number of temporary variables. This means that the spatial complexity is O (1).
    • That is, when the function is run, the scale of the data is processed in relation to a change in space and time, not a specific value.

When the spatial complexity of an algorithm is a constant, that is, it can be represented as O (1), if it is not changed with the size of N of the processed data, and when the spatial complexity of an algorithm is proportional to the logarithm of the base N of 2, it can be represented as 0 (10g2n); can be represented as 0 (n). If the parameter is an array, it is only necessary to allocate a space for it to store an address pointer transmitted by the argument, that is, a machine word space, and if the formal parameter is a reference, it is only necessary to allocate a space for it to store the address of the corresponding argument variable. To automatically reference the argument variable by the system.

    • In general, we use "time complexity" to refer to the need for runtime, using "spatial complexity" to refer to space requirements. When using "complexity" without qualifying words, it usually refers to the complexity of time.

For an algorithm, its time complexity and spatial complexity are often influenced by each other. When the pursuit of a better time complexity, the performance of the spatial complexity may be poor, that is, may lead to more storage space; Conversely, a better spatial complexity may result in poor performance of time complexity, which can lead to a longer run time. In addition, all the performance of the algorithm has more or less mutual influence. Therefore, when designing an algorithm (especially large algorithm), we should consider the performance of the algorithm, the frequency of use of the algorithm, the size of the data amount processed by the algorithm, the characteristics of the algorithm description language, the machine system environment of the algorithm running, and so on, to design a better algorithm.

Extended Reading

The list of topics for this article is as follows:

    1. The first: How did you learn the data structure?
    2. The second word: History and origins of data structure
    3. Third: Some concepts about the data structure
    4. Session Four: Logical Structure of data
    5. Session Five: The physical structure of the data
    6. Word six: About data types
    7. Seventh session: Abstract data type ADT
    8. Eighth session: The relationship between the basic concepts of supplementary data structures
    9. Nineth session: The relationship between data structure and algorithm
    10. 10th session: What is an algorithm?
    11. 11th session: Five basic features of the algorithm
    12. 12th words: What kind of algorithm is a good algorithm
    13. 13th session: Performance analysis of the algorithm
    14. 14th session: How to calculate the time complexity of the algorithm
    15. 15th session: Worst case and average of the algorithm
    16. 16th session: Spatial complexity of the algorithm

Turn: Spatial complexity of the algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.