Prove2Me
Navigate
DiscoverFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Sleator–Tarjan: move-to-front is 222-competitive for list accessing

Open
ListUpdate.mtf_two_competitive

by Nickrobbins95 · Oct 5, 2026 · Mathlib 0df444a (Lean v4.33.1)

algorithmscompetitive-analysiscompetitive-ratioonline-algorithms

Move-to-front is 2-competitive for list accessing (Sleator–Tarjan, 1985).

The list accessing problem. A set of distinct items is stored in a linear list. A request names one of the items; serving it by accessing the item that is currently in position iii (positions counted from 111 at the front) costs iii. Immediately after an access, the accessed item may be moved to any position closer to the front at no cost; each position it advances counts as one free exchange. Any other swap of two adjacent items is a paid exchange and costs 111. For an algorithm AAA and a request sequence sss write

  1. CA(s)C_A(s)CA​(s) for the total cost of the accesses (paid exchanges not included),
  2. XA(s)X_A(s)XA​(s) for the number of paid exchanges, and
  3. FA(s)F_A(s)FA​(s) for the number of free exchanges.

The move-to-front rule MTF\mathrm{MTF}MTF serves each request by moving the accessed item to the front of the list, and makes no paid exchanges.

Theorem. Let LLL be a list of distinct items and let s=(x1,…,xm)s=(x_1,\dots,x_m)s=(x1​,…,xm​) be a sequence of mmm requests, each naming an item of LLL. Let AAA be any algorithm, online or offline, that serves sss starting from the list LLL, and let MTF\mathrm{MTF}MTF serve sss starting from the same list LLL. Then

CMTF(s)≤2 CA(s)+XA(s)−FA(s)−m.C_{\mathrm{MTF}}(s)\le 2\,C_A(s)+X_A(s)-F_A(s)-m .CMTF​(s)≤2CA​(s)+XA​(s)−FA​(s)−m.

Since the total cost of AAA is CA(s)+XA(s)C_A(s)+X_A(s)CA​(s)+XA​(s) and FA(s)≥0F_A(s)\ge 0FA​(s)≥0, it follows that CMTF(s)≤2 (CA(s)+XA(s))−mC_{\mathrm{MTF}}(s)\le 2\,(C_A(s)+X_A(s))-mCMTF​(s)≤2(CA​(s)+XA​(s))−m: move-to-front never pays more than twice the cost of any algorithm, even one that knows the whole request sequence in advance. This is the result that founded the competitive analysis of online algorithms.

Formalization Note The inequality is stated with the subtracted terms moved to the left, CMTF+FA+m≤2CA+XAC_{\mathrm{MTF}}+F_A+m\le 2C_A+X_ACMTF​+FA​+m≤2CA​+XA​, to avoid truncated subtraction in N\mathbb NN. Lists are Lean List α with L.Nodup, and the requests are req : Fin m → α with every req t ∈ L. Positions in Lean are 000-based (List.idxOf), so an access costs idxOf + 1. The run of MTF\mathrm{MTF}MTF is the sequence of lists M0=LM_0=LM0​=L, Mt+1=xtM_{t+1}=x_tMt+1​=xt​ followed by MtM_tMt​ with xtx_txt​ erased. Algorithm AAA is described, for each request ttt, by the list paid t of the paid exchanges it performs before that access (an entry ppp swaps the items at 000-based positions ppp and p+1p+1p+1; an entry with p+1≥∣L∣p+1\ge |L|p+1≥∣L∣ swaps nothing but is still charged) and by the 000-based position dest t to which it then moves the accessed item, which must be at most the item's current position; BtB_tBt​ is AAA's list at the moment of the ttt-th access and At+1A_{t+1}At+1​ its list after the free move. Paid exchanges made after the last access only increase XAX_AXA​, so performing all paid exchanges before accesses loses no generality. Sleator and Tarjan state Theorem 1 for sequences of accesses, insertions and deletions starting from the empty list; this is the static, access-only version in which both algorithms start from the same list, the setting of Borodin and El-Yaniv, Chapter 1.

Preamble
import Mathlib
Formal statement
namespace ListUpdate

/-- **Sleator–Tarjan (1985), Theorem 1** (static list accessing, both algorithms starting
from the same list). Move-to-front versus an arbitrary (possibly offline) algorithm `A`:
`C_MTF(s) + F_A(s) + m ≤ 2 C_A(s) + X_A(s)`, i.e. `C_MTF ≤ 2 C_A + X_A - F_A - m`.

* `L` is the common initial list of distinct items, `req t` the `t`-th of `m` requests.
* `M t` is move-to-front's list before request `t`.
* `A t` is algorithm `A`'s list before request `t`; `A` first performs the paid exchanges
  `paid t` (an entry `p` swaps the items at 0-based positions `p` and `p + 1`), giving the
  list `B t` on which the access is made, and then moves the accessed item forward to
  the 0-based position `dest t` by free exchanges.
* Accessing the item at 0-based index `i` costs `i + 1`. -/
theorem mtf_two_competitive {α : Type*} [DecidableEq α]
    (L : List α) (hL : L.Nodup) (m : ℕ) (req : Fin m → α) (hreq : ∀ t, req t ∈ L)
    (M : Fin (m + 1) → List α) (hM0 : M 0 = L)
    (hM : ∀ t : Fin m, M t.succ = req t :: (M t.castSucc).erase (req t))
    (paid : Fin m → List ℕ) (dest : Fin m → ℕ)
    (A : Fin (m + 1) → List α) (B : Fin m → List α) (hA0 : A 0 = L)
    (hB : ∀ t : Fin m, B t = (paid t).foldl
      (fun (l : List α) (p : ℕ) => l.take p ++ ((l.drop p).take 2).reverse ++ l.drop (p + 2))
      (A t.castSucc))
    (hdest : ∀ t : Fin m, dest t ≤ (B t).idxOf (req t))
    (hA : ∀ t : Fin m, A t.succ = ((B t).erase (req t)).insertIdx (dest t) (req t)) :
    (∑ t, ((M t.castSucc).idxOf (req t) + 1)) + (∑ t, ((B t).idxOf (req t) - dest t)) + m ≤
      2 * (∑ t, ((B t).idxOf (req t) + 1)) + ∑ t, (paid t).length := by
  sorry

end ListUpdate
Source
D. D. Sleator and R. E. Tarjan, Amortized efficiency of list update and paging rules, Communications of the ACM 28(2) (1985) 202–208, Theorem 1 (C_MF(s) ≤ 2C_A(s) + X_A(s) − F_A(s) − m), here in its static access-only form with a common initial list; see also A. Borodin and R. El-Yaniv, Online Computation and Competitive Analysis, Cambridge University Press 1998, Chapter 1, Theorem 1.1.

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me