6.Advanced Counting Techniques 1 Copyright M.R.K. Krishna Rao 2003 Ch 6. Recurrence Relations A...

56
1 6.Advanced Counting 6.Advanced Counting Techniques Techniques Copyright M.R.K. Krishna Rao 2003 Ch 6. Recurrence Ch 6. Recurrence Relations Relations •A recurrence relation for the sequence {a n } is an equation that expresses a n in terms of one or more of the previous terms of the sequence, namely, a 0 , a 1 , …, a n-1 , for all integers n with n n 0 , where n 0 is a nonnegative integer. A sequence is called a solution of a recurrence relation if its terms satisfy the recurrence relation.
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    0

Transcript of 6.Advanced Counting Techniques 1 Copyright M.R.K. Krishna Rao 2003 Ch 6. Recurrence Relations A...

16.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Ch 6. Recurrence Ch 6. Recurrence RelationsRelations

• A recurrence relation for the sequence {an} is an equation that expresses an in terms of one or more of the previous terms of the sequence, namely, a0, a1, …, an-1, for all integers n with n n0, where n0 is a nonnegative integer.

• A sequence is called a solution of a recurrence relation if its terms satisfy the recurrence relation.

26.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Recurrence RelationsRecurrence Relations

•In other words, a recurrence relation is like a recursively defined sequence, but without specifying any initial values (initial conditions).

•Therefore, the same recurrence relation can have (and usually has) multiple solutions.

•If both the initial conditions and the recurrence relation are specified, then the sequence is uniquely determined.

36.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Recurrence RelationsRecurrence Relations

•Example: Consider the recurrence relation an = 2an-1 – an-2 for n = 2, 3, 4, …

•Is the sequence {an} with an=3n a solution of this recurrence relation?•For n 2 we see that 2an-1 – an-2 = 2(3(n – 1)) – 3(n – 2) = 3n = an.

•Therefore, {an} with an=3n is a solution of the recurrence relation.

46.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Recurrence RelationsRecurrence Relations

•Is the sequence {an} with an=5 a solution of the same recurrence relation?•For n 2 we see that 2an-1 – an-2 = 25 - 5 = 5 = an.

•Therefore, {an} with an=5 is also a solution of the recurrence relation.

•In other words, the same recurrence relation can have (and usually has) multiple solutions.•If both the initial conditions and the recurrence relation are specified, then the sequence is uniquely determined.

56.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Modeling with Recurrence RelationsModeling with Recurrence Relations

•Example: •Someone deposits SR 10,000 in a savings account at a bank yielding 5% per year with interest compounded annually. How much money will be in the account after 30 years?

•Solution:

•Let Pn denote the amount in the account after n years.

•How can we determine Pn on the basis of Pn-

1?

66.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Modeling with Recurrence RelationsModeling with Recurrence Relations

•We can derive the following recurrence relation:•Pn = Pn-1 + 0.05Pn-1 = 1.05Pn-1.•The initial condition is P0 = 10,000.•Then we have:•P1 = 1.05P0 •P2 = 1.05P1 = (1.05)2P0

•P3 = 1.05P2 = (1.05)3P0

•…•Pn = 1.05Pn-1 = (1.05)nP0

•We now have a formula to calculate Pn for any natural number n and can avoid the iteration.

76.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Modeling with Recurrence RelationsModeling with Recurrence Relations

•Let us use this formula to find P30 under the

•initial condition P0 = 10,000:

•P30 = (1.05)3010,000 = 43,219.42

•After 30 years, the account contains • SR 43,219.42.

86.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Rabbit ReproductionRabbit Reproduction

Month

Adult pairs Young Total

1 0 1 1

2 0 1 1

3 1 1 2

4 1 2 3

5 2 3 5

6 3 5 8

Fibonacci defined a sequence when observing the reproduction of rabbits. Starting with one male/female pair of newborn rabbits, he came up with the following:

Question: After n months, how many pairs of mature rabbits will there be?

96.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Rabbit Reproduction - Rabbit Reproduction - Fibonacci Fibonacci numbersnumbers

• This sequence of numbers {fn} satisfies the recurrence relation

fn = fn-1 + fn-2 , n 3

• With initial conditions f1 = 1 and f2 = 1.

• The sequence of Fibonacci numbers are:  1, 1, 2, 3, 5, 8, 13, 21, ……

106.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Modeling with Recurrence RelationsModeling with Recurrence Relations

