Recurrence Relations
• How to think recursively??
• How to write and solve Recurrence Relations ??
Eating an Apple
an an/2 an/2
an/4 an/4 an/4 an/4
an = an/2 + an/2
= 2 an/2
Base Case: a1 = 1 Sequence {an}
(Initial Condition)
Binary sequence of length n
n
0 or 1
(n-1)
an– number of binary sequences of length n
a4– number of binary sequences of length 4
ak– number of binary sequences of length k
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 ???
If he takes first step, rest of the ways- an-1
If he takes second step, rest ways- an-2
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
• A sequence is a discrete structure used to
represent an ordered list.
• {an} - notation to describe the sequence
• an represents an individual term of the
sequence {an}
• For example, 1, 2, 3, 5, 8 is a sequence with
five terms and 1, 3, 9, 27, 81 , . . . , 3n, . . . is an
infinite sequence.
Definition
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³ n0, where n0 is
a non-negative integer.
A sequence is called a solution of a recurrence relation if its terms
satisfy the recurrence relation.
Example: Given the recurrence relation an=2an-1 - an-2
3, 5, 7, 9, 11, … satisfies the recurrence relation.
2, 3, 4, 5, 6, … also satisfies the recurrence relation.
The initial conditions for a sequence specify the terms that
precede the first term where the recurrence relation takes effect.
Questions about Recurrence Relations
Example 1:
Let {an} be a sequence that satisfies the
recurrence relation an = an-1 + 3 for n =
1,2,3,4,…. and suppose that a0 = 2.
What are a1 , a2 and a3?
[Here a0 = 2 is the initial condition.]
Solution: We see from the recurrence relation that
a1 = a0 + 3 = 2 + 3 = 5
a2 = 5 + 3 = 8
a3 = 8 + 3 = 11
Fibonacci Sequence
Definition:
Define the Fibonacci sequence, f0 , f1 , f2, …, by:
Initial Conditions: f0 = 0, f1 = 1
Recurrence Relation: fn = fn-1 + fn-2
Example: Find f2 , f3 , f4, f5 and f6 .
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.)
The Initial Position in the Tower of Hanoi Puzzle
Modeling with Recurrence Relations –
The Tower of Hanoi (cont.)
Solution: Let {Hn} denote the number of moves needed to solve the Tower of
Hanoi Puzzle with n disks. Set up a recurrence relation for the sequence
{Hn}. Begin with n disks on peg 1.
We can transfer the top n −1 disks,
following the rules of the puzzle,
to peg 3 using Hn−1 moves.
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