Recall that an offline algorithm sees the whole input, whereas an online algorithm only sees prefixes at a time.
Definition (Competitive Analysis). A deterministic online algorithm A is α-competitive if, for all sequences R, we have CostA(R)≤α⋅CostOpt(R)+c for some fixed constant c. (Here Opt is the best offline algorithm.)
Problem. (Self-Organizing List) Given a list L of n distinct elements, we have:
Access(x): Finds and returns x in the list, has a cost of Rank(x).
Transpose(i): Swaps the ith and (i+1)th elements of the list, has a cost of 1.
So the cost of an algorithm is CostA(R)=∑i=1k[RankLi−1(xi)+ti], where ti is the # of transpositions in step i.
An adversary gives a sequence R=(x1,x2,…,xk) online. How should we perform transpositions to minimize cost?
First, some preliminary observations about the problem:
Swapping L[i] and L[i+1] decreases the cost of Access(L[i+1]) by 1 and increases the cost of Access(L[i]) by 1.
Transpositions only help if, say, L[i+1] is accessed more than once.
An adversary can just “unluckily” always pick xi to be the last element of L. So the worst case is Ω(n⋅∣R∣).
Example. If L=[1,2,…,n], and R=(n,n−1,…,1), then we might as well do no transpositions, which is Θ(n2). This is because each xi appears in R only once, so transpositions are not useful.
Example. If L=[1,2,…,n], and R=(n,n,…,n), then we can use n−1 transpositions, then perform ∣R∣ accesses with cost 1. This is the best offline algorithm, and it is Θ(n+∣R∣).
In contrast, the online algorithm called “don't transpose anything” is Θ(n⋅∣R∣). Can we do better—be competitive?
Solution. (Move To Front) After every Access(xi), move xi to the front via transposition. This yields:
CostMtf(R)=i=1∑k[2RankLi−1(xi)−1].
We claim that Mtf is 4-competitive: for any R, Mtf does at most ×4 worse than the best (offline) algorithm.
Proof: First, some notation. Say that Mtf and Opt look like:
Mtf:L0→L1→L2→⋯→Lk and Opt:L0→L1∗→L2∗→⋯→Lk∗,
and let's make the following definitions:
ri:=RankLi−1(xi) and ri∗:=RankLi−1∗(xi) and ci:=2ri−1 and ci∗:=ri∗+ti,
where Opt uses ti transpositions on the ith step. (Here, ci and ci∗ are the costs of Mtf and Opt, respectively.) Let's also define:
Definition. An inversion is a pair (a,b) such that RankLi(a)<RankLi(b) yet RankLi∗(a)>RankLi∗(b).
Finally, let's define a potential function:
Φi=Φ(Li,Li∗):=2×[# of inversions between Li and Li∗].
Note that Φi is also twice the minimum # of transpositions to transform Li into Li∗.
Every Access(xi) yields an effect of ΔΦ=0.
Every Transpose(i) yields an effect of either ΔΦ=+2 or ΔΦ=−2.
Given a state (Li−1,Li−1∗) and a query Access(xi), we put every element x=xi in one of four categories:
Say x∈Ai if x comes beforexi in Li−1 and x comes beforexi in Li−1∗.
Say x∈Bi if x comes beforexi in Li−1 and x comes afterxi in Li−1∗.
Say x∈Ci if x comes afterxi in Li−1 and x comes beforexi in Li−1∗.
Say x∈Di if x comes afterxi in Li−1 and x comes afterxi in Li−1∗.
Given these labels, we can say the following about the ith step of Mtf.
ri=∣Ai∣+∣Bi∣+1 and ri∗=∣Ai∣+∣Ci∣+1 and ΔΦi≤2×[∣Ai∣−∣Bi∣+ti].
We can now bound our imaginary costs ci^ like so: