1 / 92

CSCI2110 – Discrete Mathematics Tutorial 6 Recursion

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.

terry
Télécharger la présentation

CSCI2110 – Discrete Mathematics Tutorial 6 Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI2110 – Discrete MathematicsTutorial 6Recursion Wong Chung Hoi (Hollis) 12-7-2011

  2. Agenda • How to solve recurrence • Counting Rabbits • Double Tower of Hanoi • Tower of Hanoi with adjacency requirement • Counting strings • Triangulated polygon

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

  4. Agenda • How to solve recurrence • Counting Rabbits • Double Tower of Hanoi • Tower of Hanoi with adjacency requirement • Counting strings • Triangulated polygon

  5. 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 …

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

  7. Agenda • How to solve recurrence • Counting Rabbits • Double Tower of Hanoi • Tower of Hanoi with adjacency requirement • Counting strings • Triangulated polygon

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

  9. Double tower of Hanoi – Solution • T1 = ?

  10. Double tower of Hanoi – Solution • T1 = 1

  11. Double tower of Hanoi – Solution • T1 = 2

  12. Double tower of Hanoi – Solution • T1 = 2,T2=?

  13. Double tower of Hanoi – Solution • T1 = 2,T2=1

  14. Double tower of Hanoi – Solution • T1 = 2,T2=2

  15. Double tower of Hanoi – Solution • T1 = 2,T2=3

  16. Double tower of Hanoi – Solution • T1 = 2,T2=4

  17. Double tower of Hanoi – Solution • T1 = 2,T2=5

  18. Double tower of Hanoi – Solution • T1 = 2,T2=6, T3 = ? • What is the recurrence equation?

  19. Double tower of Hanoi – Solution • Observation: • We must first move the top 2(n-1) discs

  20. Double tower of Hanoi – Solution • Observation: • We must first move the top 2(n-1) discs • Then move the 2 largest discs

  21. Double tower of Hanoi – Solution • Observation: • We must first move the top 2(n-1) discs • Then move the 2 largest discs

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

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

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

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

  26. Double tower of Hanoi – Extension • What about triple, quadruple tower of Hanoi? • What if we have to preserve the order of disc?

  27. Double tower of Hanoi – Extension • What about triple, quadruple tower of Hanoi? • What if we have to preserve the order of disc?

  28. Double tower of Hanoi – Extension • What about triple, quadruple tower of Hanoi? • What if we have to preserve the order of disc?

  29. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = ?

  30. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 1

  31. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 2

  32. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3

  33. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = ?

  34. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 1

  35. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 2

  36. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 3

  37. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 4

  38. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 5

  39. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 6

  40. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 7

  41. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 8

  42. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 9

  43. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 10

  44. Double tower of Hanoi – Extension • Let Sn be the number of moves for 2n discs that preserves order. • S1 = 3, S2 = 11

  45. Double tower of Hanoi – Extension • Observation: • Sn =

  46. Double tower of Hanoi – Extension • Observation: • Sn = Sn-1

  47. Double tower of Hanoi – Extension • Observation: • Sn = Sn-1 + 1

  48. Double tower of Hanoi – Extension • Observation: • Sn = Sn-1 + 1 + Sn-1

  49. Double tower of Hanoi – Extension • Observation: • Sn = Sn-1 + 1 + Sn-1 + 1

  50. Double tower of Hanoi – Extension • Observation: • Sn = Sn-1 + 1 + Sn-1 + 1 + Sn-1

More Related