130 likes | 746 Views
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.
E N D
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 • 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.
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); }
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); }
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.
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.
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)
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
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
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