MIT 6.1220 — Lecture 4

Online Algorithms and Competitive Analysis

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 AA is α\alpha-competitive if, for all sequences RR, we have CostA(R)αCostOpt(R)+c\textsc{Cost}_{\textsc{A}}(R) \leq \alpha \cdot \textsc{Cost}_{\textsc{Opt}}(R) + c for some fixed constant cc. (Here Opt\textsc{Opt} is the best offline algorithm.)

Problem. (Self-Organizing List) Given a list LL of nn distinct elements, we have:

So the cost of an algorithm is CostA(R)=i=1k[RankLi1(xi)+ti]\textsc{Cost}_A(R) = \sum_{i = 1}^k \left [ \textsc{Rank}_{L_{i - 1}}(x_i) + t_i \right ], where tit_i is the # of transpositions in step ii.

An adversary gives a sequence R=(x1,x2,,xk)R = (x_1, x_2, \dots, x_k) online. How should we perform transpositions to minimize cost?

First, some preliminary observations about the problem:

Example. If L=[1,2,,n]L = [1, 2, \dots, n], and R=(n,n1,,1)R = (n, n - 1, \dots, 1), then we might as well do no transpositions, which is Θ(n2)\Theta(n^2). This is because each xix_i appears in RR only once, so transpositions are not useful.

Example. If L=[1,2,,n]L = [1, 2, \dots, n], and R=(n,n,,n)R = (n, n, \dots, n), then we can use n1n - 1 transpositions, then perform R|R| accesses with cost 11. This is the best offline algorithm, and it is Θ(n+R)\Theta(n + |R|).

In contrast, the online algorithm called “don't transpose anything” is Θ(nR)\Theta(n \cdot |R|). Can we do better—be competitive?

Solution. (Move To Front) After every Access(xi)\textsc{Access}(x_i), move xix_i to the front via transposition. This yields:

CostMtf(R)=i=1k[2RankLi1(xi)1].\textsc{Cost}_{\textsc{Mtf}}(R) = \sum_{i = 1}^k \left [ 2 \textsc{Rank}_{L_{i - 1}}(x_i) - 1 \right ].

We claim that Mtf\textsc{Mtf} is 44-competitive: for any RR, Mtf\textsc{Mtf} does at most ×4\times 4 worse than the best (offline) algorithm.

Proof: First, some notation. Say that Mtf\textsc{Mtf} and Opt\textsc{Opt} look like:

Mtf:L0L1L2Lk  and  Opt:L0L1L2Lk,\textsc{Mtf}: L_0 \to L_1 \to L_2 \to \dots \to L_k ~ \text{ and } ~ \textsc{Opt}: L_0 \to L_1^* \to L_2^* \to \dots \to L_k^*,

and let's make the following definitions:

ri:=RankLi1(xi)  and  ri:=RankLi1(xi)  and  ci:=2ri1  and  ci:=ri+ti,r_i := \textsc{Rank}_{L_{i - 1}}(x_i) ~ \text{ and } ~ r_i^* := \textsc{Rank}_{L_{i - 1}^*}(x_i) ~ \text{ and } ~ c_i := 2r_i - 1 ~ \text{ and } ~ c_i^* := r_i^* + t_i,

where Opt uses tit_i transpositions on the ithi^{\text{th}} step. (Here, cic_i and cic_i^* are the costs of Mtf and Opt, respectively.) Let's also define:

Definition. An inversion is a pair (a,b)(a, b) such that RankLi(a)<RankLi(b)\textsc{Rank}_{L_i}(a) < \textsc{Rank}_{L_i}(b) yet RankLi(a)>RankLi(b)\textsc{Rank}_{L_i^*}(a) > \textsc{Rank}_{L_i^*}(b).

Finally, let's define a potential function:

Φi=Φ(Li,Li):=2×[# of inversions between Li and Li].\Phi_i = \Phi(L_i, L_i^*) := 2 \times [ \text{\# of inversions between $L_i$ and $L_i^*$} ].

Note that Φi\Phi_i is also twice the minimum # of transpositions to transform LiL_i into LiL_i^*.

Given a state (Li1,Li1)(L_{i - 1}, L_{i - 1}^*) and a query Access(xi)\textsc{Access}(x_i), we put every element xxix \neq x_i in one of four categories:

Given these labels, we can say the following about the ithi^{\text{th}} step of Mtf.

ri=Ai+Bi+1  and  ri=Ai+Ci+1  and  ΔΦi2×[AiBi+ti].r_i = |A_i| + |B_i| + 1 ~ \text{ and } ~ r_i^* = |A_i| + |C_i| + 1 ~ \text{ and } ~ \Delta \Phi_i \leq 2 \times [|A_i| - |B_i| + t_i].

We can now bound our imaginary costs ci^\hat{c_i} like so:

ci^=ci+ΔΦi 2(Ai+Bi+1)1+2(AiBi+ti)= 4Ai+2ti+1 4(Ai+Ci)+4ti+1< 4ci,\begin{align*}\hat{c_i} = c_i+ \Delta \Phi_i \leq \ & 2(|A_i| + |B_i| + 1) - 1 + 2(|A_i| - |B_i| + t_i) \\ = \ & 4|A_i| + 2t_i + 1 \\ \leq \ & 4(|A_i| + |C_i|) + 4t_i + 1 \\ < \ & 4c_i^*,\end{align*}

as desired.   \blacksquare