Recursion - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Recursion

Description:

Iteration or repeated execution of statements are typically achieved ... More 'provable' or reliable code since the code often closely matches its definition ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 14
Provided by: luisfgsarm
Category:

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • CS 105

2
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(Chapter 13
    of the Horstmann text)

3
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
Example 1 factorial
  • For loop version
  • public static int factorial( int n )
  • result 1
  • for( int i 1 i lt n i )
  • result i
  • return result
  • This arises from a definition of factorial that
    goes n! 123 n

5
Example 1 factorial
  • The factorial function can instead be defined as
    follows
  • n! 1 if n 0 n(n-1)! if n gt 1
  • 3! 32!, 2! 21!, 1! 10!, 0!1
  • Or, fac(3) 3fac(2) 3(2fac(1))
    3(2(1fac(0)) 3(2(1(1)))

6
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 (n0) it
    ensures that the recursion collapses and prevents
    endless execution from taking place
  • Reminiscent of mathematical induction

7
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
Example 2 power
  • Alternative definitions of the power function
  • xn xxx x (multiply n times)
  • xn 1 if n 0 xxn-1 if n gt 1
  • Come up with corresponding loop and recursive
    versions for the above definitions (very similar
    to the factorial example)

9
Example 2 power
  • Better (recursive) definition
  • xn 1 if n 0 xn/2 xn/2 if n is
    even x x(n-1)/2 x(n-1)/2 if n is odd
  • Example
  • x13 x x6 x6
  • where x6 x3 x3
  • where x3 x x1 x1
  • where x1 x x0 x0
  • where x0 1

10
Example 2 power
  • Algorithm Power( x, n )
  • Input A number x and integer n gt 0
  • Output The value xn
  • if n 0 then
  • return 1
  • if n is odd then
  • y ? Power( x, (n-1)/2 )
  • return xyy
  • if n is even then
  • y ? Power( x, n/2 )
  • return yy

11
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
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 lt j then
  • Swap Ai and Aj
  • ReverseArray( A, i1, 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 gt j

13
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
Write a Comment
User Comments (0)
About PowerShow.com