











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












Function Call Value returned sum(1) 1 sum(2) 2+sum(1) or 2+ sum(3) 3+sum(2) or 3+2+ Sum(4) 4+sum(3) or 4+3+2+
int Factorial(int n) { if (n==0) // base case return 1; else return n * Factorial(n-1); }
n n- 1 n- 1 = + , 1 < k < n (recursive solution) k k k- 1 n n! = , 1 < k < n (closed-form solution) k k!(n-k)! with base cases: n n = n (k = 1), = 1 (k = n) 1 n
"
%
'(%
'
'*%
int Fibonacci(int n) { if(n <= 1 ) // base case return n; else return(Fibonacci(n-1) + Fibonacci(n-2)); }
§ Missing base case – failure to provide an escape case. § No guarantee of convergence – failure to include within a recursive function a recursive call to solve a subproblem that is not smaller. § Excessive space requirements - a function calls itself recursively an excessive number of times before returning; the space required for the task may be prohibitive. § Excessive recomputation – illustrated in the recursive Fibonacci method which ignores that several sub- Fibonacci values have already been computed.