Module 27 of 39

Online algorithms: decisions without future information

See how online algorithms make decisions from partial input and how competitive analysis judges them.

Some algorithms see the whole input before they begin. Others must act while new requests arrive. A cache decides what to evict before it knows what file will be requested next. An elevator chooses a direction before it knows who will press a button a minute later. Such methods are online algorithms.

Background for this page. An online algorithm receives requests over time and must act before knowing later requests. An offline benchmark may inspect the whole sequence. Cost must be defined before comparing them. Related explanations: Big O, Big Omega, and Big Theta · Greedy algorithms: activity selection and exchange arguments.

An offline algorithm may see the complete request sequence in advance. That gives it information the online method lacks. Comparing the two can still tell us how much the missing future costs. In competitive analysis, we compare an online algorithm’s cost with the optimal offline cost on the same input. For a minimization problem, a claim might say the online cost is at most c times the offline optimum, perhaps plus a fixed additive amount, for every allowed input. This is a worst-case comparison across request sequences, not an average user experience.

Online and offline algorithms differ in what they know when choosing.

Take a cache that holds two pages. It contains A and B, and a request for C arrives. The cache must evict A or B. If the next request is A, evicting A was costly. If it is B, evicting B was costly. An offline method that sees the future can evict whichever page will be needed later. This is the farthest-next-use rule from the greedy chapter; an online cache cannot apply it exactly.

Online algorithms may use history instead. Least recently used (LRU) evicts the page that has not been requested for the longest time. It is a reasonable policy in many settings, but a particular sequence can make it perform badly. Other policies use frequency, random choices, or predictions. We judge a policy against the workload and the guarantee it actually offers.

The textbook studies online decisions in caching, search lists, and scheduling examples such as an elevator. The common question is not “What would I do if I knew everything?” It is “What rule can I follow now, with only the information available now?” A useful model must say when requests arrive, what actions are allowed, and what counts as cost.

A complete rent-or-buy example

Suppose renting equipment costs one unit per day and buying costs ten. You do not know how many days you will need it. Rent for nine days, then buy at the start of day ten if it is still needed. For fewer than ten days, you pay the same as the best offline choice: the number of days. For ten or more days, you pay nineteen, while the offline optimum buys for ten. Your cost is therefore at most 1.9 times optimum under these rules.

The strategy does not predict the future. It limits the damage of either future: stopping early or continuing for a long time. This is a competitive analysis for a specific cost model. Changing rental fees, resale value, or buying opportunities changes the analysis.

Practice. A two-page cache holds A and B, then receives C. Explain why no rule based only on the past can always choose the page whose eviction will avoid the next miss.

Answer. If the rule evicts A, the next request could be A. If it evicts B, the next request could be B. Both futures look identical up to the moment of choice, so past information alone cannot distinguish them.

Try another case. What if the strategy rents forever and never buys?

Reasoning. Its cost after D days is D, while buying costs ten. The ratio D/10 has no fixed bound as D grows.

View the full course and choose another topic.

Image preview

Fit