solving recurrences
play

Solving Recurrences Debdeep Mukhopadhyay IIT Madras Recurrence - PowerPoint PPT Presentation

Solving Recurrences Debdeep Mukhopadhyay IIT Madras Recurrence Relations A recurrence relation (R.R., or just recurrence ) for a sequence { a n } is an equation that expresses a n in terms of one or more previous elements a 0 , , a n


  1. Solving Recurrences Debdeep Mukhopadhyay IIT Madras

  2. Recurrence Relations • A recurrence relation (R.R., or just recurrence ) for a sequence { a n } is an equation that expresses a n in terms of one or more previous elements a 0 , …, a n − 1 of the sequence, for all n ≥ n 0 . – I.e. , just a recursive definition, without the base cases. • A particular sequence (described non- recursively) is said to solve the given recurrence relation if it is consistent with the definition of the recurrence. – A given recurrence relation may have many solutions.

  3. Example • Consider the recurrence relation a n = 2 a n − 1 − a n − 2 ( n ≥ 2). • Which of the following are solutions? a n = 3 n a n = 2 n a n = 5

  4. Further Examples • Recurrence relation for growth of a bank account with P % interest per given period: M n = M n − 1 + ( P /100) M n − 1 • Growth of a population in which each pair of rabbit yield 1 new one every year after 2 years of their birth. P n = P n − 1 + P n − 2 (Rabbits and Fibonacci relation)

  5. Solving Compound Interest RR • M n = M n − 1 + ( P /100) M n − 1 = (1 + P /100) M n − 1 = r M n − 1 (let r = 1 + P /100) = r ( r M n − 2 ) = r · r ·( r M n − 3 ) …and so on to… = r n M 0

  6. Rabbits on an Island (assuming rabbits are immortal) Year Reproducing Young Total pairs pairs pairs 1 0 1 1 2 0 1 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 Pn = Pn − 1 + Pn − 2

  7. Further Explanation 0 1 1 1 2 0 2 3 1 0 3 5 1 0 0 4 2 5 0 8 1 1 3 2 0 0 Reproducing Young Pairs Pairs

  8. Tower of Hanoi Example • Problem: Get all disks from peg 1 to peg 2. – Only move 1 disk at a time. – Never set a larger disk on a smaller one. Question: Compute the number of steps H n Peg #1 Peg #2 Peg #3

  9. Intuition H 1 =1 is evident So, H 2 =3

  10. For n=3, H 3 =7 1 step 3 3 steps steps

  11. Hanoi Recurrence Relation • Let H n = # moves for a stack of n disks. • Optimal strategy: – Move top n − 1 disks to spare peg. ( H n − 1 moves) – Move bottom disk. (1 move) – Move top n − 1 to bottom disk. ( H n − 1 moves) • Note: H n = 2 H n − 1 + 1

  12. Solving Tower of Hanoi RR H n = 2 H n − 1 + 1 = 2 (2 H n − 2 + 1) + 1 = 2 2 H n − 2 + 2 + 1 = 2 2 (2 H n − 3 + 1) + 2 + 1 = 2 3 H n − 3 + 2 2 + 2 + 1 … = 2 n − 1 H 1 + 2 n − 2 + … + 2 + 1 = 2 n − 1 + 2 n − 2 + … + 2 + 1 (since H 1 = 1) − n 1 ∑ = i 2 = i 0 = 2 n − 1

  13. Another R.R. Example • Find a R.R. & initial conditions for the number of bit strings of length n without two consecutive 0s. Assume n ≥ 3. • We can solve this by breaking down the strings to be counted into cases that end in 0 and in 1. – For each ending in 0, the previous bit must be 1, and before that comes any qualifying string of length n − 2. – For each string ending in 1, it starts with a qualifying string of length n − 1. • Thus, a n = a n − 1 + a n − 2 . (Fibonacci recurrence.)

  14. Yet another R.R. example… • Give a recurrence (and base cases) for the number of n -digit decimal strings containing an even number of 0 digits. • Can break down into the following cases: – Any valid string of length n − 1 digits, with any digit 1-9 appended. – Any invalid string of length n − 1 digits, + a 0. • a n = 9 a n − 1 + (10 n − 1 − a n − 1 ) = 8 a n − 1 + 10 n − 1 . – Base cases: a 0 = 1 ( ε ), a 1 = 9 (1-9).

  15. Catalan Numbers • Find a R.R for the number of ways we can parenthesize the product of n+1 numbers, x 0 , x 1 , …,x n to specify the order of multiplication. Call it C n . • Define C 0 =C 1 =1 (its important to have proper base cases) • If n=2, (x 0 .x 1 ).x 2 ,x 0 .(x 1 .x 2 )=>C 2 =2 – Note that C 2 =C 1 C 0 +C 0 C 1 =1+1=2 • If n=3, ((x 0 .x 1 ).x 2 ).x 3 ; (x 0 .x 1 ).(x 2 .x 3 ); (x 0 .(x 1 .x 2 )).x 3 ; x 0 .((x 1 .x 2 ).x 3 ) ; x 0 .(x 1 .(x 2 .x 3 )) => C 3 =5 – Note that C 3 =C 2 C 0 + C 1 C 1 +C 0 C 2 =2+1+2=5

  16. Catalan Numbers • The final “.” operator is outside the scope of any parenthesis. • The final . can be between any x k and x k+1 out of the n+1 numbers. • How many ways can we have parenthesis as follows: – [x 0 , x 1 , …,x k ] .[ x k+1 , x k+2 , …,x n ] C k C n-k-1 – The “.” can occur in after any x k , where k ranges from 0 to n-1 – So, the total number of possible parenthesis is: − n 1 ∑ C C Exact form of C n − − k n k 1 can be computed using = Generating functions i 0

  17. Solving Recurrences • A linear homogeneous recurrence of degree k with constant coefficients (“ k- LiHoReCoCo”) is a recurrence of the form a n = c 1 a n − 1 + … + c k a n − k , where the c i are all real, and c k ≠ 0. • The solution is uniquely determined if k initial conditions a 0 … a k − 1 are provided. This follows from the second principle of Mathematical Induction.

  18. Examples • f n =f n-1 +f n-2 is a k-LiHoReCoCo • h n =2h n-1 + 1 is not Homogenous 2 is not linear • a n =a n-1 + a n-2 • b n =nb n-1 does not have a constant co-efficient

  19. Solving LiHoReCoCos • The basic idea: Look for solutions of the form a n = r n , where r is a constant not zero (r=0 is trivial) • This requires the characteristic equation : r n = c 1 r n − 1 + … + c k r n − k , i.e. , (rearrange & × by r k − n ) r k − c 1 r k − 1 − … − c k = 0 • The solutions ( characteristic roots ) can yield an explicit formula for the sequence.

  20. Solving 2-LiHoReCoCos • Consider an arbitrary 2-LiHoReCoCo: a n = c 1 a n − 1 + c 2 a n − 2 • It has the characteristic equation (C.E.): r 2 − c 1 r − c 2 = 0 • Theorem 1: If the CE has 2 roots r 1 ≠ r 2 , then {a n } is a solution to the RR n + α 2 r 2 n for n ≥ 0 iff a n = α 1 r 1 for constants α 1 , α 2 .

  21. Example • Solve the recurrence a n = a n − 1 + 2 a n − 2 given the initial conditions a 0 = 2, a 1 = 7. • Solution: Use theorem 1: – c 1 = 1, c 2 = 2 – Characteristic equation: r 2 − r − 2 = 0 – Solutions: r = [ − ( − 1) ± (( − 1) 2 − 4·1·( − 2)) 1/2 ] / 2·1 (Using the = (1 ± 9 1/2 )/2 = (1 ± 3)/2, so r = 2 or r = − 1. quadratic – So a n = α 1 2 n + α 2 ( − 1) n . formula here.) + + = ⇔ 2 ax bx c 0 − ± − 2 b b 4 ac = x 2 a

  22. Example Continued… • To find α 1 and α 2 , solve the equations for the initial conditions a 0 and a 1 : a 0 = 2 = α 1 2 0 + α 2 ( − 1) 0 a 1 = 7 = α 1 2 1 + α 2 ( − 1) 1 Simplifying, we have the pair of equations: 2 = α 1 + α 2 7 = 2 α 1 − α 2 which we can solve easily by substitution: α 2 = 2 − α 1 ; 7 = 2 α 1 − (2 − α 1 ) = 3 α 1 − 2; 9 = 3 α 1 ; α 1 = 3; α 2 = 1. a n = 3·2 n − ( − 1) n • Final answer: Check: { a n ≥ 0 } = 2, 7, 11, 25, 47, 97 …

  23. Proof of Theorem 1 n is always a • Proof that a n = α 1 r 1 n + α 2 r 2 solution: – We know r 12 = c 1 r 1 + c 2 and r 22 = c 1 r 2 + c 2 . – Now we can show the proposed sequence satisfies the recurrence a n = c 1 a n − 1 + c 2 a n − 2 : c 1 a n − 1 + c 2 a n − 2 = c 1 ( α 1 r 1 n − 1 + α 2 r 2 n − 1 ) + c 2 ( α 1 r 1 n − 2 + α 2 r 2 n − 2 ) = α 1 r 1 n − 2 ( c 1 r 1 + c 2 ) + α 2 r 2 n − 2 ( c 1 r 2 + c 2 ) 2 + α 2 r 2 2 = α 1 r 1 n + α 2 r 2 n = a n . □ = α 1 r 1 n − 2 r 1 n − 2 r 2 This shows that if a n = α 1 r 1n + α 2 r 2n , then {a n } is a solution to the R.R.

  24. The remaining part of the proof • If {a n } is a solution of R.R. then, a n = α 1 r 1 n + α 2 r 2 n , for n=0,1,2,… • Can complete proof by showing that for any initial conditions, we can find corresponding α ’s – a 0 =C 0 = α 1 + α 2 – a 1 =C 1 = α 1 r 1 + α 2 r 2 – α 1 =(C 1 -C 0 r 2 )/(r 1 -r 2 ); α 2 =(C 0 r 1 -C 1 )/(r 1 -r 2 ) – But it turns out this is a solution only if r 1 ≠ r 2 . So the roots have to be distinct. – The recurrence relation and the initial conditions determine the sequence uniquely. It follows that a n = α 1 r 1 n + α 2 r 2 n (as we have already shown that this is a soln.)

  25. The Case of Degenerate Roots • Now, what if the C.E. r 2 − c 1 r − c 2 = 0 has only 1 root r 0 ? • Theorem 2: Then, a n = ( α 1 + α 2 n)r 0 n , for all n ≥ 0, for constants α 1 , α 2 .

  26. Example • Solve: a n =6a n-1 -9a n-2 with a 0 =1,a 2 =6 • CE is r 2 -6r+9=0 => r 0 =3 • So, the general form of the soln is: – a n =( α 1 + α 2 n)3 n – Solve the rest using the initial conditions

  27. k -LiHoReCoCos k ∑ = a c a • Consider a k -LiHoReCoCo: − n i n i = i 1 • It’s C.E. is: k − ∑ − = k k i r c r 0 i = i 1 • Thm.3: If this has k distinct roots r i , then the solutions to the recurrence are of the form: k ∑ = α n a r n i i = i 1 for all n ≥ 0, where the α i are constants.

  28. Example • Solve: a n =6a n-1 -11a n-2 +6a n-3 , with initial conditions a 0 =2, a 1 =5 and a 2 =15. CE is r 3 -6r 2 +11r-6=0 => (r-1)(r-2)(r-3)=0 Thus the soln is: a n =( α 1 1 n + α 2 2 n + α 3 3 n ) Solve the rest.

  29. Degenerate k -LiHoReCoCos • Suppose there are t roots r 1 ,…, r t with multiplicities m 1 ,…, m t . Then: ⎛ ⎞ − m 1 t i ∑ ∑ ⎜ ⎟ = α j n a n r ⎜ ⎟ n i , j i ⎝ ⎠ = = i 1 j 0 for all n ≥ 0, where all the α are constants.

Recommend


More recommend