TExcelTAD
Home › Online Algorithms & Competitive Analysis › The Lower Bound k for Deterministic Paging

The Lower Bound k for Deterministic Paging

LRU and FIFO Are k-Competitive gave an upper bound of kk for a large class of algorithms. This lesson proves that no deterministic online algorithm does better, with care about the details that the quick version in Part I skipped: why the bound is non-strict, why it needs only k+1k + 1 pages, why giving the algorithm a glimpse of the future (lookahead) does not help, and what the bound says about real caches. Throughout, ratios are for deterministic algorithms, so all adversaries coincide (Oblivious and Adaptive Adversaries).

The theorem

Theorem (Sleator and Tarjan, 1985). Let ALG be any deterministic online paging algorithm with a cache of kk pages, and let the universe have at least k+1k + 1 pages. For every nn there is a request sequence σ\sigma of length nn with

ALG(σ)=nandOPT(σ)≤nk+k.\mathrm{ALG}(\sigma) = n \qquad \text{and} \qquad \mathrm{OPT}(\sigma) \le \frac{n}{k} + k .

Hence ALG is not cc-competitive for any c<kc < k, even in the non-strict sense.

Proof

Use only pages 1,…,k+11, \ldots, k + 1 and assume both caches start full with the same kk pages (an empty start only adds kk compulsory misses). The cruel adversary simulates ALG and always requests the unique page of {1,…,k+1}\{1, \ldots, k + 1\} that is not in ALG's cache. Every request is a miss for ALG, so ALG(σ)=n\mathrm{ALG}(\sigma) = n.

Now bound OPT by the cost of Belady's rule (LFD) on the same σ\sigma. When LFD misses at time tt, it evicts the cached page whose next request is furthest away. The universe has k+1k + 1 pages, so at time tt the cache holds all of them except the evicted one, say qq. Each of the k−1k - 1 cached pages other than qq and the requested page is needed again before qq, since qq was the furthest. The next miss of LFD is the next request to qq, so between two consecutive misses there are at least k−1k - 1 hits, and LFD misses at most once every kk requests: OPT(σ)≤⌈n/k⌉\mathrm{OPT}(\sigma) \le \lceil n/k \rceil plus the start-up misses.

Since OPT(σ)→∞\mathrm{OPT}(\sigma) \to \infty as n→∞n \to \infty, an inequality n≤c (n/k+k)+αn \le c\,(n/k + k) + \alpha for all nn forces c≥kc \ge k: the bound holds for the non-strict ratio.

Three remarks on the proof.

  • The adversary needs the algorithm, not just its class. It computes ALG's cache at every step; that is why the bound is for deterministic algorithms. Against a randomized algorithm, the adversary does not know which page is missing, and the bound drops to HkH_k (The Harmonic Lower Bound).
  • k+1k + 1 pages suffice. A larger universe does not help the adversary, and with only kk pages every algorithm is trivially optimal.
  • The upper and lower bounds match exactly. LRU's k⋅OPT+kk \cdot \mathrm{OPT} + k and the adversary's nn against n/k+kn/k + k leave no gap in the leading constant.

The adversary at work

The program runs the cruel adversary against five deterministic algorithms, including CLOCK (second chance, the approximation of LRU used by operating systems) and a rule that evicts a page chosen by a fixed hash of the time step, which looks random but is deterministic. None of them gets below kk.

python · try it yourself
def make_algorithms(k):
    def lru(state, cache, t):
        return min(cache, key=lambda p: state["last"][p])
    def fifo(state, cache, t):
        return min(cache, key=lambda p: state["loaded"][p])
    def mru_like(state, cache, t):                     # most recently loaded: terrible
        return max(cache, key=lambda p: state["loaded"][p])
    def hashed(state, cache, t):                       # deterministic, but looks arbitrary
        return sorted(cache)[(t * 2654435761) % 2 ** 32 % len(cache)]
    def clock(state, cache, t):
        hand, ring, ref = state["hand"], state["ring"], state["ref"]
        while ref[ring[hand]]:
            ref[ring[hand]] = False
            hand = (hand + 1) % k
        state["hand"] = hand
        return ring[hand]
    return {"LRU": lru, "FIFO": fifo, "CLOCK": clock, "hashed": hashed, "MR-loaded": mru_like}

def cruel_adversary(evict, k, n):
    cache = list(range(k))
    state = {"last": {p: p for p in cache}, "loaded": {p: p for p in cache},
             "hand": 0, "ring": list(range(k)), "ref": {p: True for p in range(k + 1)}}
    reqs = []
    for t in range(k, k + n):
        page = next(p for p in range(k + 1) if p not in cache)     # the missing page
        reqs.append(page)
        victim = evict(state, cache, t)
        slot = cache.index(victim)
        cache[slot] = page
        state["ring"][state["ring"].index(victim)] = page
        state["ref"][page] = True
        state["last"][page] = state["loaded"][page] = t
    return reqs

