28
CSCI2110 Tutorial 8: Solving Recurrence Chow Chi Wang (cwchow ‘at’ cse.cuhk.edu.hk)

CSCI2110 Tutorial 8: Solving Recurrence

  • Upload
    bob

  • View
    109

  • Download
    0

Embed Size (px)

DESCRIPTION

CSCI2110 Tutorial 8: Solving Recurrence. Chow Chi Wang ( cwchow ‘at’ cse.cuhk.edu.hk). Solving Recurrence. How to solve recurrence?. By iteration By guessing / observation For second order linear homogenous recurrence with constant coefficients By Distinct-Roots Theorem - PowerPoint PPT Presentation

Citation preview

Page 1: CSCI2110  Tutorial 8: Solving Recurrence

CSCI2110 Tutorial 8:Solving Recurrence

Chow Chi Wang (cwchow ‘at’ cse.cuhk.edu.hk)

Page 2: CSCI2110  Tutorial 8: Solving Recurrence

Solving Recurrence

Page 3: CSCI2110  Tutorial 8: Solving Recurrence

How to solve recurrence?• By iteration• By guessing / observation• For second order linear homogenous recurrence with

constant coefficients• By Distinct-Roots Theorem• By Single-Root Theorem

Page 4: CSCI2110  Tutorial 8: Solving Recurrence

Solving recurrence by iteration• Consider this recurrence relation:

, and

• How do we solve this?

Page 5: CSCI2110  Tutorial 8: Solving Recurrence

Solving recurrence by iteration

Page 6: CSCI2110  Tutorial 8: Solving Recurrence

Second Order Recurrence• You only need to know how to solve second order linear

homogeneous recurrence relation with constant coefficients for this course.

• It looks like: , where are constants and .

• Concrete examples:• (the Fibonacci recurrence)

Page 7: CSCI2110  Tutorial 8: Solving Recurrence

Second Order Recurrencesecond order linear homogeneous recurrence relation with

constant coefficients

• Second Order means that depends only on previous two terms . And it must depends on (i.e. ).

Page 8: CSCI2110  Tutorial 8: Solving Recurrence

Second Order Recurrencesecond order linear homogeneous recurrence relation with

constant coefficients

• Linear means that the sequence terms in the recurrence relation must be of degree 1. So we are not allowed to have something like etc. in our recurrence relation.

Page 9: CSCI2110  Tutorial 8: Solving Recurrence

Second Order Recurrencesecond order linear homogeneous recurrence relation with

constant coefficients

• Homogeneous means that the space of solutions is a linear space. In other words, the sum of any set of solutions or multiples of solutions is also a solution.

• In our case, this simply means we don’t have the degree-0 term. So we are not allowed to have something this:

, where .

Page 10: CSCI2110  Tutorial 8: Solving Recurrence

Second Order Recurrencesecond order linear homogeneous recurrence relation with

constant coefficients

• Constant coefficients means that both and are constants.

Page 11: CSCI2110  Tutorial 8: Solving Recurrence

Second Order Recurrence• Which of following recurrence relations are second order

linear homogeneous recurrence with constant coefficients?1. 2. 3. 4. 5. 6.

Page 12: CSCI2110  Tutorial 8: Solving Recurrence

Solving Second Order Recurrence• To solve :

Solve the corresponding characteristic equation:

By Distinct-Roots Theorem By Single-Root Theorem

Solve using

distinct roots ? single root ?

Step 1

Step 2

Page 13: CSCI2110  Tutorial 8: Solving Recurrence

Solving Second Order Recurrence (Step 1)

• We have to solve the characteristic equation

• You may use the quadratic formula:• The roots of the quadratic equation is given by

Page 14: CSCI2110  Tutorial 8: Solving Recurrence

Solving Second Order Recurrence (Step 2)

• Depending on the root(s) we calculated in step 1:• if are distinct roots of the characteristic equation.• if is the repeated root of the characteristic equation.

• We will use the initial conditions to determine the values of .

Page 15: CSCI2110  Tutorial 8: Solving Recurrence

Solving Second Order Recurrence (Step 2)

• By substituting these conditions into the recurrence relation, we have:

for the distinct root case. We can then determine the values of by solving this system of linear equations.

Page 16: CSCI2110  Tutorial 8: Solving Recurrence

Counting Strings - Revisit• The Problem

• A string made of 3 types of alphabets “a”, “b”, “c”• Count the number of string with length and without the pattern “aa”

• Let be the number of strings of length without the pattern “aa”. Find a recurrence relation and then solve it.

Page 17: CSCI2110  Tutorial 8: Solving Recurrence

Counting Strings - Revisit• The recurrence relation is given by: .• And the initial conditions are .

• The characteristic equation is .• The roots of the equation are .• By the distinct roots theorem, we have:

for some constants .

Page 18: CSCI2110  Tutorial 8: Solving Recurrence

Counting Strings - Revisit• The recurrence relation is given by: .• And the initial conditions are .

• Using the initial conditions, we have:

• So we have .

Page 19: CSCI2110  Tutorial 8: Solving Recurrence

Counting Strings - Revisit• The recurrence relation is given by: .• And the initial conditions are .

• Therefore:

• Tip: After finding the general formulae of the sequence, always remember to test it’s correctness by trying some base cases.

Page 20: CSCI2110  Tutorial 8: 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 begins with $n?

• Let be the probability that the gambler is ruined when he begins with $. Find a recurrence relation and then solve it.

Page 21: CSCI2110  Tutorial 8: Solving Recurrence

Gambler’s Ruin• The gambler guesses the correct outcome of a die with

probability , so we have:

for

• And the boundary conditions are .

Page 22: CSCI2110  Tutorial 8: Solving Recurrence

Gambler’s Ruin• The recurrence relation is given by: • And the boundary conditions are.

• The characteristic equation is .• The roots of the equation are or .• By the distinct roots theorem, we have:

for some constants .

Page 23: CSCI2110  Tutorial 8: Solving Recurrence

Gambler’s Ruin• The recurrence relation is given by: • And the boundary conditions are.

• Using the boundary conditions, we have:

• So we have .

Page 24: CSCI2110  Tutorial 8: Solving Recurrence

Gambler’s Ruin• The recurrence relation is given by: • And the boundary conditions are.

• Therefore:

Page 25: CSCI2110  Tutorial 8: 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?

• Let be the probability that the gambler is ruined when he begins with $. Find a recurrence relation and then solve it.

• Answer: .

Page 26: CSCI2110  Tutorial 8: Solving Recurrence

Exercise• Solve the following recurrence:

1. for .2. for .3. for .

Page 27: CSCI2110  Tutorial 8: Solving Recurrence

Summary• You may solve simple recurrence by iteration, guessing /

observing.

• How to solve second order linear homogeneous recurrence with constant coefficients? (slide 12)

• Tip: After finding the general formulae of the sequence, always remember to test it’s correctness by trying some base cases.

Page 28: CSCI2110  Tutorial 8: Solving Recurrence

Thank You!