ArticleslgStudy

computer science

Space complexity

Space complexity is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Space complexity rather than just read about it. In short: The space complexity of an algorithm or a data structure is the amount of memory space required to solve an instance of the computational problem as a function of characteristics of the input. It is the memory required by an algorithm until it executes completely.

Key takeaways

  • Space complexity belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Space complexity to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Space complexity from memory before moving on to harder problems.

Reference excerpt

The space complexity of an algorithm or a data structure is the amount of memory space required to solve an instance of the computational problem as a function of characteristics of the input. It is the memory required by an algorithm until it executes completely. This includes the memory space used by its inputs, called input space, and any other (auxiliary) memory it uses during execution, which is called auxiliary space. Similar to time complexity, space complexity is often expressed asymptotically in big O notation, such as O(n), O(nlog⁡n), O(nα), O(2n), etc., where n is a characteristic of the input influencing space complexity.

Space complexity classes Analogously to time complexity classes DTIME(f(n)) and NTIME(f(n)), the complexity classes DSPACE(f(n)) and NSPACE(f(n)) are the sets of languages that are decidable by deterministic (respectively, non-deterministic) Turing machines that use O(f(n)) space. The complexity classes PSPACE and NPSPACE allow f to be any polynomial, analogously to P and NP. That is, 𝖯𝖲𝖯𝖠𝖢𝖤=⋃c∈ℤ+𝖣𝖲𝖯𝖠𝖢𝖤(nc) and 𝖭𝖯𝖲𝖯𝖠𝖢𝖤=⋃c∈ℤ+𝖭𝖲𝖯𝖠𝖢𝖤(nc)

Relationships between classes The space hierarchy theorem states that, for all space-constructible functions f(n), there exists a problem that can be solved by a machine with f(n) memory space, but cannot be solved by a machine with asymptotically less than f(n) space. The following containments between complexity classes hold. 𝖣𝖳𝖨𝖬𝖤(f(n))⊆𝖣𝖲𝖯𝖠𝖢𝖤(f(n))⊆𝖭𝖲𝖯𝖠𝖢𝖤(f(n))⊆𝖣𝖳𝖨𝖬𝖤(2O(f(n))) Furthermore, Savitch's theorem gives the reverse containment that if f∈Ω(log⁡(n)), 𝖭𝖲𝖯𝖠𝖢𝖤(f(n))⊆𝖣𝖲𝖯𝖠𝖢𝖤((f(n))2). As a direct corollary, 𝖯𝖲𝖯𝖠𝖢𝖤=𝖭𝖯𝖲𝖯𝖠𝖢𝖤. This result is surprising because it suggests that non-determinism can reduce the space necessary to solve a problem only by a small amount. In contrast, the exponential time hypothesis conjectures that for time complexity, there can be an exponential gap between deterministic and non-deterministic complexity. The Immerman–Szelepcsényi theorem states that, again for f∈Ω(log⁡(n)), 𝖭𝖲𝖯𝖠𝖢𝖤(f(n)) is closed under complementation. This shows another qualitative difference between time and space complexity classes, as nondeterministic time complexity classes are not believed to be closed under complementation; for instance, it is conjectured that NP ≠ co-NP.

LOGSPACE

L or LOGSPACE is the set of problems that can be solved by a deterministic Turing machine using only O(log⁡n) memory space with regards to input size. Even a single counter that can index the entire n-bit input requires log⁡n space, so LOGSPACE algorithms can maintain only a constant number of counters or other variables of similar bit complexity. LOGSPACE and other sub-linear space complexity is useful when processing large data that cannot fit into a computer's RAM. They are related to Streaming algorithms, but only restrict how much memory can be used, while streaming algorithms have further constraints on how the input is fed into the algorithm. This class also sees use in the field of pseudorandomness and derandomization, where researchers consider the open problem of whether L = RL. The corresponding nondeterministic space complexity class is NL.

Auxiliary space complexity The term auxiliary space refers to space other than that consumed by the input. Auxiliary space complexity could be formally defined in terms of a Turing machine with a separate input tape which cannot be written to, only read, and a conventional working tape which can be written to. The auxiliary space complexity is then defined (and analyzed) via the working tape. For example, consider the depth-first search of a balanced binary tree with n nodes: its auxiliary space complexity is Θ(log⁡n).

See also Analysis of algorithms – Study of resources used by an algorithm Computational complexity theory – Inherent difficulty of computational problems Computational resource – Aspect of computational complexity theory Time complexity – Estimate of time taken for running an algorithm

References

Worked examples

Example 1 — a first encounter with Space complexity

Start with the simplest possible case. Write down what Space complexity claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Space complexity before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Space complexity ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Space complexity

In research
Space complexity appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Space complexity in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Space complexity is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computational complexity theory, Computational resources, so understanding it makes those chapters shorter.
In everyday life
Look for Space complexity outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Space complexity” →

How to study Space complexity in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Space complexity means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Space complexity out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Space complexity in simple terms?

The space complexity of an algorithm or a data structure is the amount of memory space required to solve an instance of the computational problem as a function of characteristics of the input. It is the memory required by an algorithm until it executes completely.

Why does Space complexity matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Space complexity?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Space complexity.

Tags

  • Computational complexity theory
  • Computational resources

Keep exploring