Chptr. Intrvl Shulng Gry Algorthms Sls y Kvn Wyn. Copyrght 005 Prson-Ason Wsly. All rghts rsrv. Intrvl Shulng Intrvl Shulng: Gry Algorthms Intrvl shulng. Jo strts t s n fnshs t f. Two os omptl f thy on't ovrlp. Gol: fn mxmum sust of mutully omptl os. Gry tmplt. Consr os n som orr. Tk h o prov t's omptl wth th ons lry tkn. [Erlst strt tm] Consr os n snng orr of strt tm s. f g h 0 3 5 6 7 8 9 0 Tm [Erlst fnsh tm] Consr os n snng orr of fnsh tm f. [Shortst ntrvl] Consr os n snng orr of ntrvl lngth f - s. [Fwst onflts] For h o, ount th numr of onfltng os. Shul n snng orr of onflts. 3 Copyrght 000, Kvn Wyn
Intrvl Shulng: Gry Algorthms Intrvl Shulng: Gry Algorthm Gry tmplt. Consr os n som orr. Tk h o prov t's omptl wth th ons lry tkn. Gry lgorthm. Consr os n nrsng orr of fnsh tm. Tk h o prov t's omptl wth th ons lry tkn. rks rlst strt tm rks shortst ntrvl rks fwst onflts Sort os y fnsh tms so tht f f... f n. os slt A φ for = to n { f (o omptl wth A) A A {} } rturn A Implmntton. O(n log n). Rmmr o * tht ws lst to A. Jo s omptl wth A f s f *. 5 6 Intrvl Shulng: Anlyss Intrvl Shulng: Anlyss Thorm. Gry lgorthm s optml. Thorm. Gry lgorthm s optml. Pf. (y ontrton) Assum gry s not optml, n lt's s wht hppns. Lt,,... k not st of os slt y gry. Lt,,... m not st of os n th optml soluton wth =, =,..., r = r for th lrgst possl vlu of r. Pf. (y ontrton) Assum gry s not optml, n lt's s wht hppns. Lt,,... k not st of os slt y gry. Lt,,... m not st of os n th optml soluton wth =, =,..., r = r for th lrgst possl vlu of r. o r+ fnshs for r+ o r+ fnshs for r+ Gry: r r+ Gry: r r+ OPT: r r+... OPT: r r+... why not rpl o r+ wth o r+? Proof y nuton tht for h r >= th gr lgorthm stys h of th optml lgorthm soluton stll fsl n optml, ut ontrts mxmlty of r. 7 8 Copyrght 000, Kvn Wyn
Intrvl Prttonng. Intrvl Prttonng Intrvl prttonng. Ltur strts t s n fnshs t f. Gol: fn mnmum numr of lssrooms to shul ll lturs so tht no two our t th sm tm n th sm room. Ex: Ths shul uss lssrooms to shul 0 lturs. g h f 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 :30 Tm 0 Intrvl Prttonng Intrvl Prttonng: Lowr Boun on Optml Soluton Intrvl prttonng. Ltur strts t s n fnshs t f. Gol: fn mnmum numr of lssrooms to shul ll lturs so tht no two our t th sm tm n th sm room. Ex: Ths shul uss only 3. Df. Th pth of st of opn ntrvls s th mxmum numr tht ontn ny gvn tm. Ky osrvton. Numr of lssrooms n pth. Ex: Dpth of shul low = 3 shul low s optml.,, ll ontn 9:30 Q. Dos thr lwys xst shul qul to pth of ntrvls? f f g g h h 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 :30 Tm 9 9:30 0 0:30 :30 :30 :30 :30 3 3:30 :30 Tm Copyrght 000, Kvn Wyn 3
Intrvl Prttonng: Gry Algorthm Intrvl Prttonng: Gry Anlyss Gry lgorthm. Consr lturs n nrsng orr of strt tm: ssgn ltur to ny omptl lssroom. Osrvton. Gry lgorthm nvr shuls two nomptl lturs n th sm lssroom. Sort ntrvls y strtng tm so tht s s... s n. 0 numr of llot lssrooms for = to n { f (ltur s omptl wth som lssroom k) shul ltur n lssroom k ls llot nw lssroom + shul ltur n lssroom + + } Thorm. Gry lgorthm s optml. Pf. Lt = numr of lssrooms tht th gry lgorthm llots. Clssroom s opn us w n to shul o, sy, tht s nomptl wth ll - othr lssrooms. Sn w sort y strt tm, ll ths nomptlts r us y lturs tht strt no ltr thn s. Thus, w hv lturs ovrlppng t tm s + ε. Ky osrvton ll shuls us lssrooms. Implmntton. O(n log n). For h lssroom k, mntn th fnsh tm of th lst o. Kp th lssrooms n prorty quu. 3 Shulng to Mnmzng Ltnss. Shulng to Mnmz Ltnss Mnmzng ltnss prolm. Sngl rsour prosss on o t tm. Jo rqurs t unts of prossng tm n s u t tm. If strts t tm s, t fnshs t tm f = s + t. Ltnss: = mx { 0, f - }. Gol: shul ll os to mnmz mxmum ltnss L = mx. Ex: t 3 3 5 3 6 6 8 9 9 5 ltnss = ltnss = 0 mx ltnss = 6 3 = 9 = 8 6 = 5 = 6 5 = = 9 0 3 5 6 7 8 9 0 3 5 6 Copyrght 000, Kvn Wyn
Mnmzng Ltnss: Gry Algorthms Mnmzng Ltnss: Gry Algorthms Gry tmplt. Consr os n som orr. Gry tmplt. Consr os n som orr. [Shortst prossng tm frst] Consr os n snng orr of prossng tm t. [Erlst ln frst] Consr os n snng orr of ln. [Shortst prossng tm frst] Consr os n snng orr of prossng tm t. t 00 0 0 ountrxmpl [Smllst slk] Consr os n snng orr of slk - t. [Smllst slk] Consr os n snng orr of slk - t. t 0 0 ountrxmpl 7 8 Mnmzng Ltnss: Gry Algorthm Mnmzng Ltnss: No Il Tm Gry lgorthm. Erlst ln frst. Osrvton. Thr xsts n optml shul wth no l tm. Sort n os y ln so tht n = = 6 0 3 5 6 = 7 8 9 0 t 0 for = to n Assgn o to ntrvl [t, t + t ] s t, f t + t t t + t output ntrvls [s, f ] = = 6 = 0 3 5 6 7 8 9 0 Osrvton. Th gry shul hs no l tm. mx ltnss = = 6 = 8 3 = 9 = 9 5 = 6 = 5 0 3 5 6 7 8 9 0 3 5 9 0 Copyrght 000, Kvn Wyn 5
Mnmzng Ltnss: Invrsons Mnmzng Ltnss: Invrsons Df. An nvrson n shul S s pr of os n suh tht: < ut shul for. nvrson Df. An nvrson n shul S s pr of os n suh tht: < ut shul for. nvrson f for swp for swp ftr swp Osrvton. Gry shul hs no nvrsons. Osrvton. If shul (wth no l tm) hs n nvrson, t hs on wth pr of nvrt os shul onsutvly. Clm. Swppng two nt, nvrt os rus th numr of nvrsons y on n os not nrs th mx ltnss. Pf. Lt th ltnss for th swp, n lt ' t ftrwrs. ' k = k for ll k, f' ' If o s lt: # = = f # f f (fnton) ( fnshs t tm f ) ( < ) (fnton) Mnmzng Ltnss: Anlyss of Gry Algorthm Gry Anlyss Strtgs Thorm. Gry shul S s optml. Pf. Dfn S* to n optml shul tht hs th fwst numr of nvrsons, n lt's s wht hppns. Cn ssum S* hs no l tm. If S* hs no nvrsons, thn S = S*. If S* hs n nvrson, lt - n nt nvrson. swppng n os not nrs th mxmum ltnss n strtly rss th numr of nvrsons ths ontrts fnton of S* Gry lgorthm stys h. Show tht ftr h stp of th gry lgorthm, ts soluton s t lst s goo s ny othr lgorthm's. Exhng rgumnt. Grully trnsform ny soluton to th on foun y th gry lgorthm wthout hurtng ts qulty. Struturl. Dsovr smpl "struturl" oun ssrtng tht vry possl soluton must hv rtn vlu. Thn show tht your lgorthm lwys hvs ths oun. 3 Copyrght 000, Kvn Wyn 6
Optml Offln Chng.3 Optml Chng Chng. Ch wth pty to stor k tms. Squn of m tm rqusts,,, m. Ch ht: tm lry n h whn rqust. Ch mss: tm not lry n h whn rqust: must rng rqust tm nto h, n vt som xstng tm, f full. Gol. Evton shul tht mnmzs numr of h msss. Ex: k =, ntl h =, rqusts:,,,,,,,. Optml vton shul: h msss. rqusts h 6 Optml Offln Chng: Frthst-In-Futur Ru Evton Shuls Frthst-n-futur. Evt tm n th h tht s not rqust untl frthst n th futur. Df. A ru shul s shul tht only nsrts n tm nto th h n stp n whh tht tm s rqust. urrnt h: f futur qurs: g f f g h... h mss t ths on Thorm. [Blly, 960s] FF s optml vton shul. Pf. Algorthm n thorm r ntutv; proof s sutl. Intuton. Cn trnsform n unru shul nto ru on wth no mor h msss. x x n unru shul ru shul 7 8 Copyrght 000, Kvn Wyn 7
Frthst-In-Futur: Anlyss Thorm. FF s optml vton lgorthm. Pf. (y nuton on numr or rqusts ). Shortst Pths n Grph Invrnt: Thr xsts n optml ru shul S tht mks th sm vton shul s S FF through th frst + rqusts. Lt S ru shul tht stsfs nvrnt through rqusts. W prou S' tht stsfs nvrnt ftr + rqusts. Consr (+) st rqust = +. Sn S n S FF hv gr up untl now, thy hv th sm h ontnts for rqust +. Cs : ( s lry n th h). S' = S stsfs nvrnt. Cs : ( s not n th h n S n S FF vt th sm lmnt). S' = S stsfs nvrnt. shortst pth from Prnton CS prtmnt to Enstn's hous 30 Shortst Pth Prolm Dkstr's Algorthm Shortst pth ntwork. Drt grph G = (V, E). Sour s, stnton t. Lngth = lngth of g. Shortst pth prolm: fn shortst rt pth from s to t. Dkstr's lgorthm. Mntn st of xplor nos S for whh w hv trmn th shortst pth stn (u) from s to u. Intlz S = { s }, (s) = 0. Rptly hoos unxplor no v whh mnmzs π ( v) = mn = ( u, v) : u S ( u) +, ost of pth = sum of g osts n pth v to S, n st (v) = π(v). shortst pth to som u n xplor prt, follow y sngl g (u, v) s 9 5 5 6 0 30 5 3 8 6 6 6 3 9 Cost of pth s--3-5-t = 9 + 3 + + 6 = 8. S s (u) u v 7 t 36 37 Copyrght 000, Kvn Wyn 8
Dkstr's Algorthm Dkstr's Algorthm: Proof of Corrtnss Dkstr's lgorthm. Mntn st of xplor nos S for whh w hv trmn th shortst pth stn (u) from s to u. Intlz S = { s }, (s) = 0. Rptly hoos unxplor no v whh mnmzs π ( v) = mn = ( u, v) : u S v to S, n st (v) = π(v). ( u) +, shortst pth to som u n xplor prt, follow y sngl g (u, v) Invrnt. For h no u S, (u) s th lngth of th shortst s-u pth. Pf. (y nuton on S ) Bs s: S = s trvl. Inutv hypothss: Assum tru for S = k. Lt v nxt no to S, n lt u-v th hosn g. Th shortst s-u pth plus (u, v) s n s-v pth of lngth π(v). Consr ny s-v pth P. W'll s tht t's no shortr thn π(v). Lt x-y th frst g n P tht lvs S, n lt P' th supth to x. P s lry too long s soon s t lvs S. P' x P y S s (u) u v (P) (P') + (x,y) (x) + (x, y) π(y) π(v) s S u v nonngtv wghts nutv hypothss fn of π(y) Dkstr hos v nst of y 38 39 Dkstr's Algorthm: Implmntton For h unxplor no, xpltly mntn π(v) = mn (u) +. = (u,v) : u S Nxt no to xplor = no wth mnmum π(v). Whn xplorng v, for h nnt g = (v, w), upt π(w) = mn { π(w), π(v)+ }. Extr Sls Effnt mplmntton. Mntn prorty quu of unxplor nos, prortz y π(v). Prorty Quu PQ Oprton Insrt ExtrtMn ChngKy Dkstr n n m Arry n n Bnry hp log n log n log n -wy Hp log n log n log n F hp log n IsEmpty n Totl n m log n m log m/n n m + n log n Invul ops r mortz ouns 0 Copyrght 000, Kvn Wyn 9
Con Chngng Con Chngng Gol. Gvn urrny nomntons:, 5, 0, 5, 00, vs mtho to py mount to ustomr usng fwst numr of ons. Ex: 3. Gr s goo. Gr s rght. Gr works. Gr lrfs, uts through, n pturs th ssn of th volutonry sprt. - Goron Gko (Mhl Dougls) Cshr's lgorthm. At h trton, on of th lrgst vlu tht os not tk us pst th mount to p. Ex: $.89. 3 Con-Chngng: Gry Algorthm Con-Chngng: Anlyss of Gry Algorthm Cshr's lgorthm. At h trton, on of th lrgst vlu tht os not tk us pst th mount to p. Sort ons nomntons y vlu: < < < n. ons slt S φ whl (x 0) { lt k lrgst ntgr suh tht k x f (k = 0) rturn "no soluton foun" x x - k S S {k} } rturn S Thorm. Gr s optml for U.S. ong:, 5, 0, 5, 00. Pf. (y nuton on x) Consr optml wy to hng k x < k+ : gry tks on k. W lm tht ny optml soluton must lso tk on k. f not, t ns nough ons of typ,, k- to up to x tl low nts no optml soluton n o ths Prolm rus to on-hngng x - k nts, whh, y nuton, s optmlly solv y gry lgorthm. k k All optml solutons must stsfy Mx vlu of ons,,, k- n ny OPT P - 5 N 3 0 N + D + 5 = 9 Q. Is shr's lgorthm optml? 5 5 00 Q 3 no lmt 0 + = 75 + = 99 5 Copyrght 000, Kvn Wyn 0
/7/ Con-Chngng: Anlyss of Gry Algorthm Osrvton. Gry lgorthm s su-optml for US postl nomntons:, 0,, 3, 70, 00, 350, 5, 500. Countrxmpl. 0. Gry: 00, 3,,,,,,. Optml: 70, 70. 6 Copyrght 000, Kvn Wyn