•The Legend: In an ancient city, so the legend goes, monks in a temple have to move a pile of 64 sacred disks from one location to another. The disks are fragile; only one can be carried at a time. A disk may not be placed on top of a smaller, less valuable disk. And, there is only one other location in the temple (besides the original and destination locations) sacred enough that a pile of disks can be placed there. • So, the monks start moving disks back and forth, between the original pile, the pile at the new location, and the intermediate location, always keeping the piles in order (largest on the bottom, smallest on the top). The legend is that, before the monks make the final move to complete the new pile in the new location, the temple will turn to dust and the world will end. Should we worry about truth of this legend?

116.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Towers of HanoiTowers of Hanoi

• Question: What is the minimum # of moves required to shift N disks from one spoke to another?

• Solution: • (1) Transfer n-1 disks to peg 3 in Hn-1 steps.• (2) Transfer the largest disk to peg 2 in 1 step.• (3) Now, transfer the n-1 disks from peg 3 to peg 2.

Hn = Hn-1 + 1 + Hn-1 = 2Hn-1 + 1

• No need to worry! -- it will take more than 500 billion years!

126.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

ExampleExample

• Find a recurrence relation and give initial

• conditions for the number of bit strings of

• length n that do not have two consecutive 0s.

• How many such bit strings are there• of length eight?

136.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

SolutionSolution

• Let an be the number of bit strings of length n that do not have two consecutive 0s.

• For n 3, an = an-1 + an-2 • with initial conditions a1 = 2 and a2 = 3. • Hence, there are 55 bit strings of length 8

with no two consecutive 0s. (Seq. 2, 3, 5, 8, 13, 21, 34, 55…)

an-1

an-2

First bit

Second bit

0

0 1

1

146.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Example: (Codeword Example: (Codeword Enumeration)Enumeration)

• A string of decimal digits is considered as a valid codeword if it contains an even number of 0 digits.

• For instance, 12340407869 is valid, whereas 120987045608 is not valid.

• Let an be the number of valid n-digit codewords.

• Find a recurrence relation for an.

156.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Codeword Enumeration Codeword Enumeration (cont.)(cont.)• There are 2 cases:• Case (a): First bit is non-zero (9 ways):

– It’s okay if the remaining n-1 bits contain an even number of 0: an-1 such strings.

• Case (b): First bit is zero:– It’s okay if the remaining n-1 bits contain an

ODD number of 0: (10n-1 an-1) such strings.

an = 9 an-1 + (10n-1 an-1) = 8 an-1 + 10n-1

• with initial condition a1 = 9.

166.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Sterling NumbersSterling Numbers

• Example: (Sterling Numbers of second kind)• Let Sn,r = the number of ways a set of size n

can be partitioned into r subsets.

• Let’s find S4,1 , S4,2 , S4,3 and S4,4 .

• S4,1 = 1 -- only 1 way• S4,2 = 7 -- A+BCD, B+ACD, C+ABD, D+ABC, • AB+CD, AC+BD, AD+BC• S4,3 = 6 -- AB+C+D, AC+B+D, AD+B+C,…• S4,4 = 1 -- only 1 way

176.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Sterling Numbers…Sterling Numbers…

• Let’s find a recurrence relation for Sn,r :

• Partition of {x1, x2, … , xn} into r subsets

• = Partition of {x1, x2, … , xn} into r subsets

where one of the subsets is {xn}

• + Partition of {x1, x2, … , xn} into r subsets

where none of the subsets is {xn}

• Therefore, Sn,r, = Sn-1,r-1,+ rSn-1,r

• Blackboard Examples: 8 – Catalan numbers.• Blackboard Exercises: 2,9,13,18,21,29,37

186.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

6.2 Solving Recurrence 6.2 Solving Recurrence RelationsRelations

• In general, we would prefer to have an explicit formula to compute the value of an rather than conducting n iterations.

• Two main methods:1. Iteration (or telescoping).2. Method for linear homogeneous

recurrence relations with constant coefficients.

196.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Method 1: IterationMethod 1: Iteration

• Problem: Given a recursive expression with initial conditions a0, a1,

• Try to express an without dependence on previous terms.

• Example:

• an = 2an-1 for n > 1, with initial condition a0 = 1

• Solution: an = 2n

206.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

More on the iteration methodMore on the iteration method

• Example: Deer Population growth

• Deer population dn at time n

• Initial condition: d0 = 1000

• Increase from time n-1 to time n is 10%. Therefore the recursive function is

dn – dn-1 = 0.1dn-1

dn = 1.1dn-1

• Solution: dn = 1000(1.1)n

216.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Telescope ExampleTelescope Example

• Find a closed solution to an = 2an-1, a0= 3:

an=2an-1

226.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Telescope ExampleTelescope Example

• Find a closed solution to an = 2an-1, a0= 3:

an=2an-1=22an-2

