Algorithm Search⌘K

Proofs: Move-to-front heuristic

These are the human proofs of Move-to-front heuristic, at full length.

Move-to-front is 2-competitive, no online rule beats 2 - 2/(n+1), and the expected cost is below 2 OPT - 1 full

This proof shows: time.worst, competitive-ratio, lower-bound, time.expected.

The model is the list update model of the page. A list holds nn distinct items. To access the item at position ii costs ii. Right after an access, the accessed item may move closer to the front for free, one free exchange per position. Any swap of two neighbours at another time is a paid exchange of cost 1.

Time of one request. By the correctness proof, the loop of lines L2 and L3 runs p−1p - 1 times when the item is at position pp, and every other line runs at most once. Each line takes O(1)O(1) time on the word RAM. So a request takes Θ(p)\Theta(p) time, which is Θ(n)\Theta(n) for p=np = n and Θ(1)\Theta(1) for p=1p = 1. In the list update model the cost is exactly pp.

Part 1. The bound of Sleator and Tarjan

Statement. Let AA be any algorithm that serves a sequence σ\sigma of mm requests from the same start list as MTF. Let CAC_A be the sum of the access costs of AA, XAX_A the number of its paid exchanges and FAF_A the number of its free exchanges. Then CMTF(σ)≤2CA+XA−FA−mC_{\mathrm{MTF}}(\sigma) \le 2C_A + X_A - F_A - m.

Setup. Run MTF and AA side by side. Split the run into events: for each request, the access of both algorithms together with the move of MTF; then each free exchange of AA after that access; and each paid exchange of AA, at the time that AA makes it. Let Φ\Phi be the number of unordered pairs {y,z}\{y, z\} of items that are in a different order in the list of MTF and in the list of AA. Then Φ≥0\Phi \ge 0 always, and Φ=0\Phi = 0 at the start because the lists are equal. For an event, let its amortized cost be the cost that MTF pays in it plus the change of Φ\Phi. Summing over all events, the changes of Φ\Phi add up to Φend−0≥0\Phi_{\mathrm{end}} - 0 \ge 0, so

CMTF(σ)≤sum of the amortized costs of all events.C_{\mathrm{MTF}}(\sigma) \le \text{sum of the amortized costs of all events.}

Step 1: an access. Let the request be xx, at position kk in the list of MTF and at position ii in the list of AA. Let BB be the set of the k−1k - 1 items before xx in the list of MTF. Let vv be the number of items of BB that are after xx in the list of AA. The other k−1−vk - 1 - v items of BB are before xx in the list of AA, and there are only i−1i - 1 such items, so k−1−v≤i−1k - 1 - v \le i - 1. MTF moves xx to the front. This changes the relative order of xx and yy for each y∈By \in B and of no other pair. For the vv items yy of BB after xx in AA, the pair {x,y}\{x, y\} was an inversion and is not one now. For the k−1−vk - 1 - v other items of BB, the pair was not an inversion and is one now. MTF pays kk. So the amortized cost is

k+(k−1−v)−v=2(k−1−v)+1≤2(i−1)+1=2i−1.k + (k - 1 - v) - v = 2(k - 1 - v) + 1 \le 2(i - 1) + 1 = 2i - 1.

Step 2: a free exchange of AA. It moves the accessed item xx one place forward in the list of AA, past the item yy just before it. In the list of MTF, xx is first, since MTF moved it there in the same access. So before the exchange the pair {x,y}\{x, y\} is an inversion, and after it the pair is not. No other pair changes. MTF pays 0. The amortized cost is −1-1.

Step 3: a paid exchange of AA. It swaps two neighbours in the list of AA. Only the order of these two items changes, so Φ\Phi changes by +1+1 or −1-1. MTF pays 0. The amortized cost is at most 11.

Conclusion. Summing Step 1 over the mm requests gives at most ∑t(2it−1)=2CA−m\sum_t (2i_t - 1) = 2C_A - m. Step 2 adds −FA-F_A and Step 3 adds at most XAX_A. Therefore CMTF(σ)≤2CA+XA−FA−mC_{\mathrm{MTF}}(\sigma) \le 2C_A + X_A - F_A - m. Since FA≥0F_A \ge 0, this also gives CMTF(σ)≤2(CA+XA)−mC_{\mathrm{MTF}}(\sigma) \le 2(C_A + X_A) - m, which is less than twice the total cost of AA. ■\blacksquare

Part 2. A lower bound for every deterministic online algorithm

Statement. Call an online algorithm α\alpha-competitive if there is a number bb, which may depend on nn but not on σ\sigma, with costON(σ)≤α⋅OPT(σ)+b\mathrm{cost}_{\mathrm{ON}}(\sigma) \le \alpha \cdot \mathrm{OPT}(\sigma) + b for every σ\sigma. If a deterministic online algorithm ON is α\alpha-competitive on lists of nn items, then α≥2n/(n+1)\alpha \ge 2n/(n + 1).

