Nested and Excessive Recursion - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Nested and Excessive Recursion

Description:

Nested and Excessive Recursion Nested recursion Excessive Recursion Converting a recursive method to iterative. Iterative or Recursive? Nested recursion When a ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 13
Provided by: null6
Category:

less

Transcript and Presenter's Notes

Title: Nested and Excessive Recursion


1
Nested and Excessive Recursion
  • Nested recursion
  • Excessive Recursion
  • Converting a recursive method to iterative.
  • Iterative or Recursive?

2
Nested recursion
  • When a recursive method has a parameter defined
    in terms of itself, it is said to be nested
    recursive.

3
Nested Recursion Example
  • Another example of nested recursion is Ackerman
    formula
  • A(m,n) n 1 if m 0
  • A(m,n) A(m-1, 1) if m gt 0 n 0
  • A(m,n) A(m-1, A(m, n-1)) if m gt 0 n gt 0
  • It is interesting for computational reasons due
    to the depth of recursion and due to the nested
    recursion.
  • The execution of Ackermann(3,2) produces
    recursion of depth 30.

4
Excessive Recursion
  • Some recursive methods repeats the computations
    for some parameters, which results in long
    computation time even for simple cases
  • This can be implemented in Java as follows
  • int fib(int n)
  • if (nlt2)
  • return n
  • else
  • return fib(n-2)Fib(n-1)

5
Excessive Recursion Example
  • To show how much this formula is inefficient, let
    us try to see how Fib(6) is evaluated.
  • int fib(int n)
  • if (nlt2)
  • return n
  • else
  • return fib(n-2)Fib(n-1)

6
Excessive Recursion Example
  • For caculating fib(6), the method is called 25
    times.
  • The same calculation are repeated again and again
    because the system forgets what have been already
    calculated.
  • The method is called 2fib(n1)-1 times to
    compute fib(n)
  • 3,000,000 calls are needed to caculate the 31st
    element.

7
Iterative or Recursive?
  • Fib can be written iteratively instead of
    recursievely as follows
  • int iterativeFib(int n)
  • if (nlt2)
  • return n
  • else
  • int i2,tmp, current1, last 0
  • for ( iltni)
  • tmp current
  • currentlast
  • lasttmp
  • return current
  • The method loops (n-1) times making three
    assignments per iteration and only one addition.

8
Iterative or Recursive?
Assignments Assignments Number of Additions n
Recursive Iterative Number of Additions n
25 15 5 6
177 27 9 10
1973 42 14 15
21891 57 19 20
242785 72 24 25
2692537 87 29 30
9
Excessive Recursion Example
  • How many combinations of members items can be
    taken from a set of group items, that is how to
    calculate C(group, members)?
  • Base Case
  • If members1, return group
  • If membersgroup, return 1
  • General Case
  • If (groupgt membersgt1)
  • return C(group-1,members-1)C(group-1,members)

10
Combination Example
11
Removing Recursion
  • Some times we need to convert a recursive
    algorithm into an iterative one if
  • The Language does not support recursion
  • The recursive algorithm is expensive
  • There are two general techniques for that
  • Iteration
  • Stacking

12
When to use Recursion
  • The main two factors to consider are
  • Efficiency
  • Clarity
  • Recursive methods are less efficient in time and
    space.
  • Recursive methods are easier to write and to
    understand
  • It is Good to use recursion when
  • Relatively shallow
  • Same amount of work as the nonrecursive verion
  • Shorter and simpler than the nonrecursive solution
Write a Comment
User Comments (0)
About PowerShow.com