Recurrence Relations
Recurrence Relations
an an/2 an/2
0 or 1
(n-1)
an = an-1 + an-1
= 2 an-1
Base Case: a1 = 2
Number of binary sequences of length n
without consecutive 0s
1
(n-1)
0 1
(n-2)
an = an-1 + an-2
Base Cases:
a1=2 (either 0 or 1 at one position)
a2= 3 (01,10,11)
How many different ways are there he
can go from step 1 to step n ???
an = an-2+ an-1
RAJU
Base Case:
a1= 1
a2 =2
Good Mood à jumps two steps at a time
Bad Mood à one step at a time
Terminology
Answer:
f2 = f1 + f0 = 1 + 0 = 1,
f3 = f2 + f1 = 1 + 1 = 2,
f4 = f3 + f2 = 2 + 1 = 3,
f5 = f4 + f3 = 3 + 2 = 5,
f6 = f5 + f4 = 5 + 3 = 8.
Modeling with Recurrence Relations –
The Tower of Hanoi
In the late nineteenth century, the French
mathematician Édouard Lucas invented a puzzle
consisting of three pegs on a board with disks of
different sizes. Initially all of the disks are on the first
peg in order of size, with the largest on the bottom.
Rules: You are allowed to move the disks one at a
time from one peg to another as long as a larger
disk is never placed on a smaller.
Goal: Using allowable moves, end up with all the
disks on the second peg in order of size with largest
on the bottom.
Modeling with Recurrence Relations –
The Tower of Hanoi (cont.)
First, we use 1 move to transfer the largest disk to the second peg. Then we
transfer the n −1 disks from peg 3 to peg 2 using Hn−1 additional moves.
This can not be done in fewer steps. Hence,
Hn = 2Hn−1 + 1.
The initial condition is H1= 1 since a single disk can be transferred from peg
1 to peg 2 in one move.
Modeling with Recurrence Relations –
The Tower of Hanoi (cont.)
• We can use an iterative approach to solve this recurrence
relation by repeatedly expressing Hn in terms of the previous
terms of the sequence.
Hn = 2Hn−1 + 1
= 2(2Hn−2 + 1) + 1 = 22 Hn−2 +2 + 1
= 22(2Hn−3 + 1) + 2 + 1 = 23 Hn−3 +22 + 2 + 1
⋮
= 2n-1H1 + 2n−2 + 2n−3 + …. + 2 + 1
= 2n−1 + 2n−2 + 2n−3 + …. + 2 + 1 because H1= 1
= 2n − 1 using the formula for the sum of the terms
of a geometric series
Python Code
Recurrence Relations: Application
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 an be the number of valid n-digit codewords.
Find a recurrence relation for an.
a1 = 9 (10 one-digit strings, string 0 not valid)
• 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)
1) 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
9an−1 ways.
2) a valid string of n digits can be obtained by appending a
0 to a string of length n − 1 that is not valid. (This
produces a string with an even number of 0 digits
because the invalid string of length n − 1 has an odd
number of 0 digits.) The number of ways that this can be
done equals the number of invalid (n − 1)-digit strings.
Because there are 10n−1 strings of length n − 1, and an−1 are
valid, there are 10n−1 − an−1 valid n-digit strings obtained
by appending an invalid string of length n − 1 with a 0.
Case 1
…….
9an−1 nth
(n-1)
If this is valid codeword
1,2,3,4,5,6,7,8,9
Lets say an−1 Nine ways
Case 2
…….
nth
(n-1)
10n−1 − an−1 If this is not a valid codeword 0
(means it contains odd number of one way
zero)
an = 9an−1 + (10n−1 − an−1)
= 8an−1 + 10n−1
valid strings of length n.
THANKS