def opt_misses(reqs, k, start):
    n = len(reqs)
    nxt, seen = [0] * n, {}
    for t in range(n - 1, -1, -1):
        nxt[t] = seen.get(reqs[t], n + reqs[t])
        seen[reqs[t]] = t
    first = {p: seen.get(p, n + p) for p in start}           # next use of the start pages
    cache, misses = dict(first), 0
    for t, p in enumerate(reqs):
        if p not in cache:
            misses += 1
            del cache[max(cache, key=cache.get)]
        cache[p] = nxt[t]
    return misses

k = 5
print(f"k = {k}: every request misses for ALG; ratio = n / OPT")
print("algorithm      n=100   n=1000   n=10000")
for name, rule in make_algorithms(k).items():
    row = []
    for n in [100, 1000, 10000]:
        reqs = cruel_adversary(rule, k, n)
        row.append(n / opt_misses(reqs, k, range(k)))
    print(f"{name:10}  " + "  ".join(f"{x:7.3f}" for x in row))
k = 5: every request misses for ALG; ratio = n / OPT
algorithm      n=100   n=1000   n=10000
LRU           5.000    5.000    5.000
FIFO          5.000    5.000    5.000
CLOCK         5.000    5.000    5.000
hashed        8.333    9.009    9.074
MR-loaded   100.000  1000.000  10000.000

LRU, FIFO and CLOCK sit at exactly k=5k = 5, as the matching upper bound promises for marking and conservative algorithms. The other two rules are worse: the hashed rule settles near 9, and against the most-recently-loaded rule the adversary alternates between two pages that OPT simply keeps, so its ratio grows without bound. The sequence is different for each algorithm, which is the point: the adversary is tailored to its victim, and the theorem says only that no victim escapes below kk.

In C++

The cruel adversary against CLOCK, written as a standalone simulation: CLOCK keeps a reference bit per frame and a hand that sweeps the frames, clearing bits until it finds a frame whose bit is 0. Belady's rule on the generated sequence gives OPT.

cpp · try it yourself
#include <cstdio>
#include <set>
#include <unordered_map>
#include <vector>

int belady(const std::vector<int>& r, int k, const std::vector<int>& start) {
    int n = r.size(), misses = 0;
    std::vector<int> nxt(n);
    std::unordered_map<int, int> seen;
    for (int t = n - 1; t >= 0; --t) { nxt[t] = seen.count(r[t]) ? seen[r[t]] : n + r[t]; seen[r[t]] = t; }
    std::set<std::pair<int, int>> s;
    std::unordered_map<int, int> key;
    for (int p : start) { int nu = seen.count(p) ? seen[p] : n + p; key[p] = nu; s.insert({nu, p}); }
    for (int t = 0; t < n; ++t) {
        if (key.count(r[t])) s.erase({key[r[t]], r[t]});
        else { ++misses; auto f = std::prev(s.end()); key.erase(f->second); s.erase(f); }
        key[r[t]] = nxt[t];
        s.insert({nxt[t], r[t]});
    }
    return misses;
}

int main() {
    for (int k : {2, 4, 8, 16}) {
        std::vector<int> frame(k), start(k);
        std::vector<bool> ref(k, true);
        for (int i = 0; i < k; ++i) frame[i] = start[i] = i;
        int hand = 0, n = 20000;
        std::vector<int> reqs;
        for (int t = 0; t < n; ++t) {
            std::vector<bool> cached(k + 1, false);
            for (int p : frame) cached[p] = true;
            int page = 0;
            while (cached[page]) ++page;                           // the page CLOCK lacks
            reqs.push_back(page);
            while (ref[hand]) { ref[hand] = false; hand = (hand + 1) % k; }
            frame[hand] = page;                                     // evict and load
            ref[hand] = true;
            hand = (hand + 1) % k;
        }
        int opt = belady(reqs, k, start);
        std::printf("k = %2d: CLOCK %d misses, OPT %d, ratio %.3f\n", k, n, opt, double(n) / opt);
    }
}
k =  2: CLOCK 20000 misses, OPT 10000, ratio 2.000
k =  4: CLOCK 20000 misses, OPT 5000, ratio 4.000
k =  8: CLOCK 20000 misses, OPT 2500, ratio 8.000
k = 16: CLOCK 20000 misses, OPT 1250, ratio 16.000

Lookahead does not help

