Tutorial 12: Solving recurrence
Gary Sham
November 25, 2013
Disclaimer: Some of the animation in this presentation are taken from the
slides by Chow Chi Wang , who was a TA in this course in the previous year.
Solving recurrence
We want to find a closed form for the recurrence relations
e.g. For the Fibonacci sequence, instead of√ √
Fn = Fn−1 + Fn−2 , we want Fn = √15 × (( 1+2 5 )n − ( 1−2 5 )n )
How to solve?
Iteration
Guessing and prove by induction
Distinct-Roots Theorem for Second Order Recurrence
Relation
Question
Consider the recurrence relation:
Tn = 3Tn−1 + 2, and T1 = 2
Find the closed form solution.
Question
Consider the recurrence relation:
Tn = 3Tn−1 + 2, and T1 = 2
Find the closed form solution.
Solution
Tn = 3Tn−1 + 2
= 3(3Tn−2 + 2) + 2
= 32 · Tn−2 + 3 · 2 + 2
= ···
= 3n−1 · T1 + 3n−2 · 2 + 3n−3 · 2 + · · · + 2
= 2(3n−1 + 3n−2 + · · · + 1)
= 3n − 1
Second Order Recurrence Relation
an = Aan−1 + Ban−2
A and B are real numbers and B 6= 0
e.g. For Fibonacci sequence, A = B = 1
Distinct-Roots Theorem
For a recurrence relation an = Aan−1 + Ban−2
A and B are real numbers and B 6= 0
If t 2 − At − B = 0 has two distinct roots r and s, then
an = Cr n + Dsn for some C and D
Reminder: the quadratic formula:
2
For quadratic formula ax
√ + bx + c = 0
−b± b2 −4ac
The roots are x = 2a
Question
(Revisit) Fibonacci Variant taught in tutorial 11:
We have a single pair of rabbits (male and female) initially.
Assume that:
(a) the rabbit pairs are not fertile during their first month of life,
but thereafter give birth to four new male/female pairs at the
end of every month;
(b) the rabbits will never die.
In tutorial 11 we know the recurrence relation fn = fn−1 + 4fn−2 ,
f0 = f1 = 1, find the closed form solution.
Solution
Solve the quadratic equation t 2 − t − 4 = 0
√
1± (−1)2 −4(−4)
t= 2
√ √
1+ 17 1− 17
r= 2 , s = 2
√ √
fn = C( 1+2 17 )n + D( 1−2 17 )n
√ √
f0 = 1 = C( 1+2 17 )0 + D( 1−2 17 )0 =C+D
√ √
f1 = 1 = C( 1+2 17 )1 + D( 1−2 17 )1 =C−D
√ √
17+1 17−1
By solution C and D, we get C = √ and D = √
2 17 2 17
√ √ √ √
17+1
∴ fn = √ ( 1+2 17 )n + 17−1
√ ( 1−2 17 )n
2 17 2 17
Question
(Exam 2010 Q3)
You want to climb a flight of n stairs, and on each step you only
climb 1 or 2 stairs.
Let fn be the number of different ways reaching the top. For
example, when n = 3, we have fn = 3, and the three ways are
(1, 1, 1), (1, 2) and (2, 1).
Solution
When we are at the top (the n-th stair), it can be reached
by the n − 1 stair or the n − 2 stair directly.
Each way in fn−1 can produce a way in fn by climbing 1
stair.
Each way in fn−2 can produce a way in fn by climbing 2
stair directly.
Note that we cannot count the way that in fn−2 and climb 1
stair for two times, which has already been counted in a
way in fn−1
The recurrence relation: fn = fn−1 + fn−2
Solve it as it is a Second Order Recurrence Relation.
Question
Variation of the Tower of Hanoi:
There are three poles in a row and 2n disks, two of each of n
different sizes.
Disks are transferred one by one from one pole to another with
the following restrictions:
(a) At no time may a larger disk be placed on top of a smaller
disk;
(b) Any disk may be placed on top of another disk of the same
size.
Let Tn be the number of move needed to transfer all disks from
A to C, and the order of the disks of the same size is not
important.
Solution
We assume we know the answer of n − 1 version of the
problem:
Solution
(Cont.)
Total step: Tn−1
Solution
(Cont.)
Total step: Tn−1 + 1
Solution
(Cont.)
Total step: Tn−1 + 2
Solution
(Cont.)
Total step: 2Tn−1 + 2
∴ we get the recurrence relation Tn = 2Tn−1 + 2
This recurrence relation can be solved by iteration.
Question
Variation of the Tower of Hanoi:
It is the same as the original problem, but with an additional
restriction that the disks are only allowed to be moved to
adjacent pole, e.g. A → B, B → C and the opposition directions
are allowed, but A → C and C → A are not allowed.
Solution
Again, we assume we can solve the n − 1 version:
Solution
(Cont.)
Total step: Tn−1
Note that Tn−1 denotes ”the number of steps to move n − 1
disks from A to C”, so the number of steps we use here is Tn−1
and NOT 2Tn−1
Solution
(Cont.)
Total step: Tn−1 + 1
Solution
(Cont.)
Total step: 2Tn−1 + 1
Solution
(Cont.)
Total step: 2Tn−1 + 2
Solution
(Cont.)
Total step: 3Tn−1 + 2
∴ we get the recurrence relation Tn = 3Tn−1 + 2
This recurrence relation can be solved by iteration.
Question
For a permutation {a1 , a2 , a3 , · · · , an }, we call it is a
231-avoiding permutation if there does not exist
1 ≤ i < j < k ≤ n such that aj > ai > ak .
e.g. 123 and 321 are good but 231 is not, 21543 is good but
23541 is not.
Find the total number of 231-avoiding permutation.
Solution
Consider the largest number n, there are n possibilities of
its position.
Let its position be i, then we separate the permutation into
two parts, one with i − 1 numbers and another with n − i
numbers.
If there exists a number x in the first part and a number y
in the second part, where y < x, then we come up with a
231-pattern: x → the largest number → y
i.e. For all number in the first part, it must be smaller than
all number in the second part.
Solution
(Cont.)
For two permutations, we can easily meet that restriction
by adding a constant to the second permutation.
e.g. {1, 2} and {3, 1, 2} to {1, 2} and
{3 + 2 = 5, 1 + 2 = 3, 2 + 2 = 4}
So we can treat the first part as a permutation of
{1, 2, · · · , i − 1} and the second part as a permutation of
{i, i + 1, · · · , n − 1}
Moreover, those two parts of permutation must also be
231-avoiding.
We get the recurrence relation fn = ni=1 fi−1 fn−i
P
FYI, that is actually the Catalan number.
Q&A