Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm...

40
November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.1 Introduction to Algorithms 6.046J/18.401J LECTURE 14 Competitive Analysis Self-organizing lists Move-to-front heuristic Competitive analysis of MTF Prof. Charles E. Leiserson

Transcript of Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm...

Page 1: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.1

Introduction to Algorithms6.046J/18.401J

LECTURE 14Competitive Analysis• Self-organizing lists• Move-to-front heuristic• Competitive analysis of

MTF

Prof. Charles E. Leiserson

Page 2: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.2

Self-organizing listsList L of n elements•The operation ACCESS(x) costs rankL(x) =distance of x from the head of L.

•L can be reordered by transposing adjacent elements at a cost of 1.

Page 3: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.3

Self-organizing listsList L of n elements•The operation ACCESS(x) costs rankL(x) =distance of x from the head of L.

•L can be reordered by transposing adjacent elements at a cost of 1.

Example:

1212 33 5050 1414 1717 44L

Page 4: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.4

Self-organizing listsList L of n elements•The operation ACCESS(x) costs rankL(x) =distance of x from the head of L.

•L can be reordered by transposing adjacent elements at a cost of 1.

Example:

1212 33 5050 1414 1717 44L 1212 33 5050 1414

Accessing the element with key 14 costs 4.

Page 5: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.5

Self-organizing listsList L of n elements•The operation ACCESS(x) costs rankL(x) =distance of x from the head of L.

•L can be reordered by transposing adjacent elements at a cost of 1.

Example:

1212 33 505033 1414 1717 445050L

Transposing 3 and 50 costs 1.

Page 6: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.6

On-line and off-line problems

Definition. A sequence S of operations is provided one at a time. For each operation, an on-line algorithm A must execute the operation immediately without any knowledge of future operations (e.g., Tetris).An off-line algorithm may see the whole sequence S in advance.

Goal: Minimize the total cost CA(S).The game of Tetris

Page 7: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.7

Worst-case analysis of self-organizing lists

An adversary always accesses the tail (nth) element of L. Then, for any on-line algorithm A, we have

CA(S) = Ω(|S|⋅ n) in the worst case.

Page 8: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.8

Average-case analysis of self-organizing lists

Suppose that element x is accessed with probability p(x). Then, we have

∑∈

⋅=Lx

