Online algorithms
叶德仕
yedeshi@zju.edu.cn
1
Online & offline
Traditional theoretical analysis is
concerned with off-line problems
where the complete input is given and
we look for a time-efficient
algorithm.
In on-line problems the input is not
known in advance but instead it is
revealed during the operation of the
algorithm.
2
Simple Example
(Ski-Rental Problem) Karp92
Rental: $100/tour
Purchase: $1,000 (can be used
forever)
Question: Buy or Rental?
Inputs: 11111….. (1: new ski tour)
You do not know how many more 1’s
are coming.
You have to decide buy or rental at
each input 1. 3
Three Online Algorithms
1. Always rental.
2. But at the first 1.
3. Buy at the 10th 1 (rental before
then)
Your action must be decided at each
1 (tour) uniquely by the algorithm
4
Competitive Analysis
CR (Competitive Ratio) r Rental:$100
Buy: $1,000
Cost of algorithm Alg
Cost of optimal offline algorithm Opt
Alg r Opt c
100n
1. Always rental: r
1000
5
Competitive Analysis
CR (Competitive Ratio) r Rental:$100
Buy: $1,000
Cost of algorithm Alg
Cost of optimal offline algorithm Opt
Alg r Opt c
2. Buy at the 1000
beginning r 10.0
100 6
Competitive Analysis
CR (Competitive Ratio)r
Rental:$100
Buy: $1,000
Cost of algorithm Alg
Cost of optimal offline algorithm Opt
Alg r Opt c
3. Buy before 100 9 1000
the 10th tour r 1.9
1000 7
General lower bound
Thm: There is no online algorithm can
achieve competitive ratio less than 2
-1/B.
Proof. The adversary decides the
duration T.
1) If buy before B - 1, stops T.
2) If never buy, increase T
3) Once buy B between B – 1 and 2B,
stops T.
8
Ski rental problem
A skier must decide every day she
goes skiing whether to rent or buy
skis, unless or until she decides to
buy them. The cost to rent skis for a
day is 1 unit, while the cost to buy
skis is B units. The skiier doesn’t
know how many days she will go on
skiing before she gets tired of it
(or breaks a leg). Call this (unknown)
number of days T.
9
Analysis
Claim: The competitive ratio 2 − 1/B.
10
Randomized Ski rental
Suppose that the cost B of buying skis is 3
units.
Suppose that our skier buys the skis on Day
1,2,3 with probabilities p1, p2, p3
respectively, where p1 + p2 + p3 = 1.
If the adversary decides that the skier
will give up skiing after T days
The expected total cost will be:
11
Randomized Ski rental
The expected competitive ratio is
27/19 = 1.42105
Proof. Let p1 = 4/19, p2 = 6/19, p3 =
9/19
12