2-1 Recurrence Relations
2-1 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
Reference:
https://www.youtube.com/watch?v=Ajy8XweC3L8
def tower (n, source, aux, dest)
if n == 1:
print(“move disk from source”, source, “to dest”, dest)
return
tower(n - 1, source, dest, aux)
print(“move disk from source”, source, “to dest”, dest)
tower(n - 1, aux, source, dest)
Python Code
THANKS