CSE 20 Lecture 13 Analysis of Recursive Functions

  • Slides: 18
Download presentation
CSE 20 Lecture 13: Analysis of Recursive Functions CK Cheng 1

CSE 20 Lecture 13: Analysis of Recursive Functions CK Cheng 1

3. Analysis • 3. 1 Introduction • 3. 2 Homogeneous Linear Recursion 2

3. Analysis • 3. 1 Introduction • 3. 2 Homogeneous Linear Recursion 2

3. 1 Introduction Derive the bound of functions or recursions Estimate CPU time and

3. 1 Introduction Derive the bound of functions or recursions Estimate CPU time and memory allocation Eg. Page. Rank 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

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 5 8 13 21 34 4

Example: Fibonacci Sequence 0 1 2 3 4 5 6 7 8 9 0

Example: Fibonacci Sequence 0 1 2 3 4 5 6 7 8 9 0 1 1 2 3 5 8 13 21 34 5

Example: Fibonacci Sequence, i. Clicker Suppose g 0=0 and g 1=2, gn=gn-1+gn-2 what is

Example: Fibonacci Sequence, i. Clicker Suppose g 0=0 and g 1=2, gn=gn-1+gn-2 what is gn • A. gn=fn • B. gn=2 fn • C. gn=3 fn • D. None of the above 6

3. 2 Homogeneous Linear Recursion • (1) Arithmetic Recursion a, a+d, a+2 d, …,

3. 2 Homogeneous Linear Recursion • (1) Arithmetic Recursion a, a+d, a+2 d, …, a+kd • (2) Geometric Recursion a, ar 2, …, ark • (3) Linear Recursion an= e 1 an-1+e 2 an-2+…+ekan-k+ f(n) 7

Linear Recursion and Homogeneous Linear Recursion • Linear Recursion: There are no powers or

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. 8

Solving Linear Recursion Input: Formula and k initial values Output: an as a function

Solving Linear Recursion Input: Formula and k initial values Output: an as a function of n 1. Set characteristic polynomial: 2. Find the root of the characteristic polynomial (assuming ri are distinct) 3. Express 4. Determine ci from k initial values 9

Solving Linear Recursion Input: Formula and k initial values 1. Set characteristic polynomial: Rewrite

Solving Linear Recursion Input: Formula and k initial values 1. Set characteristic polynomial: Rewrite the formula with n=k Replace ai with xi 10

Solving Linear Recursion Input: Formula and k initial values 2. Find the root of

Solving Linear Recursion Input: Formula and k initial values 2. Find the root of the polynomial Or, 11

Solving Linear Recursion Input: Formula and k initial values 3. Express (assuming that the

Solving Linear Recursion Input: Formula and k initial values 3. Express (assuming that the roots are distinct. ) 4. Determine ci from k initial values 12

Solving Linear Recursion Input: Formula and k initial values 3. Set (when the roots

Solving Linear Recursion Input: Formula and k initial values 3. Set (when the roots are not distinct. ) where ri is a root of multiplicity wi 13

Example on Fibonacci sequence Input: initial values a 0=0 and a 1=1; and recursion

Example on Fibonacci sequence Input: initial values a 0=0 and a 1=1; and recursion formula an=an-1+an-2. Rewrite recursion: an-an-1 -an-2=0. Or a 2 -a 1 -a 0=0. 1. Characteristic polynomial: x 2 -x-1=0. 2. Roots of the polynomial: 3. Set: an=c 1 r 1 n+c 2 r 2 n. 14

Example on Fibonacci sequence Input: initial values a 0=0 and a 1=1; and recursion

Example on Fibonacci sequence Input: initial values a 0=0 and a 1=1; and recursion formula an=an-1+an-2. 4. Determine ci from k initial values a 0=c 1 r 10+c 2 r 20 => c 1+c 2=0 a 1=c 1 r 11+c 2 r 21 => c 1 r 1+c 2 r 2=1. Thus, we have an=c 1 r 1 n+c 2 r 2 n, where 15

Example 2 16

Example 2 16

Example 2 (cont) Two initial values a 0=c 1+c 2: c 1+c 2=1 a

Example 2 (cont) Two initial values a 0=c 1+c 2: c 1+c 2=1 a 1=2 c 1+(-1)c 2: 2 c 1 -c 2=1 Thus, we have c 1=2/3, c 2=1/3. Since an=c 1 r 1 n+c 2 r 2 n, we have an=2/3× 2 n+1/3×(-1)n, 17

Example 3 18

Example 3 18