Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 #33 2015T1.

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 #33 2015T1."— Presentation transcript:

1 Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 #33 2015T1

2 © Peter Andreae COMP 102 33:2 Outline A new control structure: Recursion Recursion Simple Recursive methods Recursive methods that return values ⇒ Understanding methods and parameters properly Very important for COMP103; not an assessed topic for COMP102 :

3 © Peter Andreae COMP 102 33:3 What is Recursion? Something is recursive if it is defined in terms of itself Recursion is a powerful mental tool for: Defining mathematical functions Describing complex patterns/structures Designing programs/algorithms Solving problems But isn’t that circular? There must be a non-recursive alternative (“base case”) The recursive case must be “smaller” © Lindsay Groves

4 © Peter Andreae COMP 102 33:4 Recursive Functions A function is recursive if it is defined in terms of itself: Factorial, iterative definition: 10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 3 x 2 x 1 n! = n x n-1 x n-2 x … x 2 x 1 Factorial, recursive definition: 1! = 1 n! = n x (n-1)!, for n > 1 Fibonacci numbers: Fib(0) = 0 Fib(1) = 1 Fib(n) = Fib(n-1) + Fib(n-2), for n > 1 What’s Fib(5) ? © Lindsay Groves In both cases: There is a non-recursive ("base") case. The arguments in the recursive cases are closer to the base case.

5 © Peter Andreae COMP 102 33:5 Recursive Specifications of Language A Java expression can be: 〈constant 〉 〈variable 〉 〈expression 〉 〈operator 〉 〈expression 〉 ( 〈expression 〉 ) 〈methodname 〉 ( 〈expression 〉, …, 〈expression 〉 ) 〈arrayexpression 〉 [ 〈expression 〉 ] 〈expression 〉 == 〈expression 〉 etc A Java statement can be: 〈variable 〉 = 〈expression 〉 ; 〈methodname 〉 ( 〈expression 〉, …, 〈expression 〉 ) if ( 〈condition 〉 ) { 〈statement 〉 … } else { 〈 statement 〉 … } while ( 〈condition 〉 ) { 〈statement 〉 … } etc. © Lindsay Groves

6 © Peter Andreae COMP 102 33:6 Recursive Methods Methods can call other methods: public void drawBubbles(double x, double y, int n){ for (int i = 0; i<n; i++ ) { this.drawBubble(x, y, 10); y = y – 15; } } public void drawBubble(double x, double y, double size){ UI.setColor(Color.blue); UI.fillOval(x, y, size, size); } A method can also call itself ! Definition of drawBubbles method Calling drawBubble method Definition of drawBubble method Calling setColor & fillOval methods

7 © Peter Andreae COMP 102 33:7 Repetition with Recursion /** Draw a stream of n bubbles up from a point (x, y ) */ public void drawBubbles(double x, double y, int n){ // draw one bubble this.drawBubble(x, y, 10); // if there are any remaining bubbles if ( n > 1 ) { // draw them int restY = y-15; this.drawBubbles(x, restY, n-1); } }

8 © Peter Andreae COMP 102 33:8 More Recursion /** Draw stream of bubbles from (x,y) to the top of the screen */ public void drawBubbles2(double x, double y){ if ( y>0 ) { // Draw first bubble this.drawBubble(x, y, 10); // Draw the rest of the bubbles. int restY = y-15; this.drawBubbles2(x, y-15); } }

9 © Peter Andreae COMP 102 33:9 Recursion vs Iteration Iteration: break problem into sequence of the typical case identify the typical case (body) identify the increment to step to the next case identify the keep-going or stopping condition identify the initialisation Recursion: (simple) break problem into first and rest identify the first case identify the recursive call for the rest work out the arguments for the rest identify when you should do the recursive call. make sure there is a stopping case (base case) may need a wrapper method to initialise

10 © Peter Andreae COMP 102 33:10 Recursion with wrapper method /** 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(double x, double y){ this.drawBubblesRec(x, y, 10); } /** Recursive method */ public void drawBubblesRec(double x, double y, double size){ if ( y>0 ) { this.drawBubble(x-size/2, y-size/2, size); this.drawBubblesRec(x, y-size-6, size+2); } } Do First Arguments for rest Initial size Condition Do Rest

11 © Peter Andreae COMP 102 33:11 Tail recursion vs nested recursion. 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) this.onion(layers-1); UI.print( “ )“ ); } open close do the inside