Proof. Fix m≥1m \ge 1. Because ON is deterministic, we can simulate it and build σ\sigma one request at a time: each request is the item that is last in the list of ON at that moment. Each access costs ON exactly nn, so costON(σ)≥mn\mathrm{cost}_{\mathrm{ON}}(\sigma) \ge mn.

For each of the n!n! orders π\pi of the items, consider the offline algorithm that first sorts the start list into the order π\pi by swaps of neighbours (bubble sort uses at most n(n−1)/2n(n-1)/2 swaps) and then never changes the list. Its cost is at most n(n−1)/2+∑t=1mposπ(xt)n(n-1)/2 + \sum_{t=1}^{m} \mathrm{pos}_\pi(x_t). For a fixed item, its position in a uniformly random order π\pi is uniform on {1,…,n}\{1, \ldots, n\} and has mean (n+1)/2(n + 1)/2. By linearity of expectation, the mean of ∑tposπ(xt)\sum_t \mathrm{pos}_\pi(x_t) over all π\pi is m(n+1)/2m(n + 1)/2, so some π\pi has a sum at most this mean. Hence OPT(σ)≤m(n+1)/2+n(n−1)/2\mathrm{OPT}(\sigma) \le m(n + 1)/2 + n(n - 1)/2.

Then mn≤α (m(n+1)/2+n(n−1)/2)+bmn \le \alpha\,(m(n + 1)/2 + n(n - 1)/2) + b for every mm. Divide by mm and let m→∞m \to \infty: n≤α(n+1)/2n \le \alpha (n + 1)/2, so α≥2n/(n+1)=2−2/(n+1)\alpha \ge 2n/(n + 1) = 2 - 2/(n + 1). ■\blacksquare

Part 3. Expected cost under independent requests

Statement. Let the requests be independent, and let each be item jj with probability pj>0p_j > 0, where ∑jpj=1\sum_j p_j = 1. Number the items so that p1≥p2≥⋯≥pnp_1 \ge p_2 \ge \cdots \ge p_n. Let EtE_t be the expected cost of the tt-th request of MTF. Then Et→EMTF=1+2∑j<kpjpk/(pj+pk)E_t \to E_{\mathrm{MTF}} = 1 + 2\sum_{j<k} p_j p_k/(p_j + p_k) as t→∞t \to \infty, and EMTF≤2EOPT−1E_{\mathrm{MTF}} \le 2E_{\mathrm{OPT}} - 1, where EOPT=∑kk pkE_{\mathrm{OPT}} = \sum_k k\, p_k is the least expected cost of a fixed order.

Step 1: the order of two items. Fix two items y≠zy \ne z and let q=py+pzq = p_y + p_z. By the correctness of MTF-ACCESS, just before request tt, the item yy is before zz exactly when the last of the requests 1,…,t−11, \ldots, t - 1 that is for yy or zz is for yy, or when no such request exists and yy is before zz in the start list. The last such request is request ss with probability q(1−q)t−1−sq(1 - q)^{t - 1 - s}, and given that request ss is for yy or zz, it is for yy with probability py/qp_y/q, by independence. So

Pr⁡[y before z]=∑s=1t−1(1−q)t−1−s py+δ (1−q)t−1=pyq(1−(1−q)t−1)+δ (1−q)t−1,\Pr[y \text{ before } z] = \sum_{s=1}^{t-1} (1 - q)^{t-1-s}\, p_y + \delta\,(1 - q)^{t - 1} = \frac{p_y}{q}\left(1 - (1 - q)^{t-1}\right) + \delta\,(1 - q)^{t-1},

with δ∈{0,1}\delta \in \{0, 1\}. Since 0<q≤10 < q \le 1, this tends to py/(py+pz)p_y/(p_y + p_z).

Step 2: the cost. The cost of a request for zz is 1 plus the number of items before zz. Request tt is independent of the list before it, so by linearity

Et=∑zpz(1+∑y≠zPr⁡[y before z])→1+∑z∑y≠zpz pypy+pz=1+2∑j<kpj pkpj+pk.E_t = \sum_z p_z \Big(1 + \sum_{y \ne z} \Pr[y \text{ before } z]\Big) \to 1 + \sum_z \sum_{y \ne z} \frac{p_z\, p_y}{p_y + p_z} = 1 + 2 \sum_{j < k} \frac{p_j\, p_k}{p_j + p_k}.

Step 3: the comparison. For j<kj < k, pj≥pkp_j \ge p_k, so pj/(pj+pk)≤1p_j/(p_j + p_k) \le 1 and pjpk/(pj+pk)≤pkp_j p_k/(p_j + p_k) \le p_k. Hence

