Output-Sensitive Complexity
Context: FIT2004_MOC Β· a bound that names the output size alongside the input size β the Week 1 range-reporting example separating Linear Search from Binary Search + scan
Quick Revision
- π― Objective: when an algorithm must emit items, quote β the search term and the reporting term are separate and neither absorbs the other.
- β‘ Key Constraint: is a free parameter, not a constant β dropping it is the mark-loss; for a range report is simply false.
π Core
1. The problem shape
- Setup β sorted array of integers, bounds ; print every element strictly between them, of them.
- Why must appear β printing is work: outputs cost however cleverly they were found. and grow independently (), so by the free-parameter rule in Algorithmic Complexity both belong in the bound.
2. The two algorithms
- Algo 1 β linear scan β walk , test each against β scan print (since ).
- Algo 2 β binary search then scan β Binary Search for the smallest element (), then scan forward until an element () β .
- Where the win is β Algo 2 never touches the out-of-range elements left of ; Algo 1 touches all of them.
3. Optimality and the flip point
- Lower bound β any correct algorithm is (it must emit items) and (comparison-based location in a sorted array) βΉ Algo 2 is optimal, its meets the problemβs .
- When it flips β at both are and the binary search buys nothing; the gap is largest at β vs .
- Generalises β the same accounting governs any reporting problem (all pairs within distance , all matches of a pattern): effort splits into locate report.
β οΈ Common Mistakes
- π‘ Quoting Algo 2 as β ignores the print loop; the answer is and the missing term is exactly what is being tested.
- π‘ Absorbing into βbecause β β legal for Algo 1, fatal for Algo 2 β collapsing to discards the whole point.
- π‘ Binary-searching for itself β need not be present; search for the first element greater than , or the scan starts in the wrong place.
π§ Active Recall
Both algorithms print the same values, yet only one is called optimal. On what grounds?
- Hint: Compare each upper bound against the problemβs lower bound, not against each other.
Answer
- Short answer: the problem is β you must locate the boundary and emit items. Algo 2 achieves , matching it; Algo 1βs does not.
- Why: Optimal means meets β per Big-O Notation, a bound coinciding with the problemβs intrinsic lower bound cannot be improved; Algo 1 wastes comparisons on elements it can prove are out of range.
Why is allowed in an asymptotic bound at all, when a capped parameter like is not?
- Hint: What can grow without limit as the instance grows?
Answer
- Short answer: is unbounded β it ranges over and grows with the instance β so it is a legitimate free parameter; a capped quantity contributes and is discarded by .
- Why: Asymptotics describe growth β Algorithmic Complexityβs rule is to list which parameters may grow before quoting a bound; passes that test, does not.