236.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Telescope ExampleTelescope Example

• Find a closed solution to an = 2an-1, a0= 3:

an=2an-1=22an-2=23an-3

246.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Telescope ExampleTelescope Example

• Find a closed solution to an = 2an-1, a0= 3:

an=2an-1=22an-2=23an-3 =…

256.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Telescope ExampleTelescope Example

• Find a closed solution to an = 2an-1, a0= 3:

an=2an-1=22an-2=23an-3 =… =2ian-i

266.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Telescope ExampleTelescope Example

• Find a closed solution to an = 2an-1, a0= 3:

an=2an-1=22an-2=23an-3 =… =2ian-i =…

276.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Telescope ExampleTelescope Example

• Find a closed solution to an = 2an-1, a0= 3:

an=2an-1=22an-2=23an-3 =… =2ian-i =… =2na0

286.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Telescope ExampleTelescope Example

• Find a closed solution to an = 2an-1, a0= 3:

• Plug in a0= 3 for final answer:

• an = 3·2n

an=2an-1=22an-2=23an-3 =… =2ian-i =… =2na0

296.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Method 2:Method 2: Linear Recurrences Linear Recurrences

• The only case for which telescoping works with a high probability is when the recurrence gives the next value in terms of a single previous value. But…

• There is a class of recurrence relations which can be solved analytically in general. These are called linear recurrences and include the Fibonacci recurrence.

306.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•Definition: A linear homogeneous recurrence relation of degree k with constant coefficients is a recurrence relation of the form:

an = c1an-1 + c2an-2 + … + ckan-k,•Where c1, c2, …, ck are real numbers, and ck 0.

•A sequence satisfying such a recurrence relation is uniquely determined by the recurrence relation and the k initial conditions

a0 = C0, a1 = C1, a2 = C2, …, ak-1 = Ck-1.

316.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•Examples:

•The recurrence relation Pn = (1.05)Pn-1 is a linear homogeneous recurrence relation of degree one.

•The recurrence relation fn = fn-1 + fn-2 is a linear homogeneous recurrence relation of degree two.

•The recurrence relation an = an-5 is a linear homogeneous recurrence relation of degree five.

•an 2an-1 + 1 is not homogeneous!•an = an-1 + an-2

2 is not linear !

326.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•When solving such recurrence relations, we try basically to find solutions of the form an = rn, where r is a constant.

•an = rn is a solution of the recurrence relationan = c1an-1 + c2an-2 + … + ckan-k if and only if

•rn = c1rn-1 + c2rn-2 + … + ckrn-k.

•Divide this equation by rn-k and subtract the right-hand side from the left:

•rk - c1rk-1 - c2rk-2 - … - ck-1r - ck = 0

•This is called the characteristic equation of the recurrence relation.

336.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•The solutions of this equation are called the characteristic roots of the recurrence relation.•The characteristic roots help in giving an explicit formula for the sequence. Let us consider linear homogeneous recurrence relations of degree two.

•Theorem 1: Let c1 and c2 be real numbers. Suppose that r2 – c1r – c2 = 0 has two distinct roots r1 and r2.•Then the sequence {an} is a solution of the recurrence relation an = c1an-1 + c2an-2 if and only if an = 1r1

n + 2r2n for n = 0, 1, 2, …, where 1 and 2

are constants.

•See pp. 414 and 415 for the proof.

346.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•Example: What is the solution of the recurrence relation an = an-1 + 2an-2 with a0 = 2 and a1 = 7 ?

•Solution: The characteristic equation of the recurrence relation is r2 – r – 2 = 0.•Its roots are r = 2 and r = -1.

•Hence, the sequence {an} is a solution to the recurrence relation if and only if:

•an = 12n + 2(-1)n for some constants 1

and 2.

356.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•Given the equation an = 12n + 2(-1)n and the initial conditions a0 = 2 and a1 = 7, it follows that

•a0 = 2 = 1 + 2

•a1 = 7 = 12 + 2 (-1)

•Solving these two equations gives us1 = 3 and 2 = -1.

•Therefore, the solution to the recurrence relation and initial conditions is the sequence {an} with an = 32n – (-1)n.

366.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•Example: What is the solution of the recurrence relation an = an-1 + 6an-2 with a0 = 1 and a1 = 2 ?

•Solution: The characteristic equation of the recurrence relation is r2 – r – 6 = 0.•Its roots are r = 3 and r = -2.

•Hence, the sequence {an} is a solution to the recurrence relation if and only if:

•an = 13n + 2(-2)n for some constants 1

and 2.

376.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•Given the equation an = 13n + 2(-2)n and the initial conditions a0 = 1 and a1 = 2, it follows that

