> For the complete documentation index, see [llms.txt](https://cs61b-2.gitbook.io/cs61b-textbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs61b-2.gitbook.io/cs61b-textbook/39.-compression-complexity-p-np/39.3-space-time-bounded-compression.md).

# 39.3 Space/Time-Bounded Compression

As described in the previous chapter, it is impossible to write the "perfect" compression algorithm that requires the fewest bits to output some bitstream $$B$$.&#x20;

## Space-Bounded Compression

However, what about the problem of space-bounded compression? In this problem, we take in two inputs: a bitstream $$B$$ and a target size $$S$$. The goal, then, is to find a program of length $$\leq S$$ that outputs $$B$$.&#x20;

It turns out that such a problem is also uncomputable. If it were, then we could simply binary search on different values of $$S$$ to find the optimal compression program size, which is impossible as shown in te previous section.

## Space-Time-Bounded Compression

What if we take our problem from above, and add a constraint that we can run at most $$T$$ lines of bytecode?

It might seem unintuitive, but this kind of problem is actually solvable. We will use the following algorithm:

```
for length L = 1....S:
    for each possible program P of length L:
        while (P is running && !(B is outputted) && lines_executed < T):
            run the next line of P
```

The runtime of this algorithm is $$O(T \* 2^S)$$, and in the end, it will either output some program `P` that has the correct output and is bounded by $$T$$ and $$S$$, or return that no such program is possible.

## Efficient Bounded Compression

The runtime above is exponential in $$S$$. Thus, we might ask if it's possible to solve the space-time-bounded compression problem *efficiently*. As we'll see in the next chapter, this depends on our definition of efficiency.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cs61b-2.gitbook.io/cs61b-textbook/39.-compression-complexity-p-np/39.3-space-time-bounded-compression.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
