Output-Sensitive Complexity
Context: FIT2004_MOC Β· a bound that names the output size alongside the input size β the Week 1 range-reporting example that separates 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. Let be how many are printed.
- Why must appear β printing is work: outputs cost regardless of how cleverly they were found.
- Two free parameters β and can 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 index , test each against , print on a hit β scanning printing (since ).
- Algo 2 β binary search then scan β Binary Search for the smallest element (), then scan forward printing until an element is reached () β .
- Where the win is β Algo 2 never touches the out-of-range elements to the 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 (most of the array in range) both are and the binary search buys nothing; the gap is largest when β vs .
- Generalises β the same accounting governs any reporting problem (all pairs within distance , all matches of a pattern); asymptotic effort is split 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 (where ), fatal for Algo 2 β collapsing to discards the whole point of the algorithm.
- π‘ Binary-searching for itself β need not be present; search for the first element greater than (a boundary/predecessor search), 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 you must emit items. Algo 2 achieves , matching it; Algo 1βs does not.
- Why: Optimal means meets β per Big-O Notation, a that coincides with the problemβs intrinsic lower bound cannot be improved by any algorithm; 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.