Presentation is loading. Please wait.

Presentation is loading. Please wait.

after UCI ICS/Math 6A, Summer 20079-AdvancedCounting -1 Recurrence Relations (RRs) A “Recurrence Relation”

Similar presentations


Presentation on theme: "after UCI ICS/Math 6A, Summer 20079-AdvancedCounting -1 Recurrence Relations (RRs) A “Recurrence Relation”"— Presentation transcript:

1 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -1 Recurrence Relations (RRs) A “Recurrence Relation” for a sequence {a n } is an equation that expresses a n in terms of one or more of the previous terms in the sequence (i.e., a 0,a 1,a 2,…,a n-1 ) for all n≥n 0. Examples: a 0 =1, a 1 =3, a 2 =4; for n ≥3, a n = a n-1 +a n-2 -a n-3 1,3,4,6,7,9,10,12,13,15,16,18,19,21,22,… a 0 =2, a 1 =4, a 2 =3; for n ≥3, a n = a n-1 +a n-2 -a n-3 2,4,3,5,4,6,5,7,6,8,7,9,8,10,9,11,… a 0 =4, a 1 =3, a 2 =3; for n ≥3, a n = a n-1 +a n-2 -a n-3 4,3,3,2,2,1,1,0,0,-1,-1,-2,-2,-3,-3,-4,-4,…

2 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -2 Solutions to Recurrence Relations A sequence {a n } is called a “solution” of the recurrence relation if its terms satisfy the recurrence relation. Examples: For n ≥3, a n = a n-1 +a n-2 -a n-3 ; Initial conditions: a 0 =1, a 1 =3, a 2 =4. Solution: 1,3,4,6,7,9,10,12,13,15,16,18,19,21,22,… For n ≥3, a n = a n-1 +a n-2 -a n-3 ; Initial conditions: a 0 =2, a 1 =4, a 2 =3. Solution: 2,4,3,5,4,6,5,7,6,8,7,9,8,10,9,11,… For n ≥3, a n = a n-1 +a n-2 -a n-3 ; Initial conditions: a 0 =4, a 1 =3, a 2 =3. Solution: 4,3,3,2,2,1,1,0,0,-1,-1,-2,-2,-3,-3,-4,-4,… Every recurrence relationship has many solutions each determined uniquely by its own initial conditions. We say a function f:N→R is a “solution” to a recurrence relation if the sequence {f(n)} is a solution to it. Example: f(n)=5 ∙2 n is a solution to the recurrence relation a n =2∙a n-1.

3 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -3 Solving Recurrence Relations If a n =4a n-1 and a 0 =3, find a function f such that f(n)=a n. a n =4a n-1 =4 2 a n-2 =4 3 a n-3 =...=4 n a 0 =3  4 n. If a n =a n-1 +n and a 0 =4, find a function f such that f(n)=a n. a n =a n-1 +n=a n-2 +n+(n-1) =a n-2 +n+(n-1)+(n-2)=... =a 0 +n+(n-1)+(n-2)+...+1=4+n(n+1)/2=(n 2 +n+8)/2. If a n =2na n-1 and a 0 =5, find a function f such that f(n)=a n. a n =2na n-1 =2 2 n(n-1)a n-2 =2 3 n(n-1)a n-3 =...=2 n n!a 0 =5  2 n n!. If a n =2a n-1 +1 and a 0 =0, find a function f such that f(n)=a n. a n =2a n-1 +1=2 2 a n-2 +2+1=2 3 a n-3 +4+2+1=2 4 a n-4 +8+4+2+1=... =2 n-1 +2 n-2 +2 n-3 +...+8+4+2+1=2 n -1.

4 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -4 Modeling with Recurrence Relations An initial deposit of P 0 dollars deposited at 7% annual interest. P n =(1+0.07)P n-1 is the value after n years. P n =(1+0.07) n P 0 Rabbits on an island. Each pair producing a new pair every month. Fibonacci Numbers: f 0 =0, f 1 =1; for n ≥2, f n = f n-1 +f n-2. Tower of Hanoi Disks of decreasing diameter on 1 of 3 pegs. Move disks to another peg, always maintaining decreasing disk diameters on each peg. H n = number of moves to transfer n disks from 1 peg to another. H 1 =1; for n ≥2, H n = H n-1 +1 + H n-1 = 2H n-1 +1. 1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535, H n = 2 n -1.

5 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -5 RRs for Counting Bit Strings How many bit strings of length n do not contain 2 consecutive 0’s? b 1 =2 ({0,1}), b 2 =3 ({01,10,11}) For n ≥2: 01counted or 1counted b n = b n-2 + b n-1. Recognize this? How many bit strings of length n contain 2 consecutive 0’s? b 0 =b 1 =0 For n ≥2: 00any or 01counted or 1counted b n = 2 n-2 + b n-2 + b n-1.

