skip to content

Why does logarithmic space, the class L, charge only a work tape and never the read-only input a machine scans?

level: middleimportance: must knowfreq 58%

answer

  1. two tapes, only one is charged
  2. input read-only, never copied
  3. counted cells live on the work tape
  4. log n bits holds one index
  5. pointers and counters, not a buffer

basics

~20 s

Logarithmic space counts only the read-write work tape. The input is already present, read-only and freely re-readable, so charging for it would put every problem at n cells and leave no sublinear class at all.

solid answer

~50 s

The model has two tapes that are accounted differently. The **input tape** holds the n symbols of the instance: the head may move in both directions and revisit any cell as often as it likes, but nothing may be written there. The **work tape** starts blank, and the cells it uses are the only storage counted. The convention is what makes the class meaningful: the input is the question rather than scratch space, and if it were charged then every computation would cost at least n cells and the whole sublinear range would collapse. The bound then says something sharp. One index into an n-symbol input needs about `log2 n` bits, so `O(log n)` cells hold a constant number of positions plus a constant number of counters bounded by n — pointers and tallies over data you must revisit, never a copy of it. `L` is the deterministic version; `NL` is the same machine allowed to guess.

code

pseudocode · 11 lines
pseudocode
// work memory: two numbers below n, so about 2*log2(n) bits
i = 0
balance = 0
while i < length(input):            // input is read-only and not charged
    record = read(input, i)        // any position, any number of times
    if record = OPEN:  balance = balance + 1
    if record = CLOSE: balance = balance - 1
    if balance < 0: return REJECT  // a close with no matching open
    i = i + 1
if balance = 0: return ACCEPT
return REJECT

go deeper

for a junior

Recall the two tapes and which one is measured: the input is read-only and free, the work tape is what the bound counts. That alone answers the question as asked.

for a middle

Explain the arithmetic that makes the class useful: an index into n symbols costs about log2 n bits, so the work tape holds a few pointers and counters and nothing proportional to the data. Say why charging for the input would empty the class.

for a senior

Show where the model bites in practice. A tool with a fixed buffer over a huge read-only archive can revisit the archive freely but must recompute rather than remember, and you should be able to say which of two designs that rules out.

for a principal

The trade the model encodes is recomputation against retention. Be ready to argue when a design should buy a small, auditable memory ceiling at the price of repeated passes, and when that trade is simply the wrong shape for the workload.

## The model the bound is defined on A space bound is a claim about one specific machine, and the accounting only makes sense once that machine is on the table. The machine used to define logarithmic space has three tapes with three different roles: - a **read-only input tape** holding the n symbols of the instance. The head moves in both directions and may revisit any cell any number of times, but no cell can be altered. - a **read-write work tape**, initially blank. The number of cells it ever touches is the machine's space usage, and the only thing counted. - a **write-only output tape** for machines that emit an answer longer than a yes/no. Its head only moves forward, it is never read back, and it is not counted either. A machine *runs in space s(n)* when, on every input of length n, it uses at most s(n) work cells. **L** is the set of decision problems solvable this way with s(n) = O(log n) by a deterministic machine. **NL** is the same tape arrangement with a machine whose transition may branch, accepting when some branch accepts. ## Why the input is not charged Three reasons, and the third is the one that gives the class its character. 1. The input is already in memory before the computation begins. It is the question, not the answer's scratch space, and every model charges for what the algorithm *creates*. 2. If the input were charged, every machine would cost at least n cells, every problem would sit in linear space, and there would be no sublinear range in which to distinguish anything. The interesting question — can you solve this with a few pointers, or must you build a structure proportional to the data? — would have no home. 3. Because re-reading is free, the accounting *forces recomputation*. A routine that cannot afford to remember what it saw must walk back and look again. That is the behaviour the class is designed to isolate. | tape | direction | writable | counted | |---|---|---|---| | input | both ways, unlimited re-reads | no | no | | work | both ways | yes | **yes — this is the bound** | | output | forward only | write-only | no | ## What O(log n) cells actually buy Addressing arithmetic decides this. A position in an n-symbol input is a number below n, which needs about `log2 n` bits. So a logarithmic work tape holds: - a **constant number of indices** into the input — the equivalent of a handful of pointers you may move around at will; - **counters** bounded by n, or by any fixed power of n, since k·log n is still O(log n); - a constant number of symbols copied out of the input, such as the value currently under a pointer. It does **not** hold: any constant fraction of the input, a set of the distinct values seen so far, a lookup structure keyed by input values, or any tally whose count of *entries* grows with n. A structure with n entries needs at least n cells no matter how small each entry is. So the honest one-line translation is: *a logspace routine is a handful of pointers and counters over data it must revisit rather than store.* ## Free re-reading is not a single pass This is the most common confusion in the area. A one-pass streaming algorithm with O(log n) memory is a **strictly stronger** commitment: it sees each symbol once, in order, and cannot go back. A logspace machine may sweep the input left to right, then right to left, then jump to a position it computed, as many times as it likes — the only thing it may not do is write any of it down beyond its few cells. Many problems are easy for one and hopeless for the other. ## L, NL, and the example the class is built around The reserved illustration for this pair is reachability on a graph presented in the input. Asking whether one node reaches another along directed edges is the signature problem of **NL**: a guessing machine walks from the start node, holding only the current node's index and a step counter, guessing which edge to take next — two counters, nothing else. The deterministic version is the harder question. For undirected edges, a much later result placed the problem in **L**. Whether the guessing helps in general — whether L and NL differ — is a question about the inclusion chain rather than about the model, and it is not settled by anything in the accounting above. The practical shape of all this is a tool that scans an enormous read-only archive through a fixed buffer it may never grow: it may pass over the archive as often as the clock allows, and what it can decide is exactly what a few pointers and counters can decide.

  • If a routine bounded this way must emit an answer far larger than its work tape, where does that output go?
    Onto a separate write-only output tape whose head only moves forward and which is never read back. It is not charged either, for the same reason the input is not: it is not scratch space the algorithm reasons over. That is why a space-bounded transducer can emit a long result while still being counted as logarithmic space.
  • Is a routine in L the same thing as a streaming algorithm that uses O(log n) memory?
    No, and the difference runs in one direction. A logspace machine may revisit any input position any number of times and in any order; a one-pass streaming algorithm sees each symbol once and cannot go back. Every one-pass logarithmic-memory algorithm is a logspace routine, but not the reverse, so streaming is the stronger commitment.
  • What exactly does NL add to the same tape arrangement?
    Nothing about storage — the tapes and the counting are identical. NL allows the transition to branch, and the machine accepts when at least one branch accepts. Directed reachability is the example it is built around: guess the next edge, keep only the current node's index and a step counter.

The archive is a reference library you may walk back into as often as you like; the work tape is the single index card in your pocket. The bound limits what you may write on the card, not how far you walk.

saying these in an interview costs you the question

  • Says the machine may copy the input onto its work tape first
  • Assumes the input may be scanned only once, confusing logspace with one-pass streaming
  • Claims log n cells cannot even address an n-symbol input
  • Thinks the input goes uncharged because it is considered small
  • Says L and NL are plainly the same class since both use log space
  • Counts a structure with n tiny entries as logarithmic space