28
MODELING WITH RECURRENCE RELATIONS

Modeling with Recurrence Relations

Embed Size (px)

Citation preview

Page 1: Modeling with Recurrence Relations

MODELING WITH

RECURRENCE RELATIONS

Page 2: Modeling with Recurrence Relations

CONTENTS

COUNTING TECHNIQUES

MOTIVATION

RECURSIVE DEFINITIONS

RECURRENCE RELATIONS

MODELING WITH RECURRENCE RELATIONS

(PROBLEMS AND SOLUTIONS)

Page 3: Modeling with Recurrence Relations

– Product rule

– Sum rule

– The Inclusion-Exclusion Principle

– The Pigeonhole Principle

– Permutations

– Combinations

1 2 1 2 1 2A A A A A A

Counting technique

Number of ways = n1n2 Β·Β·Β· nm

Number of ways = n1 + n2 +…+ nm

If N objects are placed into k boxes, then there is at least one box

containing at least objects.

N k

!, 1 2 1

!

nP n r n n n n r

n r

!,

! !

n nC n r

r r n r

Page 4: Modeling with Recurrence Relations

MOTIVATION

The number of bacteria in a colony doubles every hour. If the colony begins with 5 bacteria, how many will be present in n hours?

Let an : the number of bacteria at the end of n hoursThen;

an = 2an-1, a0=5

What we are doing actually?

We find a formula/model for an (the relationship between an and a0) – this is called recurrence relations between the term of sequence.

Since bacteria double every hour Initial condition

By using

recursive

definition

Solution:

Page 5: Modeling with Recurrence Relations

RECURSIVE DEFINITIONS

Recursion – a process to define an object explicitly

The object can be a sequence, function or set (for BCT2078 purposes)

The ideas – construct new elements from known elements.

Process – specify some initial elements in a basis step and provide a rule for constructing new

elements from those we already have in the recursive step.

Mathematical Induction can be used to prove the result.

The given

complex

problem

This

simple

version

can be

solved

This simple

version

can be

solved

Can be

solved if

Can be

solved if

Can be

solved if

an = 2an-1 an-1 = 2an-2, a1 = 2a0, a0=5

Page 6: Modeling with Recurrence Relations

RECURRENCE RELATION

A recursive algorithm provides the solution of a problem of size n in term of the solutions of one or more instances of the same problem in smaller size.

The initial condition specify the terms that precede the first term where the recurrence relation takes effect.

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 call solution of a recurrence relation if its term satisfy the recurrence relation.

an = 2an-1

a0 = 5 Initial condition

Recurrence relationRecursive

definition

Page 7: Modeling with Recurrence Relations

Modeling with Recurrence Relation

We can use recurrence relation to model a wide variety of problems

such as

Finding a compound interest

Counting a population of rabbits

Determining the number of moves in the Tower of Hanoi puzzle

Counting bit strings

Page 8: Modeling with Recurrence Relations

RABBITS

A young pair of rabbits, one of each sex, is placed on an

island.

A pair of rabbits does not breed until they are 2 months old.

After they are 2 months old, each pair of rabbits produces

another pair each month.

Assume that none of the rabbits die.

How many rabbits are there after n months?

Page 9: Modeling with Recurrence Relations

RABBITS

Let 𝒇𝒏 denote the number of pairs of rabbits after n

months.

π’‡πŸ = 1 { reproducing pairs = 0, young pairs = 1 }

π’‡πŸ = 1 { reproducing pairs = 0, young pairs = 1 }

π’‡πŸ‘ = 2 { reproducing pairs = 1, young pairs = 1 }

π’‡πŸ’ = 3 { reproducing pairs = 1, young pairs = 2 }

π’‡πŸ“ = 5 { reproducing pairs = 2, young pairs = 3 }

π’‡πŸ”= 8 { reproducing pairs = 3, young pairs = 5 }

Page 10: Modeling with Recurrence Relations

RABBITS

The rabbit population can be modeled by a recurrence relation.

At the end of the first month, the number of rabbits on the island is π’‡πŸ = 1.

At the end of the second month, the number of rabbits on the island is π’‡πŸ = 1.

The number of pairs of rabbits after n months 𝒇𝒏 is equal to the number of

pairs of rabbits from the previous month π’‡π’βˆ’πŸ plus the number of pairs of

newborn rabbits, which equals π’‡π’βˆ’πŸ, since each newborn pair comes from a

pair that is at least two months old, so

𝒇𝒏 = π’‡π’βˆ’πŸ + π’‡π’βˆ’πŸ for n >= 3.

Page 11: Modeling with Recurrence Relations

SOLVING RABBITS PROBLEM

The recurrence system

