45
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.

CSCI2110 Tutorial 7: Recursion

  • Upload
    jace

  • View
    50

  • Download
    2

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: CSCI2110  Tutorial 7: Recursion

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.

Page 2: CSCI2110  Tutorial 7: Recursion

Recursion

Page 3: CSCI2110  Tutorial 7: 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.

Page 4: CSCI2110  Tutorial 7: Recursion

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

Page 5: CSCI2110  Tutorial 7: Recursion

Fibonacci VariationMonth 0

Month 1

Month 2

Month 3

Month 4

Key:

Baby rabbit pair

Fertile rabbit pair

()

()

()

()

()

Page 6: CSCI2110  Tutorial 7: Recursion

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,

,

Therefore:

for , and .

Page 7: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi

(Q3) In this variation of the Tower of Hanoi, there are three poles in a row and 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 disks from one pole to another. Find a recurrence relation for

Page 8: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi

A B C

disks

Suppose it takes steps to transfer disks

Page 9: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi

A B C

disks

# of moves:

Page 10: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi

A B C

# of moves:

Page 11: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi

A B C

# of moves:

Page 12: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi

A B C

# of moves:

Page 13: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi

A B C

# of moves:

Therefore :

Page 14: CSCI2110  Tutorial 7: Recursion

Double Tower of HanoiWe have: , and .

Page 15: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

A B C

(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

disks

Page 16: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

Suppose it takes steps to transfer disks

A B C

disks

Page 17: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

A B C

disks

# of moves:

Page 18: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

A B C

# of moves:

Page 19: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

A B C

# of moves:

Page 20: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

A B C

# of moves:

Page 21: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

A B C

# of moves:

Page 22: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

A B C

# of moves:

Page 23: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

A B C

# of moves:

Page 24: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - Extension

A B C

# of moves:

Therefore :

Page 25: CSCI2110  Tutorial 7: Recursion

Double Tower of Hanoi - ExtensionWe have:, and .

Page 26: CSCI2110  Tutorial 7: Recursion

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 disks from one pole to another. Find a recurrence relation for

Page 27: CSCI2110  Tutorial 7: Recursion

Tower of Hanoi with Adjacency Requirement

A B C

Direct moves from pole A to pole C and the opposite direction are not allowed!

Page 28: CSCI2110  Tutorial 7: Recursion

Tower of Hanoi with Adjacency Requirement

A B C

disks

Suppose it takes steps to transfer disks from A to C

Page 29: CSCI2110  Tutorial 7: Recursion

Tower of Hanoi with Adjacency Requirement

A B C

disks

# of moves:

Page 30: CSCI2110  Tutorial 7: Recursion

Tower of Hanoi with Adjacency Requirement

A B C

# of moves:

Page 31: CSCI2110  Tutorial 7: Recursion

Tower of Hanoi with Adjacency Requirement

A B C

# of moves:

Page 32: CSCI2110  Tutorial 7: Recursion

Tower of Hanoi with Adjacency Requirement

A B C

# of moves:

Page 33: CSCI2110  Tutorial 7: Recursion

Tower of Hanoi with Adjacency Requirement

A B C

# of moves:

Page 34: CSCI2110  Tutorial 7: Recursion

Tower of Hanoi with Adjacency Requirement

A B C

# of moves:

Therefore :

Page 35: CSCI2110  Tutorial 7: Recursion

We have:, and .

Tower of Hanoi with Adjacency Requirement

Page 36: CSCI2110  Tutorial 7: Recursion

Catalan Number• Recall that the recurrence equation of the form

has a closed form

which is called the Catalan number

Page 37: CSCI2110  Tutorial 7: Recursion

Triangulated Polygon• (Q6) Let be the number of ways to cut an 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 :

Page 38: CSCI2110  Tutorial 7: Recursion

Triangulated Polygon

• T1 = 1

• T2 = 2

• T3 = 5

Page 39: CSCI2110  Tutorial 7: Recursion

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

A B

n = 4

Page 40: CSCI2110  Tutorial 7: Recursion

Triangulated Polygon• Observation:

• Tn = T0 Tn-1, where T0 is defined to be 1.

A B

n = 4

Tn-1

T0

Page 41: CSCI2110  Tutorial 7: Recursion

Triangulated Polygon• Observation:

• Tn = T0 Tn-1 + T1Tn-2

A B

n = 4

T1

Tn-2

Page 42: CSCI2110  Tutorial 7: Recursion

Triangulated Polygon• Observation:

• Tn = T0 Tn-1 + T1Tn-2 + T2Tn-3 + …

A B

n = 4

T2Tn-3

Page 43: CSCI2110  Tutorial 7: Recursion

Triangulated Polygon• Observation:

• The closed form is given by Catalan number .

A B

n = 4

T0

Tn-1

Page 44: CSCI2110  Tutorial 7: Recursion

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.

Page 45: CSCI2110  Tutorial 7: Recursion

Thank You!