1 / 12

Nested and Excessive Recursion

Nested and Excessive Recursion. Nested recursion Excessive Recursion Converting a recursive method to iterative. Iterative or Recursive?. Nested recursion. When a recursive method has a parameter defined in terms of itself, it is said to be nested recursive. Nested Recursion Example.

kameryn
Télécharger la présentation

Nested and Excessive Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 > 0 && n == 0 A(m,n) = A(m-1, A(m, n-1)) if m > 0 && n > 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 (n<2) return n; else return fib(n-2)+Fib(n-1); }

  5. Fib(6) Fib(5) Fib(4) Fib(3) Fib(2) Fib(2) Fib(1) Fib(0) Fib(1) Fib(4) Fib(0) Fib(1) Fib(3) Fib(2) Fib(2) Fib(3) Fib(1) Fib(0) Fib(1) Fib(0) Fib(1) Fib(2) Fib(1) Fib(0) Fib(1) 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 (n<2) 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 2*fib(n+1)-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 (n<2) return n; else { int i=2,tmp, current=1, last = 0; for ( ; i<=n;++i) { tmp = current; current+=last; last=tmp; } return current; } } • The method loops (n-1) times making three assignments per iteration and only one addition.

  8. Iterative or Recursive?

  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 members=1, return group If members=group, return 1 • General Case: If (group> members>1) return C(group-1,members-1)+C(group-1,members)

  10. comb(6,4) comb(5,3) comb(5,4) comb(4,2) comb(4,3) comb(4,3) comb(4,4) comb(3,1) comb(3,2) comb(3,2) comb(3,3) comb(3,2) comb(3,3) 1 3 comb(2,1) comb(2,2) 1 1 comb(2,1) comb(2,2) comb(2,1) comb(2,2) 1 2 2 1 1 2 1 1 1 3 1 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

More Related