Recursion - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Recursion

Description:

Simple recursive routines follow a standard pattern. ... are very naturally programmed recursively, and all but unmanageable iteratively. ... – PowerPoint PPT presentation

Number of Views:621
Avg rating:3.0/5.0
Slides: 11
Provided by: Abeys
Category:

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • A function is said to be recursive if it calls
    itself, either directly or indirectly.
  • void repeat( int n )
  • cout ltlt n ltlt " "
  • repeat( --n )

2
Recursion contd.
  • Simple recursive routines follow a standard
    pattern. Typically there is a base case ( or
    cases ) that is tested for, upon entry to the
    function. Then there is a general recursive case
    in which one of the variables, often an integer,
    is passed as an argument in such a way as to
    ultimately lead to the base case.

3
Recursion contd.
  • When writing a recursive function, there are two
    rules that need to be adhered to.
  • Every recursive function must have a base case,
    where the result is known
  • When a function invokes itself, the value is
    defined in terms of previously defined function
    values, and the value used for testing for the
    base case must change.

4
Recursion contd.
  • void repeat( int n )
  • if( nlt0 )
  • cout ltlt "END" ltlt endl
  • return
  • else
  • cout ltlt n ltlt " "
  • repeat( --n )

5
Recursion Example
  • int sum( int n )
  • if (n lt 1 )
  • return n // base case
  • else
  • return (n sum( n -1 )) // current value is
    defined in terms of
  • // a different value of the parameter
  • sum(1) 1
  • sum(2) 2 sum(1) 2 1
  • sum(3) 3 sum(2) 3 2 1

6
Factorial Example
  • Lets find the factorial of n where n is a
    non-negative integer.
  • Product of integers from 1 to n
  • If n 0, then n! is 1
  • 0! 1
  • n! n(n-1) 3.2.1 for n gt 0
  • int factorial( int n )
  • if (n lt 1 )
  • return 1
  • else
  • return ( n factorial(n-1 ) )

What is the Big-Oh?
7
Iterative Version of the Factorial Example
  • int factorial( int n )
  • int value 1
  • for ( int i2, iltn, i )
  • value i
  • return value

What is the Big-Oh for this?
8
Pros and Cons of Recursion
  • Disadvantages
  • Slow time is taken for function call
  • Uses more memory requires space for stack frame
  • Advantages
  • When it is a simple, elegant solution that is
    easy to understand
  • When the number of recursive calls is a fraction
    of n

9
When to Use Recursion
  • 1. Iterative functions are typically faster than
    their recursive counterparts. So, if speed is an
    issue, you would normally use iteration.
  • 2. If the stack limit is too constraining then
    you will prefer iteration over recursion.
  • 3. Some procedures are very naturally programmed
    recursively, and all but unmanageable
    iteratively. Here, then, the choice is clear.

10
Example of When to Use Recursion
Algorithm Power( x, n ) Input base x, exponent
n where n ³ 0 Output value of xn if n 0
then return 1 if n is odd then y
Power(x,(n-1)/2) return x y y else y
Power(x,n/2) return y y
Write a Comment
User Comments (0)
About PowerShow.com