Initial conditions: π’‡πŸ = 1, π’‡πŸ = 1

Recurrence relation: 𝒇𝒏 = π’‡π’βˆ’πŸ + π’‡π’βˆ’πŸ for n >= 3. (Linear Homogeneous RR

with const coeff.)

For small values of n, we get

(π’‡πŸ,, π’‡πŸ , π’‡πŸ‘,...) = (1,1,2,3,5,8,13,...)

Characteristic equation : π’“πŸ- 𝒓 -1 =0

Characteristic roots: π’“πŸ= (1+√5)/2 and π’“πŸ = (1- √5)/2

Page 12: Modeling with Recurrence Relations

SOLVING RABBITS PROBLEM

Therefore, 𝒇𝒏 = π‘¨πŸβˆ—((1+√5)/2)𝒏

+ π‘¨πŸβˆ—((1βˆ’βˆš5)/2)𝒏

, π‘¨πŸ and π‘¨πŸ are

constants

π‘¨πŸ = √5/5 ; π‘¨πŸ = βˆ’βˆš5/5

𝒇𝒏 = π‘¨πŸβˆ—((1+√5)/2)𝒏

+ π‘¨πŸβˆ—((1βˆ’βˆš5)/2)𝒏

=𝟏

βˆšπŸ“βˆ—(1+√5)/2)

𝒏-

𝟏

βˆšπŸ“βˆ—(1βˆ’βˆš5)/2)

𝒏

Page 13: Modeling with Recurrence Relations

TOWER OF HANOI

Initially, n discs are placed on the first peg. Move the n discs one at a time

from one peg to another such that no larger disc is ever placed on a

smaller disc.

Goal: Move the discs from peg 1 to peg 2.

Page 14: Modeling with Recurrence Relations

TOWER OF HANOI

Let 𝐻𝑛 denote the number of moves needed to solve the tower of Hanoi

problem with n discs.

1) Move the top n-1 discs from peg 1 to peg 3 using Hn-1 moves.

Page 15: Modeling with Recurrence Relations

TOWER OF HANOI

Let 𝐻𝑛 denote the number of moves needed to solve the tower of Hanoi

problem with n discs.

1) Move the top n-1 discs from peg 1 to peg 3 using Hn-1 moves.

2) Move the largest disc from peg 1 to peg 2.

Page 16: Modeling with Recurrence Relations

TOWER OF HANOI

Let 𝐻𝑛 denote the number of moves needed to solve the tower of Hanoi problem with n

discs.

1) Move the top n-1 discs from peg 1 to peg 3 using Hn-1 moves.

2) Move the largest disc from peg 1 to peg 2.

3) Move the n-1 discs from peg 3 to peg 2 using Hn-1 moves.

Page 17: Modeling with Recurrence Relations

TOWER OF HANOI

Let 𝐻𝑛 denote the number of moves needed to solve the tower of Hanoi problem with n

discs.

1) Move the top n-1 discs from peg 1 to peg 3 using π»π‘›βˆ’1 moves.

2) Move the largest disc from peg 1 to peg 2.

3) Move the n-1 discs from peg 3 to peg 2 using π»π‘›βˆ’1 moves.

Page 18: Modeling with Recurrence Relations

TOWER OF HANOI

Let 𝐻𝑛 denote the number of moves needed to solve the tower of Hanoi

problem with n discs. Thus the no of moves is given by

Where:

1 12 1 ; 1n nH H H

Hn-1 moves

Moves the largestdisk to peg 2

transfer the top ofn-1 disks to peg 2

1 moveHn-1 moves

transfer the top ofn-1 disks to peg 3

+ +

Page 19: Modeling with Recurrence Relations

SOLVING TOWER OF HANOI PROBLEM

Initial condition: 𝐻1 = 1

Recurrence relation: 𝐻𝑛 = 2π»π‘›βˆ’1+1 for n >= 2.

For small values of n, we get

(𝐻1𝐻2𝐻3𝐻4,...) = (1,3,7,15,...)

Therefore, we can guess that 𝐻𝑛 = πŸπ’ - 1

Page 20: Modeling with Recurrence Relations

SOLVING TOWER OF HANOI PROBLEM

How can we prove that 𝑯𝒏 = πŸπ’-1 holds for all n>=1?

By induction.

Base Step: π‘―πŸ = 1 = 𝟐𝟏 βˆ’ 𝟏, so our claim holds for n=1.

Inductive Step: Suppose that 𝐻𝑛 = πŸπ’ - 1 holds for some n>=1.

It follows that

𝑯𝒏+𝟏 = 2𝐻𝑛 + 1 = 2(πŸπ’- 1 ) + 1 = πŸπ’+𝟏-2+1= πŸπ’+𝟏-1