•a0 = 1 = 1 + 2

•a1 = 2 = 13 + 2 (-2)

•Solving these two equations gives us1 = 4/5 and 2 = 1/5.

•Therefore, the solution to the recurrence relation and initial conditions is the sequence {an} with an = (4/5)(3)n + (1/5)(-2)n.

386.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•Example: Give an explicit formula for the Fibonacci numbers.•Solution: The Fibonacci numbers satisfy the recurrence relation fn = fn-1 + fn-2 with initial conditions f0 = 0 and f1 = 1.

•The characteristic equation is r2 – r – 1 = 0.•Its roots are

2

51,

2

5121

rr

396.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence Solving Recurrence RelationsRelations

•Therefore, the Fibonacci numbers are given by

nn

nf

2

51

2

5121

•for some constants 1 and 2.

•We can determine values for these constants so that the sequence meets the conditions f0 = 0 and f1 = 1:

0210 f

12

51

2

51211

f

406.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Solving Recurrence RelationsSolving Recurrence Relations

•The unique solution to this system of two equations and two variables is

5

1,

5

121

•So finally we obtained an explicit formula for the Fibonacci numbers:

nn

nf

2

51

5

1

2

51

5

1

416.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Homogeneous - Homogeneous - ComplicationsComplications

Repeating roots in characteristic equation. Repeating roots imply that don’t learn anything new from second root, so may not have enough information to solve formula with given initial conditions. We’ll see how to deal with this on next slide.

Non-real number roots in characteristic equation. If the sequence has periodic behavior, may get complex roots (for example an = -an-2). We won’t worry about this case (in principle, same method works as before, except use complex arithmetic).

426.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Complication: Repeating Complication: Repeating RootsRoots

EG: Solve an = 2an-1-an-2 , a0 = 1, a1 = 2

Find characteristic equation by plugging in an = r n:

r 2 - 2r +1 = 0Since r 2 - 2r +1 = (r -1)2 the root r = 1 repeats.

If we tried to solve by using general solution an = 1 r1

n+ 2 r2n = 1 1n+ 2 1n = 1 + 2

which forces an to be a constant function ().

SOLUTION: Multiply second solution by n so general solution looks like:

an = 1 r1n+ 2 nr1

n

436.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Complication: Repeating Complication: Repeating RootsRoots• Solve an = 2an-1-an-2, a0 = 1, a1 = 2

• General solution: an = 1 1n+ 2n1n = 1 +

2n

• Plug into initial conditions

• 1 = a0 = 1 + 2.0·10= 1

• 2 = a1 = 1 ·11+ 2·1·11= 1 + 2

• Plugging first equation 1 = 1 into second:• 2 = 1+ 2 implies 2= 1.

• Final answer: an = 1+n

446.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Complication: Repeating Complication: Repeating RootsRoots

•In general, if the characteristic equation has only one root, the following theorem helps!

•Theorem 2: Let c1 and c2 be real numbers with c2 0. Suppose that r2 – c1r – c2 = 0 has only one root r0. A sequence {an} is a solution of the recurrence relation an = c1an-1 + c2an-2 if and only if an = 1r0

n + 2nr0n, for n = 0, 1, 2, …,

where 1 and 2 are constants.

456.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Complication: Repeating Complication: Repeating RootsRoots

•Example: What is the solution of the recurrence relation an = 6an-1 – 9an-2 with a0 = 1 and a1 = 6?

•Solution: The only root of r2 – 6r + 9 = 0 is r0 = 3.Hence, the solution to the recurrence relation is

•an = 13n + 2n3n for some constants 1 and 2.

•To match the initial condition, we need

•a0 = 1 = 1

a1 = 6 = 13 + 23

•Solving these equations yields 1 = 1 and 2 = 1.

•Consequently, the overall solution is given by

•an = 3n + n3n.

466.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

kk-LiHoReCoCos-LiHoReCoCos

• Consider a k-LiHoReCoCo:• It’s C.E. is:

• Thm.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.• Example 6 from textbook.

01

k

i

iki

k rcr

k

i

niin ra

1

k

iinin aca

1

476.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Degenerate Degenerate kk-LiHoReCoCos-LiHoReCoCos

• Theorem 4: Suppose there are t roots r1,…,rt with multiplicities m1,…,mt. Then:

• for all n≥0, where all the α are constants.

• Example 8 from textbook.• Blackboard Ex. 18, 20, 22

t

i

ni

m

j

jjin rna

i

1

1

0,

486.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

The Nonhomogeneous CaseThe Nonhomogeneous Case

