fullscreen
timer
qrcode
plickers
selector
edit
reset

Greedy Algorithms (4)

COS 320 - Algorithm Design

Greedy Algorithms (4)

optimal caching

optimal caching

Caching

Applications: CPU, RAM, hard drive, web, browser, ...

Goal: Eviction schedule that minimizes the number of evictions

optimal caching

Ex: \(k=2\), initial cache \(= ab\), requests: \(a, b, c, b, c, a, b\)

Optimal eviction schedule: 2 evictions

cache
a a b
b a b
c a c
b b c
c b c
a b a
b b a

Note: red highlight on cache miss (eviction)

optimal offline caching: greedy algorithms

optimal offline caching: greedy algorithms

LIFO: Evict item brought in most recently (stack)

cache
a a w x y z
d d w x y z
a a w x y z
b b w x y z
c c w x y z
e e w x y z
g ? ? ? ? ?
b
e
d

optimal offline caching: greedy algorithms

LIFO: Evict item brought in most recently (stack)

cache
a a w x y z
d d w x y z
a a w x y z
b b w x y z
c c w x y z
e e w x y z
g g w x y z
b ? ? ? ? ?
e
d

optimal offline caching: greedy algorithms

FIFO: Evict item brought in least recently (queue)

cache
a v w x y a
d v w x d a
a v w x d a
b v w b d a
c v c b d a
e e c b d a
g ? ? ? ? ?
b
e
d

optimal offline caching: greedy algorithms

FIFO: Evict item brought in least recently (queue)

cache
a v w x y a
d v w x d a
a v w x d a
b v w b d a
c v c b d a
e e c b d a
g e c b d g
b ? ? ? ? ?
e
d

optimal offline caching: greedy algorithms

LRU: Evict item whose most recent access was earliest

cache
a v w x y a
d v w x d a
a v w x d a
b v w b d a
c v c b d a
e e c b d a
g ? ? ? ? ?
b
e
d

optimal offline caching: greedy algorithms

LRU: Evict item whose most recent access was earliest

cache
a v w x y a
d v w x d a
a v w x d a
b v w b d a
c v c b d a
e e c b d a
g e c b g a
b ? ? ? ? ?
e
d

optimal offline caching: greedy algorithms

LFU: Evict item that was least frequently requested

cache
a a w x y z
d a d x y z
a a d x y z
b a d b y z
c a d b c z
e a d b c e
g ? ? ? ? ?
b
e
d

optimal offline caching: greedy algorithms

LFU: Evict item that was least frequently requested

cache
a a w x y z
d a d x y z
a a d x y z
b a d b y z
c a d b c z
e a d b c e
g a g b c e
b ? ? ? ? ?
e
d

optimal offline caching: farthest-in-future

Farthest-in-future algorithm evicts item in the cache that is not requested until farthest in the future (clairvoyant algorithm)

Theorem [Bélády 1966]: FF is optimal eviction schedule
Pf: Algorithm and theorem are intuitive; proof is subtle.

optimal offline caching: farthest-in-future

Farthest-in-future algorithm evicts item in the cache that is not requested until farthest in the future (clairvoyant algorithm)

cache
a a b c d e
f ? ? ? ? ?
a
b
c
e
g
b
e
d
cache
a a b c d e
f a b c f e
a ? ? ? ? ?
b
c
e
g
b
e
d
cache
a a b c d e
f a b c f e
a a b c f e
b ? ? ? ? ?
c
e
g
b
e
d
cache
a a b c d e
f a b c f e
a a b c f e
b a b c f e
c ? ? ? ? ?
e
g
b
e
d
cache
a a b c d e
f a b c f e
a a b c f e
b a b c f e
c a b c f e
e ? ? ? ? ?
g
b
e
d
cache
a a b c d e
f a b c f e
a a b c f e
b a b c f e
c a b c f e
e a b c f e
g ? ? ? ? ?
b
e
d
cache
a a b c d e
f a b c f e
a a b c f e
b a b c f e
c a b c f e
e a b c f e
g a b c g e
b ? ? ? ? ?
e
d
cache
a a b c d e
f a b c f e
a a b c f e
b a b c f e
c a b c f e
e a b c f e
g a b c g e
b a b c g e
e ? ? ? ? ?
d
cache
a a b c d e
f a b c f e
a a b c f e
b a b c f e
c a b c f e
e a b c f e
g a b c g e
b a b c g e
e a b c g e
d ? ? ? ? ?
cache
a a b c d e
f a b c f e
a a b c f e
b a b c f e
c a b c f e
e a b c f e
g a b c g e
b a b c g e
e a b c g e
d a b c g d

