Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 74

Turing Machine Space Complexity

Recommended resources

Hello! Welcome to the next lesson in our journey through computational complexity.

In our previous lessons, we've focused almost exclusively on time as the primary resource for computation. We defined complexity classes like P and NP based on how an algorithm's running time scales with the input size. However, time is not the only critical resource. In your own programming experience, you know that memory usage, or space, is often just as important.

Today, we will shift our focus to this second fundamental resource. This lesson addresses the learning outcome: Define the space complexity of a Turing Machine. We will formalize the intuitive idea of "memory usage" within the Turing Machine model, see how it relates to time complexity, and lay the groundwork for exploring major space-based complexity classes.

1. From Time to Space: A New Measure of Complexity

Just as time complexity measures the number of steps a Turing Machine takes, space complexity measures the number of tape cells it uses. A key difference, however, is that a tape cell can be read from and written to many times, whereas a time step is consumed only once. This suggests that space might be a more powerful, reusable resource than time.

Let's begin by watching a video that introduces the formal definition of space complexity for both deterministic and nondeterministic Turing Machines.

17. Space Complexity, PSPACE, Savitch's Theorem

This segment from an MIT OpenCourseWare lecture by Prof. Michael Sipser provides a clear and precise introduction to space complexity.

Watch the video from 01:38 to 06:02. The first part (until 03:56) defines space complexity for a deterministic Turing Machine (DTM). The second part extends this definition to nondeterministic Turing Machines (NTMs).

Let's summarize and formalize the key points from the video.

Formal Definition

Let be a deterministic Turing machine (DTM) that is a decider (i.e., it halts on all inputs). The space complexity of is the function , where is the maximum number of distinct tape cells that scans on any input of length .

  • If the space complexity of is , we say that runs in SPACE().
  • For a nondeterministic Turing Machine (NTM), the space complexity is the maximum number of tape cells it uses on any single branch of its computation.

Notice the parallel to time complexity. We always consider the worst-case input of a given length . For NTMs, we consider the maximum resource usage on any one path to an answer, not the sum across all branches.

For a more detailed textual definition and an example, you can review the following document.

An Introductory Survey of Computational Space Complexity

This survey on computational space complexity provides a concise formal definition and contrasts space complexity with the time complexity you're already familiar with.

Read the sections 'If time complexity is the number of (Turing machine) steps...' through to the formal definition ('Formal definition: Let M be a deterministic Turing machine...'). Also, review the example of calculating the space complexity for a brute-force SAT solver, which highlights the reusability of space.

An important subtlety, mentioned in the Washington.edu lecture notes (LINK), is that for many problems, the smallest meaningful space complexity is . This is because space is often required just to maintain a pointer or counter for the input tape, which has length .


2. Multi-Tape vs. Single-Tape Machines

In our study of time complexity, we learned that converting a multi-tape TM to a single-tape TM incurs a quadratic time overhead. Does a similar penalty apply to space? Let's find out.

17. Space Complexity, PSPACE, Savitch's Theorem

This next clip from the same MIT lecture discusses the space overhead of simulating a multi-tape TM on a single-tape TM.

Watch from 07:18 to 11:15. Pay close attention to why the space overhead is only linear, unlike the time overhead.

As the video explains, the simulation is much more efficient in terms of space. To simulate tapes on a single tape, we can simply lay out the contents of the tapes one after another. If the original machine used space on its tapes, the single-tape machine will use space (plus some small overhead for markers). This is a linear overhead, not quadratic. This robustness across machine models is a key property of space complexity.

Test your understanding!

Consider a Turing Machine designed to check if a string is a palindrome (e.g., racecar). A simple algorithm on a 2-tape TM would be:

  1. Copy the input from tape 1 to tape 2.
  2. Move the head on tape 1 to the beginning and the head on tape 2 to the end of the copied string.
  3. Compare the symbols step-by-step, moving tape 1's head right and tape 2's head left.

If the input string has length , what is the space complexity of this 2-tape TM? What does this imply about the space complexity of a single-tape TM that could solve the same problem?

Show answer

On the 2-tape TM, both tape 1 (the input tape) and tape 2 use space. The total space complexity is .