6 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -6 Counting Code Words How many strings of n digits have an even number of 0’s? a 1 =9 For n ≥2: Either the first digit isn’t 0 and the rest has an even number of 0’s (there are 9a n-1 of these) or the first digit is 0 and the rest does not have an even number of digits (there are 10 n-1 -a n-1 of these) a n = 9a n-1 +(10 n-1 -a n-1 )=8a n-1 +10 n-1

7 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -7 Catalan Numbers C n = number of ways to parenthesize the product of n+1 numbers. C 1 =1: (1x2); C 2 =2: (1x(2x3)), ((1x2)x3) C 3 =5: (1x(2x(3x4))), (1x((2x3)x4)), ((1x2)x(3x4)), ((1x(2x3))x4), (((1x2)x3)x4) C 0 =C 1 =1 C n =C 0 C n-1 +C 1 C n-2 +C 2 C n-3 +…+C n-3 C 2 +C n-2 C 1 +C n-1 C 0 (Covered in Sec.7.4, Ex.41) 2 5 14 C n = Number of binary trees with n+1 leaves.

8 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -8 Solving Recurrence Relations We’ve seen several examples of Recurrence Relations a n = a∙a n-1 b n = n∙b n-1 f n =f n-2 +f n-1 b n =2 n-2 +b n-2 +b n-1 b n =2 n-3 +b n-3 +b n-2 +b n-1. b n = b n-1 +b n-2 -b n-3 H n =2H n-1 +1 In each case, many sequences satisfy the relationship and one also needs to set/have initial conditions get a unique solution. Goal: “closed form” expression for function, f, for the sequence, giving the n th term in a formula that doesn’t depend on earlier ones. Example: a n =d*a n instead of a n = a∙a n-1 H n =2 n -1 instead of H n =2H n-1 +1 We’ll look at a class of useful recurrence relations where deriving a closed form formula is easy.

9 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -9 Recurrence Relations When c 1, c 2, …, c k are constants and c k ≠0, a n = c 1 a n-1 +c 2 a n-2 +…+c k a n-k +F(n) is said to be a linear recurrence relation of degree k with constant coefficients. Linear = each a j appears only to the 1 st power, no a j 2, a j 3,... degree k = k previous terms, c k ≠0. constant coefficients = c j are constants, not functions c j (n). Additionally, if F(n)=0, the relation is called homogeneous. Homogeneous linear recurrence relation of degree k with constant coefficients: a n = c 1 a n-1 +c 2 a n-2 +…+c k a n-k where c k ≠0 We like these because we can solve them explicitly.

10 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -10 Examples of Recurrence Relations HLRRwCC = Homogeneous linear recurrence relation with constant coefficients a n = a∙a n-1 HLRRwCC of degree 1 a n = (a n-1 ) 2 Not Linear a n = n∙a n-1 Coefficients are not constant f n =f n-2 +f n-1 HLRRwCC of degree 2 b n =2 n-2 +b n-2 +b n-1. Not Homogeneous b n =2 n-3 +b n-3 +b n-2 +b n-1 Not Homogeneous a n = a n-1 +a n-2 -a n-4 HLRRwCC of degree 4 H n =2H n-1 +1 Not Homogeneous Remember: A recurrence relation has many solutions. Only when the initial conditions are specified is the solution unique. For degree k, you need k contiguous initial conditions.

11 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -11 Solving Linear (degree 1) HLRRwCC The relation is a n = c∙a n-1 All solutions are of the form a n = d∙c n The function is f (n)= d∙c n With the initial condition a 0 specified, the unique solution is a n = a 0 c n We saw this in computing compound interest An initial deposit of P 0 dollars deposited at 7% annual interest. P n =(1+0.07)P n-1 is the value after n years. P n =(1+0.07) n P 0

12 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -12 Fibonacci Solved Degree 1: f(n)=c*f(n-1); solution: f(n)=a*r n for some a and r Degree 2: f(n)=c 1 *f(n-1)+c 2 *f(n-2); solution: ? Consider Fibonacci numbers: F(n)=F(n-1)+F(n-2), and let’s try F(n)=r n. It follows that r 2 =r+1. There are 2 solutions to this quadratic equation: r 1 =(1+√5)/2, r 2 =(1-√5)/2 Therefore F 1 (n)=[(1+√5)/2] n and F 2 (n)=[(1-√5)/2] n are both solutions. Observe: If F 1,F 2 are solutions then so is F(n) = d 1 *F 1 (n) + d 2 *F 2 (n) Theorem (next slide): Every solution to this recurrence relation is of the form F(n) = d 1 *F 1 (n) + d 2 *F 2 (n) for some d 1,d 2 For our initial conditions: F(0)=0 and F(1)=1 we get d 1 +d 2 =0 and d 1 ∙r 1 +d 2 ∙r 2 =1 Therefore 1=d 1 ∙(r 1 -r 2 )=d 1 ∙(√5). Therefore d 1 = 1/√5 and d 2 = -1/√5 Therefore the closed formula for Fibonacci numbers is:

