Download presentation
Presentation is loading. Please wait.
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 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
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); } Fib(6) Fib(4) Fib(3) Fib(2) Fib(1) Fib(0) Fib(5)
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?
Assignments Number of Additions n Recursive Iterative 25 15 5 6 177 27 9 10 1973 42 14 21891 57 19 20 242785 72 24 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 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
Combination Example comb(6,4) comb(5,4) comb(5,3) comb(4,2) comb(4,3)
1 comb(2,1) comb(2,2) comb(3,3) comb(3,2) comb(3,1) comb(4,4)
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.