Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.

Similar presentations


Presentation on theme: "CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a."— Presentation transcript:

1 CS212: DATASTRUCTURES Lecture 3: Recursion 1

2 Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a Recursive Method  Recursion vs. iteration  Simple Example

3 The Concept of Recursion  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; } } 3

4 Why recursion? smaller version  Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first smaller problem  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 4

5 Factorial – A case study  Factorial definition: the factorial of a number is product of the integral values from 1 to the number. 5 factorial 3 3*2*1 = 6 =

6 Factorial – A case study  Iteration algorithm definition (non-recursive) 6 factorial 4 product [4..1] = product [4,3,2,1] = 4*3*2*1 = 24 = 1 if n = 0 Factorial(n) = n*(n-1)*(n-2)*…*3*2*1 if n > 0

7 Factorial – A case study  Recursion algorithm definition 7 factorial 3 3 * factorial 2 = 3 * (2 * factorial 1) = 3 * (2 * (1 * factorial 0)) = 3 * (2 * (1 * 1)) = 3 * (2 * 1) = = 6 3 * 2 = 1 if n = 0 Factorial(n) = n * ( Factorial(n-1) ) if n > 0 7 7

8 Content of a Recursive Method  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.  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… Recursive calls Base case 8

9 Content of a Recursive Method 9 void myFunction( int counter) { if(counter == 0) return ; else { cout <<counter<<endl; myFunction(--counter); return; } } void myFunction( int counter) { if(counter == 0) return ; else { cout <<counter<<endl; myFunction(--counter); return; } } Use an if-else statement to distinguish between a Base case and a recursive step

10 Iterative factorial algorithm Algorithm iterativeFactorial (val n ) Calculates the factorial of a number using a loop. Pre n is the number to be raised factorially Return n! is returned 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 Algorithm recursiveFactorial (val n ) Calculates the factorial of a number using recursion Pre n is the number to be raised factorially Return n! is returned 1 if (n equal 0) 1 return 1 2 else 1 return (n * recursiveFactorial(n - 1)) 3 end if end recursiveFactorial Recursive factorial Algorithm 10

11 Tracing Recursive Method recursiveFactorial (4) Each recursive call is made with a new, independent set of arguments Previous calls are suspended 11

12 Recursion vs. iteration  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 12

13 A Simple Example of Recursion Algorithm LinearSum(A, n ) 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 13

14 References: Text book, chapter6: Recursion End Of Chapter 14


Download ppt "CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a."

Similar presentations


Ads by Google