Sovig Recurrece Relatios Part 1. Homogeeous liear 2d degree relatios with costat coefficiets. Cosider the recurrece relatio ( ) T () + at ( 1) + bt ( 2) = 0 This is called a homogeeous liear 2d degree recurrece relatio with costat coefficiets: 2d degree because it gives T () i terms of T ( 1) ad T ( 2), liear ad costat coefficiets because of the form of the left side, ad homogeeous because of the zero o the right had side. The idea for solvig this relatio is to guess a solutio of the form T () = x for some umber x, ad the to simply substitute this expressio ito the equatio to determie the value(s) of x that work. Sice T ( 1) = x 1 ad T ( 2) = x 2 we get the equatio x + ax 1 + bx 2 = 0 Sice x is clearly ot zero, we ca divide by x 2 to get x 2 + ax + b = 0 which is called the characteristic equatio for the recurrece relatio ( ). Case 1: If this equatio factors as (x r 1 )(x r 2 ) = 0 with r 1 r 2 (so that the characteristic equatio has two distict roots), the the geeral solutio to ( ) is T () = c 1 (r 1 ) + c 2 (r 2 ) where c 1 ad c 2 are costats. This is called a geeral solutio because every fuctio T () that is a solutio to the relatio ( ) has this form. If we also have iitial coditios T (0) = t 0 ad T (1) = t 1 the we ca determie the values of c 1 ad c 2 ad thus get the exact formula for T (). We ll illustrate the process with a typical example. Example 1. Solve the recurrece relatio T () 4T ( 1) + 3T ( 2) = 0, T (0) = 0, T (1) = 2 Note: This could also be writte as 0 = 0 T () = 2 = 1 4T ( 1) 3T ( 2) > 1 Solutio: The characteristic equatio is x 2 4x + 3 = 0, or (x 3)(x 1) = 0, so the geeral solutio is T () = c 1 3 + c 2 1 = c 1 3 + c 2. To fid c 1 ad c 2 we plug i the iitial coditios to get two equatios i those two variables: 0 = T (0) = c 1 3 0 + c 2 = c 1 + c 2 2 = T (1) = c 1 3 1 + c 2 = 3c 1 + c 2
It s easy to solve these equatios for the solutio c 1 = 1, c 2 = 1, so the fial aswer is T () = 3 1 Check: We ca check our aswer quickly ad easily. The recurrece formula gives us T (2) = 4T (1) 3T (0) = 4(2) 0 = 8 T (3) = 4T (2) 3T (1) = 4(8) 3(2) = 26 T (4) = 4T (3) 3T (2) = 4(26) 3(8) = 80 It appears that the sequece is ideed givig us umbers that are oe less tha the powers of 3 (1, 3, 9, 27, 81,... ), so the formula T () = 3 1 seems to be correct. Case 2: If the characteristic equatio factors as (x r) 2 = 0 (with a sigle root), the we use as our geeral solutio the formula T () = c 1 r + c 2 r As before, iitial coditios ca be used to solve for c 1 ad c 2. Here is a typical example. Example 2. Solve the recurrece relatio 3 = 0 T () = 17 = 1 10T ( 1) 25T ( 2) > 1 Solutio: The characteristic equatio is x 2 10x + 25 = 0, or (x 5) 2 = 0, so the geeral solutio is T () = c 1 5 + c 2 5. As before, we fid c 1 ad c 2 by pluggig i the iitial coditios: 3 = T (0) = c 1 5 0 + c 2 (0) = c 1 17 = T (1) = c 1 5 1 + c 2 (1)5 1 = 5c 1 + 5c 2 The solutio here is c 1 = 3 ad c 2 = 2/5, so the exact solutio is T () = (3)5 + (2/5)5 = (15 + 2)5 1 Check: The recurrece formula gives us values T (2) = 10T (1) 25T (0) = 10(17) 25(3) = 95 T (3) = 10T (2) 25T (1) = 525 These are ideed the values give by our formula as well: T (2) = (15 + 4)5 1 = 95, ad T (3) = (15 + 6)5 2 = 525. Case 3: It ca happe i geeral recurrece relatios that the characteristic equatio x 2 + ax + b = 0 has o real roots, but istead has two complex umber roots. There is a method of solutio for such recurreces, but we will ot cocer ourselves with this case sice it does ot typically arise i recurreces that come from studyig recursive algorithms. 2
Part 2. No-homogeeous liear 2d degree relatios with costat coefficiets. Now cosider what happes whe the right side of equatio ( ) (from page 1) is ot zero. We get a equatio of the form ( ) T () + at ( 1) + bt ( 2) = f() We ll lear how to solve ( ) i the special case that f() =(polyomial i )r. Actually, this requires oly a slight modificatio of the method for the homogeeous case. We simply multiply the characteristic equatio by (x r) k+1 where k is the degree of the polyomial part of f(). The solutio method the proceeds as before. This is best illustrated by examples. Example 3. Solve the recurrece relatio { 1 = 0 T () = 3T ( 1) + 2 > 0 Note: This is actually a 1st degree relatio (sice T ( 2) does ot appear), but the same method applies. Solutio: If the ohomogeeous term 2 were ot preset, the characteristic equatio would be simply x 3 = 0. I the presece of the ohomogeeous term, however, we must multiply this by (x 2) 0+1 (I this case f() = 2 is a 0-degree polyomial [the costat 1] times a 2 term.) So, the characteristic equatio is actually (x 3)(x 2) = 0, so the geeral solutio is T () = c 1 3 + c 2 2. Note that sice the relatio is oly 1st degree, we oly have oe iitial coditio. Yet we ll eed two equatios to fid the costats c 1 ad c 2. We ca get a secod value to use by simply applyig the recurrece formula for = 1: T (1) = 3T (0) + 2 1 = 3(1) + 2 = 5. We ow proceed as usual: 1 = T (0) = c 1 3 0 + c 2 2 0 = c 1 + c 2 5 = T (1) = c 1 3 1 + c 2 2 1 = 3c 1 + 2c 2 These equatios have solutio c 1 = 3 ad c 2 = 2, so the exact solutio is T () = (3)3 (2)2 = 3 +1 2 +1 Check: The recurrece formula gives us values T (2) = 3T (1) + 2 2 = 3(5) + 4 = 19 T (3) = 3T (2) + 2 3 = 3(19) + 8 = 65 These are boure out by our solutio: T (2) = 3 3 2 3 = 27 8 = 19, ad T (3) = 3 4 2 4 = 81 16 = 65. 3
Example 4. Solve the recurrece relatio { + 1 = 0, 1 T () = 5T ( 1) 6T ( 2) + 3 2 > 1 Solutio: Here agai, the ohomogeeous term ivolves a zero-degree polyomial, so the modified characteristic equatio will be (x 2 5x + 6)(x 2) 0+1 = 0, or (x 3)(x 2) 2 = 0. The geeral solutio must ow ivolve three terms: a 3 term, a 2 term, ad (because the (x 2) factor appears twice i the characteristic equatio) a 2 term. Thus: T () = c 1 3 + c 2 2 + c 3 2. We ll eed three equatios to solve for the three costats, yet we have oly the two iitial coditios T (0) = 1 ad T (1) = 2 to use. As i the last example, we ca geerate oe more value by usig the recurrece formula: T (2) = 5T (1) 6T (0) + 3 2 2 = 5(2) 6(1) + 12 = 16. This gives us the three equatios 1 = T (0) = c 1 3 0 + c 2 2 0 + c 3 (0) = c 1 + c 2 2 = T (1) = c 1 3 1 + c 2 2 1 + c 3 (1)2 1 = 3c 1 + 2c 2 + 2c 3 16 = T (2) = c 1 3 2 + c 2 2 2 + c 3 (2)2 2 = 9c 1 + 4c 2 + 8c 3 A little effort gives the solutio c 1 = 12, c 2 = 11, ad c 3 = 6. The exact solutio is the T () = 12 3 11 2 62 Note that we could have safely cocluded that T () is i Θ(3 ) simply from its geeral solutio (without eve solvig for c 1, c 2, ad c 3 ). Check: I additio to T (2) = 16, the recurrece formula gives us T (3) = 5T (2) 6T (1) + 3 2 3 = 5(16) 6(2) + 24 = 92 Checkig these agaist our solutio, we fid T (2) = 12 3 2 11 2 2 6(2)2 2 = 108 44 48 = 16 ad T (3) = 12 3 3 11 2 3 6(3)2 3 = 324 88 144 = 92. Example 5. Predict the big-theta behavior of a fuctio T () satisfyig the recurrece relatio T () = 7T ( 1) 10T ( 2) + (2 + 5)3. Solutio: The modified characteristic equatio is (x 2 7x + 10)(x 3) 1+1 = 0, or factorig, (x 2)(x 5)(x 3) 2 = 0. The geeral solutio is thus T () = c 1 2 + c 2 5 + c 3 3 + c 4 3. This will clearly be i Θ(5 ). Part 3. Chage of variable techique for decrease-by-costat-factor recurrece relatios. May recursive algorithms (such as biary search or merge sort ) work by dividig the iput i half ad callig itself o the ow-half-sized iput. Aalyzig the efficiecy of such algorithms leads to recurrece relatios that give T () i terms of T ( 2 ) istead of i terms of T ( 1). These decrease by a costat-factor recurrece relatios ca be coverted ito stadard liear recurrece relatios by applyig a simple chage of variable. Let s say we have a recurrece relatio of the form T () = at ( b ) + f() 4
where b is some positive iteger (usually b = 2). We will defie a ew fuctio S(k) by the rule S(k) = T (b k ). Our recurrece relatio the gives S(k) = T (b k ) = at ( ) + f(b k ) b k b = at (b k 1 ) + f(b k ) = as(k 1) + f(b k ) which is a first-order liear recurrece relatio for S. This ca the be solved by the methods we ve already covered. Example 6. Solve the recurrece relatio { 2 = 1 T () = 3T ( 2 ) + log2 () > 1 Solutio: Usig the substitutio S(k) = T (2 k ) this recurrece formula becomes S(k) = T (2 k ) = 3T ( ) + (2 k )log 2 (2 k ) 2 k 2 = 3T (2 k 1 ) + (2 k )k = 3S(k 1) + k2 k The characteristic equatio of this relatio is (x 3)(x 2) 1+1 = 0, so its geeral solutio is S(k) = c 1 3 k + c 2 2 k + c 3 k2 k. What about iitial coditios for S? We ca use the recurrece formula for T to get T (1) = 2, T (2) = 3T (1) + 2 = 8, ad T (4) = 3T (2) + 8 = 32. So, S(0) = T (2 0 ) = T (1) = 2, S(1) = T (2 1 ) = T (2) = 8, ad S(2) = T (2 2 ) = T (4) = 32. We ca ow use these values to get equatios for c 1, c 2, ad c 3 : 2 = S(0) = c 1 3 0 + c 2 2 0 + 0 8 = S(1) = c 1 3 1 + c 2 2 1 + c 3 (1)2 1 = 3c 1 + 2c 2 + 2c 3 32 = S(2) = c 1 3 2 + c 2 2 2 + c 3 (2)2 2 = 9c 1 + 4c 2 + 8c 3 Solvig this three-by-three system of equatios (by whatever method you prefer) leads to the solutio c 1 = 8, c 2 = 6, ad c 3 = 2. This gives the solutio to the recurrece relatio for S as S(k) = 8 3 k 6 2 k 2k2 k But we wat a formula for T, ot S. Rememberig that S(k) = T (2 k ) ad assumig is a power of 2, we ca say T () = S(log 2 ) = 8 3 log 2 6 2 log 2 2(log 2 )2 log 2 = 8 3 log 2 6 2log 2 valid for all values of that are powers of 2. Here we have used the familiar logarithm law that says 2 log 2 is equal to. What ca we do to simplify the term 3 log 2? This just requires a bit of logarithmic trickery: 3 log 2 = (2 log 2 3 ) log 2 = (2 log 2 ) log 2 3 = log 2 3 So, we ca rewrite our aswer as 5
T () = 8 log 2 3 6 2log 2 for = 1, 2, 4, 8,... Two otes about this solutio are i order: First, sice log 2 3 is clearly betwee 1 ad 2 (sice 3 is betwee 2 1 ad 2 2 ) we ca say that our solutio is i O( 2 ) (at least for the values = 1, 2, 4, 8,... ). Secod, we kow that our formula for T () is valid for powers of 2 that is, it correctly computes T (1), T (2), T (4), T (8), ad so o. What about the other values of? Thaks to somethig called the Smoothess Rule we ca at least say that our formula for T () has the correct big-theta behavior. See your text for the exact statemet, but the basic idea is this: Smoothess Rule: Suppose T () is i Θ(f()) for values of that are powers of a costat b 2. The if f() is a ice fuctio (here ice icludes the polyomials, 2, 3, etc. but ot expoetial or factorial fuctios) we ca say that T () really is i Θ(f()). Homework Problems Solve each of the followig recurrece relatios. 1 = 0 (A) T () = 4 = 1 8T ( 1) 15T ( 2) > 1 5 = 0 (B) T () = 9 = 1 6T ( 1) 9T ( 2) > 1 (C) T () = { 1 = 0 2T ( 1) + 3 > 0 1 = 0 (D) T () = 2 = 1 4T ( 1) 3T ( 2) + 1 > 1 Hit: The ohomogeeous term is 1. { 2 = 1 (E) T () = T ( 3 ) + 7 > 1 { 2 = 1 (F) T () = 5T ( 2 ) + 3 > 1 6