CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

27
CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence Wong Chung Hoi (Hollis) [email protected] 19-10-2011

description

CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence. Wong Chung Hoi (Hollis) [email protected] 19-10-2011. Agenda. How to solve recurrence? Solving Second order linear homogeneous recurrence Fibonacci Series Counting Rabbits (Revisit) Counting Strings (Revisit) - PowerPoint PPT Presentation

Transcript of CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Page 1: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

CSCI2110 – Discrete MathematicsTutorial 7

Solving Recurrence

Wong Chung Hoi (Hollis)[email protected]

19-10-2011

Page 2: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Agenda

• How to solve recurrence?• Solving Second order linear homogeneous

recurrence– Fibonacci Series– Counting Rabbits (Revisit)– Counting Strings (Revisit)– Gambler’s Ruin

Page 3: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

How to solve recurrence?

• By iteration• By guessing / observation• For second order linear homogenous

recurrence– By Distinct Roots Theorem– By Single Root Theorem

Page 4: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Second Order Linear Homogeneous Recurrence

• The format: ak = Aak-1 + Bak-2

• Second Order– ak depends on ak-1, ak-2, but not ak-3, ak-4, or k etc

• Linear– ak-1, ak-2 has degree 1

• Homogeneous– All terms has the same degree, i.e. no constant

term

Page 5: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Second Order Linear Homogenous Recurrence?

• ak = 3ak-2

• ak = 2ak-2 + 3ak-3

• ak = 3ak-1 + 2

• ak = 4ak-12+4ak-2

2

• ak = 93ak-1 + 10-2ak-2

• ak = ak-1 + k

• ak = a1ak-1 + a2ak-2, a1 = 1, a2 = 2

Page 6: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Solving Second Order Linear Homogenous Recurrence

• To solve: ak = Aak-1 + Bak-2, a1 = C, a2 = D

Solve the characteristic equationt2 – At – B = 0

Distinct-Roots Theoreman = Ern + Fsn

Single-Root Theoreman = Ern + Fnrn

Solve E, F by a1, a2

t has 2 roots? t has single root?

Page 7: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Solving Characteristic Equation

The characteristic equation for recurrence an = Aan-1 + Ban-2

is given by t2 – At – B = 0

Exercise: Solve for t in terms of A and B.

Page 8: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Distinct-Roots Theorem

If the characteristic equation t2 - At - B = 0 has 2distinct roots s, r for t.Then, the closed form of an is given by

an = Ern + Fsn

Exercise: With the assumption when n = k-2, k-1, closed form an = Ern + Fsn is equivalent to recurrence an = Aan-1 + Ban-2, show that when n = k, they are equivalent.

Page 9: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Single-Root Theorem

If the characteristic equation t2 - At - B = 0 hasonly single root r for t.Then, the closed form of an is given by

an = Ern + Fnrn

Exercise: Show that A = -2r, B = r2.

With the assumption when n = k-2, k-1, closed form an = Ern + Fnrn is equivalent to recurrence an = Aan-1 + Ban-2, show that when n = k, they are equivalent.

Page 10: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Solving E, F

With boundary values a1 = C, a2 = Dsolve E,F by

C = Er + Fs, D = Er2 + Fs2 for distinct rootsC = Er + Fr, D = Er2 + F2r2 for single root

Page 11: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Agenda

• How to solve recurrence?• Solving Second order linear homogeneous

recurrence– Fibonacci Series– Counting Rabbits (Revisit)– Counting Strings (Revisit)– Gambler’s Ruin

Page 12: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Fibonacci Series• an = an-1 + an-2, a0 = 1, a1 = 11. Characteristic equation

t2 – t - 1 = 0t = (1+51/2)/2 or (1-51/2)/2

2. Distinct roots theoreman = E ((1+51/2)/2)n + F ((1-51/2)/2)n

3. Solving E, F by a0, a1

1 = E + F1 = E (1+51/2)/2 + F (1-51/2)/2

E = (51/2 + 1) / (2 x 51/2)F = (51/2 - 1) / (2 x 51/2)

an = 5-1/2 x((1+51/2)/2)n+1 – 5-1/2 x ((1-51/2)/2)n+1

Page 13: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Fibonacci Series• an = an-1 + an-2, a0 = 2, a1 = 41. Characteristic equation

t2 – t - 1 = 0t = (1+51/2)/2 or (1-51/2)/2

2. Distinct roots theoreman = E ((1+51/2)/2)n + F ((1-51/2)/2)n

3. Solving E, F by a0, a1

2 = E + F4 = E (1+51/2)/2 + F (1-51/2)/2

E = (51/2 + 3) / 51/2

F = (51/2 - 3) / 51/2

an = (1 + 3/51/2) ((1+51/2)/2)n + (1 – 3/51/2) ((1-51/2)/2)n

Page 14: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Agenda

• How to solve recurrence?• Solving Second order linear homogeneous

recurrence– Fibonacci Series– Counting Rabbits (Revisit)– Counting Strings (Revisit)– Gambler’s Ruin

Page 15: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Counting Rabbits - Revisit• Problem

– Start with 1 pair of baby rabbit.– Rabbits take 2 months to become

