Recursion - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Recursion

Description:

Order of monks, the priests of Brahma, in Benares are working on this puzzle ... Even if the monks move a disk every second. It'll take them 584 billion years ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 38
Provided by: anselmo9
Category:
Tags: monks | recursion

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • COMP 114
  • Tuesday February 26

2
Announcements
  • Program 2 due, 1100 AM Thursday
  • Dont forget
  • Folder, envelope
  • Clean, working code
  • No bugs
  • Good documentation!!!

3
Topics
  • Last Time
  • File I/O
  • This Class
  • GUI Message Box
  • Recursion

4
Message Box
  • Used to signal errors
  • Several types, but they all work the same.
  • Demo Message Box Project

5
Recursion
  • In computer science, recursion is when a method
    calls itself.
  • Not as crazy as it seems.
  • First, lets look at a non-programming example

6
Recursive Definition
  • Define a list of number separated by commas
  • Example 23, 27, 31, 1
  • like this
  • a LIST is a number
  • or a LIST is a number comma LIST

7
Example
  • Try to see whether 23, 27, 31, 1 is a list
  • Apply definition

LIST number or LIST number , LIST
23, 27, 31, 1
23, LIST
27, LIST
31, LIST
1
8
Erroneous Examples
  • 2 . 5 , 6
  • 5, A

LIST number or LIST number , LIST
Compilers work like this!
9
Base Case
  • Notice that we had a case (LIST number) that
    does no recursion
  • Thats called the base case
  • Otherwise, wed have infinite recursion
  • Go back to example we stopped at base case

LIST number or LIST number , LIST
10
Recursion Used in Math
  • Induction
  • Recursive proofs
  • Also have to provide a base case
  • The one thats trivial to prove

11
What About Programs?
  • We can solve a lot of math problems recursively.
  • Example

public static int sum(int n) if(n lt
1) return 0 return( sum(n 1) n )
12
Example
public static int sum(int n) if(n lt
1) return 0 return( sum(n 1) n )
sum(4)
13
Progress
  • Notice that we must make progress toward base
    case
  • There is steady reduction in size of problem
  • Otherwise, trouble.

14
Iterative Solution
  • Of course, for this problem a recursive solution
    doesnt make sense.
  • Elegant but slower than

public static int sum(int n) int result
0 for(int i 1 i lt n i) result
i return(n)
15
How Does This Work?
  • When method is called (recursive or not), new
    space is created for local variables
  • In data structure called a stack
  • Every time method is called, local variables are
    different
  • Cant get to values in old local variables. They
    are out of scope

16
Costs of Recursion
  • Method call overhead
  • Stack overhead
  • Basically, space needs to be allocated for method
    variables
  • Instructions to invoke method
  • Recursive solutions can be made Iterative
  • Sometimes hard to follow, though

17
Indirect Recursion
  • Method A can call method B
  • Then method B calls method A
  • Still recursive

18
Tail Recursion
  • When recursion happens just before the method
    exits, its called tail recursion
  • Its simplest
  • Recursion can be anywhere in method, though

public static int sum(int n) if(n lt
1) return 0 return( sum(n 1) n )
19
Towers of Hanoi
  • Game on a board with three pegs
  • Three disks with holes (torii or donuts)
  • Goal move disks to 3rd peg
  • Rules
  • Move one disk at a time
  • Bigger disk cant be on top of smaller disk

20
Towers of Hanoi Applet
  • http//www.mazeworks.com/hanoi/
  • Lets try it!

21
Recursive Algorithm
  • Simple Algorithm for N rings
  • Move top N 1 rings to extra peg
  • Move big ring to destination
  • Move N 1 rings to destination

22
Whats the Base Case?
  • When theres only one ring to move, just move it.

23
Code
  • void moveTwr (int disks, int start, int end, int
    temp)
  • if (disks 1)
  • moveOneDisk (start, end)
  • else
  • moveTwr (disks-1, start, temp, end)
  • moveOneDisk (start, end)
  • moveTwr (disks-1, temp, end, start)
  • Called as
  • moveTwr (totalDisks, 1, 3, 2)

24
Recursion Step 1
  • Move top N 1 rings to extra peg
  • Move big ring (N) to destination
  • Move N 1 rings to destination

Move top 2 rings
25
Recursion Step 1
  • Move top N 1 rings to extra peg
  • Move big ring (N) to destination
  • Move N 1 rings to destination

Move top 2 rings
Move big ring (base)
26
Recursion Step 1
  • Move top N 1 rings to extra peg
  • Move big ring (N) to destination
  • Move N 1 rings to destination

Move top 2 rings
Move big ring (base)
Move 2 rings
27
Recursion Step 1
  • Move top N 1 rings to extra peg
  • Move big ring (N) to destination
  • Move N 1 rings to destination

Move top 2 rings
Move big ring (base)
Move 2 rings
28
The Legend
  • Order of monks, the priests of Brahma, in Benares
    are working on this puzzle
  • Using 64 gold disks
  • When they finish, the world will end.

29
Dont Worry!
  • To solve puzzle with N rings takes 2N 1 steps
  • This is called exponential time complexity (well
    talk about this soon)
  • Even if the monks move a disk every second
  • Itll take them 584 billion years

30
The Truth
  • This puzzle was invented by French mathematician
    Edouard Lucas
  • Sold as toy
  • No monks working on it

31
Another Example Koch Snowflake
  • Take a line

32
Divide in Three
33
Replace Middle Part
with two lines of same length
34
Keep Doing This
  • Show Example
  • Project Koch App
  • From Lewis and Loftus book

35
Fractal
  • Koch snowflake is an example of a fractal
  • Fractal is geometric shape made of
  • same patterns
  • at different scales
  • and sometimes different orientations
  • Many are recursively defined
  • Benoit Mandelbrot of IBM Research popularized
    fractal research

36
Fractal Images
37
Next Time
  • Running Time of Algorithms
  • Big O notation
Write a Comment
User Comments (0)
About PowerShow.com