Online algorithms solve problems where input arrives piece-by-piece, and decisions must be made without knowledge of future inputs. Competitive analysis is the gold standard for measuring such an algorithm’s quality by comparing its performance to an optimal offline algorithm that has full future knowledge. This detailed article explains competitive analysis, its importance, and how to measure and interpret the performance of online algorithms through practical examples and visual aids.

What is Competitive Analysis?

Competitive analysis evaluates the performance of an online algorithm by comparing it with an optimal offline algorithm running on the same input. The optimal offline algorithm knows the entire sequence of input in advance and can make globally optimal decisions. Competitive analysis uses the competitive ratio as a metric—a worst-case bound on how much worse the online algorithm performs relative to the offline optimal.

Formally, for a cost-minimization problem, if ALG is an online algorithm and OPT is the offline optimal, competitive ratio c satisfies:

ALG(input) ≤ c × OPT(input) + α

Here, α is a constant independent of input size. A smaller competitive ratio means the online algorithm performs closer to the optimal solution.

Competitive Analysis: Measure Online Algorithm Performance with Practical Examples

Why Competitive Analysis Matters

In scenarios where future input is unknown or unpredictable, competitive analysis provides a robust way to benchmark algorithms without relying on probabilistic assumptions. It ensures developers can understand the worst-case efficiency guarantees and make informed choices in real-time decision-making settings:

  • Resource allocation and caching
  • Scheduling
  • Network routing
  • Online search and bidding

Key Terms in Competitive Analysis

  • Online Algorithm: Algorithm acting without knowledge of future input.
  • Offline Algorithm: Algorithm with complete future knowledge, producing optimal results.
  • Competitive Ratio: Worst-case ratio of the online cost to offline cost.
  • Adversary: A hypothetical entity presenting the worst-case input to maximize the competitive ratio.

Example: The Paging Problem

The paging problem illustrates competitive analysis clearly: An online cache algorithm must decide which pages to evict when the cache is full and a new page request arrives. The goal is to minimize cache misses.

Consider an optimal offline algorithm OPT which evicts the page not needed for the longest future duration. The online algorithm LRU (Least Recently Used) evicts the page used least recently, without knowledge of future requests.

sequenceDiagram
participant User as Input Requests (Pages)
participant LRU as Online Algorithm (LRU)
participant OPT as Offline Optimal
User->>LRU: Request Page A
LRU->>User: Cache Miss (loads A)
User->>LRU: Request Page B
LRU->>User: Cache Miss (loads B)
User->>LRU: Request Page A
LRU->>User: Cache Hit
User->>LRU: Request Page C
LRU->>User: Cache Miss (evicts B)
User->>OPT: Requests known in advance
OPT->>User: Evicts B optimally for misses