Gree s goo. Gree s rght. Gree works. Gree lrfes, uts through, n ptures the essene of the evolutonry sprt. - Goron Geko (Mhel Dougls) Anlyss of Algorthms Pyush Kumr (Leture 4: Greey Algorthms) Welome to 453 oure: K. Wyne, Greey Algorthms Optmzton prolem: Mn/Mx n oetve. Mnmze the totl length of spnnng tree. Mnmze the sze of fle usng ompresson (The mother of ll prolems) Greey Algorthm Attempt to o est t eh step wthout onserton of future onserton For some prolems, Lolly optml hoe les to glol opt. Follows Gree s goo phlosophy Requres Optml ustruture Wht exmples hve we lrey seen? Greey Algorthms For some prolems, Gree s goo works. For some, t fns goo soluton whh s not glol opt Heursts Approxmton Algorthms For some, t n o very.
Prolem of Chnge Venng mhne hs qurters, nkels, pennes n mes. Nees to return N ents hnge. Wnte: An lgorthm to return the N ents n mnmum numer of ons. Wht o we o? 4. Intervl heulng 5 Intervl heulng Intervl sheulng. Jo strts t s n fnshes t f. Two os omptle f they on't overlp. Gol: fn mxmum suset of mutully omptle os. e f g 0 3 4 5 6 7 8 9 0 h Tme
Intervl heulng: Greey Algorthms Greey templte. Conser os n some orer. Tke eh o prove t's omptle wth the ones lrey tken. [Erlest strt tme] Conser os n senng orer of strt tme s. [Erlest fnsh tme] Conser os n senng orer of fnsh tme f. [hortest ntervl] Conser os n senng orer of ntervl length f - s. [Fewest onflts] For eh o, ount the numer of onfltng os. heule n senng orer of onflts. Intervl heulng: Greey Algorthms Greey templte. Conser os n some orer. Tke eh o prove t's omptle wth the ones lrey tken. reks erlest strt tme reks shortest ntervl reks fewest onflts Intervl heulng: Greey Algorthm Greey lgorthm. Conser os n nresng orer of fnsh tme. Tke eh o prove t's omptle wth the ones lrey tken. ort os y fnsh tmes so tht f f... f n. os selete A for = to n { f (o omptle wth A) A A {} } return A Implementton. O(n log n). Rememer o * tht ws e lst to A. Jo s omptle wth A f s f *. 3
Intervl heulng: Anlyss Theorem. Greey lgorthm s optml. Pf. (y ontrton) Assume greey s not optml, n let's see wht hppens. Let,,... k enote set of os selete y greey. Let,,... m enote set of os n the optml soluton wth =, =,..., r = r for the lrgest possle vlue of r. o r+ fnshes efore r+ Greey: r r+ OPT: r r+... why not reple o r+ wth o r+? Intervl heulng: Anlyss Theorem. Greey lgorthm s optml. Pf. (y ontrton) Assume greey s not optml, n let's see wht hppens. Let,,... k enote set of os selete y greey. Let,,... m enote set of os n the optml soluton wth =, =,..., r = r for the lrgest possle vlue of r. o r+ fnshes efore r+ Greey: r r+ OPT: r r+... soluton stll fesle n optml, ut ontrts mxmlty of r. 4. Intervl Prttonng 4
Intervl Prttonng Intervl prttonng. Leture strts t s n fnshes t f. Gol: fn mnmum numer of lssrooms to sheule ll letures so tht no two our t the sme tme n the sme room. Ex: Ths sheule uses 4 lssrooms to sheule 0 letures. e g h f 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 4 4:30 Tme Intervl Prttonng Intervl prttonng. Leture strts t s n fnshes t f. Gol: fn mnmum numer of lssrooms to sheule ll letures so tht no two our t the sme tme n the sme room. Ex: Ths sheule uses only 3. f g e h 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 4 4:30 Tme Intervl Prttonng: Lower Boun on Optml oluton Def. The epth of set of open ntervls s the mxmum numer tht ontn ny gven tme. Key oservton. Numer of lssrooms neee epth. Ex: Depth of sheule elow = 3 sheule elow s optml.,, ll ontn 9:30 Q. Does there lwys exst sheule equl to epth of ntervls? f g e h 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 4 4:30 Tme 5
Intervl Prttonng: Greey Algorthm Greey lgorthm. Conser letures n nresng orer of strt tme: ssgn leture to ny omptle lssroom. ort ntervls y strtng tme so tht s s... s n. 0 numer of llote lssrooms for = to n { f (leture s omptle wth some lssroom k) sheule leture n lssroom k else llote new lssroom + sheule leture n lssroom + + } Implementton. O(n log n). For eh lssroom k, mntn the fnsh tme of the lst o e. Keep the lssrooms n prorty queue. Intervl Prttonng: Greey Anlyss Oservton. Greey lgorthm never sheules two nomptle letures n the sme lssroom. Theorem. Greey lgorthm s optml. Pf. Let = numer of lssrooms tht the greey lgorthm llotes. Clssroom s opene euse we neee to sheule o, sy, tht s nomptle wth ll - other lssrooms. ne we sorte y strt tme, ll these nomptltes re use y letures tht strt no lter thn s. Thus, we hve letures overlppng t tme s +. Key oservton ll sheules use lssrooms. 4. heulng to Mnmze Lteness 8 6
heulng to Mnmzng Lteness Mnmzng lteness prolem. ngle resoure proesses one o t tme. Jo requres t unts of proessng tme n s ue t tme. If strts t tme s, t fnshes t tme f = s + t. Lteness: = mx { 0, f - }. Gol: sheule ll os to mnmze mxmum lteness L = mx. Ex: 3 4 5 6 t 3 4 3 6 8 9 9 4 5 lteness = lteness = 0 mx lteness = 6 3 = 9 = 8 6 = 5 = 6 5 = 4 4 = 9 0 3 4 5 6 7 8 9 0 3 4 5 Mnmzng Lteness: Greey Algorthms Greey templte. Conser os n some orer. [hortest proessng tme frst] Conser os n senng orer of proessng tme t. [Erlest elne frst] Conser os n senng orer of elne. [mllest slk] Conser os n senng orer of slk - t. Mnmzng Lteness: Greey Algorthms Greey templte. Conser os n some orer. [hortest proessng tme frst] Conser os n senng orer of proessng tme t. t 0 ounterexmple 00 0 [mllest slk] Conser os n senng orer of slk - t. t 0 0 ounterexmple 7
Mnmzng Lteness: Greey Algorthm Greey lgorthm. Erlest elne frst. ort n os y elne so tht n t 0 for = to n Assgn o to ntervl [t, t + t ] s t, f t + t t t + t output ntervls [s, f ] mx lteness = = 6 = 8 3 = 9 4 = 9 5 = 4 6 = 5 4 6 8 4 0 3 5 7 9 0 3 5 Mnmzng Lteness: No Ile Tme Oservton. There exsts n optml sheule wth no le tme. = 4 = 6 0 3 4 5 6 = 7 8 9 0 = 4 = 6 = 6 7 0 3 4 5 8 9 0 Oservton. The greey sheule hs no le tme. Mnmzng Lteness: Inversons Def. An nverson n sheule s pr of os n suh tht: < ut sheule efore. nverson efore swp Oservton. Greey sheule hs no nversons. Oservton. If sheule (wth no le tme) hs n nverson, t hs one wth pr of nverte os sheule onseutvely. 8
Mnmzng Lteness: Inversons Def. An nverson n sheule s pr of os n suh tht: < ut sheule efore. nverson f efore swp fter swp f' Clm. wppng two ent, nverte os reues the numer of nversons y one n oes not nrese the mx lteness. Pf. Let e the lteness efore the swp, n let ' e t fterwrs. ' k = k for ll k, ' If o s lte: = f (efnton) = f ( fnshes t tme f ) f ( ) (efnton) Mnmzng Lteness: Anlyss of Greey Algorthm Theorem. Greey sheule s optml. Pf. Defne * to e n optml sheule tht hs the fewest numer of nversons, n let's see wht hppens. Cn ssume * hs no le tme. If * hs no nversons, then = *. If * hs n nverson, let - e n ent nverson. swppng n oes not nrese the mxmum lteness n strtly ereses the numer of nversons ths ontrts efnton of * Greey Anlyss trteges Greey lgorthm stys he. how tht fter eh step of the greey lgorthm, ts soluton s t lest s goo s ny other lgorthm's. Exhnge rgument. Grully trnsform ny soluton to the one foun y the greey lgorthm wthout hurtng ts qulty. truturl. Dsover smple "struturl" oun ssertng tht every possle soluton must hve ertn vlue. Then show tht your lgorthm lwys heves ths oun. 9
4.3 Optml Chng 8 Optml Offlne Chng Chng. Che wth pty to store k tems. equene of m tem requests,,, m. Che ht: tem lrey n he when requeste. Che mss: tem not lrey n he when requeste: must rng requeste tem nto he, n evt some exstng tem, f full. Gol. Evton sheule tht mnmzes numer of he msses. Ex: k =, ntl he =, requests:,,,,,,,. Optml evton sheule: he msses. requests he Optml Offlne Chng: Frthest-In-Future Frthest-n-future. Evt tem n the he tht s not requeste untl frthest n the future. urrent he: e f future queres: g e e f e f g h... he mss eet ths one Theorem. [Belly, 960s] FF s optml evton sheule. Pf. Algorthm n theorem re ntutve; proof s sutle. 0
Reue Evton heules Def. A reue sheule s sheule tht only nserts n tem nto the he n step n whh tht tem s requeste. Intuton. Cn trnsform n unreue sheule nto reue one wth no more he msses. x x n unreue sheule reue sheule Reue Evton heules Clm. Gven ny unreue sheule, n trnsform t nto reue sheule ' wth no more he msses. oesn't enter he t requeste tme Pf. (y nuton on numer of unreue tems) uppose rngs nto the he t tme t, wthout request. Let e the tem evts when t rngs nto the he. Cse : evte t tme t', efore next request for. Cse : requeste t tme t' efore s evte. ' ' t t t t t' t' t' t' e evte t tme t', efore next request e requeste t tme t' Cse Cse Frthest-In-Future: Anlyss Theorem. FF s optml evton lgorthm. Pf. (y nuton on numer or requests ) Invrnt: There exsts n optml reue sheule tht mkes the sme evton sheule s FF through the frst + requests. Let e reue sheule tht stsfes nvrnt through requests. We proue ' tht stsfes nvrnt fter + requests. Conser (+) st request = +. ne n FF hve gree up untl now, they hve the sme he ontents efore request +. Cse : ( s lrey n the he). ' = stsfes nvrnt. Cse : ( s not n the he n n FF evt the sme element). ' = stsfes nvrnt.
Frthest-In-Future: Anlyss Pf. (ontnue) Cse 3: ( s not n the he; FF evts e; evts f e). egn onstruton of ' from y evtng e nste of f sme e f sme e f ' + sme e sme f now ' grees wth FF on frst + requests; we show tht hvng element f n he s no worse thn hvng element e ' Frthest-In-Future: Anlyss Let ' e the frst tme fter + tht n ' tke fferent ton, n let g e tem requeste t tme '. must nvolve e or f (or oth) ' sme e sme f ' Cse 3: g = e. Cn't hppen wth Frthest-In-Future sne there must e request for f efore e. Cse 3: g = f. Element f n't e n he of, so let e' e the element tht evts. f e' = e, ' esses f from he; now n ' hve sme he f e' e, ' evts e' n rngs e nto the he; now n ' hve the sme he Note: ' s no longer reue, ut n e trnsforme nto reue sheule tht grees wth FF through step + Frthest-In-Future: Anlyss Let ' e the frst tme fter + tht n ' tke fferent ton, n let g e tem requeste t tme '. must nvolve e or f (or oth) ' sme e sme f ' otherwse ' woul tke the sme ton Cse 3: g e, f. must evt e. Mke ' evt f; now n ' hve the sme he. ' sme g sme g '
Chng Perspetve Onlne vs. offlne lgorthms. Offlne: full sequene of requests s known pror. Onlne (relty): requests re not known n vne. Chng s mong most funmentl onlne prolems n C. LIFO. Evt pge rought n most reently. LRU. Evt pge whose most reent ess ws erlest. FF wth reton of tme reverse! Theorem. FF s optml offlne evton lgorthm. Proves ss for unerstnng n nlyzng onlne lgorthms. LRU s k-ompettve. [eton 3.8] LIFO s rtrrly. 3