Suppose the algorithm may see the next ℓ\ell requests before serving the current one. The adversary defeats this by repeating every request ℓ\ell times in a row: the repetitions are hits for everybody, and the lookahead window shows only copies of the current page, so the algorithm learns nothing it can use. The ratio still tends to kk (Borodin and El-Yaniv discuss this; Albers, 1997, studies a "strong lookahead" model in which the window must contain ℓ\ell distinct pages, and there lookahead does help).

python · try it yourself
def lookahead_misses(reqs, k, ell):
    """Evict a cached page not seen in the next ell requests (LRU among them), else LRU."""
    cache, last, misses = [], {}, 0
    for t, p in enumerate(reqs):
        if p not in cache:
            misses += 1
            if len(cache) == k:
                window = set(reqs[t + 1:t + 1 + ell])
                unseen = [q for q in cache if q not in window] or cache
                cache.remove(min(unseen, key=lambda q: last[q]))
            cache.append(p)
        last[p] = t
    return misses

def adversary_with_repeats(k, ell, blocks):
    """Requests the page missing from the algorithm's cache, each one ell + 1 times in a row."""
    reqs = []
    for _ in range(blocks):
        cache = simulate_cache(reqs, k, ell)
        page = next(p for p in range(k + 1) if p not in cache)
        reqs += [page] * (ell + 1)
    return reqs

def simulate_cache(reqs, k, ell):
    cache, last = [], {}
    for t, p in enumerate(reqs):
        if p not in cache:
            if len(cache) == k:
                window = set(reqs[t + 1:t + 1 + ell])
                unseen = [q for q in cache if q not in window] or cache
                cache.remove(min(unseen, key=lambda q: last[q]))
            cache.append(p)
        last[p] = t
    return cache

def opt_misses(reqs, k):
    cache, misses = set(), 0
    for t, p in enumerate(reqs):
        if p not in cache:
            misses += 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 misses

k = 4
for ell in [0, 3, 10]:
    reqs = adversary_with_repeats(k, ell, 160)
    alg, opt = lookahead_misses(reqs, k, ell), opt_misses(reqs, k)
    print(f"lookahead {ell:2}: {len(reqs):4} requests, ALG {alg}, OPT {opt}, ratio {alg / opt:.2f}")
lookahead  0:  160 requests, ALG 160, OPT 43, ratio 3.72
lookahead  3:  640 requests, ALG 160, OPT 43, ratio 3.72
lookahead 10: 1760 requests, ALG 160, OPT 43, ratio 3.72

With 160 distinct blocks the ratio is 3.723.72 for every lookahead, on its way to k=4k = 4 as the number of blocks grows. A subtlety: the adversary must know the algorithm's eviction before choosing the next distinct page, and the lookahead algorithm's eviction depends on that future page. With repeats of length ℓ+1\ell + 1, the window at a miss shows only copies of the current page, so the eviction is fixed before the adversary moves on, and the construction is consistent.

What the bound says about real caches

The factor kk is a statement about adversarial sequences. Real request streams have locality of reference, and the competitive ratio hides that LRU usually beats FIFO in practice even though both have ratio exactly kk. Several refined models capture locality:

  • Access graphs (Borodin, Irani, Raghavan and Schieber, 1995): requests must follow the edges of a known graph, as a program's page references follow its control flow. The best ratio then depends on the graph, and on a path or a tree LRU is much better than kk.
  • Diffuse adversaries (Koutsoupias and Papadimitriou, 2000): the adversary chooses a distribution from a restricted class rather than a single sequence.
  • Working-set models (Albers, Favrholdt and Giel, 2005): sequences are characterized by how many distinct pages appear in windows of each length, and LRU is optimal among deterministic algorithms in this model.

Test yourself

How many distinct pages does the cruel adversary need against a cache of size kk?

  1. kk
  2. k+1k + 1
  3. 2k2k
  4. Unboundedly many

Why is the deterministic lower bound kk a bound on the non-strict ratio?

  1. Because the sequences are short
  2. Because OPT≤n/k+k\mathrm{OPT} \le n/k + k grows without bound with nn, so no additive constant can absorb the gap
  3. Because OPT is zero
  4. Because LRU is strictly kk-competitive

In the proof, why does Belady's rule miss at most once every kk requests on the adversary's sequence?

  1. Because the requests are random
  2. Because the evicted page is requested only after all k−1k - 1 other cached pages, and with k+1k + 1 pages only that page can cause the next miss
  3. Because Belady never misses
  4. Because the adversary repeats pages

How does the adversary neutralize a lookahead of ℓ\ell requests?

  1. It cannot; lookahead always helps
  2. It repeats every request ℓ+1\ell + 1 times, so the window shows only the current page
  3. It requests ℓ\ell new pages
  4. It uses k+ℓk + \ell pages