LA xxpSC )(rank)()]([E ,

which is minimized when L is sorted in decreasing order with respect to p.

Heuristic: Keep a count of the number of times each element is accessed, and maintain L in order of decreasing count.

Page 9: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.9

The move-to-front heuristic

Practice: Implementers discovered that the move-to-front (MTF) heuristic empirically yields good results.IDEA: After accessing x, move x to the head of L using transposes:

cost = 2 ⋅ rankL(x) .

The MTF heuristic responds well to locality in the access sequence S.

Page 10: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.10

Competitive analysis

Definition. An on-line algorithm A is α-competitive if there exists a constant ksuch that for any sequence S of operations,

CA(S) ≤ α ⋅ COPT(S) + k ,where OPT is the optimal off-line algorithm (“God’s algorithm”).

Page 11: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.11

MTF is O(1)-competitiveTheorem. MTF is 4-competitive for self-organizing lists.

Page 12: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.12

MTF is O(1)-competitiveTheorem. MTF is 4-competitive for self-organizing lists.Proof. Let Li be MTF’s list after the ith access, and let Li* be OPT’s list after the ith access.Let ci = MTF’s cost for the ith operation

= 2 ⋅ rankL (x) if it accesses x;i–1

ci* = MTF’s cost for the ith operation= rankLi–1*(x) + ti ,

where ti is the number of transposes that OPT performs.

Page 13: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.13

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .

Page 14: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.14

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Example.

EE CC AA DD BBLi

CC AA BB DD EELi*

Page 15: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.15

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Example.

EE CC AA DD BBLi

CC AA BB DD EELi*

Φ(Li) = 2 ⋅ |…|

Page 16: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.16

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Example.

EE CC AA DD BBLi

CC AA BB DD EELi*

Φ(Li) = 2 ⋅ |(E,C), …|

Page 17: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.17

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Example.

EE CC AA DD BBLi

CC AA BB DD EELi*

Φ(Li) = 2 ⋅ |(E,C), (E,A), …|

Page 18: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.18

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Example.

EE CC AA DD BBLi

CC AA BB DD EELi*

Φ(Li) = 2 ⋅ |(E,C), (E,A), (E,D), …|

Page 19: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.19

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Example.

EE CC AA DD BBLi

CC AA BB DD EELi*

Φ(Li) = 2 ⋅ |(E,C), (E,A), (E,D), (E,B), …|

Page 20: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.20

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Example.

EE CC AA DD BBLi

CC AA BB DD EELi*

Φ(Li) = 2 ⋅ |(E,C), (E,A), (E,D), (E,B), (D,B)|

Page 21: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.21

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Example.

EE CC AA DD BBLi

CC AA BB DD EELi*

Φ(Li) = 2 ⋅ |(E,C), (E,A), (E,D), (E,B), (D,B)|= 10 .

Page 22: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.22

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .

Page 23: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.23

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Note that• Φ(Li) ≥ 0 for i = 0, 1, …,• Φ(L0) = 0 if MTF and OPT start with the

same list.

Page 24: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.24

Potential functionDefine the potential function Φ:Li → R by

Φ(Li) = 2 ⋅ |(x, y) : x pLiy and y pLi* x|

= 2 ⋅ # inversions .Note that• Φ(Li) ≥ 0 for i = 0, 1, …,• Φ(L0) = 0 if MTF and OPT start with the

same list.How much does Φ change from 1 transpose?• A transpose creates/destroys 1 inversion.• ∆Φ = ±2 .

Page 25: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.25

What happens on an access?Suppose that operation i accesses element x, and define

A = y ∈ Li–1 : y pLi–1x and y pLi–1* x,

B = y ∈ Li–1 : y pLi–1x and y fLi–1* x,

C = y ∈ Li–1 : y fLi–1x and y pLi–1* x,

D =y ∈ Li–1 : y fLi–1x and y fLi–1* x.

A ∪ BA ∪ B xx C ∪ DC ∪ DLi–1

A ∪ CA ∪ C xx B ∪ DB ∪ DLi–1*

Page 26: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.26

What happens on an access?A ∪ BA ∪ B xx C ∪ DC ∪ DLi–1

r = rankLi–1(x)

A ∪ CA ∪ C xx B ∪ DB ∪ DLi–1*r* = rankLi–1* (x)

We have r = |A| + |B| + 1 and r* = |A| + |C| + 1.

Page 27: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.27

What happens on an access?A ∪ BA ∪ B xx C ∪ DC ∪ DLi–1

r = rankLi–1(x)

A ∪ CA ∪ C xx B ∪ DB ∪ DLi–1*r* = rankLi–1* (x)

We have r = |A| + |B| + 1 and r* = |A| + |C| + 1.

When MTF moves x to the front, it creates |A| inversions and destroys |B| inversions. Each transpose by OPT creates ≤ 1 inversion. Thus, we have

Φ(Li) – Φ(Li–1) ≤ 2(|A| – |B| + ti) .

Page 28: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.28

Amortized cost

The amortized cost for the ith operation of MTF with respect to Φ is

ĉi = ci + Φ(Li) – Φ(Li–1)

Page 29: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.29

Amortized cost

The amortized cost for the ith operation of MTF with respect to Φ is

ĉi = ci + Φ(Li) – Φ(Li–1)≤ 2r + 2(|A| – |B| + ti)

Page 30: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.30

Amortized cost

The amortized cost for the ith operation of MTF with respect to Φ is

ĉi = ci + Φ(Li) – Φ(Li–1)≤ 2r + 2(|A| – |B| + ti)= 2r + 2(|A| – (r – 1 – |A|) + ti)

(since r = |A| + |B| + 1)

Page 31: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.31

Amortized cost

The amortized cost for the ith operation of MTF with respect to Φ is

ĉi = ci + Φ(Li) – Φ(Li–1)≤ 2r + 2(|A| – |B| + ti)= 2r + 2(|A| – (r – 1 – |A|) + ti)= 2r + 4|A| – 2r + 2 + 2ti

Page 32: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.32

Amortized cost

The amortized cost for the ith operation of MTF with respect to Φ is

ĉi = ci + Φ(Li) – Φ(Li–1)≤ 2r + 2(|A| – |B| + ti)= 2r + 2(|A| – (r – 1 – |A|) + ti)= 2r + 4|A| – 2r + 2 + 2ti = 4|A| + 2 + 2ti

Page 33: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.33

Amortized cost

The amortized cost for the ith operation of MTF with respect to Φ is

ĉi = ci + Φ(Li) – Φ(Li–1)≤ 2r + 2(|A| – |B| + ti)= 2r + 2(|A| – (r – 1 – |A|) + ti)= 2r + 4|A| – 2r + 2 + 2ti = 4|A| + 2 + 2ti ≤ 4(r* + ti)

(since r* = |A| + |C| + 1 ≥ |A| + 1)

Page 34: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.34

Amortized cost

The amortized cost for the ith operation of MTF with respect to Φ is

ĉi = ci + Φ(Li) – Φ(Li–1)≤ 2r + 2(|A| – |B| + ti)= 2r + 2(|A| – (r – 1 – |A|) + ti)= 2r + 4|A| – 2r + 2 + 2ti = 4|A| + 2 + 2ti ≤ 4(r* + ti)= 4ci*.

Page 35: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.35

The grand finale

∑=

=S

iicSC

1MTF )(

Thus, we have

Page 36: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.36

The grand finale

( )∑

=−

=

Φ−Φ+=

=

S

iiii

S

ii

LLc

cSC

11

1MTF

)()(ˆ

)(

Thus, we have

Page 37: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.37

The grand finale

( )

)()(4

)()(ˆ

)(

01

11

1MTF

S

S

ii

S

iiii

S

ii

LL*c

LLc

cSC

Φ−Φ+⎟⎟⎠

⎞⎜⎜⎝

⎛≤

Φ−Φ+=

=

=

=−

=

Thus, we have

Page 38: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.38

The grand finale

( )

)(4

)()(4

)()(ˆ

)(

OPT

01

11

1MTF

SC

LL*c

LLc

cSC

S

S

ii

S

iiii

S

ii

⋅≤

Φ−Φ+⎟⎟⎠

⎞⎜⎜⎝

⎛≤

Φ−Φ+=

=

=

=−

=

Thus, we have

,

since Φ(L0) = 0 and Φ(L|S|) ≥ 0.

Page 39: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.39

Addendum

If we count transpositions that move x toward the front as “free” (models splicing x in and out of Lin constant time), then MTF is 2-competitive.

Page 40: Introduction to Algorithms - MIT OpenCourseWareCompetitive analysis Definition. An on-line algorithm A is α-competitive if there exists a constant k such that for any sequence S of

November 2, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.40

Addendum

If we count transpositions that move x toward the front as “free” (models splicing x in and out of Lin constant time), then MTF is 2-competitive.

What if L0 ≠ L0*?• Then, Φ(L0) might be Θ(n2) in the worst case.• Thus, CMTF(S) ≤ 4 ⋅ COPT(S) + Θ(n2), which is

still 4-competitive, since n2 is constant as |S| → ∞.