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 distinct items. To access the item at position costs . 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 times when the item is at position , and every other line runs at most once. Each line takes time on the word RAM. So a request takes time, which is for and for . In the list update model the cost is exactly .
Part 1. The bound of Sleator and Tarjan
Statement. Let be any algorithm that serves a sequence of requests from the same start list as MTF. Let be the sum of the access costs of , the number of its paid exchanges and the number of its free exchanges. Then .
Setup. Run MTF and 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 after that access; and each paid exchange of , at the time that makes it. Let be the number of unordered pairs of items that are in a different order in the list of MTF and in the list of . Then always, and 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 . Summing over all events, the changes of add up to , so
Step 1: an access. Let the request be , at position in the list of MTF and at position in the list of . Let be the set of the items before in the list of MTF. Let be the number of items of that are after in the list of . The other items of are before in the list of , and there are only such items, so . MTF moves to the front. This changes the relative order of and for each and of no other pair. For the items of after in , the pair was an inversion and is not one now. For the other items of , the pair was not an inversion and is one now. MTF pays . So the amortized cost is
Step 2: a free exchange of . It moves the accessed item one place forward in the list of , past the item just before it. In the list of MTF, is first, since MTF moved it there in the same access. So before the exchange the pair is an inversion, and after it the pair is not. No other pair changes. MTF pays 0. The amortized cost is .
Step 3: a paid exchange of . It swaps two neighbours in the list of . Only the order of these two items changes, so changes by or . MTF pays 0. The amortized cost is at most .
Conclusion. Summing Step 1 over the requests gives at most . Step 2 adds and Step 3 adds at most . Therefore . Since , this also gives , which is less than twice the total cost of .
Part 2. A lower bound for every deterministic online algorithm
Statement. Call an online algorithm -competitive if there is a number , which may depend on but not on , with for every . If a deterministic online algorithm ON is -competitive on lists of items, then .
Proof. Fix . Because ON is deterministic, we can simulate it and build 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 , so .
For each of the orders of the items, consider the offline algorithm that first sorts the start list into the order by swaps of neighbours (bubble sort uses at most swaps) and then never changes the list. Its cost is at most . For a fixed item, its position in a uniformly random order is uniform on and has mean . By linearity of expectation, the mean of over all is , so some has a sum at most this mean. Hence .
Then for every . Divide by and let : , so .
Part 3. Expected cost under independent requests
Statement. Let the requests be independent, and let each be item with probability , where . Number the items so that . Let be the expected cost of the -th request of MTF. Then as , and , where is the least expected cost of a fixed order.
Step 1: the order of two items. Fix two items and let . By the correctness of MTF-ACCESS, just before request , the item is before exactly when the last of the requests that is for or is for , or when no such request exists and is before in the start list. The last such request is request with probability , and given that request is for or , it is for with probability , by independence. So
with . Since , this tends to .
Step 2: the cost. The cost of a request for is 1 plus the number of items before . Request is independent of the list before it, so by linearity
Step 3: the comparison. For , , so and . Hence
Step 4: the best fixed order. A fixed order that puts item at position has expected cost . If some items have and , swapping their positions changes the cost by . So a least-cost order has the values in non-increasing order, and its cost is ; ties between equal values do not change the cost.
MTF-ACCESS returns the position of the item and moves it to the front full
This proof shows: correctness, termination.
Statement. Let be a singly linked list of nodes with distinct keys in this order, and let for some with . Then MTF-ACCESS stops, returns , and after the call holds the keys in this order. No node is created or freed.
Setup. Write for the nodes in list order at the start, so , for , , and . 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) , , when and when , and for each with .
Initialization. Line L1 sets , and . The last part of (S) is about an empty range of . Since , (S) holds.
Maintenance. Assume (S) and that the test succeeds, so . Since the keys are distinct and , we have , so and exists. Line L3 sets , and increases by 1. With the new value of , , , , and for all , because this was known for below the old and was just shown for the old . So (S) holds again.
Termination. The quantity is a nonnegative integer by (S), and each run of line L3 lowers it by exactly 1. So the loop runs at most times. When it stops, , so and, the keys being distinct, . Then , and or when .
The move. If , then , lines L5 to L7 do not run, and is unchanged, which is the claimed order . If , then and:
- Line L5 sets , which is , or when . Now the chain from is , and is outside it.
- Line L6 sets . The chain from is . Line L6 reads before line L7 changes it.
- Line L7 sets .
So holds , the chain ends in after or (no cycle, since each node appears once), and the nodes are the same nodes. Line L8 returns .