Therefore, the claim follows by induction on n.

Page 21: Modeling with Recurrence Relations

CODEWORD ENUMERATION

A computer system considers a string of decimal digits a valid

codeword if it contains an even number of 0 digits.

For instance, 1230407869 is valid, whereas 120987045608 is not valid.

Let 𝒂𝒏 be the number of valid n-digit codewords.

Page 22: Modeling with Recurrence Relations

CODEWORD ENUMERATION

Note that π’‚πŸ = 9

A recurrence relation can be derived for this sequence by

considering how a valid n-digit string can be obtained from

strings of n-1 digits.

Two ways to form a valid string with n digits from a string with

one fewer digit.

Page 23: Modeling with Recurrence Relations

CODEWORD ENUMERATION

First, a valid string of n digits can be obtained by appending a valid

string of n βˆ’ 1 digits with a digit other than 0. This appending can be

done in nine ways. Hence, a valid string

with n digits can be formed in this manner in 9π‘Žπ‘›βˆ’1 ways

Page 24: Modeling with Recurrence Relations

CODEWORD ENUMERATION

Second, a valid string of n digits can be obtained by appending a 0 to a string of

length n βˆ’ 1 that is not valid. The number of ways that this can be done equals

the number of invalid (n βˆ’ 1)-digit strings. Because there are πŸπŸŽπ’βˆ’πŸ strings of length

n βˆ’ 1, and π’‚π’βˆ’πŸ are valid, there are πŸπŸŽπ’βˆ’πŸ βˆ’ π’‚π’βˆ’πŸ valid n-digit strings obtained by

appending an invalid string of length n βˆ’ 1 with a 0.

Since all valid strings of length n are produced in one of these two ways, it follows

that there are

𝒂𝒏 = 9π’‚π’βˆ’πŸ + (πŸπŸŽπ’βˆ’πŸ βˆ’ π’‚π’βˆ’πŸ)

= 8π’‚π’βˆ’πŸ + πŸπŸŽπ’βˆ’πŸ

valid strings of length n.

Page 25: Modeling with Recurrence Relations

SOLVING CODEWORD ENUMERATION

Initial condition: π‘Ž1 = 1

Recurrence relation: π‘Žπ‘› = 8π‘Žπ‘›βˆ’1 + 10π‘›βˆ’1 for n >= 2. (Non-homogeneous RR with const coeff.)

=> π‘Žπ‘› - 8π‘Žπ‘›βˆ’1 = 10π‘›βˆ’1

We need to solve 𝒂𝒏 = 𝒂𝒏(𝒉)

+ 𝒂𝒏(𝒑)

Characteristic equation: r - 8 = 0

Characteristic root: π‘Ÿ1 = 8

Ξ² = 10

Page 26: Modeling with Recurrence Relations

SOLVING CODEWORD ENUMERATION

Therefore, 𝒂𝒏(𝒉)

= π‘¨πŸβˆ— πŸ–π’ , where π‘¨πŸ is constant

And, 𝒂𝒏(𝒑)

= π’“πŸŽ βˆ—(π‘·πŸ βˆ— πŸπŸŽπ’βˆ’πŸ) , where π‘·πŸ is a constant

=> π‘·πŸ(πŸπŸŽπ’βˆ’πŸ βˆ’ πŸ– βˆ— πŸπŸŽπ’βˆ’πŸ) = πŸπŸŽπ’βˆ’πŸ , since π‘Žπ‘› - 8π‘Žπ‘›βˆ’1 = 10π‘›βˆ’1

=> π‘·πŸ = 5

=> 𝒂𝒏(𝒑)

= 5βˆ— πŸπŸŽπ’βˆ’πŸ

Therefore, 𝒂𝒏 = 𝒂𝒏(𝒉)

+ 𝒂𝒏(𝒑)

= (π‘¨πŸβˆ— πŸ–π’) + (5 βˆ— πŸπŸŽπ’βˆ’πŸ)

=> π‘¨πŸ = Β½ (π’‚πŸ = 9)

Page 27: Modeling with Recurrence Relations

SOLVING CODEWORD ENUMERATION

Therefore,

𝒂𝒏 = 𝒂𝒏(𝒉)

+ 𝒂𝒏(𝒑)

= (π‘¨πŸβˆ— πŸ–π’) + (5 βˆ— πŸπŸŽπ’βˆ’πŸ)

= (1/2 βˆ— πŸ–π’) + (5 βˆ— πŸπŸŽπ’βˆ’πŸ)

=𝟏

𝟐(πŸ–π’ + πŸπŸŽπ’)

Page 28: Modeling with Recurrence Relations

THAT’S ALL : THANK YOU