quiz 6: greedy algorithms

live view ]

Which item will be evicted next using farthest-in-future schedule?

  1. d

  2. e

  3. c

  4. a

cache
b d b y a
c d b c a
e d e c a
f ? ? ? ?
c
d
a
e
a
c

reduced eviction schedules

A reduced schedule is a schedule that brings an item \(d\) into the cache in step \(j\) only if there is a request for \(d\) in step \(j\) and \(d\) is not already in the cache.

an unreduced schedule:

cache
a a b c
a a b c
c a d c
d a d c
a a c b
b a c b
c a c b
d d c b
d d c d

a reduced schedule:

cache
a a b c
a a b c
c a b c
d a d c
a a d c
b a d b
c a c b
d d c b
d d c b

reduced eviction schedules

Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.

Pf by induction on number of steps \(j\):

reduced eviction schedules

unreduced schedule \(S\):

cache
      c
      c
      c
x     d
y     d
z     d
e     e
      e

schedule \(S'\):

cache
      c
      c
      c
x     c
y     c
z     c
e     e
      e

reduced eviction schedules

Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.

Pf by induction on number of steps \(j\):

reduced eviction schedules

unreduced schedule \(S\):

cache
      c
      c
      c
x     d
y     d
z     d
d     d
      d

schedule \(S'\):

cache
      c
      c
      c
x     c
y     c
z     c
d     d
      d

reduced eviction schedules

Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.

Pf by induction on number of steps \(j\):

reduced eviction schedules

unreduced schedule \(S\):

cache
  d a c
  d a c
  d a c
d d a d
d d a d
c c a d
b c a b
d c a d

schedule \(S'\):

cache
  d a c
  d a c
  d a c
d d a c
d d a c
c c a c
b c a b
d c a d

reduced eviction schedules

Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.

Pf by induction on number of steps \(j\):

reduced eviction schedules

unreduced schedule \(S\):

cache
  d a c
  d a c
  d a c
d d a d
d d a d
c c a d
a c a d
d c a d

schedule \(S'\):

cache
  d a c
  d a c
  d a c
d d a c
d d a c
c c a c
a c a c
d c a d

reduced eviction schedules

Claim: Given any unreduced schedule \(S\), can transform it into a reduced schedule \(S'\) with no more evictions.

Pf by induction on number of steps \(j\):

farthest-in-future analysis

Theorem: FF is optimal eviction algorithm

Pf: Follows directly from the following invariant

farthest-in-future analysis

Invariant: There exists an optimal reduced schedule \(S\) that has the same eviction schedule as \(S_{FF}\) through the first \(j\) steps

Pf by induction on number of steps \(j\):

farthest-in-future analysis

Schedule \(S\) at steps \(j, j+1\)

cache
  same e f
d same e d

Schedule \(S'\) at step \(j, j+1\)

cache
  same e f
d same d f

farthest-in-future analysis

Let \(j'\) be the first step after \(j+1\) that \(S'\) must take a different action from \(S\) (involves either \(e\) or \(f\) or neither); let \(g\) denote the item requested in step \(j'\).

Schedule \(S\) at step \(j, j+1, j'\)

cache
  same e f
d same e d
e same e d

Schedule \(S'\) at step \(j,j+1,j'\)

cache
  same e f
d same d f
e same d f

farthest-in-future analysis

Schedule \(S\) at step \(j, j+1, j'\)

cache
  same e f
d same e d
f same e d

Schedule \(S'\) at step \(j,j+1,j'\)

cache
  same e f
d same d f
f same d f

farthest-in-future analysis

Schedule \(S\) at step \(j, j+1, j'\)

cache
  same e f
d same e d
g same g d

Schedule \(S'\) at step \(j,j+1,j'\)

cache
  same e f
d same d f
g same d f

caching perspective

Online vs. offline algorithms


FIFO: Evict item brought in least recently

LRU: Evict item whose most recent access was earliest (FF with direction of time reversed!)

caching perspective

Theorem: FF is optimal offline eviction algorithm

×