Aalyzg Cotrol Strutures sequeg P, P : two fragmets of a algo. t, t : the tme they tae the tme requred to ompute P ;P s t t Θmaxt,t For loops for to m do P t: the tme requred to ompute P total tme requred by the loop s smply mt? Cosder loop otrol? whle m do P
Aalyss of For Loops Whe the tme t requred for P vares as a futo of the tme taes s Ex: Fboa sequee f the strutos sde the for loop tae ostat tme, what does the algorthm tae? Is t reasoable to out the addtos at ut ost? m t futo Fboa ; j for to do j j j - retur j
Example: Computg Fboa Sequee It s ot reasoable to out the addtos at ut ost uless s vary small assume the tme eeded s the exat order of the umber of fgures the operads the values of ad j at the ed of -th terato are f - ad f, respetvely Movre s: the sze of f s Θ the -th terato taes Θ- Θ Θ the tme tae by the algorthm O
Aalyss of Reursve Algorthms futo Fbre f < the retur else retur Fbre- Fbre- Let T be the tme tae by a all o Fbre f <, the algorthm taes some ostat tme a otherwse, t taes T- T- h a T T T h f or otherwse solvg ths reurree relato, we have T Θf, o matter h s bouded by a ostat or h Θ 4
Whle ad Repeat Loops futo Bary_SearhT[..], x ; j ; whle < j do j ; ase x < T[]: j - ; ase x T[]:, j {retur }; ase x > T[]: ; retur No obvous way to ow how may tmes the loop wll terate two approahes fd a futo of the varables volved whose value dereases eah tme aroud treat whle le reursve algorthms 5
6 Aalyss of Bary_Searh Fd a futo of the varables volved whose value dereases eah tme roud the loop osder d j - ; x < T[] x T[] x > T[] / / ˆ ˆ ˆ ; - ] [ ˆ ; ˆ d j j j d j j < / / ˆ ˆ ˆ ; ˆ ; ] [ ˆ d j j j j j d j j j s at least / ˆ d d d The value of d s at least halved eah tme roud the loop the loop s etered at most tmes lg lg log
The Alteratve Approah Th of the whle loop as f t were mplemeted reursvely let td deote the maxmum tme eeded to termate the whle loop whe there are at most d elemets : the tme to determe that the loop s fshed b: the tme requred to go roud the loop oe t d b t d f d otherwse d s at least halved eah tme roud the loop 7
Usg a Barometer Barometer struto exeuted at least as ofte as ay other struto the algorthm o harm f some strutos are exeuted up to a ostat umber of tmes more ofte tha barometer Whe a algorthm volves several ested loops ay struto of the er most loop a usually be used as barometer ths should be doe arefully 8
Usg a Barometer: A Example pgeo T[] T[] T[] T[] T[] pgeo hole 4 s U[] Pgeo-hole sortg for to s do whle U [] do T [] U [] U [] - hoose ay of the strutos the er loop as a barometer? ths taes a tme the exat order of? 9
Pgeo-Hole Sortg: The Corret Aalyss for to s do whle U [] do T [] U [] U [] - Let a be the tme eeded for U[] eah tme roud the loop Let b be the tme tae by oe struto exeuto the loop for a gve, t U[]a U[]b, d: ostat tme eeded to talze ad otrol the outer loop the omplete tme s d t a d s a b Θ s
Example: Seleto Sort proedure selett [..] for to - do mj ; mx T [] for j to do f T [j] < mx the mj j mx T [j] T [mj] T [] T [] mx For eah, the strutos the er loop are exeuted - tmes b O j
Example: Iserto Sort proedure sertt [..] for to do x T []; j - whle j > ad x < T[j] do T[j] T[j] j j - T [j ] x For a gve the whle test s performed tmes the worst ase the barometer test s performed / tmes, whh s Θ j
Average-Case Aalyss should assume a pror prob. dstrbuto of the staes Example: serto sort T [..]: subarray of T [..], the partal ra of T []: the posto t would oupy f subarray T [..] were sorted f all! permutatos of T[..] are equally lely, the partal ra of T[] s equally lely to tae ay value [,] let be the partal ra of T [] the test s performed - tmes the avg. um the test s performed total avg. Σ
Solvg Reurrees: Itellget Guesswor T T f otherwse Restrt to beg eve? Restrt to beg power of? T 5 4 9 8 65 6 665 T 4 4 4 5 5 4 4 5 4
5 Itellget Guesswor Se, t follows that lg T lg - lg T lg - T Θ lg What happes f s ot a power of? Se T s a o-dereasg futo T Θ lg / T L a b b a lg lg Q
6 Homogeeous Reurrees a t a t - a t - lear homogeeous: equal to zero wth ostat oeffets f f - f - f - f - - f - ay lear ombato of solutos s tself a soluto f ad g satsfy a t a t - a t - ad set t f d g f a g a d g a g a g a d f a f a f a dg f a dg f a dg f a t a t a t a L L L L
The Charaterst Polyomal tellget guesswor suggests loog for solutos as t x a x a x - a x - a x a x - a the haraterst polyomal px a x a x - a p x x f x r s a soluto to the haraterst equato r haraterst equato r s a soluto to the reurree a lear t r ombatos r : roots of px satsfes the reurree for ay hoe of ostats,,, 7
8 Example: Fboa f - f - - f - haraterst polyomal x - x - roots geeral soluto f r r determe ad otherwse or f f f f 5 ad 5 r r 5 ad 5 r r f f
Example 4.7.. t - t - - 4t - haraterst polyomal x - x -4 x x - 4 roots t 5 t 4t r - ad r 4 geeral soluto t - 4 f f otherwse determe ad 4 5 t 4 ad 9
Whe Roots Are Not All Dstt a t a t - a t - has oly solutos of the form provded all r are dstt whe the roots are ot all dstt ths s o loger the most geeral soluto f the t r m m p x x r x r L x rl t l m j j r j m l
Example 4.7.. t 5t t - 5t - 8t - - 4t - haraterst polyomal x - 5x 8x - 4 x - x - roots - 8t 4t r, m, r, m geeral soluto t f,, or otherwse determe,, ad 4 8 -,, -/
Ihomogeeous Reurree The lear ombato s o loger zero Example 4.7.4: t - t - frst step: redue to the homogeeous ase multply by : t - 6t - replae by - t - - 6t - substrate from t - 5t - 6t - d step: get the geeral form x - 5x 6 x - x - t
Ihomogeeous Reurrees ot. rd step: determe ostats some equatos are obtaed from the tal odto some equatos are obtaed from the reurree tself o eed to solve all ostats substtute to >, therefore t Θ ad t t t t t t t
Example 4.7.5 t - t - 5 replae by - ad multply by -6-6t - t - -6 4 - replae by - ad multply by 9 9t - - 8t - 9 - t - 8t - t - - 8t - haraterst polyomal x - 8x x - 8 x - x - geeral-form soluto t solve ostats t 4 9 8 t t -9 Θ t 8 4t 99 4
Smple Ihomogeeous Reurrees a t a t - a t - b p b s a ostat p s a polyomal of degree d the haraterst polyomal a x a x - a x - b d Example 4.7.4: t - t - x - x - Example 4.7.5: t - t - 5 x - x - Example 4.7.6: t t - x - x - Example 4.7.7: t t - x - x - 5
Geeralzed Ihomogeeous Reurrees a t a t - a t - b p b p b p b are dstt ostats p are polyomals respetvely of degree d the haraterst polyomal a x a x - a x - b d x - b d x - b d Example 4.7.9 t t otherwse haraterst polyomal x - x - x - f 6
Chage of Varables T: the term of a geeral reurree t : the term of the ew reurree Example 4.7.. T T / Let t T f f s a power of, > t T T - t - t - t - har. polyomal: x -x - t whe, T t lg ad thus T lg lg lg lg T O lg? depeds o 7
8 Example 4.7. T l T/b > T: N R, l, b, ad are tegers s a strtly postve real whe / s a exat power of b T l T/b > > Θ Θ < Θ l b l b l b l T b f f log f log > Ο Ο < Ο l b l b l b l T b f f log f log
Rage Trasformato / f T T / otherwse let t T t T T - t - let u lg t u lg t lg t - u - u - u - har. polyomal: x - x - u substtuteto - - -, - t u -- T t lg -lg- /4 by the talzato odto: -lg 9
Asymptot Reurrees Whe reurrees our the aalyss of algorthms, we usually have to deal wth T 4T f f s the exat order of tal odto uow T s postve for all geeral tehque: to sadwh T betwee two reurrees defe a Ta, b 4Ta, b b f f > T a,b a b - b Θ regardless of the values of a ad b
To aheve our goal prove the exstee of four postve real ostats r, s, u ad v suh that T r,s T T u,v Kow T a,a a T, T a,b T a,b whe a a ad b b let ad d be postve real ostats ad let be a teger suffetly large that f d ad T 4T f for all > hoose rmt/t,, smr,, umaxt/t, ad vmaxu,d T r T, T r,r T r,s T u T, T u,u T u,v