When Output Size Lower-Bounds the Work
An algorithm that explicitly produces many output records must perform enough operations to emit them all.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Count all requested objects
Enumerating every unordered pair of distinct indices from \(n\) items gives \(n(n-1)/2\) outputs. Returning one witness for a match has a smaller output. Nested iteration costs derive the count.
Change the interface deliberately
A stream can avoid storing all pairs at once, though delivering every pair still requires emitting them. Returning a formula for the number of pairs answers a different request. Include output materialization in the time and space accounting.[1]
Count explicit answers independently of searching
Listing every unordered pair of distinct item positions in a set of five takes ten pair records. Even an implementation that finds the next pair instantly still emits all ten when the interface requires explicit pairs. See counting nested iteration.
Four pairs starting at index 0
Three pairs starting at index 1
Two pairs starting at index 2
One pair starting at index 3
10 pairs in total
Change the output contract to change the work
Returning only the count can use the closed formula \(n(n-1)/2\). Streaming explicit pairs avoids keeping every pair at once but still enumerates them all if consumed. State which outputs the caller needs before attributing cost to the algorithm.