Algorithm Design nd Anlysis LECTURE 5 Supplement Greedy Algorithms Cont d Minimizing lteness Ching (NOT overed in leture) Adm Smith 9/8/10 A. Smith; sed on slides y E. Demine, C. Leiserson, S. Rskhodnikov, K. Wyne
Greedy Algorithms Build up solution to n optimiztion prolem t eh step shortsightedly hoosing the option tht urrently seems the est. Sometimes good Often does not work Proving orretness n e triky, euse lgorithm is unstrutured 9/8/10 A. Smith; sed on slides y E. Demine, C. Leiserson, S. Rskhodnikov, K.
Lst leture (KT, Chpter 4.1) Two types of orretness rguments Greedy stys hed Intervl sheduling prolem d e f g Struturl rgument Intervl prtitioning 4 3 0 1 2 3 4 5 6 7 8 9 1 0 e d g h 1 1 j Time 2 h 1 f i Tody: exhnge 9 9:3 0 1 0 10:30 1 1 11:30 1 2 12:30 1 1:3 0 2 2:3 0 3 3:3 0 4 4:3 0 9/8/10 A. Smith; sed on slides y E. Demine, C. Leiserson, S. Rskhodnikov, K.
Sheduling to minimize lteness 9/8/10 A. Smith; sed on slides y E. Demine, C. Leiserson, S. Rskhodnikov, K. Wyne
Sheduling to Minimizing Lteness Minimizing lteness prolem. Single resoure proesses one jo t time. Jo j requires t j units of proessing time nd is due t time d j. If j strts t time s j, it finishes t time f j = s j + t j. Lteness: j = mx { 0, f j - d j }. Gol: shedule ll jos to minimize mximum lteness L = mx j. Ex: 1 2 3 4 5 6 t j 3 2 1 4 3 2 d j 6 8 9 9 14 15 lteness = 2 lteness = 0 mx lteness = 6 d 3 = 9 d 2 = 8 d 6 = 15 d 1 = 6 d 5 = 14 d 4 = 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Greedy strtegies?
Minimizing Lteness: Greedy Algorithms Greedy templte. Consider jos in some order. [Shortest proessing time first] Consider jos in sending order of proessing time t j. [Erliest dedline first] Consider jos in sending order of dedline d j. [Smllest slk] Consider jos in sending order of slk d j - t j.
Minimizing Lteness: Greedy Algorithms Greedy templte. Consider jos in some order. [Shortest proessing time first] Consider jos in sending order of proessing time t j. t j 1 100 d j 1 2 10 10 ounterexmple [Smllest slk] Consider jos in sending order of slk d j - t j. 1 t j 1 2 d j 2 10 10 ounterexmple
Minimizing Lteness: Greedy Algorithm Greedy lgorithm. Erliest dedline first. Sort n jos y dedline so tht d 1 d 2 d n t 0 for j = 1 to n Assign jo j to intervl [t, t + t j ] s j t, f j t + t j t t + t j output intervls [s j, f j ] mx lteness = 1 d 1 = 6 d 2 = 8 d 3 = 9 d 4 = 9 d 5 = 14 d 6 = 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Minimizing Lteness: No Idle Time Oservtion. There exists n optiml shedule with no idle time. d = 4 d = 6 0 1 2 3 4 5 6 d = 12 7 8 9 10 11 d = 4 d = 6 d = 12 0 1 2 3 4 5 6 7 8 9 10 11 Oservtion. The greedy shedule hs no idle time.
Minimizing Lteness: Inversions Def. An inversion in shedule S is pir of jos i nd j suh tht: i < j ut j sheduled efore i. inversion efore swp j i Oservtion. Greedy shedule hs no inversions. Oservtion. If shedule (with no idle time) hs n inversion, it hs one with pir of inverted jos sheduled onseutively.
Minimizing Lteness: Inversions Def. An inversion in shedule S is pir of jos i nd j suh tht: i < j ut j sheduled efore i. inversion f i efore swp j i fter swp i j f' j Clim. Swpping two djent, inverted jos redues the numer of inversions y one nd does not inrese the mx lteness. Pf. Let e the lteness efore the swp, nd let ' e it fterwrds. ' k = k for ll k i, j ' i i If jo j is lte: j f d j f d i f d i i j i j (definition) ( j finishes t time f ) ( i j ) (definition) i
Minimizing Lteness: Anlysis of Greedy Algorithm Theorem. Greedy shedule S is optiml. Pf. Define S* to e n optiml shedule tht hs the fewest numer of inversions, nd let's see wht hppens. Cn ssume S* hs no idle time. If S* hs no inversions, then S = S*. If S* hs n inversion, let i-j e n djent inversion. swpping i nd j does not inrese the mximum lteness nd stritly dereses the numer of inversions this ontrdits definition of S*
Greedy Anlysis Strtegies Greedy lgorithm stys hed. Show tht fter eh step of the greedy lgorithm, its solution is t lest s good s ny other lgorithm's. Exhnge rgument. Grdully trnsform ny solution to the one found y the greedy lgorithm without hurting its qulity. Struturl. Disover simple "struturl" ound sserting tht every possile solution must hve ertin vlue. Then show tht your lgorithm lwys hieves this ound.
Optiml Ching 9/8/10 A. Smith; sed on slides y E. Demine, C. Leiserson, S. Rskhodnikov, K. Wyne
Optiml Offline Ching Ching. Che with pity to store k items. Sequene of m item requests d 1, d 2,, d m. Che hit: item lredy in he when requested. Che miss: item not lredy in he when requested: must ring requested item into he, nd evit some existing item, if full. Gol. Evition shedule tht minimizes numer of he misses. Ex: k = 2, initil he =, requests:,,,,,,,. Optiml evition shedule: 2 he misses. requests he
Suggestions for greedy pprohes?
Optiml Offline Ching: Frthest-In-Future Frthest-in-future. Evit item in the he tht is not requested until frthest in the future. urrent he: d e f future queries: g e d d e f d e f g h... he miss ejet this one Theorem. [Belldy, 1960s] FF is optiml evition shedule. Pf. Algorithm nd theorem re intuitive; proof is sutle.
Redued Evition Shedules Def. A redued shedule is shedule tht only inserts n item into the he in step in whih tht item is requested. Intuition. Cn trnsform n unredued shedule into redued one with no more he misses. (Ide is to defer unredued requests. Proof will e n exerise.) x d d d d d d x d n unredued shedule redued shedule
Frthest-In-Future: Anlysis Theorem. FF is optiml evition lgorithm. Pf. (y indution on numer or requests j) Invrint: There exists n optiml redued shedule S tht mkes the sme evition shedule s S FF through the first j+1 requests. Let S = opt. redued shedule tht stisfies invrint through j requests. We produe S' tht stisfies invrint fter j+1 requests. Consider (j+1) st request d = d j+1. Sine S nd S FF hve greed up until now, they hve the sme he ontents efore request j+1. Cse 1: (d is lredy in the he). S' = S stisfies invrint. Cse 2: (d is not in the he nd S nd S FF evit the sme element). S' = S stisfies invrint.
Frthest-In-Future: Anlysis Pf. (ontinued) Cse 3: (d is not in the he; S FF evits e; S evits f e). egin onstrution of S' from S y eviting e insted of f j sme e f sme e f S S' j+1 j sme e d sme d f S S' now S' grees with S FF on first j+1 requests; we show tht hving element f in he is no worse thn hving element e
Frthest-In-Future: Anlysis Let j' e the first time fter j+1 tht S nd S' tke different tion, nd let g e item requested t time j'. must involve e or f (or oth) j' sme e sme f S S' Cse 3: g = e. Cn't hppen with Frthest-In-Future sine there must e request for f efore e. Cse 3: g = f. Element f n't e in he of S, so let e' e the element tht S evits. if e' = e, S' esses f from he; now S nd S' hve sme he if e' e, S' evits e' nd rings e into the he; now S nd S' hve the sme he Note: S' is no longer redued, ut n e trnsformed into redued shedule tht grees with S FF through step j+1
Frthest-In-Future: Anlysis Let j' e the first time fter j+1 tht S nd S' tke different tion, nd let g e item requested t time j'. must involve e or f (or oth) j' sme e sme f S S' otherwise S' would tke the sme tion Cse 3: g e, f. S must evit e. Mke S' evit f; now S nd S' hve the sme he. j' sme g sme g S S'
Ching Perspetive Online vs. offline lgorithms. Offline: full sequene of requests is known priori. Online (relity): requests re not known in dvne. Ching is mong most fundmentl online prolems in CS. LIFO. Evit pge rought in most reently. LRU. Evit pge whose most reent ess ws erliest. FF with diretion of time reversed! Theorem. FF is optiml offline evition lgorithm. Provides sis for understnding nd nlyzing online lgorithms. LRU is k-ompetitive. [Setion 13.8] LIFO is ritrrily d.