13 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -13 Solutions to HLRRwCC We like HLRRwCC because we can solve them explicity. That is, we can find functions f:N→R so that the sequence {f(n)} solves the recurrence relation. Here’s part of the reason why. Lemma: If functions f and g are solutions to the HLRRwCC a n = c 1 a n-1 +c 2 a n-2 +c 3 a n-3 +…+c k a n-k then f+g is also a solution as is d∙f for any constant d. Definitions: The characteristic equation of this HLRRwCC is r k = c 1 r k-1 +c 2 r k-2 +c 3 r k-3 +…+c k-1 r+c k The solutions (roots) of this equation are called the characteristic roots of the recurrence relation

14 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -14 (Fibonacci) Degree 2 HLRRwCC 1) Write out the characteristic equation. (For Fibonacci: r 2 =r+1) 2) Find the characteristic roots (r 1 and r 2 ). ( r 1 =(1+√5)/2, r 2 =(1-√5)/2 ) 3) Any function of the form f(n)=d 1 ∙r 1 n +d 2 ∙r 2 n (d 1 and d 2 constants) solves this recurrence relation. 4a) If the characteristic roots are distinct, we can pick d 1 and d 2 to produce the required initial values. 4b) If there is only 1 characteristic root (r 1 =r 2 =r 1 ), then any function of the form f(n)=(d 1 +d 2 ∙n)∙r n (d 1 and d 2 constants) solves the recurrence relation. Example: a 0 =1, a 1 =4; for n ≥2, a n = 4a n-1 -4a n-2 1,4,12,32,80,192,448, Characteristic Equation: r 2 =4r-4  r 2 -4r+4=0  r=2 (twice) f(n)=(d 1 +d 2 ∙n)∙2 n & 1=d 1 & 4=2d 1 +2d 2  d 1 =d 2 =1; f(n)=(1+n) ∙ 2 n Check: f(6)=(1+6)∙2 6 =7∙64=448

15 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -15 All Solutions For Any HLRRwCC Thrm: If r k = c 1 r k-1 +c 2 r k-2 +…+c k-1 r+c k ( c k ≠0) has k distinct roots, r 1,r 2,…,r k, then every solution of the recursion relation a n =c 1 a n-1 +c 2 a n-2 +…+c k a n-k has the form a n = d 1 r 1 n +d 2 r 2 n +…+d k r k n for some d 1, d 2, …, d k and every such sequence solves/satisfies the recursion relation. If there are t distinct roots, each with multiplicity m i, the sequences {a n } solving the recursion relation are given by

16 stasio@uci.edustasio@uci.edu, after franklin@uci.edu UCI ICS/Math 6A, Summer 20079-AdvancedCounting -16 Degree 3 HLRRwCC Examples a 0 =2, a 1 =5, a 2 =15; for n ≥3, a n = 6∙a n-1 -11∙a n-2 - 6∙a n-3 r 3 = 6∙r 2 -11∙r-6  r 3 -6∙r 2 +11∙r+6=0  (r-1)(r-2)(r-3)=0 General solution: a n = x∙1 n +y∙2 n +z∙3 n. Initial values: 2=x+y+z; 5=x+2y+3z; 15=x+4y+9z  x=1; y=-1; z=2 Specific solution: a n = 1-2 n +2∙3 n. a 0 =1, a 1 =-2, a 2 =-1; for n ≥3, a n = -3∙a n-1 -3∙a n-2 -a n-3 r 3 = -3∙r 2 -3∙r-1  r 3 +3∙r 2 +3∙r+1=0  (r+1) 3 =0  r=-1 with multiplicity 3 General solution: a n =(x+y∙n+z∙n 2 )∙(-1) n. Initial values: 1=x; 2=x+y+z; -1=x+2y+4z  x=1; y=3; z=-2 Specific solution: a n = (1+3n-2n 2 ) (-1) n.


Download ppt "after UCI ICS/Math 6A, Summer 20079-AdvancedCounting -1 Recurrence Relations (RRs) A “Recurrence Relation”"

Similar presentations


Ads by Google