Documentation

CslibFrontier.DataStructure.ListUpdate.Basic

The list update problem: arrangements, inversions and the cost model #

This file instantiates Cslib.DataStructure.Online.Problem for the static list update (list accessing) problem of Sleator and Tarjan, Amortized Efficiency of List Update and Paging Rules, Section 2; see also Borodin and El-Yaniv, Online Computation and Competitive Analysis, Chapter 1.

Over a finite universe α, a configuration is an arrangement: a duplicate-free list containing every element of α (IsArrangement). Serving a request r by moving from arrangement s to arrangement s' costs stepCost s r s' = inversions s s' + (s'.idxOf r + 1), where inversions s s' counts the pairs of items in different relative order in s and s', and s'.idxOf r + 1 is the 1-based position of r in s'.

Relation to the Sleator–Tarjan cost model #

In [ST85a], accessing the ith item costs i. Immediately after the access, the accessed item may be moved forward at no cost ("free exchanges"), and any other exchange of adjacent items costs 1 ("paid exchanges"). stepCost s r s' is the cheapest way to go from s to s' while serving r in that model. Doing the paid exchanges first and then accessing r costs exactly stepCost s r s'. Conversely, any implementation that pays for X exchanges, accesses r at position i and then moves it forward F places satisfies X + i ≥ stepCost s r s': each adjacent exchange changes the inversion count by one, and moving r forward F places creates F inversions. Paid exchanges made after an access can be counted as made before the next access (and dropped after the last one), so this form of a step is no restriction. Every online strategy and every offline schedule therefore costs at least as much in the Sleator–Tarjan model as in this one, and move-to-front costs the same in both (moveToFront, in CslibFrontier.DataStructure.ListUpdate.MoveToFront). This justification is informal and is not formalized here.

Main definitions #

Main results #

References #

An arrangement of the universe α is a duplicate-free list containing every element.

The enumeration Finset.univ.toList of a finite type is an arrangement.

An arrangement lists each element of α exactly once.

The reversal of an arrangement is an arrangement.

Positions in an arrangement are injective.

theorem Cslib.DataStructure.ListUpdate.idxOf_reverse_add_idxOf {α : Type} [DecidableEq α] {l : List α} (hl : l.Nodup) {x : α} (hx : x ∈ l) :

Positions in a list and its reversal add up to the length minus one.

theorem Cslib.DataStructure.ListUpdate.card_filter_idxOf_lt {α : Type} [DecidableEq α] [Fintype α] {l : List α} (hl : IsArrangement l) (r : α) :
{x : α | List.idxOf x l < List.idxOf r l}.card = List.idxOf r l

In an arrangement, exactly l.idxOf r items precede r.

Inversions #

Ordered pairs (x, y) with x before y in l but y before x in l'.

The number of pairs of items in different relative order in l and l' (the Kendall tau distance between two arrangements).

theorem Cslib.DataStructure.ListUpdate.inversions_triangle {α : Type} [DecidableEq α] [Fintype α] (l₁ : List α) {l₂ : List α} (h₂ : IsArrangement l₂) (l₃ : List α) :
inversions l₁ l₃ ≤ inversions l₁ l₂ + inversions l₂ l₃

Triangle inequality for inversions through an intermediate arrangement.

Any two lists have at most n (n - 1) / 2 inversions, where n = Fintype.card α.

def Cslib.DataStructure.ListUpdate.crossBefore {α : Type} [DecidableEq α] [Fintype α] (l l' : List α) (r : α) :

Items that precede r in l but follow it in l'.

theorem Cslib.DataStructure.ListUpdate.card_filter_ne_add_card_crossBefore_le {α : Type} [DecidableEq α] [Fintype α] (l l' : List α) (r : α) :
{p ∈ inversionSet l l' | p.1 ≠ r ∧ p.2 ≠ r}.card + (crossBefore l l' r).card ≤ inversions l l'

The inversions avoiding r and the inversions (x, r) are disjoint parts of all inversions.

Items in front of r in l are either in front of r in l' or in crossBefore l l' r.

If r sits at position l.idxOf r in l, then moving to l' either creates inversions or keeps the items in front of r: l.idxOf r ≤ inversions l l' + l'.idxOf r.

The list-update problem #

def Cslib.DataStructure.ListUpdate.stepCost {α : Type} [DecidableEq α] [Fintype α] (s : List α) (r : α) (s' : List α) :

Cost of serving request r by moving from arrangement s to arrangement s': the inversions between s and s' (paid exchanges) plus the 1-based position of r in s' (access).

@[reducible, inline]

The list-update Online.Problem over a finite universe α: configurations are arrangements, every arrangement is reachable in one step, and stepCost charges rearrangement plus access.

Total access cost ∑ᵢ pos(rᵢ, sᵢ) of a schedule.

Total rearrangement cost ∑ᵢ inversions(sᵢ₋₁, sᵢ) of a schedule starting at s.

theorem Cslib.DataStructure.ListUpdate.scheduleCost_eq_accessCost_add_exchangeCost {α : Type} [DecidableEq α] [Fintype α] (s reqs : List α) (states : List (List α)) :
(problem α).scheduleCost s reqs states = accessCost reqs states + exchangeCost s reqs states

The cost of a schedule splits into access cost and rearrangement cost.