CS473-Algorthms I Lecture 3 Solvg Recurreces Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet
Solvg Recurreces The lyss of merge sort Lecture requred us to solve recurrece. Recurreces re lke solvg tegrls, dfferetl equtos, etc. Ler few trcks. Lecture 4 : Applctos of recurreces. Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 2
Recurreces Fucto expressed recursvely T T / 2 f = f > Solve for = 2 k Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 3
Recurreces Clmed swer: T = lg+ = lg Susttute clmed swer for T the recurrece Note: resultg equtos re true whe = 2 k.e. lg lg Tedous techclty: hve t show T = lg / 2 But, sce T s mootoclly o-decresg fucto of lg 2 2 lg T T lg 2 lg f =2 0 = f =2 k > lg lg Thus, celg dd t mtter much Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 4
Recurreces Techclly, should e creful out floors d celgs s the ook But, usully t s oky To gore floor/celg Just solve for exct powers of 2 or Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 5
Boudry Codtos Usully ssume T= Θ for smll Does ot usully ffect sol. f polyomlly ouded Exmple: Itl codto ffects sol. Expoetl T=T / 2 2 E.g., If T= c for costt c > 0, the T2 = T 2 =c 2, T4= T2 2 =c 4, T = Θc T 2 T 2 However 2 3 T 3 T 3 Dfferece sol. s more drmtc wth T T Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 6
Susttuto Method The most geerl method:. Guess the form of the soluto. 2. Verfy y ducto. 3. Solve for costts. Exmple: T = 4T/2 + [Assume tht T = Θ.] Guess O 3. Prove O d Ω seprtely. Assume tht Tk ck 3 for k <. Prove T c 3 y ducto. Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 7
Exmple of Susttuto T = 4T/2 + 4c /2 3 + = c/2 3 + = c 3 c/2 3 - desred resdul c 3 wheever c/2 3 0, for exmple, f c 2 d resdul Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 8
Exmple Cotued We must lso hdle the tl codtos, tht s, groud the ducto wth se cses. Bse: T = Θ for ll < 0, where 0 s sutle costt. For < 0, we hve Θ c 3, f we pck c g eough. Ths oud s ot tght! Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 9
A Tghter Upper Boud? We shll prove tht T = O 2 Assume tht Tk ck 2 for k < : T = 4T/2 + c 2 + = O 2 Wrog! We must prove the I.H. = c 2 - - c 2 for o choce of c > 0. Lose! Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 0
A Tghter Upper Boud! IDEA: Stregthe the ductve hypothess. Sutrct low-order term. Iductve hypothess: Tk c k 2 c 2 k for k < T = 4T/2 + 4 c /2 2 - c 2 /2 + = c 2-2 c 2 + = c 2 - c 2 c 2 - c 2 - c 2 f c 2 > Pck c g eough to hdle the tl codtos Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet
Recurso-Tree Method A recurso tree models the costs tme of recursve executo of lgorthm. The recurso tree method s good for geertg guesses for the susttuto method. The recurso-tree method c e urelle. The recurso-tree method promotes tuto, however. Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 2
Exmple of Recurso Tree Solve T = T/4 + T/2 + 2 : Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 3
Exmple of Recurso Tree Solve T = T/4 + T/2 + 2 : T Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 4
Exmple of Recurso Tree Solve T = T/4 + T/2 + 2 : 2 T/4 T/2 Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 5
Exmple of Recurso Tree Solve T = T/4 + T/2 + 2 : 2 /4 2 /2 2 T/6 T/8 T/8 T/4 Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 6
Exmple of Recurso Tree Solve T = T/4 + T/2 + 2 : 2 /4 2 /2 2 /6 2 /8 2 /8 2 /4 2 Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 7
Exmple of Recurso Tree Solve T = T/4 + T/2 + 2 : 2 2 /4 2 /2 2 /6 2 /8 2 /8 2 /4 2 Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 8
Exmple of Recurso Tree Solve T = T/4 + T/2 + 2 : 2 2 /4 2 /2 2 5/6 2 /6 2 /8 2 /8 2 /4 2 Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 9
Exmple of Recurso Tree Solve T = T/4 + T/2 + 2 : 2 2 /4 2 /2 2 /6 2 /8 2 /8 2 /4 2 5/6 2 25/256 2 Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 20
Exmple of Recurso Tree Solve T = T/4 + T/2 + 2 : 2 2 /4 2 /2 2 /6 2 /8 2 /8 2 /4 2 5/6 2 25/256 2 Totl = 2 + 5/6 + 5/6 2 + 5/6 2 +... = 2 geometrc seres Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 2
The Mster Method The mster method pples to recurreces of the form T = T/ + f, where, >, d f s symptotclly postve. Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 22
Three Commo Cses Compre f wth :. f = O for some costt ε > 0. f grows polyomly slower th y ε fctor. Soluto: T = Θ. 2. f = Θ lg k for some costt k 0. f d grow t smlr rtes. Soluto: T = Θ lg k+. Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 23
Three Commo Cses Compre f wth : 3. f = for some costt ε > 0. f grows polyomlly fster th y ε fctor. d f stsfes the regulrty codto tht f / c f for some costt c < Soluto: T = Θ f. Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 24
Exmples Ex: T = 4T/2 + =4, =2 = 2 ; f = CASE : f =O 2- ε for ε= T = Θ 2 Ex: T = 4T/2 + 2 =4, =2 = 2 ; f = 2 CASE 2: f = Θ 2 lg 0, tht s, k=0 T = Θ 2 lg Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 25
Exmples Ex: T = 4T/2 + 3 =4, =2 = 2 ; f = 3. CASE 3: f = 2+ ε for ε= d 4 c /2 3 c 3 reg. cod. for c=/2. T = Θ 3 Ex: T = 4T/2 + 2 / lg =4, =2 = 2 ; f = 2 / lg Mster method does ot pply. I prtculr, for every costt ε > 0, we hve ε = lg Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 26
Geerl Method Akr-Bzz T Let p e the uque soluto to k k The, the swers re the sme s for the T mster method, ut wth p sted of Akr d Bzz lso prove eve more geerl result. / / p f Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 27
Ide of Mster Theorem Recurso tree: f f / f / f / h= f / 2 f / 2 f / 2 f f / 2 f / 2 T #leves = h = = T Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 28
Ide of Mster Theorem Recurso tree: f f / f / f / h= f / 2 f / 2 f / 2 f f / 2 f / 2 T CASE : The weght creses geometrclly from the root to the leves. The leves hold costt T frcto of the totl weght. Θ Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 29
Ide of Mster Theorem Recurso tree: f f / f / f / h= f / 2 f / 2 f / 2 f f / 2 f / 2 T CASE 2 : k = 0 The weght s pproxmtely the sme o ech of the levels. Θ T lg Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 30
Ide of Mster Theorem Recurso tree: f f / f / f / h= f / 2 f / 2 f / 2 f f / 2 f / 2 T CASE 3 : The weght decreses geometrclly from the root to the leves. The root holds costt T frcto of the totl weght. Θ f Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 3
Proof of Mster Theorem: Cse d Cse 2 Recll from the recurso tree ote h = lg =tree heght T h f / 0 Lef cost No-lef cost = g Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 32
Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 33 Proof of Cse for some > 0 f O f O f f 0 0 / / h h O O g 0 / h O
Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 34 = A cresg geometrc seres sce > 0 0 0 h h h O h h Cse cot
Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 35 O O O O g O g T Cse cot O Q.E.D.
Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 36 Proof of Cse 2 lmted to k=0 0 0 0 h h h f f f 0 / lg 0 / h g lg T lg lg 0 Q.E.D.
Cocluso Next tme: pplyg the mster method. Cevdet Aykt - Blket Uversty Computer Egeerg Deprtmet 37