Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 RECURSION.

Similar presentations


Presentation on theme: "COMP 103 RECURSION."— Presentation transcript:

1 COMP 103 RECURSION

2 RECAP-TODAY RECAP TODAY Recursion Selection Sort, Insertion Sort
2 RECAP Selection Sort, Insertion Sort Cost Analysis – those were the “slow” sorts: O(n2) TODAY Recursion

3 Recursion Methods can call other methods....
3 Methods can call other methods.... ...so could a method call itself ? Something is recursive if it is defined in terms of itself We will look at: tail recursion (the simplest) diagrams of what's going on recursion with a wrapper method nested recursion multiple recursion returning values

4 Example 4 /** Draw stream of bubbles from (x,y) to the top of the screen */ public void drawBubbles(int x, int y){ if ( y>0 ) { drawOneBubble(x, y); drawBubbles(x, y-15); } y=0 y=70 x=30 drawBubbles (30, 40); drawOneBubble(30,40); drawBubbles (30, 55); drawOneBubble(30,55); drawBubbles (30, 70); drawOneBubble(30,70);

5 Tracing the calls drawBubbles(30, 70) drawBubbles(30, 55)
public void drawBubbles(int x, int y){ if ( y>0 ) { drawOneBubble(x, y); drawBubbles(x, y-15); } return drawBubbles(30, 70) drawOneBubble(30, 70) drawBubbles(30, 55) return drawOneBubble(30, 55) drawBubbles(30, 40) return drawOneBubble(30, 40) drawBubbles(30, 25) return drawOneBubble(30, 25) drawBubbles(30, 10) return drawOneBubble(30, 10) drawBubbles(30, -5) return

6 Recursion with wrapper method
6 /** Draw stream of bubbles from (x,y) to the top of the screen the bubbles should get larger as they go up. */ public void drawBubbles(int x, int y){ UI.setColor(Color.blue); drawBubblesRec(x, y, 10); } /** Recursive method */ public void drawBubblesRec(int x, int y, int size){ if ( y>0 ) { UI.fillOval(x, y, size, size); drawBubblesRec(x, y-size-6, size+2); Initial size Condition Do First Do the rest Arguments for the rest

7 Tail recursion vs nested recursion.
7 Tail recursion: recursive call is the last step in the method. easier to understand because don’t have to “go back” after call. Nested recursion: recursive call is in the middle. have to go back to previous call to finish it off. Example: Print an “onion” : ( ( ( ( ( ( ( ) ) ) ) ) ) ) public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } open do the inside close

8 Nested Recursion at work
8 onion(4) ⇒ public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); ( ( ( ( ) ) ) ) public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 4 . public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 3 . public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 2 . public void onion (int layers){ UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 1 .

9 Alternative Visualisation
9 onion(4) print( “(” ) print( “)” ) onion(3) print( “(” ) print( “)” ) onion(2) print( “(” ) print( “)” ) onion(1) print( “(” ) print( “)” ) To understand recursive methods: draw a diagram!

10 Kinds of Recursion Tail recursion vs. Nested recursion
10 Tail recursion vs. Nested recursion Tail: recursive call is the last step in a method ⇒ don’t have to “go back” and do more. Nested: recursive call is in the middle ⇒ action … recursive call … more actions Single recursion vs. Multiple recursion Single: method only makes one recursive call Multiple: method makes more than one recursive call Single Recursion

11 Multiple Recursion Draw a recursive arch-wall:
11 Draw a recursive arch-wall: Consists of an arch with two half size arch-walls on top of it.

12 Multiple Recursion to draw an ArchWall of given base size (wd,ht):
12 to draw an ArchWall of given base size (wd,ht): draw a base arch of size (wd,ht) if wd is not too small draw a half size archwall on the left draw a half size archwall on the right public void archWall (int x, int y, int wd, int ht){ drawArch(x, y, wd, ht); if ( wd > 20 ) { int w = wd/2; // width of next smaller arch int h = ht/2; // height of next smaller arch archWall(x, y-h, w, h); // left half archWall(x+w, y-h, w, h); // right half }

13 Tracing the execution:
13 archWall(10, 300, 80, 40) aw(50,280,40,20) drawArch aw(10, 280, 40, 20) drawArch aw(10,270,20,10) aw(30,270,20,10) drawArch aw(50,270,20,10) aw(70,270,20,10) drawArch drawArch drawArch drawArch

14 Recursion that returns a value
14 What if the method returns a value? ⇒ get value from recursive call, then do something with it typically, perform computation on value, then return answer. Compound interest value at end of n th year = value at end of previous year * (1 + interest). value(deposit, year) = deposit [if year is 0] = value(deposit, year-1) * (1+rate)

15 Recursion returning a value
15 /** Compute compound interest of a deposit */ public double value(double deposit, double rate, int year){ if (year == 0) return deposit; else { int prev = value(deposit, rate, year-1); return prev * (1 + rate); }

16 Recursion with return: execution
16 public double value(double deposit, double rate, int year){ if (year == 0) return deposit; else { int prev = value(deposit, rate, year-1); ← step 1 return prev * (1 + rate); ← step 2 } value(1000, 0.1, 4) value(1000, 0.1, 3) * 1.1 value(1000, 0.1, 2) * 1.1 value(1000, 0.1, 1) * 1.1 value(1000, 0.1, 0) * 1.1 1464.1 1331 1210 1100 1000


Download ppt "COMP 103 RECURSION."

Similar presentations


Ads by Google