CSE 20 Lecture 12: Analysis of Homogeneous Linear Recursion CK Cheng May 5, 2011 1 3. Analysis • • • • 3.1 Introduction 3.2 Homogeneous Linear Recursion 3.3 Pigeonhole Principle 3.4 Inclusion-Exclusion Principle 2 3.1 Introduction Derive the bound of functions or recursions Estimate CPU time and memory allocation Eg. PageRank calculation Allocation of memory, CPU time, Resource optimization MRI imaging Real time? VLSI design Design automation flow to meet the deadline for tape out? Further Study Algorithm, Complexity 3 3.1 Introduction • Derive the bound of functions or recursions • Estimate CPU time and memory allocation • Example on Fibonacci Sequence: Estimate fn. – Index: 0 1 2 3 4 5 6 7 8 9 – fn: 0 1 1 2 3 4 5 8 13 21 34 1 1 5 n 1 1 5 n fn ( ) ( ) 2 2 5 5 1 1 5 0 1 1 5 0 f0 ( ) ( ) 0 2 2 5 5 1 1 5 1 1 5 f1 ( ) ( ) 1 2 2 5 5 4 Example: Fibonacci Sequence 0 1 2 3 4 5 6 7 8 9 0 1 1 2 3 5 8 13 21 34 1 1 5 n 1 1 5 n fn ( ) ( ) 2 2 5 5 1 (1.618n 0.618n ) 2.236 1 9 1 n f9 1.618 33.98 1.618 2.236 2.236 5 3.2 Homogeneous Linear Recursion • (1) Arithmetic Recursion a, a+d, a+2d, …, a+kd • (2) Geometric Recursion a, ar, ar2, …, ark • (3) Linear Recursion an= e1an-1+e2an-2+…+ekan-k+ f(n) 6 Linear Recursion and Homogeneous Linear Recursion • Linear Recursion: There are no powers or products of • Homogenous Linear Recursion: A linear recursion with f(n)=0. 7 Solving Linear Recursion Input: Formula an e1an1 e2 an2 ... ek ank and k initial values a0 , a1 ,..., ak 1 (1) characteristic k k 1 k 2 x e x e x ... ek polynomial: 1 2 (2)Find the root of the characteristic polynomial (r1 , r2 ,..., rk ) (assuming ri are distinct) an c r c2 r2 ... ck rk (3)Set (4)Determine ci from k initial values n 1 1 n n 8 Solving Linear Recursion Input: Formula an e1an1 e2 an2 ... ek ank and k initial values a0 , a1 ,..., ak 1 (1) characteristic polynomial: Rewrite the formula with n=k ak e1ak 1 e2 ak 2 ... ek a0 Replace ai with xi k k 1 k 2 x e1 x e2 x ... ek 9 Solving Linear Recursion Input: Formula an e1an1 e2 an2 ... ek ank and k initial values a0 , a1 ,..., ak 1 2. Find the root of the k k 1 k 2 x e x e x ... ek polynomial 1 2 Or, x k e1 x k 1 e2 x k 2 ... ek 0 (r1 , r2 ,..., rk ) 10 Solving Linear Recursion Input: Formula an e1an1 e2 an2 ... ek ank and k initial values a0 , a1 ,..., ak 1 3. Set (assuming that the roots are distinct.) an c r c2 r2 ... ck rk n 1 1 n 4. Determine ci from k initial values a0 c1 c2 ... ck a1 c1r1 c2 r2 ... ck rk ... ... k 1 1 1 ak 1 c r c2 r2 k 1 ... ck rk k 1 11 n Solving Linear Recursion Input: Formula an e1an1 e2 an2 ... ek ank and k initial values a0 , a1 ,..., ak 1 3. Set (when the roots are (r1 , r2 ,..., ru ) not distinct.) u an g i (ri ) i 1 wi gi (ri ) cij n r j 1 j 1 n i where ri is a root of multiplicity wi 12 Example on Fibonacci sequence Input: initial values a0=0 and a1=1; and recursion formula an=an-1+an-2. Rewrite recursion: an-an-1-an-2=0. 1. Characteristic polynomial: x2-x-1=0. 2. Roots of the polynomial: 1 5 r1 , 2 3. Set: 1 5 r2 2 an=c1r1n+c2r2n. 13 Example on Fibonacci sequence Input: initial values a0=0 and a1=1; and recursion formula an=an-1+an-2. 4. Determine ci from k initial values a0=c1r10+c2r20: c1+c2=0 a1=c1r11+c2r21: c1r1+c2r2=1, where 1 5 1 5 r1 , r2 2 2 Thus, we have 1 1 c1 , c2 5 5 14 Example 2 Given initial values a0=1 and a1=1; and recursion formula: an=an-1+2an-2 Rewrite recursion: an-an-1-2an-2=0 1. Characteristic polynomial: x2-x-2=0 2. Characteristic roots: r1=2 and r2= -1 3. We have an=c1r1n+c2r2n=c12n+c2(-1)n 4. We use two initial values for n=0 and n=1: a0=c1+c2 a1=c12+c2(-1) 15 Example 2 (cont) Two initial values a0=c1+c2: c1+c2=1 a1=2c1+(-1)c2: 2c1-c2=1 Thus, we have c1=2/3, c2=1/3. Since an=c1r1n+c2r2n, the formula is an=2/3*2n+1/3*(-1)n, 16 Example 3 (identical roots) Given initial values a0=1 and a1=1; and recursion an=-2an-1-an-2 Rewrite the recursion: an+2an-1+an-2=0 1.Characteristic polynomial: x2+2x+1=0 2.Characteristic roots: r1=r2=-1 3.Formula for roots of multiplicity 2 an=c1r1n+c2nr1n=c1(-1)n+c2n(-1)n Note the formula is different for roots of multiplicity. 17 Example 3 (identical roots) Given initial values a0=1 and a1=1; and recursion an=-2an-1-an-2 4. Two initial conditions: a0=c1(-1)0+c20(-1)0=c1 a1=c1(-1)1+c21(-1)1=-c1-c2 with a0=1 and a1=1 Thus, c1= 1 and c2= -2. Therefore, an= (-1)n-2n(-1)n Exercise: verify the sequence a2, a3 and a4. 18