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