0% found this document useful (0 votes)
59 views

2-1 Recurrence Relations

The document discusses recurrence relations and provides examples of using recurrence relations to model sequences. It defines what a recurrence relation is and provides examples of solving simple recurrence relations. It also uses recurrence relations to model the number of moves needed to solve the Tower of Hanoi puzzle, deriving the recurrence relation Hn=2Hn-1+1 and solving it to find a closed-form solution of 2n-1 moves for n disks. Python code is also provided to implement a recursive solution to the Tower of Hanoi problem.

Uploaded by

Rohan Srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

2-1 Recurrence Relations

The document discusses recurrence relations and provides examples of using recurrence relations to model sequences. It defines what a recurrence relation is and provides examples of solving simple recurrence relations. It also uses recurrence relations to model the number of moves needed to solve the Tower of Hanoi puzzle, deriving the recurrence relation Hn=2Hn-1+1 and solving it to find a closed-form solution of 2n-1 moves for n disks. Python code is also provided to implement a recursive solution to the Tower of Hanoi problem.

Uploaded by

Rohan Srivastava
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

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
What would be the 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

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)

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

You might also like