notes - Department of Computer Science and Engineering, CUHK

advertisement
CSCI2110 Tutorial 7:
Recursion
Chow Chi Wang (cwchow ‘at’ cse.cuhk.edu.hk)
Disclaimer: Some of the animation in this presentation are taken from the slides by
Wong Chung Hoi (Hollis), who was a TA in this course in the previous year.
Recursion
Recursion
• Recursion is an important technique in computer science.
The key idea is to reduce a problem into the similar
problems in simpler cases.
• In this tutorial, we will focus on how to setup the
recurrence relations. We will discuss solving recurrence
relations in next week tutorial.
• Tip: After setting up a recurrence relation, remember to
test it’s correctness by trying some base cases.
Fibonacci Variation
(Q1) 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.
Let π‘Ÿπ‘› be the number of pairs of rabbits alive at the end of month 𝑛.
Find a recurrence relation for π‘Ÿ0 , π‘Ÿ1 , π‘Ÿ2 …
Fibonacci Variation
Month 0
(π‘Ÿ0 = 1)
Month 1
(π‘Ÿ1 = 1)
Month 2
(π‘Ÿ2 = 5)
(π‘Ÿ3 = 9)
Month 3
Month 4
(π‘Ÿ4 = 29)
Key:
Baby rabbit pair
Fertile rabbit pair
Fibonacci Variation
• Let 𝑏𝑛 be the number of baby rabbit pairs in month 𝑛.
• Let 𝑓𝑛 be the number of fertile rabbit pairs in month 𝑛.
Note that we have:
π‘Ÿπ‘› = 𝑓𝑛 + 𝑏𝑛 ,
where,
𝑓𝑛 = 𝑓𝑛−1 + 𝑏𝑛−1 = π‘Ÿπ‘›−1 ,
𝑏𝑛 = 4 ∗ 𝑓𝑛−1 = 4 ∗ π‘Ÿπ‘›−2
Therefore:
π‘Ÿπ‘› = π‘Ÿπ‘›−1 + 4 ∗ π‘Ÿπ‘›−2 for 𝑛 ≥ 2, and π‘Ÿ0 = π‘Ÿ1 = 1.
Double Tower of Hanoi
(Q3) In this variation of the Tower of Hanoi, there are three
poles in a row and 2𝑛 disks, two of each of 𝑛 different
sizes, where 𝑛 is any positive integer. Initially one of the
poles contains all the disks placed on top of each other in
pairs of decreasing size. 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 𝑇𝑛 be the minimum number of moves needed to
transfer a tower of 2𝑛 disks from one pole to another. Find
a recurrence relation for 𝑇0 , 𝑇1 , 𝑇2 …
Double Tower of Hanoi
Suppose it takes 𝑇(𝑛 − 1) steps to transfer 2(𝑛 − 1) disks
A
2𝑛 disks
B
C
Double Tower of Hanoi
# of moves: 0
A
2(𝑛 − 1) disks
B
C
Double Tower of Hanoi
# of moves: 𝑇(𝑛 − 1)
A
B
C
Double Tower of Hanoi
# of moves: 𝑇 𝑛 − 1 + 1
A
B
C
Double Tower of Hanoi
# of moves: 𝑇 𝑛 − 1 + 2
A
B
C
Double Tower of Hanoi
# of moves: 2𝑇 𝑛 − 1 + 2
A
B
Therefore : 𝑇(𝑛) = 2𝑇 𝑛 − 1 + 2
C
Double Tower of Hanoi
We have: 𝑇(𝑛) = 2𝑇 𝑛 − 1 + 2, and 𝑇 1 = 2.
𝑇 𝑛
= 2𝑇 𝑛 − 1 + 2
= 2 2𝑇 𝑛 − 2 + 2 + 2
= 4𝑇 𝑛 − 2 + 22 + 2
= 4(2𝑇 𝑛 − 3 + 2) + 22 + 2
= 8𝑇 𝑛 − 3 + 23 + 22 + 2
=β‹―
= 2𝑛−1 βˆ™ 𝑇 1 + 2𝑛 + β‹― + 22 + 2
= 2𝑛 + 2𝑛−1 + β‹― + 22 + 2
= 2𝑛+1 − 2
Double Tower of Hanoi - Extension
(Q4) We now impose an extra constraint: if two disks are of the same
size, we can only have the red disk on top of the grey disk, but
not the other way. Find a recurrence relation for 𝑇0 , 𝑇1 , 𝑇2 …
A
2𝑛 disks
B
C
Double Tower of Hanoi - Extension
Suppose it takes 𝑇(𝑛 − 1) steps to transfer 2(𝑛 − 1) disks
A
2(𝑛 − 1) disks
B
C
Double Tower of Hanoi - Extension
# of moves: 0
A
2(𝑛 − 1) disks
B
C
Double Tower of Hanoi - Extension
# of moves: 𝑇(𝑛 − 1)
A
B
C
Double Tower of Hanoi - Extension
# of moves: 𝑇 𝑛 − 1 + 1
A
B
C
Double Tower of Hanoi - Extension
# of moves: 2𝑇 𝑛 − 1 + 1
A
B
C
Double Tower of Hanoi - Extension
# of moves: 2𝑇 𝑛 − 1 + 2
A
B
C
Double Tower of Hanoi - Extension
# of moves: 3𝑇 𝑛 − 1 + 2
A
B
C
Double Tower of Hanoi - Extension
# of moves: 3𝑇 𝑛 − 1 + 3
A
B
C
Double Tower of Hanoi - Extension
# of moves: 4𝑇 𝑛 − 1 + 3
A
B
Therefore : 𝑇(𝑛) = 4𝑇 𝑛 − 1 + 3
C
Double Tower of Hanoi - Extension
We have:𝑇(𝑛) = 4𝑇 𝑛 − 1 + 3, and 𝑇 1 = 3.
𝑇 𝑛
= 4𝑇 𝑛 − 1 + 3
= 4 4𝑇 𝑛 − 2 + 3 + 3
= 16𝑇 𝑛 − 2 + 3 ∗ (4 + 1)
= 16 4𝑇 𝑛 − 3 + 3 + 4 ∗ 3 + 3
= 43 ∗ 𝑇 𝑛 − 3 + 3 ∗ 4 2 + 4 + 1
=β‹―
= 4𝑛−1 βˆ™ 𝑇 1 + 3 ∗ (4𝑛−2 + β‹― + 4 + 1)
= 3 ∗ (4𝑛−1 + β‹― + 4 + 1)
= 4𝑛 − 1
Tower of Hanoi with Adjacency Requirement
(Q5) This variation of the Tower of Hanoi is essentially the
same as the original Tower of Hanoi, but with an additional
restriction that the disks are only allowed to be moved from
one pole to an adjacent pole. Assume poles A and C are at
the two ends of the row and pole B is in the middle. In this
variation we are not allowed to move disks directly from
pole A to pole C and vice versa.
Let 𝑑𝑛 be the minimum number of moves needed to transfer
a tower of 2𝑛 disks from one pole to another. Find a
recurrence relation for 𝑑0 , 𝑑1 , 𝑑2 …
Tower of Hanoi with Adjacency Requirement
Direct moves from pole A to pole C and the opposite direction are not allowed!
A
B
C
Tower of Hanoi with Adjacency Requirement
Suppose it takes 𝑇(𝑛 − 1) steps to transfer (𝑛 − 1) disks from A to C
A
𝑛 disks
B
C
Tower of Hanoi with Adjacency Requirement
# of moves: 0
A
𝑛 − 1 disks
B
C
Tower of Hanoi with Adjacency Requirement
# of moves: 𝑇(𝑛 − 1)
A
B
C
Tower of Hanoi with Adjacency Requirement
# of moves: 𝑇 𝑛 − 1 + 1
A
B
C
Tower of Hanoi with Adjacency Requirement
# of moves: 2𝑇 𝑛 − 1 + 1
A
B
C
Tower of Hanoi with Adjacency Requirement
# of moves: 2𝑇 𝑛 − 1 + 2
A
B
C
Tower of Hanoi with Adjacency Requirement
# of moves: 3𝑇 𝑛 − 1 + 2
A
B
Therefore : 𝑇(𝑛) = 3𝑇 𝑛 − 1 + 2
C
Tower of Hanoi with Adjacency Requirement
We have:𝑇(𝑛) = 3𝑇 𝑛 − 1 + 2, and 𝑇 1 = 2.
𝑇 𝑛
= 3𝑇 𝑛 − 1 + 2
= 3 3𝑇 𝑛 − 2 + 2 + 2
= 32 βˆ™ 𝑇 𝑛 − 2 + 2 ∗ (3 + 1)
= 32 3𝑇 𝑛 − 3 + 2 + 2 ∗ (3 + 1)
= 33 βˆ™ 𝑇 𝑛 − 3 + 2 ∗ (32 + 3 + 1)
=β‹―
= 3𝑛−1 βˆ™ 𝑇 1 + 2 ∗ (3𝑛−2 + β‹― + 32 + 3 + 1)
= 2 ∗ (3𝑛−1 + 3𝑛−2 + β‹― + 32 + 3 + 1)
= 3𝑛 − 1
Catalan Number
• Recall that the recurrence equation of the form
𝑛−1
π‘Ÿπ‘› =
π‘Ÿπ‘˜ π‘Ÿπ‘›−π‘˜−1
π‘˜=0
has a closed form
1
2𝑛
π‘Ÿπ‘› =
𝑛+1 𝑛
which is called the Catalan number
Triangulated Polygon
• (Q6) Let 𝑇𝑛 be the number of ways to cut an 𝑛 + 2 sides
convex polygon into triangles by connecting vertices with
non-intersecting straight lines. Show that 𝑇𝑛 = 𝐢𝑛 , where
𝐢𝑛 is the 𝑛-th Catalan number. The following hexagons
illustrate the case 𝑛 = 4:
Triangulated Polygon
• T1 = 1
• T2 = 2
• T3 = 5
Triangulated Polygon
• Observation:
• If we fix an edge AB on a polygon, in the final cutting, this edge
must form a triangle with other 𝑛 vertex.
• This triangle partition the polygon into 3 regions
n=4
A
B
Triangulated Polygon
• Observation:
• Tn = T0 Tn-1, where T0 is defined to be 1.
n=4
Tn-1
T0
A
B
Triangulated Polygon
• Observation:
• Tn = T0 Tn-1 + T1Tn-2
n=4
Tn-2
A
T1
B
Triangulated Polygon
• Observation:
• Tn = T0 Tn-1 + T1Tn-2 + T2Tn-3 + …
n=4
Tn-3
A
T2
B
Triangulated Polygon
• Observation:
• 𝑇𝑛 = 𝑇0 𝑇𝑛−1 + 𝑇1 𝑇𝑛−2 + β‹― + 𝑇𝑛−1 𝑇0 =
𝑛−1
π‘˜=0 π‘‡π‘˜ 𝑇𝑛−π‘˜−1
• The closed form is given by Catalan number 𝐢𝑛 .
n=4
Tn-1
T0
A
B
Summary
• The key idea of recursion is to reduce a problem into the
similar problem in simpler cases.
• When setting up a recurrence relation, you assume
problems in simpler cases can be solved. You then need
to think about how these can help solving the problem.
• Tip: After setting up a recurrence relation, always
remember to test it’s correctness by trying some base
cases.
Thank You!
Download