Todays topics Recurrence relations Stating recurrences Li Ho

  • Slides: 26
Download presentation
Today’s topics • Recurrence relations – Stating recurrences – Li. Ho. Re. Co •

Today’s topics • Recurrence relations – Stating recurrences – Li. Ho. Re. Co • Reading: Sections 6. 1 -6. 2 • Upcoming – Graphs Comp. Sci 102 © Michael Frank 15. 1

§ 6. 1: Recurrence Relations • A recurrence relation (R. R. , or just

§ 6. 1: Recurrence Relations • A recurrence relation (R. R. , or just recurrence) for a sequence {an} is an equation that expresses an in terms of one or more previous elements a 0, …, an− 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. Comp. Sci 102 © Michael Frank 15. 2

Recurrence Relation Example • Consider the recurrence relation an = 2 an− 1 −

Recurrence Relation Example • Consider the recurrence relation an = 2 an− 1 − an− 2 (n≥ 2). • Which of the following are solutions? Yes an = 3 n an = 2 n No Yes an = 5 Comp. Sci 102 © Michael Frank 15. 3

Example Applications • Recurrence relation for growth of a bank account with P% interest

Example Applications • Recurrence relation for growth of a bank account with P% interest per given period: Mn = Mn− 1 + (P/100)Mn− 1 • Growth of a population in which each organism yields 1 new one every period starting 2 time periods after its birth. Pn = Pn− 1 + Pn− 2 (Fibonacci relation) Comp. Sci 102 © Michael Frank 15. 4

Solving Compound Interest RR • Mn = Mn− 1 + (P/100)Mn− 1 = (1

Solving Compound Interest RR • Mn = Mn− 1 + (P/100)Mn− 1 = (1 + P/100) Mn− 1 = r Mn− 1 (let r = 1 + P/100) = r (r Mn− 2) = r·r·(r Mn− 3) …and so on to… = r n M 0 Comp. Sci 102 © Michael Frank 15. 5

Tower of Hanoi Example • Problem: Get all disks from peg 1 to peg

Tower of Hanoi Example • Problem: Get all disks from peg 1 to peg 2. – Rules: (a) Only move 1 disk at a time. – (b) Never set a larger disk on a smaller one. Peg #1 Comp. Sci 102 Peg #2 © Michael Frank Peg #3 15. 6

Hanoi Recurrence Relation • Let Hn = # moves for a stack of n

Hanoi Recurrence Relation • Let Hn = # moves for a stack of n disks. • Here is the optimal strategy: – Move top n− 1 disks to spare peg. (Hn− 1 moves) – Move bottom disk. (1 move) – Move top n− 1 to bottom disk. (Hn− 1 moves) • Note that: Hn = 2 Hn− 1 + 1 – The # of moves is described by a Rec. Rel. Comp. Sci 102 © Michael Frank 15. 7

Solving Tower of Hanoi RR Hn = 2 Hn− 1 + 1 = 2

Solving Tower of Hanoi RR Hn = 2 Hn− 1 + 1 = 2 (2 Hn− 2 + 1) + 1 = 22 Hn− 2 + 1 = 22(2 Hn− 3 + 1) + 2 + 1 = 23 Hn− 3 + 22 + 1 … = 2 n− 1 H 1 + 2 n− 2 + … + 2 + 1 = 2 n− 1 + 2 n− 2 + … + 2 + 1 (since H 1 = 1) = = 2 n − 1 Comp. Sci 102 © Michael Frank 15. 8

Another R. R. Example • Find a R. R. & initial conditions for the

Another R. R. Example • Find a R. R. & initial conditions for the number of bit strings of length n without two consecutive 0 s. • 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, an = an− 1 + an− 2. (Fibonacci recurrence. ) – The initial conditions are: a 0 = 1 (ε), a 1 = 2 (0 and 1). Comp. Sci 102 (n− 2 bits) 1 0 © Michael Frank (n− 1 bits) 1 15. 9

Yet another R. R. example… • Give a recurrence (and base cases) for the

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. • an = 9 an− 1 + (10 n− 1 − an− 1) = 8 an− 1 + 10 n− 1. – Base cases: a 0 = 1 (ε), a 1 = 9 (1 -9). Comp. Sci 102 © Michael Frank 15. 10

§ 6. 2: Solving Recurrences General Solution Schemas • A linear homogeneous recurrence of

§ 6. 2: Solving Recurrences General Solution Schemas • A linear homogeneous recurrence of degree k with constant coefficients (“k-Li. Ho. Re. Co”) is a rec. rel. of the form an = c 1 an− 1 + … + ckan−k, where the ci are all real, and ck ≠ 0. • The solution is uniquely determined if k initial conditions a 0…ak− 1 are provided. Comp. Sci 102 © Michael Frank 15. 11

Solving Li. Ho. Re. Cos • Basic idea: Look for solutions of the form

Solving Li. Ho. Re. Cos • Basic idea: Look for solutions of the form an = rn, where r is a constant. • This requires solving the characteristic equation: rn = c 1 rn− 1 + … + ckrn−k, i. e. , (rearrange rk − c 1 rk− 1 − … − ck = 0 × by rk−n) • The solutions r to this equation are called the&characteristic roots of the Li. Ho. Re. Co. – They can yield an explicit formula for the sequence. Comp. Sci 102 © Michael Frank 15. 12

Solving 2 -Li. Ho. Re. Cos • Consider an arbitrary 2 -Li. Ho. Re.

Solving 2 -Li. Ho. Re. Cos • Consider an arbitrary 2 -Li. Ho. Re. Co: an = c 1 an− 1 + c 2 an− 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 the solutions to the RR are given by: an = α 1 r 1 n + α 2 r 2 n for n≥ 0 for any and all constants α 1, α 2. Special case: an = r 1 n and an = r 2 n are, of course, solutions. Comp. Sci 102 © Michael Frank 15. 13

Example • Solve the recurrence an = an− 1 + 2 an− 2 given

Example • Solve the recurrence an = an− 1 + 2 an− 2 given the initial conditions a 0 = 2, a 1 = 7. • Solution: Use theorem 1: – – – We have c 1 = 1, c 2 = 2 The characteristic equation is: r 2 − r − 2 = 0 Solve it: – so, r = 2 or r = − 1. – So, an = α 1 2 n + α 2 (− 1)n. Comp. Sci 102 © Michael Frank (Using the quadratic formula here. ) 15. 14

Example Continued… • To find α 1 and α 2, just solve the equations

Example Continued… • To find α 1 and α 2, just solve the equations for the initial conditions a 0 and a 1: a 0 = 2 = α 120 + α 2 (− 1)0 a 1 = 7 = α 121 + α 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. • Using α 1 and α 2, our final answer is: an = 3· 2 n − (− 1)n Check: {an≥ 0} = 2, 7, 11, 25, 47, 97 … Comp. Sci 102 © Michael Frank 15. 15

Proof of Theorem 1 • Proof that an = α 1 r 1 n+α

Proof of Theorem 1 • Proof that an = α 1 r 1 n+α 2 r 2 n is always a 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 an = c 1 an− 1 + c 2 an− 2: c 1 an− 1 + c 2 an− 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) = α 1 r 1 n− 2 r 12 + α 2 r 2 n− 2 r 22 = α 1 r 1 n + α 2 r 2 n = an. □ • Can complete the proof by showing that for any initial conditions, we can find corresponding α’s. – But it turns out this goes through only if r 1≠r 2. Comp. Sci 102 © Michael Frank 15. 16

The Case of Degenerate Roots • Now, what if the C. E. r 2

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, an = α 1 r 0 n + α 2 nr 0 n, for all n≥ 0, for some constants α 1, α 2. Comp. Sci 102 © Michael Frank 15. 17

Degenerate Root Example • Solve an = 6 an− 1− 9 an− 2 with

Degenerate Root Example • Solve an = 6 an− 1− 9 an− 2 with a 0=1, a 1=6. • The C. E. is: r 2− 6 r+9=0. – Note that b 2− 4 ac = (− 6)2− 4· 1· 9 = 36− 36 = 0. • Therefore, there is only one root, namely −b/2 a = −(− 6)/2 = 3. Comp. Sci 102 © Michael Frank 15. 18

k-Li. Ho. Re. Cos • Consider a k-Li. Ho. Re. Co: • It’s C.

k-Li. Ho. Re. Cos • Consider a k-Li. Ho. Re. Co: • It’s C. E. is: • Theorem 3: If this has k distinct roots ri, then the solutions to the recurrence are of the form: for all n≥ 0, where the αi are constants. Comp. Sci 102 © Michael Frank 15. 19

Degenerate k-Li. Ho. Re. Cos • Suppose there are t roots r 1, …,

Degenerate k-Li. Ho. Re. Cos • Suppose there are t roots r 1, …, rt with multiplicities m 1, …, mt. Then: for all n≥ 0, where all the α are constants. Comp. Sci 102 © Michael Frank 15. 20

Li. No. Re. Cos • Linear nonhomogeneous RRs with constant coefficients may (unlike Li.

Li. No. Re. Cos • Linear nonhomogeneous RRs with constant coefficients may (unlike Li. Ho. Re. Cos) contain some terms F(n) that depend only on n (and not on any ai’s). General form: an = c 1 an− 1 + … + ckan−k + F(n) The associated homogeneous recurrence relation (associated Li. Ho. Re. Co). Comp. Sci 102 © Michael Frank 15. 21

Solutions of Li. No. Re. Cos • A useful theorem about Li. No. Re.

Solutions of Li. No. Re. Cos • A useful theorem about Li. No. Re. Cos: – If an = p(n) is any particular solution to the Li. No. Re. Co: – Then all of its solutions are of the form: an = p(n) + h(n), where an = h(n) is any solution to the associated homogeneous RR Comp. Sci 102 © Michael Frank 15. 22

Li. No. Re. Co Example • Find all solutions to an = 3 an−

Li. No. Re. Co Example • Find all solutions to an = 3 an− 1+2 n. Which solution has a 1 = 3? – Notice this is a 1 -Li. No. Re. Co. Its associated 1 -Li. Ho. Re. Co is an = 3 an− 1, whose solutions are all of the form an = α 3 n. Thus the solutions to the original problem are all of the form an = p(n) + α 3 n. So, all we need to do is find one p(n) that works. Comp. Sci 102 © Michael Frank 15. 23

Trial Solutions • If the extra terms F(n) are a degree-t polynomial in n,

Trial Solutions • If the extra terms F(n) are a degree-t polynomial in n, you should try a general degree-t polynomial as the particular solution p(n). • This case: F(n) is linear so try an = cn + d. cn+d = 3(c(n− 1)+d) + 2 n (for all n) (2 c+2)n + (2 d− 3 c) = 0 (collect terms) So c = − 1 and d = − 3/2. So an = −n − 3/2 is a solution. • Check: an≥ 1 = {− 5/2, − 7/2, − 9/2, … } Comp. Sci 102 © Michael Frank 15. 24

Finding a Desired Solution • From the previous, we know that all general solutions

Finding a Desired Solution • From the previous, we know that all general solutions to our example are of the form: an = −n − 3/2 + α 3 n. Solve this for α for the given case, a 1 = 3: 3 = − 1 − 3/2 + α 31 α = 11/6 • The answer is an = −n − 3/2 + (11/6)3 n. Comp. Sci 102 © Michael Frank 15. 25

Double Check Your Answer! • Check the base case, a 1=3: an = −n

Double Check Your Answer! • Check the base case, a 1=3: an = −n − 3/2 + (11/6)3 n a 1 = − 1 − 3/2 + (11/6)31 = − 2/2 − 3/2 + 11/2 = − 5/2 + 11/2 = 6/2 = 3 • Check the recurrence, an = 3 an− 1+2 n: −n − 3/2 + (11/6)3 n = 3[−(n− 1) − 3/2 + (11/6)3 n− 1]+2 n = 3[−n − 1/2 + (11/6)3 n− 1] + 2 n = − 3 n − 3/2 + (11/6)3 n + 2 n = −n − 3/2 + (11/6)3 n ■ Comp. Sci 102 © Michael Frank 15. 26