Because a multi-tape TM can be converted to a single-tape TM with only linear space overhead, we can conclude that palindromes can also be decided in space on a single-tape machine.


3. The Relationship Between Time and Space

We've now defined two fundamental computational resources. A natural question arises: how are they related? Knowing the time complexity of an algorithm must tell us something about its space complexity, and vice versa.

The following video segment explains the two fundamental relationships between time and space.

17. Space Complexity, PSPACE, Savitch's Theorem

This final clip from the MIT lecture establishes the crucial theoretical bounds that connect time and space complexity.

Watch from 11:15 to 16:54. This is a conceptually dense and important section. Focus on the justification for both inclusions: TIME within SPACE, and SPACE within exponential TIME.

Let's break down these two critical results. Let be a function where .

  1. TIME(f(n)) ⊆ SPACE(f(n))
    This relationship is straightforward. In one time step, a Turing Machine can move its head at most one cell. Therefore, a TM that runs for steps cannot possibly visit more than distinct tape cells. Its time bound is an upper bound on its space usage.

  2. SPACE(f(n)) ⊆ TIME(2^(O(f(n))))
    This is a more profound result. It shows that any problem solvable with a certain amount of space can be solved in a corresponding amount of exponential time. Why?
    The proof relies on the number of possible configurations of the TM. A configuration is a snapshot of the machine at one instant, defined by:

    • The current state of the finite control.
    • The contents of the tape being used.
    • The position of the tape head.

    If a TM uses space, let's count the number of distinct configurations.

    • Number of states: a constant, .
    • Number of head positions: .
    • Number of possible tape contents: If the tape alphabet is , there are ways to write symbols on cells.

    The total number of configurations is the product: . This entire expression is exponential in , which we write as .
    Since our TMs are deciders, they must halt. If a TM runs for more steps than the total number of unique configurations, it must have repeated a configuration. If it repeats a configuration, it will loop forever. Therefore, the running time must be bounded by the number of configurations. This gives us the exponential time bound.


4. Space Complexity Classes

Just as we grouped problems into time complexity classes like P and NP, we can do the same for space.

Space Complexity Growth Rates
This graph illustrates how the space requirements for algorithms of different complexity classes—such as constant O(1), logarithmic O(log n), linear O(n), and exponential O(2ⁿ)—grow as the input size 'n' increases. The function \(s(n)\) in our definition of space complexity can take any of these forms.

The formal definitions for space complexity classes mirror those for time:

  • SPACE(f(n)) is the class of languages that are decidable by a deterministic Turing machine using space.
  • NSPACE(f(n)) is the class of languages that are decidable by a nondeterministic Turing machine using space.

This framework allows us to define major classes based on polynomial space bounds, which you will explore in the next lesson.

Test your understanding!

An engineer on your team designs an algorithm and proves that it runs in space on a deterministic Turing machine. Without knowing anything else about the algorithm, what is the best guaranteed upper bound you can place on its running time?

Show answer

Using the relationship SPACE(s(n)) ⊆ TIME(2^(O(s(n)))), we can substitute . Therefore, the best guaranteed upper bound on the algorithm's running time is . It might be much faster, but we know it cannot run for longer than that without getting into an infinite loop.


Conclusion

In this lesson, we have formally defined space complexity and established its core properties. This provides us with a new dimension for analyzing the difficulty of computational problems.

Key Takeaways:

  • Space complexity of a Turing Machine is the maximum number of tape cells it scans on any input of a given length .
  • For an NTM, space is measured as the maximum used on any single computational branch.
  • Unlike time, converting a multi-tape TM to a single-tape TM incurs only a linear space overhead.
  • Time and space are fundamentally related:
    • Any problem solvable in time is solvable in space: TIME(f(n)) ⊆ SPACE(f(n)).
    • Any problem solvable in space is solvable in exponential time with respect to that space: SPACE(f(n)) ⊆ TIME(2^(O(f(n)))).

Preview of the Next Lesson:
Now that we have formally defined space complexity, we are ready to explore one of the most important complexity classes built upon this concept: PSPACE. This is the class of all problems decidable by a deterministic Turing Machine using a polynomial amount of space. In the next lesson, we will define PSPACE, investigate its properties, and explore its relationship to the familiar time-based classes P and NP.