Recursion - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Recursion

Description:

The Towers of Hanoi. Move from Src to Dst. Move from Src to Aux. Move from Dst to Aux ... Towers of Hanoi. Suppose it takes one minute for a monk to move a disk. ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 20
Provided by: csCor
Category:
Tags: hanoi | recursion

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion!
2
Question
  • Stephen Kings books
  • J. R. R. Tolkiens 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/hin
z/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 2Tn-1 1

11
The Towers of Hanoi
  • Let Tn denote the number of moves needed to move
    n disks.
  • T1 1
  • T2 3
  • Tn 2Tn-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
  • Tn12n1-1.
  • Tn12Tn12(2n-1)1
  • 2n1-2 1 2n1-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)624-1 minutes
  • (103)615 minutes
  • 251016 hours
  • 1016 days
  • 1000000000000000 days
  • the age of universe

14
Recursion
  • Sierpinski triangle
  • Waclaw Sierpinski Polish mathematician
    1882-1969

15
Sierpinski 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 gt arr.length - 1)
  • return 1
  • if (n arr.length 1)
  • return arrn
  • return last(n1, 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 lt arr.length 1)
  • n
  • if (n arr.length 1)
  • return arrn
  • else
  • return 1

19
Tail recursion
  • The transformed program doesnt use any extra
    memory
  • The transformation can save space
  • Exercise Transform Towers of Hanoi.
Write a Comment
User Comments (0)
About PowerShow.com