CS 401
Greedy Algorithms / Optimal Caching
Xiaorui Sun
1
CS 401 Greedy Algorithms / Optimal Caching Xiaorui Sun 1 Recap - - PowerPoint PPT Presentation
CS 401 Greedy Algorithms / Optimal Caching Xiaorui Sun 1 Recap and Outline Homework 1 due today, Homework 2 will be out later today Greedy algorithm: Best current partial solution at each step Greedy Analysis Strategies
Xiaorui Sun
1
2
Greedy algorithm stays ahead: Interval Scheduling Structural: Interval Partition Exchange argument: Minimize Lateness
Complex exchange argument
4
Type Latency Capacity Registers 0.25 ns 36 KB L1 Cache 1 ns 192 KB L2 Cache 3 ns 1.5 MB L3 Cache 14 ns 15 MB DRAM (DDR4) 66 ns 32 GB SDD 0.15 ms 480 GB Internet 7 ms 2 GB left in Dropbox L
My computer at home (2020)
5
6
must bring requested item into cache, and evict some existing item, if full. Goal
Example: ! = &, initial cache = ), *, requests: ), *, +, *, +, ), ), *. Optimal eviction schedule: & evictions.
a b a b c b c b c b a b b c b c a a b a a b b cache requests a
7
g a b c e d a b b a c d e a f a d e f g h ...
a b current cache: c d e f future queries:
cache miss eject this one
Which item we should evict?
8
d b c d e c ? ? ? c e f c d a e a d b y b a a ? a cache requests d x y a
9
g a b c e d a b b a c d e a f a d e f g h ...
a b current cache: c d e f future queries:
cache miss eject this one
Which item we should evict?
Definition
in a step in which that item is requested. Intuition
evictions.
a x an unreduced schedule c a d c a d b a c b a x b a c b a b c a b c a c d a b c a a a b a reduced schedule c a b c a d c a d c a d b a c b a c b a c b a c d a b c a a a b c a a b c a 6
11
Claim
schedule !′ with no more evictions. Proof (by induction on number of unreduced items)
Case 1: # evicted at time $′, before next request for #. Case 2: # requested at time $′ before # is evicted. ▪
t t' d c t t' c
S'
d
S
d requested at time t'
t t' d c t t' c
S'
e
S
d evicted at time t', before next request
e d c c d
12
Theorem
Let " be reduced schedule that satisfies invariant through # requests. We produce "′ that satisfies invariant after # + & requests.
contents before request # + &. Case 1: () is already in the cache). "′ = " satisfies invariant. (used " is reduced here) Case 2: () is not in the cache and " and ",-, evict the same element). "′ = " satisfies invariant. Invariant: There exists an optimal reduced schedule " that makes the same eviction schedule as ",-, through the first # + & requests.
13
Case 3: (! is not in the cache; "#$# evicts %; " evicts & ≠ %).
having element & in cache is no worse than having element %
– Continue building "’ to be the same as " until forced to be different j same f same f e e S S' j same d same f d e S S' j+1 evicted by "#$# evicted by "
14
Let !′ be the first time after ! + $ that % and %′ must take a different action, and let & be item requested at time !′.
Case 3a: & = (. Can't happen: ( was evicted by Farthest-In-Future so there must be a request for ) before (. Case 3b: & = ). Element ) can't be in cache of %, so let (′ be the element that % evicts.
cache
Note: %′ is no longer reduced, but can be transformed into a reduced schedule that agrees with %,-, through step ! + $
same
e
same
f S S' j'
Action of S must involve e or f (or both)
15
Let !′ be the first time after ! + $ that % and %′ must take a different action, and let & be item requested at time !′.
Case 3c: & ≠ ( and & ≠ ). % must evict (. Make %′ evict ); now % and %′ have the same cache. ▪
same
e
same
f S S' j'
Action of S must involve e or f (or both)
In each case can now extend %’ using rest of % at no extra cost. %’ is optimal, reduced, and agrees with %+,+ for ! + $ steps Optimality of %+,+ follows by induction.
same
g
same
g S S' j'
16
Offline: full sequence of requests is known a priori. Online (reality): requests are not known in advance. Caching is among most fundamental online problems in CS.
Provides basis for understanding and analyzing online algorithms. LRU is k-competitive. [Section 13.8] LIFO is arbitrarily bad.
FIF with direction of time reversed!