Chapter 8: Online Algorithms

Chapter 8: Online Algorithms · Study Questions
The online modelCompetitive ratioSki rentalPaging: FIFO, LRU, OPTList updateData streamsReservoir sampling
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Online Model

Question 1: Which Algorithm Can Run Online?

Items arrive one by one, and after each arrival the sorted order of everything seen so far must be ready. Which algorithm fits this online model naturally?

B: Insertion sort puts each arriving item into the sorted sequence built so far; it never needs to know the future. Selection sort must know the minimum of the whole input even in its first step; merge sort and QuickSort also need the entire input (offline).

Multiple Choice · Online Model

Question 2: Definition of the Competitive Ratio

For a minimization problem, what does it mean that an online algorithm ALG is "c-competitive"?

A: The competitive ratio is a worst-case guarantee: for every σ the cost never exceeds c times OPT (plus a constant). No probability assumption is made (B describes an average case). It measures cost, not running time. D speaks of a single input; the guarantee covers all inputs.

Multiple Choice · Online Model

Question 3: Types of Adversary

When analyzing a randomized paging algorithm, which statement is true of the oblivious adversary?

C: The oblivious adversary knows the algorithm but cannot see the coin flips; it fixes the sequence beforehand and the expected cost is measured. B describes the adaptive adversary, against which randomness helps much less. The 2Hk ratio of the marking algorithm holds against the oblivious adversary.

Step-by-Step Trace · Ski Rental

Question 4: The Break-Even Strategy

skiCost(5, 5, 7): B = 5, buying day k = 5, actual number of days d = 7. How does the cost accumulate, and what is the competitive ratio?

day 1day 2day 3day 4day 5day 6day 7

Buying costs 5 TL, renting costs 1 TL per day. Strategy: rent for k − 1 = 4 days; if the skier is still skiing on day 5, buy. The season will last d = 7 days (the algorithm does not know this).

Rday 1day 2day 3day 4day 5day 6day 7

day = 1 ≠ k → rent: cost = 1.

Rday 1Rday 2day 3day 4day 5day 6day 7

day = 2 ≠ k → rent: cost = 2.

Rday 1Rday 2Rday 3day 4day 5day 6day 7

day = 3 ≠ k → rent: cost = 3.

Rday 1Rday 2Rday 3Rday 4day 5day 6day 7

day = 4 ≠ k → rent: cost = 4.

Rday 1Rday 2Rday 3Rday 4Bday 5–day 6–day 7

day = 5 = k → buy: cost = 4 + 5 = 9, break. The remaining days (6, 7) are free.

Rday 1Rday 2Rday 3Rday 4Bday 5–day 6–day 7

ALG = 9, OPT = min(d, B) = min(7, 5) = 5 → ratio 1.8 = 2 − 1/B. With d = 5 we would also get ALG 9 and OPT 5: the adversary's best choice is d = k.

int skiCost(int B, int k, int d) {
  int cost = 0;
  for (int day = 1; day <= d; day++) {
    if (day == k) { cost += B; break; }  // buy
    cost += 1;  // rent
  }
  return cost;
}

Output Question · Ski Rental

Question 5: Ratios for B = 12

In ski rental the purchase price is B = 12 TL. (a) What is the competitive ratio of the break-even strategy (rent for 11 days, buy on day 12)? (b) What is the worst-case ratio of a strategy that buys on day 4; which d does the adversary choose?

Show answer
  • (a) The adversary picks d = 12: ALG = 11 + 12 = 23, OPT = 12 → 23/12 = 2 − 1/12 ≈ 1.92.
  • (b) The adversary ends the season on the buying day: d = 4. ALG = 3 rentals + 12 = 15, OPT = min(4, 12) = 4 → 15/4 = 3.75.

In general: for k < B the ratio is (k − 1 + B)/k, for k ≥ B it is (k − 1 + B)/B; both are smallest at k = B, with value 2 − 1/B. Buying too early and buying too late are both bad.

Classic Question · Ski Rental

Question 6: Why Nothing Beats 2 − 1/B

Using an adversary argument, show that no deterministic online strategy for ski rental can have a competitive ratio better than 2 − 1/B. How does randomization change this bound?

Show answer

Every deterministic strategy is described by one number: k = the buying day. The adversary knows the code, so it ends the season exactly on the buying day: d = k.

  • ALG = (k − 1) + B, OPT = min(k, B).
  • k < B (k ≤ B − 1): ratio (k − 1 + B)/k = 1 + (B − 1)/k ≥ 1 + 1 = 2 > 2 − 1/B.
  • k ≥ B: ratio (k − 1 + B)/B ≥ (2B − 1)/B.

For every k the ratio is ≥ 2 − 1/B; equality holds at k = B. Randomly choosing k keeps the adversary from knowing the buying day: the expected ratio drops to e/(e − 1) ≈ 1.58 (Karlin et al.).

Step-by-Step Trace · Paging

Question 7: Tracing LRU Paging

lruFaults(new int[]{2, 3, 2, 1, 5, 2, 4, 5, 3, 2}, 3) is called. How does the list cache change, and how many page faults occur?

head: oldest

