1 / 19

Recursion!

Recursion!. Question. Stephen King’s books J. R. R. Tolkien’s books Recursion Do they have something in common?. Answer:. J. R. R. Tolkien has written “ Two towers ” Stephen King has written Dark Tower books The Towers of Hanoi !. The Towers of Hanoi.

talbot
Download Presentation

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. Recursion!

  2. Question • Stephen King’s books • J. R. R. Tolkien’s books • Recursion Do they have something in common?

  3. Answer: • J. R. R. Tolkien has written “Two towers” • Stephen King has written Dark Tower books • The Towers of Hanoi!

  4. The Towers of Hanoi • The Legend. In an ancient city in India, so the legend goes, monks in a temple have to move a pile of 64 sacred disks from one location to another. The disks are fragile; only one can be carried at a time. A disk may not be placed on top of a smaller, less valuable disk. And, there is only one other location in the temple (besides the original and destination locations) sacred enough that a pile of disks can be placed there. .jedi .jedi .jedi .jedi Source: http://www.math.toronto.edu/mathnet/games/towers.html

  5. The Towers of Hanoi Source: http://www.mathematik.uni-muenchen.de/~hinz/tower.jpg

  6. The Towers of Hanoi • How should the monks proceed? • Will they make it?

  7. The Towers of Hanoi • Recursive solution! • For N = 0 do nothing • Move the top N-1 disks from Src to Aux (using Dst as an intermediary peg) • Move the bottom disks from Src to Dst • Move N-1 disks from Aux to Dst (using Src as an intermediary peg) • The first call: Solve(3, 1, 2, 3)

  8. The Towers of Hanoi • Move from Src to Dst • Move from Src to Aux • Move from Dst to Aux • Move from Src to Dst • Move from Aux to Src • Move from Aux to Dst • Move from Src to Dst

  9. The Towers of Hanoi • How much time will monks spend? ¹ • For one disk, only one move is necessary • For two disks, we need three moves • For n disks???

  10. The Towers of Hanoi • Let Tn denote the number of moves needed to move n disks. • T1 = 1 • T2 = 3 • Tn = 2*Tn-1 + 1

  11. The Towers of Hanoi • Let Tn denote the number of moves needed to move n disks. • T1 = 1 • T2 = 3 • Tn = 2*Tn-1 + 1 • Tn = 2n - 1 • How to prove it?

  12. Induction! • Base case: T1 = 1 = 21 - 1. OK! • Inductive hypothesis: Tn = 2n -1 • Inductive step: we show that: Tn+1=2n+1-1. Tn+1=2*Tn+1=2*(2n-1)+1= =2n+1-2 + 1= 2n+1-1. QED!

  13. Towers of Hanoi • Suppose it takes one minute for a monk to move a disk. The whole task hence would take 264-1 minutes = (210)6*24-1 minutes ≈ (103)6*15 minutes ≈ 25*1016 hours ≈ 1016 days = 1000000000000000 days ≈ the age of universe

  14. Recursion • Sierpiński triangle • Wacław Sierpiński – Polish mathematician 1882-1969

  15. Sierpiński Triangle • Draw a black triangle • Draw a white triangle in the middle of the triangle. • Call the procedure for three left black triangles

  16. Tail recursion • Consider the following method: • int last(int n, int []arr) // returns the last element in arr // starting from n // -1 if n is too large { if (n > arr.length - 1) return –1; if (n == arr.length – 1) return arr[n]; return last(n+1, arr); }

  17. Tail recursion • The recursive function call is the last command in the method • We can transform then recursion into iteration • Some compilers can do it!

  18. Tail recursion • int last(int n, int []arr) // returns the last element in arr // starting from n { while (n < arr.length – 1) n++; if (n == arr.length – 1) return arr[n]; else return –1; }

  19. Tail recursion • The transformed program doesn’t use any extra memory • The transformation can save space • Exercise: Transform Towers of Hanoi.

More Related