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 #31 2013T1.

Similar presentations


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

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

2 © Peter Andreae COMP 102 31: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 Announcements:

3 © Peter Andreae COMP 102 31: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 31: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 31: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 31: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); } Why can’t a method call itself ? Definition of drawBubbles method Calling drawBubble method Definition of drawBubble method Calling setColor & fillOval methods

7 © Peter Andreae COMP 102 31: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); // draw the remaining bubbles if ( n > 1 ) { this.drawBubbles(x, y-15, n-1); } }

8 © Peter Andreae COMP 102 31: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. this.drawBubbles2(x, y-15); } }

9 © Peter Andreae COMP 102 31: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 31: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 31: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 31: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 31: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 31:14 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)

15 © Peter Andreae COMP 102 31:15 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); } }

16 © Peter Andreae COMP 102 31:16 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 #31 2013T1."

Similar presentations


Ads by Google