210 likes | 373 Views
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.
E N D
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 • 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
The Towers of Hanoi Source: http://www.mathematik.uni-muenchen.de/~hinz/tower.jpg
The Towers of Hanoi • How should the monks proceed? • Will they make it?
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)
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
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???
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
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?
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!
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
Recursion • Sierpiński triangle • Wacław Sierpiński – Polish mathematician 1882-1969
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
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); }
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!
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; }
Tail recursion • The transformed program doesn’t use any extra memory • The transformation can save space • Exercise: Transform Towers of Hanoi.