TExcelTAD

Exercises

Fourteen problems covering the whole course: competitive ratios and adversaries, ski rental, paging, list update, kk-server and online matching. Try each one on paper first. Every problem has a short Answer to check yourself against and a full Solution; several solutions include a program that confirms the result, which you can edit and run.

Tip: For an upper bound, look for a potential function or a phase decomposition and compare the algorithm with OPT phase by phase. For a lower bound, decide first whether you need it for deterministic algorithms (build the sequence adaptively) or for randomized ones (choose an input distribution and use Yao's principle), and make sure OPT grows without bound if the bound is non-strict.

# Exercise Practises
1 List scheduling, tight strict ratios
2 The wrong buying day deterministic ski rental
3 Randomized ski rental for B=3B = 3 deriving the distribution
4 The hard distribution for B=2B = 2 Yao's principle
5 Randomness that leaks adaptive adversaries
6 Belady's anomaly Belady, FIFO, LRU
7 Counting phases marking and conservative algorithms
8 How much more memory? resource augmentation
9 One phase of MARK the 2Hk2H_k analysis
10 Coupon collecting for OPT the HkH_k lower bound
11 One step of move-to-front the list-update potential
12 One step of double coverage the kk-server potential
13 RANKING on a small triangle online matching
14 Primal-dual ski rental for B=2B = 2 the online primal-dual method

Exercise 1: List scheduling, tight

Graham's list scheduling is (2−1m)(2 - \frac{1}{m})-competitive for makespan on mm machines (The Competitive Ratio, Precisely). For m=4m = 4, give a job sequence on which list scheduling has makespan exactly 74\frac{7}{4} times the optimum. Is this a lower bound on the strict or the non-strict ratio of list scheduling?

Answer

Twelve jobs of size 1, then one job of size 4: list scheduling reaches makespan 7, OPT 4. Since every job can be scaled by any factor ss, the same ratio holds with OPT=4s\mathrm{OPT} = 4s for every ss, so it bounds the non-strict ratio as well.

Solution

The twelve unit jobs spread evenly, three per machine, and the job of size 4 lands on a machine with load 3: makespan 7. OPT puts the size-4 job alone and four unit jobs on each of the other three machines: makespan 4. A single instance only bounds the strict ratio, but multiplying all sizes by ss multiplies both makespans by ss, so ALG=74OPT\mathrm{ALG} = \frac{7}{4}\mathrm{OPT} on instances with arbitrarily large OPT, and no additive constant can help.

Exercise 2: The wrong buying day

Ski rental with B=7B = 7: a skier buys on the morning of day 4 if the season is still on. What is the worst-case ratio of this strategy, and which season length attains it? Which buying day is best, and what is its ratio?

Answer

c(4)=(4−1+7)/min⁡(4,7)=10/4=2.5c(4) = (4 - 1 + 7)/\min(4, 7) = 10/4 = 2.5, attained by a 4-day season. The best day is 7, with ratio 13/7≈1.85713/7 \approx 1.857.

Solution

By the analysis of every deterministic strategy, strategy jj is worst when the season ends on the purchase day, with ratio (j−1+B)/min⁡(j,B)(j - 1 + B)/\min(j, B). For j=4j = 4 this is 10/410/4. The function decreases up to j=Bj = B and increases after, so j=7j = 7 is best with (6+7)/7=2−1/7(6 + 7)/7 = 2 - 1/7.

Exercise 3: Randomized ski rental for B=3B = 3

Derive the optimal randomized strategy for B=3B = 3 (buying day J∈{1,2,3}J \in \{1, 2, 3\}) by equalizing the expected cost over seasons, and compute its ratio exactly.

Answer

Pr⁡[J=1,2,3]=419,619,919\Pr[J = 1, 2, 3] = \frac{4}{19}, \frac{6}{19}, \frac{9}{19}, ratio 2719≈1.421\frac{27}{19} \approx 1.421.

Solution

The recurrence B pD=(B−1) pD+1B\,p_D = (B - 1)\,p_{D+1} of Randomized Ski Rental gives p1:p2:p3=4:6:9p_1 : p_2 : p_3 = 4 : 6 : 9, so p=(4,6,9)/19p = (4, 6, 9)/19. The ratio is c3=1/(1−(2/3)3)=27/19c_3 = 1/(1 - (2/3)^3) = 27/19. The program checks that every season length gives this ratio.

python · try it yourself
from fractions import Fraction as F

B = 3
p = [F(4, 19), F(6, 19), F(9, 19)]
for D in range(1, 6):
    cost = sum(pi * ((i - 1 + B) if D >= i else D) for i, pi in enumerate(p, start=1))
    print(f"season {D}: E[cost] = {cost}, OPT = {min(D, B)}, ratio = {cost / min(D, B)}")
print("1/(1 - (2/3)^3) =", 1 / (1 - F(2, 3) ** 3))
season 1: E[cost] = 27/19, OPT = 1, ratio = 27/19
season 2: E[cost] = 54/19, OPT = 2, ratio = 27/19
season 3: E[cost] = 81/19, OPT = 3, ratio = 27/19
season 4: E[cost] = 81/19, OPT = 3, ratio = 27/19
season 5: E[cost] = 81/19, OPT = 3, ratio = 27/19
1/(1 - (2/3)^3) = 27/19

Exercise 4: The hard distribution for B=2B = 2

For ski rental with B=2B = 2, find a distribution over seasons (one short season D=1D = 1 and one long season) on which both deterministic strategies "buy on day 1" and "buy on day 2" have the same expected cost. What lower bound does Yao's principle give?

Answer

Pr⁡[D=1]=Pr⁡[long]=12\Pr[D = 1] = \Pr[\text{long}] = \frac{1}{2}. Both strategies cost 2 in expectation, E[OPT]=1.5\mathbb{E}[\mathrm{OPT}] = 1.5, so no randomized strategy beats 43\frac{4}{3}, which equals c2=1/(1−(1/2)2)c_2 = 1/(1 - (1/2)^2).

Solution

Buying on day 1 costs 2 in both seasons. Buying on day 2 costs 1 in the short season and 1+2=31 + 2 = 3 in the long one. With probability qq for the short season, the costs are 22 and q+3(1−q)q + 3(1 - q); they are equal for q=12q = \frac{1}{2}. Later buying days cost more on the long season, so they are worse. E[OPT]=12⋅1+12⋅2=32\mathbb{E}[\mathrm{OPT}] = \frac{1}{2} \cdot 1 + \frac{1}{2} \cdot 2 = \frac{3}{2}, and every deterministic strategy has E[A]≥2=43 E[OPT]\mathbb{E}[A] \ge 2 = \frac{4}{3}\,\mathbb{E}[\mathrm{OPT}]. By Yao's principle, the strict randomized ratio is at least 43\frac{4}{3}, which the distribution p=(13,23)p = (\frac{1}{3}, \frac{2}{3}) achieves.

Exercise 5: Randomness that leaks

A randomized paging algorithm is HkH_k-competitive against oblivious adversaries. Its eviction choices are observable by an attacker who also controls the requests (for example through timing). What ratio can the attacker force? What does the theorem of Ben-David et al. say about the existence of a good deterministic algorithm if some randomized paging algorithm were cc-competitive against adaptive online adversaries?

Answer

Ratio kk: the attacker is an adaptive adversary and requests the page that is not cached. A cc-competitive algorithm against adaptive online adversaries would imply a deterministic c2c^2-competitive one; since deterministic paging cannot beat kk, such a cc must satisfy c≥kc \ge \sqrt{k} (and in fact c≥kc \ge k by a direct argument).

Solution

With k+1k + 1 pages and knowledge of the cache, the adversary makes every request a miss, while OPT on the resulting sequence misses at most once every kk requests (Oblivious and Adaptive Adversaries). The relation Cdet≤Cad-on2C_{\text{det}} \le C_{\text{ad-on}}^2 gives the weaker k\sqrt{k}; the direct argument of Raghavan and Snir (1989) shows that adaptive online adversaries force kk.

Exercise 6: Belady's anomaly

For the request sequence 1 2 3 4 1 2 5 1 2 3 4 5, count the misses of FIFO, LRU and Belady's rule with 3 and with 4 cache slots (empty start). What is surprising?

Answer
slots FIFO LRU OPT
3 9 10 7
4 10 8 6

FIFO misses more with the larger cache: Belady's anomaly (Belady, Nelson and Shedler, 1969).

Solution

LRU and OPT are stack algorithms: the cache with kk slots is always a subset of the cache with k+1k + 1 slots, so more memory never hurts. FIFO lacks this inclusion property, and this sequence exploits it.

python · try it yourself
def misses(reqs, k, policy):
    cache, count = [], 0
    for t, p in enumerate(reqs):
        if p in cache:
            if policy == "lru":
                cache.remove(p)
                cache.append(p)
            continue
        count += 1
        if len(cache) == k:
            if policy == "opt":
                rest = reqs[t + 1:]
                cache.remove(max(cache, key=lambda q: rest.index(q) if q in rest else len(rest)))
            else:
                cache.pop(0)
        cache.append(p)
    return count

reqs = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
for k in [3, 4]:
    print(f"{k} slots: FIFO {misses(reqs, k, 'fifo')}, LRU {misses(reqs, k, 'lru')}, OPT {misses(reqs, k, 'opt')}")
3 slots: FIFO 9, LRU 10, OPT 7
4 slots: FIFO 10, LRU 8, OPT 6

Exercise 7: Counting phases

With k=3k = 3, split A B C A D A B E B A C D E into phases. Bound the misses of any marking algorithm and of OPT using the phase argument, and compare with the actual counts for LRU and OPT.

Answer

Phases: A B C A | D A B | E B A | C D E, so m=4m = 4. Marking algorithms miss at most km=12km = 12 times; OPT misses at least m−1=3m - 1 = 3 times (in fact at least k+m−2=5k + m - 2 = 5 with an empty start). LRU misses 9 times and OPT 7 times.

Solution

A phase is the longest stretch with at most kk distinct pages (LRU and FIFO Are k-Competitive). The program computes the phases and the counts.

python · try it yourself
def phases(reqs, k):
    out, cur = [], []
    for p in reqs:
        if p not in cur and len(set(cur)) == k:
            out.append(cur)
            cur = []
        cur.append(p)
    return out + [cur]

def lru(reqs, k):
    cache, m = [], 0
    for p in reqs:
        if p in cache:
            cache.remove(p)
        else:
            m += 1
            if len(cache) == k:
                cache.pop(0)
        cache.append(p)
    return m

def opt(reqs, k):
    cache, m = set(), 0
    for t, p in enumerate(reqs):
        if p not in cache:
            m += 1
            if len(cache) == k:
                rest = reqs[t + 1:]
                cache.remove(max(cache, key=lambda q: rest.index(q) if q in rest else len(rest)))
            cache.add(p)
    return m

reqs = "A B C A D A B E B A C D E".split()
ph = phases(reqs, 3)
print(" | ".join(" ".join(x) for x in ph), f"-> {len(ph)} phases")
print(f"LRU {lru(reqs, 3)} <= k*m = {3 * len(ph)};  OPT {opt(reqs, 3)} >= k + m - 2 = {3 + len(ph) - 2}")
A B C A | D A B | E B A | C D E -> 4 phases
LRU 9 <= k*m = 12;  OPT 7 >= k + m - 2 = 5

Exercise 8: How much more memory?

The offline optimum has h=20h = 20 slots. How many slots kk does LRU need so that the resource-augmentation bound guarantees ratio at most 1.51.5? At most 1.11.1?

Answer

k≥57k \ge 57 for 1.51.5, and k≥209k \ge 209 for 1.11.1.

Solution

The bound is kk−h+1\frac{k}{k - h + 1} (Resource Augmentation). kk−19≤1.5  ⟺  k≤1.5k−28.5  ⟺  k≥57\frac{k}{k - 19} \le 1.5 \iff k \le 1.5k - 28.5 \iff k \ge 57. kk−19≤1.1  ⟺  0.1k≥20.9  ⟺  k≥209\frac{k}{k - 19} \le 1.1 \iff 0.1k \ge 20.9 \iff k \ge 209.

Exercise 9: One phase of MARK

A phase of the marking algorithm with k=10k = 10 has m=3m = 3 new pages, requested first, followed by the 7 old pages. What is the expected number of misses in the phase? Compare with the cruder bound mHkmH_k.

Answer

m(1+H10−H3)=3(1+2.9290−1.8333)≈6.287m(1 + H_{10} - H_3) = 3(1 + 2.9290 - 1.8333) \approx 6.287; the cruder bound is 3H10≈8.7873H_{10} \approx 8.787.

Solution

The new pages miss once each. The jj-th old page misses with probability 310−j+1\frac{3}{10 - j + 1}, j=1,…,7j = 1, \ldots, 7: 3(110+19+⋯+14)=3(H10−H3)≈3.2873\bigl(\frac{1}{10} + \frac{1}{9} + \cdots + \frac{1}{4}\bigr) = 3(H_{10} - H_3) \approx 3.287 (The Randomized Marking Algorithm). Total ≈6.287\approx 6.287.

Exercise 10: Coupon collecting for OPT

Requests are uniform over k+1=4k + 1 = 4 pages and the cache has k=3k = 3 slots. What is the expected number of requests between two consecutive misses of OPT? What is every deterministic algorithm's miss rate, and what ratio does this give?

Answer

(k+1)Hk=4⋅116=223≈7.33(k + 1)H_k = 4 \cdot \frac{11}{6} = \frac{22}{3} \approx 7.33 requests; every deterministic algorithm misses at rate 14\frac{1}{4}; the ratio is 1/43/22=116=H3\frac{1/4}{3/22} = \frac{11}{6} = H_3.

Solution

After a miss, OPT's next miss comes when the last of the 3 pages other than the current one has been requested again: waits of 43,42,41\frac{4}{3}, \frac{4}{2}, \frac{4}{1} for the three of them, 223\frac{22}{3} in total (The Harmonic Lower Bound). OPT's long-run miss rate is 322\frac{3}{22}, and 14/322=2212=116\frac{1}{4} \big/ \frac{3}{22} = \frac{22}{12} = \frac{11}{6}.

Exercise 11: One step of move-to-front

MTF's list is c a d b e and a competitor's list is a b c d e. Item d is requested. Compute MTF's cost kk, the competitor's position ii, the change of the inversion potential, and check the amortized bound k+ΔΦ≤2i−1k + \Delta\Phi \le 2i - 1.

Answer

k=3k = 3, i=4i = 4, ΔΦ=+2\Delta\Phi = +2, amortized cost 5≤75 \le 7.

Solution

In front of d in MTF's list are c and a, both also in front of d in the competitor's list, so v=0v = 0 and moving d to the front creates k−1−v=2k - 1 - v = 2 inversions. The amortized cost is 3+2=5≤2⋅4−13 + 2 = 5 \le 2 \cdot 4 - 1 (List Update).

python · try it yourself
def inversions(a, b):
    pos = {x: i for i, x in enumerate(b)}
    s = [pos[x] for x in a]
    return sum(s[i] > s[j] for i in range(len(s)) for j in range(i + 1, len(s)))

mtf, other = list("cadbe"), list("abcde")
before = inversions(mtf, other)
k, i = mtf.index("d") + 1, other.index("d") + 1
mtf.insert(0, mtf.pop(k - 1))
after = inversions(mtf, other)
print(f"k = {k}, i = {i}, Phi {before} -> {after}, amortized {k + after - before} <= {2 * i - 1}")
k = 3, i = 4, Phi 3 -> 5, amortized 5 <= 7

Exercise 12: One step of double coverage

Three servers on a line. DC's servers are at 0, 4 and 10; OPT's servers (after OPT has served the request) are at 3, 4 and 12. The request is at 3. Perform DC's move and compute the change of Φ=3M+D\Phi = 3M + D.

Answer

DC moves 0 and 4 by 1 each (to 1 and 3), cost 2. MM: 5→55 \to 5; DD: 20→1820 \to 18. ΔΦ=−2=−cost\Delta\Phi = -2 = -\text{cost}.

Solution

The request lies between 0 and 4, so δ=min⁡(3,1)=1\delta = \min(3, 1) = 1 (Double Coverage). The sorted matching pairs (0,3),(4,4),(10,12)(0, 3), (4, 4), (10, 12) before, cost 5, and (1,3),(3,4),(10,12)(1, 3), (3, 4), (10, 12) after, also 5. Pairwise distances: 4+10+6=204 + 10 + 6 = 20 before, 2+9+7=182 + 9 + 7 = 18 after. So ΔΦ=3⋅0−2=−2\Delta\Phi = 3 \cdot 0 - 2 = -2, exactly minus DC's cost, as the proof guarantees.

Exercise 13: RANKING on a small triangle

Offline vertices u1,u2,u3u_1, u_2, u_3; online vertices arrive in the order v1,v2,v3v_1, v_2, v_3 with v1∼{u1,u2,u3}v_1 \sim \{u_1, u_2, u_3\}, v2∼{u2,u3}v_2 \sim \{u_2, u_3\}, v3∼{u3}v_3 \sim \{u_3\}. Compute RANKING's expected matching size exactly by averaging over all 6 rankings, and compare with (1−1/e)⋅3(1 - 1/e) \cdot 3.

Answer

E[size]=136≈2.167\mathbb{E}[\text{size}] = \frac{13}{6} \approx 2.167, a fraction 1318≈0.722\frac{13}{18} \approx 0.722 of OPT =3= 3, above 1−1/e≈0.6321 - 1/e \approx 0.632.

Solution
python · try it yourself
from fractions import Fraction
from itertools import permutations

adj = [[0, 1, 2], [1, 2], [2]]
total = 0
for order in permutations(range(3)):
    rank = {u: r for r, u in enumerate(order)}
    free, size = {0, 1, 2}, 0
    for nbrs in adj:
        cands = [u for u in nbrs if u in free]
        if cands:
            free.remove(min(cands, key=lambda u: rank[u]))
            size += 1
    total += size
print("E[size] =", Fraction(total, 6), "=", round(total / 6 / 3, 4), "of OPT")
E[size] = 13/6 = 0.7222 of OPT

Losses happen when an earlier vertex takes u3u_3, the only neighbour of v3v_3; for example v1v_1 does so with probability 13\frac{1}{3}. Small instances do better than 1−1/e1 - 1/e; the bound is approached only as the triangle grows (RANKING).

Exercise 14: Primal-dual ski rental for B=2B = 2

Run SKI-PRIMAL-DUAL of the primal-dual lesson by hand for B=2B = 2. What is CC, what is xx after days 1 and 2, and what ratio does the method guarantee? Compare with the optimal randomized ratio for B=2B = 2.

Answer

C=(1.5)2−1=1.25C = (1.5)^2 - 1 = 1.25; x=0.4x = 0.4 after day 1 and x=1x = 1 after day 2; the guarantee is 1+1/C=1.81 + 1/C = 1.8, worse than the optimal 43\frac{4}{3} of Exercise 4.

Solution

Day 1: x=0⋅1.5+11.25⋅2=0.4x = 0 \cdot 1.5 + \frac{1}{1.25 \cdot 2} = 0.4. Day 2: x=0.4⋅1.5+0.4=1x = 0.4 \cdot 1.5 + 0.4 = 1. Each day raises the primal by 1+1C=1.81 + \frac{1}{C} = 1.8 and the dual by 1. The method's constant is tuned for large BB (where 1+1/C→ee−11 + 1/C \to \frac{e}{e-1}); for tiny BB the exact equalizing distribution is much better.

Test yourself

You want a lower bound on the competitive ratio of every randomized algorithm against an oblivious adversary. Which tool fits?

  1. An adaptive adversary that watches the coins
  2. Yao's principle: a distribution on inputs that is bad for every deterministic algorithm
  3. A potential function
  4. Resource augmentation

Which paging algorithms never suffer Belady's anomaly?

  1. FIFO
  2. Stack algorithms such as LRU and OPT, whose kk-slot cache is always contained in their (k+1)(k+1)-slot cache
  3. Every algorithm
  4. Only randomized ones

In the double-coverage potential, what is ΔΦ\Delta\Phi when DC moves two servers by δ\delta each towards a request between them?

  1. +2δ+2\delta
  2. At most −2δ-2\delta
  3. Exactly 0
  4. −kδ-k\delta