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