Separating Time and Extra Space

Time describes operations performed. Extra space describes how much live storage the procedure uses beyond its declared input and output.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Keep a running statistic
  2. Count a changed result separately
  3. Trace the storage that changes the time estimate
  4. Name the precise space being compared

Keep a running statistic

A scan computing the mean can keep a sum and a count. It visits every reading and retains a constant number of numeric values in a word-level model. A running average shows the state sufficient at each point.

Count a changed result separately

Returning every prefix total requires one output per reading. If a caller can consume results immediately, a streaming interface may avoid retaining those outputs in the producer. See prefix totals for the difference between one result and all intermediate ones.[1]

Trace the storage that changes the time estimate

Finding whether there are repeated values can compare all distinct index pairs using constant auxiliary state. A seen set can find a repeat while scanning once under a suitable hash-table cost model, at the price of storing distinct values. For four distinct values, pairwise comparison uses six comparisons. See seen-state sets.

Input [3, 8, 2, 6]
Pair scan compares 6 pairs, retains no collection
Set scan inserts 4 keys and checks each for membership
On [3, 8, 2, 3] the set detects the final match

Name the precise space being compared

If the input already exists in memory, distinguish auxiliary storage from total storage. If the source streams, pair comparison may require replay or storing inputs. State whether a returned collection counts toward the space result.

References

  1. Jeff Erickson, Algorithms ↩