• Linear nonhomogeneous recurrences with constant coefficients may (unlike LiHoReCoCos) contain terms F(n) that depend only on n .

• Consider the Tower of Hanoi recurrence an = 2an-1+1.

• Could solve using telescoping. • Instead let’s solve it methodically. Rewrite:

an - 2an-1 = 1 Solve with the RHS set to 0, i.e. solve the

homogeneous case. Add a particular solution to get general

solution.

496.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

The Nonhomogeneous CaseThe Nonhomogeneous Case

• Add a particular solution to get general solution. • I.e. use rule:

• an - 2an-1 = 1 Solve with the RHS set to 0, i.e. solve

• an - 2an-1 = 0• Characteristic equation: r - 2 = 0• so unique root is r = 2. General

solution to homogeneous equation is• an = 12n

General Nonhomogeneous =

General homogeneous

ParticularNonhomogeneous +

506.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

The Nonhomogeneous CaseThe Nonhomogeneous Case

Add a particular solution to get general solution for an - 2an-1 = 1. Use rule:

There are little tricks for guessing particular nonhomogeneous solutions. For example, when the RHS is constant, the guess should also be a constant.

• So guess a particular solution of the form bn=C.• Plug into the original recursion:• 1 = bn – 2bn-1 = C – 2C = -C. Therefore C = -1.• General solution: an = 12n -1.

General Nonhomogeneous =

General homogeneous

ParticularNonhomogeneou

s +

516.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

The Non-homogeneous CaseThe Non-homogeneous Case

• Finally, use initial conditions to get closed solution. In the case of the Towers of Hanoi recursion, initial condition is:

a1 = 1

• Using general solution an = 12n -1 we get:

• 1 = a1 = 121 -1 = 21 –1.

• Therefore, 2 = 21, so 1 = 1.

• Final answer: an = 2n -1

526.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

More ComplicatedMore Complicated

• Example: Find the general solution to recurrence from the bit strings example:

• an = 2an-2 + 2n-3 - an-3

Rewrite as an - 2an-2 + an-3 = 2n-3 and solve homogeneous part:

Characteristic equation: r 3 - 2r +1 = 0. Guess root r = 1 as integer roots. r = 1 works, so divide out by (r -1) to get r 3 - 2r +1 = (r -1)(r 2 +r -1).

536.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

More ComplicatedMore Complicated

• r 3 - 2r +1 = (r -1)(r 2 +r -1).• Quadratic formula on r 2 +r -1:

• r = (-1 5)/2

• So r1 = 1, r2 = (-1+5)/2, r3 = (-1-5)/2

• General homogeneous solution:

• an = 1 + 2[(-1+5)/2]n +

3[(-1-5)/2]n

546.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

More ComplicatedMore Complicated

Nonhomogeneous particular solution to an - 2an-2 + an-3 = 2n-3

Guess the form bn = k 2n. Plug guess in: k 2n - 2k 2n-2

+ k 2n-3 = 2n-3

Simplifies to: k =1/5. So particular solution is bn = 2n/5

Final (nearly) answer: an= 1 + 2[(-1+5)/2]n + 3 [(-1-5)/2]n +

2n/5

General Nonhomogeneous =

General homogeneous

ParticularNonhomogeneous +

556.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

Particular solutions tableParticular solutions table

• 1. Linear combinations work.• 2. If f(n) contains rn and r is a characteristic

root of multiplicity k, then multiply by nk.

f n ac An A n An A n A n An t A n A n A n Ar r R Ar

n A n B nn A n B n

n r r A n A n A n Ar n r

np

ttt

tt

n n

t n ntt

tt

n

( ),

,sin sin coscos sin cos

( )sin

a constant , a constant

, Z

+ +

( )

+

1 02

22

1 0

11

1 0

11

1 0

n

n nA n B n

r n r A n B n( sin cos

cos sin cos

+ )

( + )

566.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

6.A

dva

nce

d C

ou

nti

ng

Tec

hn

iqu

es6.

Ad

van

ced

Co

un

tin

g T

ech

niq

ues

Copyright M.R.K. Krishna Rao 2003

An example from another An example from another textbooktextbookEx. 10.31

( )

+

( )

a a a f n na c c

f n aA

n A n A n AA

r r A rA nA n AA n A n

n n n

nh n n

np

n n

n n

n n

n n n n

n n n n

2 1

1 2

02

32

2 1

4

5

6

7 8

9 10

10 21 03 7

53 27 11 1131 3 76 3 32 3 8 9 3 94 3 3 7 3 7

( ),

( ), ,

( )( ) ( )( ) ( )

( )

Blackboard ExercisesBlackboard Exercises: : 32, 3432, 34