120 likes | 235 Vues
In Lecture 6 of the ISTE workshop on effective teaching and learning of computer programming, Dr. Deepak B. Phatak explains recursion—a fundamental concept in programming. The lecture covers the implementation of solutions via recursive functions, comparisons with iterative solutions, and a detailed examination of computational costs associated with recursion. Key topics include the factorial function, the Fibonacci series, and the necessity of defining initial conditions for recursive definitions. This lecture is an essential resource for educators and learners in computer science.
E N D
Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lecture 6, Recursion Thursday 1 July 2010 Two-week ISTE workshop onEffective teaching/learning of computer programming
Overview • Recursion • Implementing a solution through recursive function calls • Normal execution of a function • Recursive execution • Computational cost of recursion • Need, definition and usage Lecture 6 Recursion
Iteration Vs Recursion • We have seen some problems which implement the algorithm using iteration • Newton-Raphson method • Finding maximum of a set of given values • Calculating factorial of a given number • Determining Nth term of Hemachandra numbers (Fibonacci numbers) • There are problems where the solution can also be defined as a recursive formula, e.g. f(n) = n! = 1 * 2 * 3 * … *(n-1) * n which can also be expressed as f(n) = n * f(n-1) Lecture 6 Recursion
Recursive definitions • Recursion is the process of defining something in terms of itself. • It is an elegant mathematical concept • permits very concise definition of operations • nth term f(n) of Hemachandra (Fibonacci) series is given by f(n) = f(n-1) + f(n-2) [nth term is defined in terms the (n-1)th and (n-2)th terms] Lecture 6 Recursion
Recursive definitions … • For such definitions to be meaningful and valid, some ‘initial conditions must be defined, otherwise the recursive process simply cannot begin. • Thus, n! is defined as f(0) = 1 f(n) = n * f(n-1), for n > 0 • Similarly, terms in Hemachandra (Fibonacci) series are defined as f(0) = 1 f(1) = 1 f(n) = f(n-1) + f(n-2), for n > 1 Lecture 6 Recursion
Execution of the iterative solution • To understand what happens internally when recursion is used, we should first look at the iterative solution int factorial (int n){ int f = 1; if (n == 0) return f; for (i =1; i <= n, i++){ f = f * i; }; return f; } Lecture 6 Recursion
function invocation • Assume that the above function is invoked in the main program in following manner … int n =3, answer; answer = factorial (n); cout << answer; --- • What happens when the function call is made ? Lecture 6 Recursion
function invocation • The control is handed over to factorial function with value of the parameter n = 3 • The computer allocates memory to the variables used in the function, i.e., to f, i , n • This n is different from the n in main program, hence it gets a separate memory location Lecture 6 Recursion
Function invocation Remember the instruction being executed, save context Collect all parameter values Hand over control to function Restore old context and resume execution Set up a logical block for factorial execution Allocate memory to variables n, i, f Calculate answer Release allocated memory and go back main function Lecture 6 Recursion
Function invocation • There are two type of ‘overheads’ which accompany a function call • Additional memory has to be allocated every time a function is invoked • For the data elements defined and used in the function • For temporarily storing the ‘context’ of the current program • Additional computational overhead • For transfer of control, parameter transfer, etc. Lecture 6 Recursion
Factorial function using recursion #include <iostream> using namespace std; // prog6-1.c // calculate factorial of a given integer n, int findfactorial(int n){ int f; if (n ==0) { f = 1;} else { f = findfactorial (n-1) * n; } return f; } Lecture 6 Recursion
Factorial … recursion int main(){ int n, fact; cout << “give an integer value”; n = 3; fact = findfactorial(n); cout << “factorial is “ << fact return 0; } Lecture 6 Recursion