an adult.– Adult rabbits gives birth to 3 pairs

of baby rabbits every month.– Rabbits NEVER DIE!– Number of pairs of rabbits in the end of the year?

1. Denote Rn as the number of rabbit after n month.2. R0 = 1, R1 = 1, R2 = 1, R3 = 4, R4 = 73. Rn = 3Rn-3 + Rn-1

Page 16: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Counting Rabbits - Revisit• Problem

– Start with 1 pair of baby rabbit.– Rabbits take 1 months to become

an adult.– Adult rabbits gives birth to 3 pairs

of baby rabbits every month.– Rabbits NEVER DIE!– Number of pairs of rabbits in the end of the year?

1. Denote Rn as the number of rabbit after n month.2. R0 = 1, R1 = 1, R2 = 4, R3 = 73. Rn = 3Rn-2 + Rn-1

Page 17: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Counting Rabbits - Revisit

4. Finding the closed form:1. Characteristic equation

t2 – t – 3 = 0t = (1-131/2)/2 or (1+131/2)/2

2. Distinct-Roots TheoremRn = E((1-131/2)/2)n + F((1+131/2)/2)n

3. Solve E, F by R0, R1

1 = E + F1 = E(1-131/2)/2 + F(1+131/2)/2

E = (131/2 - 1) / (2 x 131/2)F = (131/2 + 1) / (2 x 131/2)

Page 18: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Agenda

• How to solve recurrence?• Solving Second order linear homogeneous

recurrence– Fibonacci Series– Counting Rabbits (Revisit)– Counting Strings (Revisit)– Gambler’s Ruin

Page 19: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Counting Strings - Revisit

• Problem– A string made of 3 types of alphabets “a”, “b”, “c”– Count the number of string with length n and

without the pattern “aa”1. Let Ln be the number of string with length n

and without pattern “aa”2. L1 = 3, L2 = 8, L3 = 22…3. Ln = 2Ln-1 + 2Ln-2,

Page 20: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Counting Strings - Revisit

Find the closed form for Ln = 2Ln-1 + 2Ln-2

1. Characteristic Equationt2 – 2t – 2 = 0

t = 1 + 31/2, or 1 – 31/2

2. By distinct roots theoremLn = E(1+31/2)n + F(1-31/2)n

3. Solve E,F by L1, L2

3 = E(1+31/2) + F(1-31/2)8 = E(1+31/2)2 + F(1-31/2)2

E = ?F = ?

Page 21: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Counting Strings - Revisit3. Solve E,F by L1, L2

3 = E(1+31/2) + F(1-31/2)8 = E(1+31/2)2 + F(1-31/2)2

This is too complicated! Trick: What if L0 is part of our sequence?

L2 = 2L1 + 2L0 → 8 = 2 x 3 + 2 L0 → L0 = 1Solve E,F by L0, L1

1 = E + F3 = E(1+31/2) + F(1-31/2)

Exercise: Show that Ln = [(31/2+2)(1+31/2)n + (31/2 - 2)(1-31/2)n] / (2 x 31/2)

Page 22: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Agenda

• How to solve recurrence?• Solving Second order linear homogeneous

recurrence– Fibonacci Series– Counting Rabbits (Revisit)– Counting Strings (Revisit)– Gambler’s Ruin

Page 23: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Gambler’s Ruin

• Problem:– Betting $1 on the outcome of a die until he has $300.– +$1 if he won, -$1 if he lost.– Probability that the gambler is ruined when he begin

with $n?1. Let Pn be the probability when he begin with $n.2. P0 = 1, P300 = 03. Recurrence equation:

Pn-1 = 1/6 x Pn + 5/6 x Pn-2

Page 24: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Gambler’s Ruin4. Finding the closed form:

1. Characteristic equation:t2 - 6t + 5 = 0t = 1 or 5

2. Distinct-Roots TheoremPn = E + F5n

3. Solve E, F by P0, P300

1 = E + F0 = E + F5300

E = 5300 / (5300 - 1)F = 1/(1- 5300)

Pn = (5300 – 5n) / (5300-1)

5. Exercise: Verify the closed form.Exercise: Approximate P20.

Page 25: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Gambler’s Ruin 2

• Problem:– Betting $1 on the outcome of a coin until he has $300.– +$1 if he won, -$1 if he lost.– Probability that the gambler is ruined when he begin

with $n?1. Let Pn be the probability when he begin with $n.2. P0 = 1, P300 = 03. Recurrence equation:

Pn-1 = 1/2 x Pn + 1/2 x Pn-2

Page 26: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Gambler’s Ruin 24. Finding the closed form:

1. Characteristic equation:t2 - 2t + 1 = 0t = 1

2. Single-Root TheoremPn = E1n + Fn1n

3. Solve E, F by P0, P300

1 = E0 = E + 300F

E = 1F = -1/300

Pn = 1-n/300

5. Exercise: Verify the closed form.Exercise: CalculateP20.

Page 27: CSCI2110 – Discrete Mathematics Tutorial 7 Solving Recurrence

Summary

• Ways of solving recurrence– Iteration, guessing / observing

• What is Second order linear homogenous recurrence?

• How to solve?