990 likes | 1.82k Views
CSCI2110 – Discrete Mathematics Tutorial 6 Recursion. Wong Chung Hoi (Hollis) 12-7-2011. Agenda. How to solve recurrence Counting Rabbits Double Tower of Hanoi Tower of Hanoi with adjacency requirement Counting strings Triangulated polygon. How to solve recurrence.
E N D
CSCI2110 – Discrete MathematicsTutorial 6Recursion Wong Chung Hoi (Hollis) 12-7-2011
Agenda • How to solve recurrence • Counting Rabbits • Double Tower of Hanoi • Tower of Hanoi with adjacency requirement • Counting strings • Triangulated polygon
How to solve recurrence • Recurrence Equation and Boundary Values • E.g. Rn = Rn-1 + Rn-2 + n,T0 = 0, T1 = 1 • Solving recurrence problem: • Define a recurrence variable, e.g. Rn • Try for a few base cases, e.g. R1, R2,… • Express Rn in terms of Rn-1, Rn-2,… • If it is not possible, try another approach • Find its closed form if necessary, e.g. calculating large n • By observation, guessing or iteration • Verify the closed form • By base cases or induction
Agenda • How to solve recurrence • Counting Rabbits • Double Tower of Hanoi • Tower of Hanoi with adjacency requirement • Counting strings • Triangulated polygon
Counting Rabbits - Problem • Problem • Start with 1 pair of baby rabbit. • Rabbits take 2 months to become an adult. • Adult rabbits gives birth to 3 pairs of baby rabbits every month. • Rabbits NEVER DIE! • Number of pairs of rabbits in the end of the year? • Denote Rn as the number of rabbit after n month. • R0= 1, R1 = 1, R2 = 1, R3 = 3+1=4, R4 = 4+3 = 7 …
Counting Rabbits - Solution • Observation: Rabbit born at nth month = 3Rn-3 • Rn = 3Rn-3 + Rn-1 • By Repeatedly substitution R5 = 10 R6 = 22 R7= 43 R8 = 73 R9 = 139 R10 = 268 R11 = 487 R12 = 904
Agenda • How to solve recurrence • Counting Rabbits • Double Tower of Hanoi • Tower of Hanoi with adjacency requirement • Counting strings • Triangulated polygon
Double tower of Hanoi – Problem • How many moves is needed to move the whole tower? • Denote Tn as the number of moves for moving 2n discs from one pole to another. B C A 2n discs
Double tower of Hanoi – Solution • T1 = ?
Double tower of Hanoi – Solution • T1 = 1
Double tower of Hanoi – Solution • T1 = 2
Double tower of Hanoi – Solution • T1 = 2,T2=?
Double tower of Hanoi – Solution • T1 = 2,T2=1
Double tower of Hanoi – Solution • T1 = 2,T2=2
Double tower of Hanoi – Solution • T1 = 2,T2=3
Double tower of Hanoi – Solution • T1 = 2,T2=4
Double tower of Hanoi – Solution • T1 = 2,T2=5
Double tower of Hanoi – Solution • T1 = 2,T2=6, T3 = ? • What is the recurrence equation?
Double tower of Hanoi – Solution • Observation: • We must first move the top 2(n-1) discs
Double tower of Hanoi – Solution • Observation: • We must first move the top 2(n-1) discs • Then move the 2 largest discs
Double tower of Hanoi – Solution • Observation: • We must first move the top 2(n-1) discs • Then move the 2 largest discs
Double tower of Hanoi – Solution • Observation: • We must first move the top 2(n-1) discs • Then move the 2 largest discs • Move the 2(n-1) discs again
Double tower of Hanoi – Solution • Observation: • We must first move the top 2(n-1) discs • Then move the 2 largest discs • Move the 2(n-1) discs again • Tn = Tn-1 + 2 + Tn-1 = 2Tn-1 + 2
Double tower of Hanoi – Solution • Finding the closed form of Tn = 2Tn-1 + 2 • By iteration: Tn = 2Tn-1 + 2 = 2(2Tn-2 + 2) + 2 = 22Tn-2+22+2 = 22(2Tn-3+2)+22+2 = 23Tn-3+23+22+2 … = 2n-1T1+2n-1+2n-2+…+2 = 2n+2n-1+…+2 = 2(1-2n) / (1-2) = 2n+1-2
Double tower of Hanoi – Solution • Verifying Tn = 2Tn-1+2, T1 = 2 is equivalent to Tn=2n+1-2 • By induction: • Case n = 1, T1 = 21+1-2 = 2 • Assume when n = k-1, they are equivalent • Case n = k, Tk= 2Tk-1+2 = 2(2(k-1)+1-2) + 2 = 2k+1 -2
Double tower of Hanoi – Extension • What about triple, quadruple tower of Hanoi? • What if we have to preserve the order of disc?
Double tower of Hanoi – Extension • What about triple, quadruple tower of Hanoi? • What if we have to preserve the order of disc?
Double tower of Hanoi – Extension • What about triple, quadruple tower of Hanoi? • What if we have to preserve the order of disc?
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = ?
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 1
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 2
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = ?
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 1
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 2
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 3
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 4
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 5
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 6
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 7
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 8
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 9
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 10
Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 11
Double tower of Hanoi – Extension • Observation: • Sn =
Double tower of Hanoi – Extension • Observation: • Sn = Sn-1
Double tower of Hanoi – Extension • Observation: • Sn = Sn-1 + 1
Double tower of Hanoi – Extension • Observation: • Sn = Sn-1 + 1 + Sn-1
Double tower of Hanoi – Extension • Observation: • Sn = Sn-1 + 1 + Sn-1 + 1
Double tower of Hanoi – Extension • Observation: • Sn = Sn-1 + 1 + Sn-1 + 1 + Sn-1