Title: Recursion
1Recursion
- COMP 114
- Tuesday February 26
2Announcements
- Program 2 due, 1100 AM Thursday
- Dont forget
- Folder, envelope
- Clean, working code
- No bugs
- Good documentation!!!
3Topics
- This Class
- GUI Message Box
- Recursion
4Message Box
- Used to signal errors
- Several types, but they all work the same.
- Demo Message Box Project
5Recursion
- In computer science, recursion is when a method
calls itself. - Not as crazy as it seems.
- First, lets look at a non-programming example
6Recursive 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
7Example
- 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
8Erroneous Examples
LIST number or LIST number , LIST
Compilers work like this!
9Base 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
10Recursion Used in Math
- Induction
- Recursive proofs
- Also have to provide a base case
- The one thats trivial to prove
11What 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 )
12Example
public static int sum(int n) if(n lt
1) return 0 return( sum(n 1) n )
sum(4)
13Progress
- Notice that we must make progress toward base
case - There is steady reduction in size of problem
- Otherwise, trouble.
14Iterative 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)
15How 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
16Costs 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
17Indirect Recursion
- Method A can call method B
- Then method B calls method A
- Still recursive
18Tail 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 )
19Towers 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
20Towers of Hanoi Applet
- http//www.mazeworks.com/hanoi/
- Lets try it!
21Recursive 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
22Whats the Base Case?
- When theres only one ring to move, just move it.
23Code
- 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)
24Recursion 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
25Recursion 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)
26Recursion 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
27Recursion 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
28The Legend
- Order of monks, the priests of Brahma, in Benares
are working on this puzzle
- When they finish, the world will end.
29Dont 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
30The Truth
- This puzzle was invented by French mathematician
Edouard Lucas - Sold as toy
- No monks working on it
31Another Example Koch Snowflake
32Divide in Three
33Replace Middle Part
with two lines of same length
34Keep Doing This
- Show Example
- Project Koch App
- From Lewis and Loftus book
35Fractal
- 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
36Fractal Images
37Next Time
- Running Time of Algorithms
- Big O notation