12 © Peter Andreae COMP 102 33:12 Nested Recursion at work onion(4) ⇒ public void onion (int layers){ UI.print( “(“ ); if (layers > 1) this.onion(layers-1); UI.print( “)“ ); public void onion (int layers){ UI.print( “ ( “ ); if (layers > 1) this.onion(layers-1); UI.print( “ ) “ ); } 4. ✔ ✔ ✔ (((()))) public void onion (int layers){ UI.print( “ ( “ ); if (layers > 1) this.onion(layers-1); UI.print( “ ) “ ); } 3. ✔ ✔ ✔ public void onion (int layers){ UI.print( “ ( “ ); if (layers > 1) this.onion(layers-1); UI.print( “ ) “ ); } 2. ✔ ✔ ✔ public void onion (int layers){ UI.print( “ ( “ ); if (layers > 1) this.onion(layers-1); UI.print( “ ) “ ); } 1. ✔ ✔ ✔

13 © Peter Andreae COMP 102 33:13 Nested recursion What will nested(5) print out? public void nested (int num){ if (num == 1) UI.print(“ 10 ”); else { UI.print( “(” + num + “+” ); this.nested(num-1); UI.print( “) *” + num); } (5 + (4 + (3 + (2 + 10 ) * 2) * 3 ) * 4) * 5

14 © Peter Andreae COMP 102 33:14 Multiple Recursion Draw a recursive arch-wall: Consists of an arch with two half size arch-walls on top of it.

15 © Peter Andreae COMP 102 33:15 Multiple Recursion: ArchWall 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 left, int base, int wd, int ht){ this.drawArch(left, base, wd, ht); if ( wd > 20 ) { int w = wd/2; // width of next smaller arch int h = ht/2; // height of next smaller arch int mid = left+w; // height of next smaller arch int top = base-ht; // height of next smaller arch this.archWall(left, top, w, h); // left half this.archWall(mid, top, w, h); // right half }

16 © Peter Andreae COMP 102 33:16 Tracing the execution: archWall(10, 300, 80, 60) drawArch aw(10, 240, 40, 30) aw(50,240,40,30) drawArch aw(10,210,20,15) aw(30,210,20,15) drawArch aw(50,210,20,15) aw(70,210,20,15) drawArch

17 © Peter Andreae COMP 102 33:17 Multiple Recursion “Pouring” Paint in a painting program: colour this pixel spread to each of the neighbour pixels colour the pixel spread to its neighbours colour the pixel spread to its neighbours …

18 © Peter Andreae COMP 102 33:18 Spreading Paint private int rows = 25; private int cols = 35; private Color[ ][ ] grid = new Color[40][50]; // the grid of colours, /** Spread new colour to all adjacent cells with oldColour */ public void spread(int row, int col, Color oldColour, Color newColour){ if (row =this.rows || col =this.cols) { return; } if ( ! this.grid[row][col].equals(oldColour) ) { return; } this.setPixel(row, col, newColour); spread(row-1,col, oldColour, newColor); spread(row+1,col, oldColour, newColor); spread(row,col-1, oldColour, newColor); spread(row,col+1, oldColour, newColor); }

19 © Peter Andreae COMP 102 33:19 Recursion that returns a value. 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)

20 © Peter Andreae COMP 102 33:20 Recursion returning a value /** Compute compound interest of a deposit */ public double compound(double deposit, double rate, int years){ if (years == 0) return deposit; else return ( this.compound(deposit, rate, years-1) * (1 + rate) ); } alternative : public double compound(double deposit, double rate, int years){ if (years == 0) return deposit; else { double prev = this.compound(deposit, rate, years-1); return prev * (1 + rate); } }

21 © Peter Andreae COMP 102 33:21 Recursion with return: execution public double value(double deposit, double rate, int year){ if (year == 0) return deposit; else { double prev = this.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 1000 1100 1210 1331 1464.1


Download ppt "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 #33 2015T1."

Similar presentations


Ads by Google