k = 3, the list is empty. Requests: 2 3 2 1 5 2 4 5 3 2. The head of the list is the page unused for the longest time.

2

Page 2 is absent → faults = 1. There is room.

23

Page 3 is absent → faults = 2. There is room.

32hit

Page 2 is in the list: remove returns true → hit; it is added at the tail, now the most recently used.

321

Page 1 is absent → faults = 3. There is room.

2153 out

Page 5 is absent → faults = 4. Full → the head (least recently used) 3 is evicted.

152hit

Page 2 is in the list: remove returns true → hit; it is added at the tail, now the most recently used.

5241 out

Page 4 is absent → faults = 5. Full → the head (least recently used) 1 is evicted.

245hit

Page 5 is in the list: remove returns true → hit; it is added at the tail, now the most recently used.

4532 out

Page 3 is absent → faults = 6. Full → the head (least recently used) 2 is evicted.

5324 out

Page 2 is absent → faults = 7. Full → the head (least recently used) 4 is evicted.

532

In 10 requests: 7 faults, 3 hits. Because remove scans the list, each request costs O(k); with a LinkedHashMap it becomes O(1).

int lruFaults(int[] requests, int k) {
  var cache = new LinkedList<Integer>();
  int faults = 0;
  for (int page : requests) {
    if (cache.remove((Integer) page)) {
      cache.addLast(page);  // make most recent
    } else {
      faults++;
      if (cache.size() == k)
        cache.removeFirst();  // evict the oldest
      cache.addLast(page);
    }
  }
  return faults;
}

Output Question · Paging

Question 8: Belady's OPT Policy

For the request sequence 2 3 2 1 5 2 4 5 3 2 and k = 3, how many page faults does Belady's OPT policy (evict the page whose next use is farthest away) make? Which page is evicted at each eviction?

Show answer
Request2321524532
ResultFF✓FF✓F✓✓F
Evicted124
  • When 5 arrives the cache is {2, 3, 1}: next uses are 2 → 6th, 3 → 9th, 1 → never → 1 is evicted.
  • When 4 arrives the cache is {2, 3, 5}: 2 → 10th, 3 → 9th, 5 → 8th → the farthest is 2 is evicted.
  • When the last 2 arrives the cache is {4, 3, 5}: 4 is never requested again → 4 is evicted.

OPT = 6 faults; LRU and FIFO make 7. The faults on the first 3 distinct pages (2, 3, 1) are unavoidable under every policy.

Output Question · Paging

Question 9: Belady's Anomaly

Request sequence 0 1 2 3 0 1 4 0 1 2 3 4. How many page faults do FIFO and LRU make with k = 3 and with k = 4 frames? Interpret the result.

Show answer
Policyk = 3k = 4
FIFO910
LRU108

Although the cache grew, FIFO's faults increased from 9 to 10: Belady's anomaly. With k = 4, pages 0 and 1 are evicted by queue order just before they are needed.

LRU and OPT are each a stack algorithm: the pages in k frames are always a subset of those in k + 1 frames, so faults cannot increase when frames are added (LRU: 10 → 8).

Multiple Choice · Paging

Question 10: FIFO versus LRU

The cache holds pages 3, 5, 8 (FIFO load order 3, 5, 8; the LRU usage order is also 3, 5, 8 at the start). Pages 3 (a hit) and then 9 (a fault) are requested. Which pages do FIFO and LRU evict for 9?

A: In FIFO a hit does not change the order: the page loaded first, 3, is evicted (even though it was just used). In LRU the hit makes 3 the most recent (order 5, 8, 3), so 5, unused for the longest time, is evicted. The only difference between the policies is whether a hit updates the order.

Multiple Choice · Paging

Question 11: Ratio of the Marking Algorithm

The randomized marking algorithm is 2Hk-competitive against an oblivious adversary. For k = 4, about what is this ratio?

D: H₄ = 1 + 1/2 + 1/3 + 1/4 = 25/12 ≈ 2.08 → 2H₄ = 25/6 ≈ 4.17. The value 2.08 is the lower bound for randomized algorithms, Hk; 4.00 is the ratio k of deterministic LRU/FIFO; 8.00 = 2k.

Classic Question · Paging

Question 12: The Deterministic Lower Bound k

Using an adversary sequence with only k + 1 distinct pages, show that no deterministic paging algorithm can have a competitive ratio better than k.

Show answer
  • The adversary knows the algorithm's code; at every step it requests the one page that is not in ALG's cache (one of the k + 1 pages is always outside). ALG faults on every request: n faults in n requests.
  • When OPT faults it evicts the page whose next use is farthest away. The other k − 1 pages must be requested before that page, so at least k requests pass before the next fault: OPT makes at most ⌈n/k⌉ faults.

The ratio ALG / OPT ≥ n / (n/k) = k (Sleator and Tarjan). LRU and FIFO reach this bound (k-competitive); doing better requires randomness (2Hk).

Step-by-Step Trace · List Update

Question 13: Tracing Move-to-Front

mtfCost is called with the list A B C D and the requests D D C D C D. How do the cost and the list change at each request?

ABCD

List A B C D; requests D D C D C D. Accessing the item at position i costs i units (scanned from the front).

