Professional Documents
Culture Documents
Lec14 PDF
Lec14 PDF
6.046J/18.401J LECTURE 14
Competitive Analysis Self-organizing lists Move-to-front heuristic Competitive analysis of MTF
Self-organizing lists
List 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.
November 2, 2005
L14.2
Self-organizing lists
List 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: L 12 12 3 3 50 50 14 14 17 17 4 4
November 2, 2005
L14.3
Self-organizing lists
List 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: L 12 12 3 3 50 50 14 14 17 17 4 4
Self-organizing lists
List 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: L 12 12 50 3 50 3 50 3 50 3 14 14 17 17 4 4
November 2, 2005
L14.7
E[C A ( S )] =
xL
p( x) rank L ( x) ,
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.
November 2, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.8
November 2, 2005
L14.9
Competitive analysis
Definition. An on-line algorithm A is -competitive if there exists a constant k such that for any sequence S of operations, CA(S) COPT(S) + k , where OPT is the optimal off-line algorithm (Gods algorithm).
November 2, 2005
L14.10
MTF is O(1)-competitive
Theorem. MTF is 4-competitive for selforganizing lists.
November 2, 2005
L14.11
MTF is O(1)-competitive
Theorem. MTF is 4-competitive for selforganizing lists. Proof. Let Li be MTFs list after the ith access, and let Li* be OPTs list after the ith access. Let ci = MTFs cost for the ith operation = 2 rankL (x) if it accesses x; i1 ci* = MTFs cost for the ith operation = rankL *(x) + ti , i1 where ti is the number of transposes that OPT performs.
November 2, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.12
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions .
November 2, 2005
L14.13
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions . Example. E C A D B Li C A D B E Li* C C A A B B D D E E
November 2, 2005
L14.14
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions . Example. E C A D B Li C A D B E Li* C C A A B B D D E E
(Li) = 2 |{}|
November 2, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.15
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions . Example. E C A D B Li C A D B E Li* C C A A B B D D E E
(Li) = 2 |{(E,C), }|
November 2, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.16
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions . Example. E C A D B Li C A D B E Li* C C A A B B D D E E
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions . Example. E C A D B Li C A D B E Li* C C A A B B D D E E
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions . Example. E C A D B Li C A D B E Li* C C A A B B D D E E
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions . Example. E C A D B Li C A D B E Li* C C A A B B D D E E
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions . Example. E C A D B Li C A D B E Li* C C A A B B D D E E
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions .
November 2, 2005
L14.22
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 2 # inversions . Note that (Li) 0 for i = 0, 1, , (L0) = 0 if MTF and OPT start with the same list.
November 2, 2005
L14.23
Potential function
Define the potential function :{Li} R by (Li) = 2 |{(x, y) : x pL y and y pL * x}| i i = 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 .
November 2, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.24
Li1 Li1*
November 2, 2005
AB AC x
CD
BD
L14.25
BD
r* = rankLi1* (x)
November 2, 2005
L14.26
BD
r* = rankLi1* (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) (Li1) 2(|A| |B| + ti) .
November 2, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.27
Amortized cost
The amortized cost for the ith operation of MTF with respect to is i = ci + (Li) (Li1)
November 2, 2005
L14.28
Amortized cost
The amortized cost for the ith operation of MTF with respect to is i = ci + (Li) (Li1) 2r + 2(|A| |B| + ti)
November 2, 2005
L14.29
Amortized cost
The amortized cost for the ith operation of MTF with respect to is i = ci + (Li) (Li1) 2r + 2(|A| |B| + ti) = 2r + 2(|A| (r 1 |A|) + ti) (since r = |A| + |B| + 1)
November 2, 2005
L14.30
Amortized cost
The amortized cost for the ith operation of MTF with respect to is i = ci + (Li) (Li1) 2r + 2(|A| |B| + ti) = 2r + 2(|A| (r 1 |A|) + ti) = 2r + 4|A| 2r + 2 + 2ti
November 2, 2005
L14.31
Amortized cost
The amortized cost for the ith operation of MTF with respect to is i = ci + (Li) (Li1) 2r + 2(|A| |B| + ti) = 2r + 2(|A| (r 1 |A|) + ti) = 2r + 4|A| 2r + 2 + 2ti = 4|A| + 2 + 2ti
November 2, 2005
L14.32
Amortized cost
The amortized cost for the ith operation of MTF with respect to is i = ci + (Li) (Li1) 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)
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) (Li1) 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*.
November 2, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.34
November 2, 2005
L14.35
November 2, 2005
L14.36
November 2, 2005
L14.37
Addendum
If we count transpositions that move x toward the front as free (models splicing x in and out of L in constant time), then MTF is 2-competitive.
November 2, 2005
L14.39
Addendum
If we count transpositions that move x toward the front as free (models splicing x in and out of L in 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| .
November 2, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L14.40