Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion CS 105. 10/02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration.

Similar presentations


Presentation on theme: "Recursion CS 105. 10/02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration."— Presentation transcript:

1 Recursion CS 105

2 10/02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration revisited Iteration or repeated execution of statements are typically achieved through loops In Java, the the available loop forms are: the for loop, while loop, and do-while loop There is an alternative: Recursion (sections 2.5.1 and 4.1 of the textbook)

3 10/02/05 L7: Files Slide 3 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Recursion defined Recursion: occurs when a method calls itself Methods can call other methods so it should be possible (at least syntactically) for a method to invoke itself Appears counter-intuitive: Implies infinite definition May result in endless execution Analogy: mirror in front of another mirror

4 10/02/05 L7: Files Slide 4 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Example 1: factorial For loop version: public static int factorial( int n ) { result = 1; for( int i = 1; i <= n; i++ ) result *= i; return result; } This arises from a definition of factorial that goes: n! = 1*2*3* … *n

5 10/02/05 L7: Files Slide 5 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Example 1: factorial The factorial function can instead be defined as follows: n! =1if n = 0 n*(n-1)! if n >= 1 3! = 3*2!, 2! = 2*1!, 1! = 1*0!, 0!=1 Or, fac(3)= 3*fac(2) = 3*(2*fac(1)) = 3*(2*(1*fac(0)) = 3*(2*(1*(1)))

6 10/02/05 L7: Files Slide 6 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Example 1: factorial Recursive version: public static int fac( int n ) { if ( n == 0 ) return 1; else return n * fac( n-1 ); } Note the importance of the base case (n==0): it ensures that the recursion collapses and prevents endless execution from taking place Reminiscent of mathematical induction

7 10/02/05 L7: Files Slide 7 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved About recursion Advantages of recursion More elegant code More “provable” or reliable code since the code often closely matches its definition Disadvantages Possibly slower code (uses and may abuse the runtime stack) Sometimes difficult to trace through an execution

8 10/02/05 L7: Files Slide 8 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Example 2: power Alternative definitions of the power function x n = x*x*x* … *x (multiply n times) x n =1if n = 0 x*x n-1 if n >= 1 Come up with corresponding loop and recursive versions for the above definitions (very similar to the factorial example)

9 10/02/05 L7: Files Slide 9 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Example 2: power Better (recursive) definition x n =1if n = 0 x n/2 * x n/2 if n is even x * x (n-1)/2 * x (n-1)/2 if n is odd Example x 13 = x * x 6 * x 6 where x 6 = x 3 * x 3 where x 3 = x * x 1 * x 1 where x 1 = x * x 0 * x 0 where x 0 = 1

10 10/02/05 L7: Files Slide 10 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Example 2: power Algorithm Power( x, n ): Input: A number x and integer n >= 0 Output: The value x n if n = 0 then return 1 if n is odd then y  Power( x, (n-1)/2 ) return x*y*y if n is even then y  Power( x, n/2 ) return y*y

11 10/02/05 L7: Files Slide 11 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Time complexity analysis Count the number of multiplications performed Iterative version: O(n) First recursive version: O(n) Second recursive version: O( log n )

12 10/02/05 L7: Files Slide 12 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Example 3: array reversal Algorithm ReverseArray( A,i,j ): Input: An array A, integer indices i and j Output: The reversal of elements in A from index i to j if i < j then Swap A[i] and A[j] ReverseArray( A, i+1, j-1 ) Let S be an array (indexed at 0..n-1). A reversal of the entire array will occur with the call ReverseArray( S, 0, n-1 ) The base case is “invisible”: occurs when i >= j

13 10/02/05 L7: Files Slide 13 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Summary Recursion is an alternative to loops Recursion occurs when a method invokes itself Often useful when the definition of the problem (or its solution) is itself a recurrence Make sure that a base case is included so that the recursion collapses and the execution terminates


Download ppt "Recursion CS 105. 10/02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Iteration."

Similar presentations


Ads by Google