DABC+4

D: at position 4 → cost 4, total 4; D moves to the front.

DABC+1

D: at position 1 → cost 1, total 5; D moves to the front.

CDAB+4

C: at position 4 → cost 4, total 9; C moves to the front.

DCAB+2

D: at position 2 → cost 2, total 11; D moves to the front.

CDAB+2

C: at position 2 → cost 2, total 13; C moves to the front.

DCAB+2

D: at position 2 → cost 2, total 15; D moves to the front.

DCAB

MTF total 15. On the same sequence a list that never moves anything pays 4+4+3+4+3+4 = 22, and transpose (move one step forward) pays 17: the frequently requested D and C quickly come to the front.

int mtfCost(LinkedList<Character> list, char[] requests) {
  int total = 0;
  for (char x : requests) {
    int position = list.indexOf(x) + 1;  // 1-based
    total += position;
    list.remove((Character) x);
    list.addFirst(x);  // move to front
  }
  return total;
}

Multiple Choice · List Update

Question 14: List Update Strategies

Which statement about strategies for the list update problem is true?

C: Sleator–Tarjan: MTF ≤ 2 · OPT; the potential Φ is the number of inverted pairs between the MTF and OPT lists (amortized analysis). When the last two items are requested in turn, transpose pays ≈ n per request; frequency count clings to the old order when popularity shifts. On some sequences (e.g., C A C D B A on A B C D) MTF can be worse than not moving at all.

Output Question · Data Streams

Question 15: Online Median

The items 7, 3, 9, 1, 5 are inserted in turn into a two-heap online median structure (left: max-heap, right: min-heap), and after each insertion System.out.print(median() + " ") is run. What is the output? What do the heaps hold at the end?

void add(int x) {
  if (left.isEmpty() || x <= left.peek())
    left.offer(x);  // smaller half
  else
    right.offer(x);  // larger half
  if (left.size() > right.size() + 1)
    right.offer(left.poll());
  else if (right.size() > left.size())
    left.offer(right.poll());
}
double median() {
  if (left.size() == right.size())
    return (left.peek() + right.peek()) / 2.0;
  return left.peek();
}
Show answer

Output: 7.0 5.0 7.0 5.0 5.0

xleft (max-heap)right (min-heap)median
77—7 (odd)
337(3 + 7)/2 = 5
93, 7 (7 moved back)97
11, 37, 9(3 + 7)/2 = 5
51, 3, 57, 95

9 goes into right; since right > left in size, the top of right, 7, moves to left. When 1 arrives, left grows to 3 items and its top 7 passes to right. median() returns a double, so with an odd count it prints 7.0, not 7.

Output Question · Data Streams

Question 16: Reservoir Sampling

reservoir(new int[]{5, 8, 2, 9, 4, 7}, 2) is called; random.nextInt(i + 1) returns 1, 3, 0, 4 in turn. What list is returned? With what probability does the item at index 3 (9) enter the reservoir, and with what probability is it in the reservoir at the end of the stream?

List<Integer> reservoir(int[] a, int k) {
  List<Integer> r = new ArrayList<>();
  for (int i = 0; i < Math.min(k, a.length); i++)
    r.add(a[i]);
  for (int i = k; i < a.length; i++) {
    int j = random.nextInt(i + 1);  // 0..i
    if (j < k)
      r.set(j, a[i]);
  }
  return r;
}
Show answer
  • The first k = 2 items: r = [5, 8].
  • i = 2 (2), j = 1 < 2 → r[1] = 2 → [5, 2].
  • i = 3 (9), j = 3 ≥ 2 → not inserted.
  • i = 4 (4), j = 0 → r[0] = 4 → [4, 2].
  • i = 5 (7), j = 4 → not inserted.

Result [4, 2].

4021

The probability that 9 enters is k/(i + 1) = 2/4 = 1/2; its probability of being in the reservoir at the end of the stream, like every item's, is k/n = 2/6 = 1/3 (1/2 · 4/5 · 5/6).

Multiple Choice · Data Streams

Question 17: Probability of Staying in the Reservoir

In reservoir sampling with k = 3, with what probability does the item at index i = 11 enter the reservoir when it arrives? If the stream ends with n = 20 items in total, with what probability is that item in the reservoir at the end?

D: Entering: k/(i + 1) = 3/12 = 1/4. At each later step t > i it survives with probability (t)/(t + 1); the product 1/4 · 12/13 · … · 19/20 = 3/20 = k/n: the same for every item. 3/11 comes from mistaking the range for nextInt(i); 1/20 is for a one-item sample.

Multiple Choice · Data Streams

Question 18: The nextInt(i) Bug

In the reservoir code, what happens if random.nextInt(i + 1) is replaced by random.nextInt(i) (i ≥ k)?

B: nextInt(i) yields 0 … i − 1; the probability of j < k becomes k/i instead of k/(i + 1). Late arrivals enter more often than they should and early ones are evicted more often: the sample is not uniform. At i = k the probability rises to 1: the item at index k always enters.

The End

Chapter 8: Online Algorithms: Study Questions
Lecture slides: Chapter 8: Online Algorithms · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.