EMTF≤1+2∑j<kpk=1+2∑k(k−1) pk=1+2(EOPT−1)=2EOPT−1.E_{\mathrm{MTF}} \le 1 + 2\sum_{j<k} p_k = 1 + 2 \sum_k (k - 1)\, p_k = 1 + 2(E_{\mathrm{OPT}} - 1) = 2E_{\mathrm{OPT}} - 1.

Step 4: the best fixed order. A fixed order that puts item jj at position rjr_j has expected cost ∑jrjpj\sum_j r_j p_j. If some items a,ba, b have ra<rbr_a < r_b and pa<pbp_a < p_b, swapping their positions changes the cost by (rb−ra)(pa−pb)<0(r_b - r_a)(p_a - p_b) < 0. So a least-cost order has the pp values in non-increasing order, and its cost is ∑kk pk=EOPT\sum_k k\,p_k = E_{\mathrm{OPT}}; ties between equal pp values do not change the cost. ■\blacksquare

MTF-ACCESS returns the position of the item and moves it to the front full

This proof shows: correctness, termination.

Statement. Let LL be a singly linked list of n≥1n \ge 1 nodes with distinct keys y1,y2,…,yny_1, y_2, \ldots, y_n in this order, and let x=ypx = y_p for some pp with 1≤p≤n1 \le p \le n. Then MTF-ACCESS(L,x)(L, x) stops, returns pp, and after the call LL holds the keys yp,y1,…,yp−1,yp+1,…,yny_p, y_1, \ldots, y_{p-1}, y_{p+1}, \ldots, y_n in this order. No node is created or freed.

Setup. Write N1,…,NnN_1, \ldots, N_n for the nodes in list order at the start, so Nt.key=ytN_t.key = y_t, Nt.next=Nt+1N_t.next = N_{t+1} for t<nt < n, Nn.next=nilN_n.next = nil, and L.head=N1L.head = N_1. Lines L1 to L3 do not write any pointer field, so these facts hold during the loop.

Invariant of line L2. Each time line L2 tests its condition:

(S) 1≤i≤p1 \le i \le p, cur=Nicur = N_i, prev=Ni−1prev = N_{i-1} when i≥2i \ge 2 and prev=nilprev = nil when i=1i = 1, and yt≠xy_t \ne x for each tt with 1≤t<i1 \le t < i.

Initialization. Line L1 sets i=1i = 1, cur=L.head=N1cur = L.head = N_1 and prev=nilprev = nil. The last part of (S) is about an empty range of tt. Since p≥1p \ge 1, (S) holds.

Maintenance. Assume (S) and that the test succeeds, so cur.key=yi≠xcur.key = y_i \ne x. Since the keys are distinct and yp=xy_p = x, we have i≠pi \ne p, so i<p≤ni < p \le n and Ni.next=Ni+1N_i.next = N_{i+1} exists. Line L3 sets prev=Niprev = N_i, cur=Ni+1cur = N_{i+1} and increases ii by 1. With the new value of ii, 1≤i≤p1 \le i \le p, cur=Nicur = N_i, prev=Ni−1prev = N_{i-1}, and yt≠xy_t \ne x for all t<it < i, because this was known for tt below the old ii and was just shown for the old ii. So (S) holds again.

Termination. The quantity p−ip - i is a nonnegative integer by (S), and each run of line L3 lowers it by exactly 1. So the loop runs at most p−1p - 1 times. When it stops, cur.key=xcur.key = x, so yi=xy_i = x and, the keys being distinct, i=pi = p. Then cur=Npcur = N_p, and prev=Np−1prev = N_{p-1} or nilnil when p=1p = 1.

The move. If p=1p = 1, then prev=nilprev = nil, lines L5 to L7 do not run, and LL is unchanged, which is the claimed order y1,…,yny_1, \ldots, y_n. If p≥2p \ge 2, then prev=Np−1prev = N_{p-1} and:

  1. Line L5 sets Np−1.next=Np.nextN_{p-1}.next = N_p.next, which is Np+1N_{p+1}, or nilnil when p=np = n. Now the chain from L.head=N1L.head = N_1 is N1,…,Np−1,Np+1,…,NnN_1, \ldots, N_{p-1}, N_{p+1}, \ldots, N_n, and NpN_p is outside it.
  2. Line L6 sets Np.next=L.head=N1N_p.next = L.head = N_1. The chain from NpN_p is Np,N1,…,Np−1,Np+1,…,NnN_p, N_1, \ldots, N_{p-1}, N_{p+1}, \ldots, N_n. Line L6 reads L.headL.head before line L7 changes it.
  3. Line L7 sets L.head=NpL.head = N_p.

So LL holds yp,y1,…,yp−1,yp+1,…,yny_p, y_1, \ldots, y_{p-1}, y_{p+1}, \ldots, y_n, the chain ends in nilnil after NnN_n or Nn−1N_{n-1} (no cycle, since each node appears once), and the nodes are the same nn nodes. Line L8 returns i=pi = p. ■\blacksquare

Back to Move-to-front heuristic