1 CS212: DATASTRUCTURES Lecture 3: Recursion Lecture Contents 2 The Concept of Recursion Why recursion? Factorial – A case study Content of a Recursive Method Recursion vs. iteration Simple Example The Concept of Recursion 3 A recursive function is a function that calls itself , either directly or indirectly ( through another function). For example : void myFunction( int counter) { if(counter == 0) return; else { cout <<counter<<endl; myFunction(--counter); return; } } Why recursion? 4 Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first Recursion is a technique that solves a problem by solving a smaller problem of the same type Allows very simple programs for very complex problems Factorial – A case study 5 Factorial definition: the factorial of a number is product of the integral values from 1 to the number. factorial 3 = 3*2*1 = 6 Factorial – A case study 6 Iteration algorithm definition (non-recursive) 1 if n = 0 n*(n-1)*(n-2)*…*3*2*1 if n > 0 Factorial(n) = factorial 4 = product [4..1] = product [4,3,2,1] = 4*3*2*1 = 24 Factorial – A case study 7 Recursion algorithm definition 1 if n = 0 n * ( Factorial(n-1) ) if n > 0 Factorial(n) = = factorial 3 = 3 * factorial 2 = 3 * (2 * factorial 1) = 3 * (2 * (1 * factorial 0)) = 3 * (2 * (1 * 1)) = 3 * (2 * 1) = 3 * 2 76 Recursive Computation of n! 8 Recursive Computation of n! 9 10 Content of a Recursive Method 11 Base case(s). One or more stopping conditions that can be directly evaluated for certain arguments there should be at least one base case. Recursive calls. Recursive calls Base case One or more recursive steps, in which a current value of the method can be computed by repeated calling of the method with arguments (general case). The recursive procedure call must use a different argument that the original one: otherwise the procedure would always get into an infinite loop… Content of a Recursive Method 12 Use an if-else statement to distinguish between a Base case and a recursive step void myFunction( int counter) { if(counter == 0) return ; else { cout <<counter<<endl; myFunction(--counter); return; } } Iterative factorial algorithm Recursive factorial Algorithm Algorithm iterativeFactorial (val n <integer>) Calculates the factorial of a number using a loop. Pre n is the number to be raised factorially Return n! is returned Algorithm recursiveFactorial (val n <integer>) Calculates the factorial of a number using recursion Pre n is the number to be raised factorially Return n! is returned 13 1 i=1 2 factN = 1 3 loop (i <= n) 1 factN = factN * i 2 i=i+1 4 end loop 5 return factN end iterativeFactorial 1 if (n equal 0) 1 return 1 2 else 1 return (n * recursiveFactorial(n - 1)) 3 end if end recursiveFactorial Tracing Recursive Method 14 recursiveFactorial (4) Each recursive call is made with a new, independent set of arguments Previous calls are suspended Recursion vs. iteration 15 Iteration can be used in place of recursion An iterative algorithm uses a looping construct A recursive algorithm uses a branching structure Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code A Simple Example of Recursion 16 Algorithm LinearSum(A <integer>, n <integer>) pre A integer array A and an integer n, such that A has at least n elements post The sum of the first n integers in A if n = 1 then sum= A[0] else sum= LinearSum(A, n - 1) + A[n - 1] End IF Return sum end LinearSum call LinearSum return 15 + A [4 ] = 15 + 5 = 20 ( A ,5 ) call LinearSum return 13 + A [3 ] = 13 + 2 = 15 (A ,4 ) call LinearSum return 7 + A [2 ] = 7 + 6 = 13 (A ,3 ) call LinearSum return (A ,2 ) call LinearSum 4 + A [1 ] = 4 + 3 = 7 (A , 1 ) return A [0 ] = 4 17 End Of Chapter References: • Text book, chapter6: Recursion