Online Algorithm

Context: FIT2004_MOC · the classification that decides which algorithms are even admissible for a problem — before complexity is discussed at all · drilled through the streaming -smallest pattern, whose answer is a size- Heap · contrast the offline Quickselect, which needs every item resident

Quick Revision

  • 🎯 Objective: process input one item at a time, committing to a decision without seeing the rest ➔ maintain the answer incrementally instead of computing it once at the end.
  • 📦 Core Components: offline ➔ all items up front, may re-read freely | online ➔ item arrives, act, discard; may be unknown or unbounded.
  • ⚡ Key Constraint: for the -smallest, the heap is a max-heap, not a min-heap ➔ you need the worst admitted item at the root, because the root is the eviction threshold every new arrival is tested against.

📝 How It Works

1. Online vs Offline

  • Offline ➔ the whole input is available before the algorithm starts ⟹ it may scan repeatedly, sort, index, or partition destructively — Merge Sort, Quickselect, [[Heap|bottom-up build_heap]].
  • Online ➔ items arrive sequentially and the algorithm must hold a valid answer after every arrival, having seen no future item — insertion sort, [[Heap|add/rise]], the size- heap below.
  • Why it matters(1) the stream may exceed memory ⟹ space is unaffordable · (2) may be unknown or infinite ⟹ “sort it first” is not an option · (3) an answer may be required now, at every instant, not after the last item lands.
  • The classification precedes the bound ➔ an offline algorithm is worse than useless on a stream, while an online one solves it — feasibility outranks asymptotics ➔ Algorithmic Complexity.

2. The Streaming -Smallest

  • Structure ➔ a max-heap capped at size , holding the smallest items seen so far.
  • Invariantafter every arrival, the heap contains exactly the smallest items of the prefix seen so far, and its root is the largest of them.
  • Admit ➔ while the heap holds items, insert unconditionally in .
  • Test then act ➔ once full, compare the arrival against the root only: arrival root ⟹ reject in · arrival root ⟹ get_max to evict the root, then add the arrival ⟹ .
  • Why the root alone decides ➔ the root is the largest of the current smallest, i.e. the weakest member. An arrival root is all members, so it cannot displace any of them; an arrival root displaces exactly one, and the root is unambiguously the one that must go. Comparing against the other items is redundant work.
  • The mirror largestmin-heap of size , root smallest admitted the threshold. Always heap on the opposite extreme to the one you are collecting.
  • Answering “the -th smallest” ➔ it is the root at termination; the heap yields the whole -set for free, which Quickselect does not.

3. Cost Profile

  • Time ➔ each of arrivals costs to reject or to admit; the reject path dominates in practice once the heap has settled on small values.
  • Space , independent of ➔ this is the property that makes the algorithm online at all; no strategy storing can process an unbounded stream.
  • Single pass ➔ each item is examined exactly once and then discarded — the formal statement of “online”.

⚙️ Core Implementation

🔹 Streaming -smallest with a size- max-heap

⚖️ Core Decision Matrix

StrategyTimeAuxiliary spaceOnline?Selection rule
Sort, take first Nothe sorted list is wanted anyway
Quickselect expectedNo — must partition all resident, one rank, mutation allowed
Size- max-heapYes unknown/unbounded, or with a memory cap
Full Heap of , extract No close to , all items resident
Sorted array, insert each arrivalYes tiny (2–3), where shifting beats heap overhead

When It Flips: with all resident and one rank wanted, Quickselect's beats — take it. The heap wins the moment either premise fails: unbounded , a memory cap, or a requirement that the answer be valid at every instant. At the heap degenerates to , i.e. Heapsort — so is the operating assumption.

📊 Exam Execution Trace & Applied Exercises

Manual Execution Trace

Stream with . Heap contents shown as a set; only the root is ordered.

StepArrivalvs rootActionHeap afterRoot (threshold)
0 (Init)
1fill
2fill
3fill (now full)
4evict , add
5evict , add
6reject,
7evict , add

Final: — the smallest, with the root being the rd smallest. The threshold is monotonically non-increasing ( after the fill phase), so rejections get cheaper as the stream runs.

Applied Exercise

Problem: A sensor emits readings; report the smallest under a memory cap that forbids storing the stream. Justify the algorithm and quote both bounds.

Final Extracted Output: the size- max-heap — the only candidate whose space is rather than . Note the winner is asymptotically slower in time than Quickselect; the constraint that selects it is space and the online requirement, not speed.

⚠️ Common Mistakes

  • 💡 Calling Quickselect online because it is fast ➔ it partitions the entire array, so it needs every item before it can emit anything; time does not make an algorithm streamable.
  • 💡 Comparing the arrival against all heap items ➔ destroys the reject path and makes each arrival ; the heap exists precisely so that one comparison suffices.
  • 💡 Quoting ➔ the heap is capped at , so operations cost ; the bound only degenerates to when .

🧠 Active Recall