Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Programming Recursion.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
Computer Science II Recursion Professor: Evan Korth New York University.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Programming with Recursion
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursion.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Prof. S.M. Lee Department of Computer Science. Answer:
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
CS 1704 Introduction to Data Structures and Software Engineering.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
M180: Data Structures & Algorithms in Java
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
Lecture 7. Solution by Substitution Method T(n) = 2 T(n/2) + n Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n)
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Lecture #3 Analysis of Recursive Algorithms
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Recursion Powerful Tool
Recursion.
Sort & Search Algorithms
Recursion Version 1.0.
Chapter 15 Recursion.
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm design and Analysis
Basics of Recursion Programming with Recursion
Chapter 18 Recursion.
Self-Referencing Functions
ITEC324 Principle of CS III
Presentation transcript:

Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion

2 Overview  What is recursion?  Examples of Recursive Function  Wrapper Function  Program Call Stack in Recursion  Inline recursive function?  Common Pitfalls in Recursion  When function declaration is necessary?

Department of Computer Science and Engineering, HKUST 3 Recursion What is recursion?

4  Recall that to write a function, we can call other functions to help us.  Recursion is: Call the function itself to help itself.  For example (computation of factorial - n!): factorial(n) = 1 * 2 * 3 * … * n If we know the value of factorial(n-1), then we can simply multiply it with n to produce factorial(n). However, we don’t know factorial(n-1) in hand.  We may call the function itself to compute factorial(n-1).

5 What is recursion? int factorial( int n ){ if ( n == 0 ) // base case return 1; else return n * factorial(n-1); // recursive case }

6 What is recursion? (Dry run the example)  Compute factorial(4): factorial(4) : return 4 * factorial(3) factorial(3) : return 3 * factorial(2) factorial(2) : return 2 * factorial(1) factorial(1) : return 1 * factorial(0) Here, factorial(0) will return 1, so factorial(1) : return 1 * 1// ie. return 1 factorial(2) : return 2 * 1// ie. return 2 factorial(3) : return 3 * 2// ie. return 6 factorial(4) : return 4 * 6// ie. return 24

7 What is recursion?  A recursion consists of at least two parts: Base case:  The problem is simple enough, we can solve it without other help.  Here, factorial(0) = 1, simple enough. Recursive case:  We don’t know how to solve the problem, say factorial(2).  So call the function itself with a smaller input and then combine that result to form the solution of the larger input.

Department of Computer Science and Engineering, HKUST 8 Recursion Examples of Recursive Function

9 Example 1  Compute the x y (y is a non-negative integer): // pre-condition: y >= 0 double exp(double x, int y){ if(y==0) return 1; return x * exp(x, y-1); }

10 Example 1 (Dry run)  Compute exp(3.2,3) exp(3.2, 3) returns 3.2 * exp(3.2, 2) exp(3.2, 2) returns 3.2 * exp(3.2, 1) exp(3.2, 1) returns 3.2 * exp(3.2, 0) exp(3.2, 0) returns 1 exp(3.2, 1) returns 3.2 * 1 // returns 3.2 exp(3.2, 2) returns 3.2 * 3.2// returns exp(3.2, 3) returns 3.2 * 10.24// returns

11 Example 2  Write a recursive function that counts the number of zero digits in a non-negative integer.  Example: zeros(10200) returns 3. int zeros(int n){ if(n==0) return 1; if(n < 10) return 0; if(n%10 == 0) return 1 + zeros(n/10); else return zeros(n/10); }

12 Example 2 (Dry run)  Compute zeros(230400) Since %10 == 0, zeros(230400) = 1 + zeros(23040) Since 23040%10 == 0, zeros(23040) = 1 + zeros(2304) Since 2304%10 != 0, zeros(2304) = zeros(230) Since 230%10 == 0, zeros(230) = 1 + zeros(23) Since 23%10 != 0, zeros(23) = zeros(2) Since 2 < 10 (and 2 != 0), zeros(2) = 0  Therefore, zeros(230400) = 3

13 Example 2 (Cont’)  We can have multiple base cases and multiple recursive cases. Here, we have 2 base cases and 2 recursive cases.

14 Example 3  Computation of Fibonacci number: int fibonacci( int n ){ if ( n <= 1 ) return 1; return fibonacci(n-1) + fibonacci(n-2); }  Note that a recursive function may call itself more than once.

15 Example 3 (Dry run)  Compute fibonacci(4): fibonacci(4) returns fibonacci(3) + fibonacci(2) fibonacci(3) returns fibonacci(2) + fibonacci(1) fibonacci(2) returns fibonacci(1) + fibonacci(0) fibonacci(1) and fibonacci(0) returns 1 fibonacci(2) returns 1+1// returns 2 fibonacci(3) returns 2 + 1// returns 3 fibonacci(4) returns 3 + 2// returns 5

16 Example 4  Linear Search: Objective: search a number (target) from an array. Idea:  Compare target with every number in the array.  Use left and right to indicate the range of array needs to be considered. int linearSearch( int array[], int left, int right, int target){ if ( left == right ) return NOT_FOUND; if ( array[left] == target ) return left; return linearSearch( array, left+1, right, target); } Call: linearSearch( array, 0, n, target); // array from 0 to (n-1)

17 Example 4 (Dry run 1)  Consider array = {2, 5, 6, 8, 9}, target = 8:  Initial Call = linearSearch( array, 0, 5, 8 ) Returns linearSearch( array, 1, 5, 8 ) Returns linearSearch( array, 2, 5, 8 ) Returns linearSearch( array, 3, 5, 8 ) linearSearch( array, 3, 5, 8 ) returns 3  Therefore, linearSearch( array, 0, 5, 8 ) returns 3 (the index of the location which contains target).

18 Example 4 (Dry run 2)  Consider array = {2, 5, 9, 8}, target = 11:  Initial Call = linearSearch( array, 0, 4, 11 ) Returns linearSearch( array, 1, 4, 11 ) Returns linearSearch( array, 2, 4, 11 ) Returns linearSearch( array, 3, 4, 11 ) Returns linearSearch( array, 4, 4, 11 ), which returns NOT_FOUND  Therefore, linearSearch( array, 0, 4, 11 ) returns NOT_FOUND.

19 Example 4 (Cont’)  Recall that the input in the recursive calls must be smaller. Here the size of the effective considering range of the array is one less than the original input.

20 Example 5  Binary Search Objective: search a number (target) from a ascending sorted array. Idea:  Compare target with the middle element of the array.  If array[middle] is the same as target, you find it.  If array[middle] is larger than your target, then the second half of the array must not contain your target (since it is sorted).  discard it from further consideration.  Similar for another symmetric case.

21 Example 5 (Cont’) int bSearch( int array[ ], int left, int right, int target ){ if ( left > right ) return NOT_FOUND;// base case #1 int mid = (left + right) / 2;// integer division if ( target == array[mid] ) return mid; // base case #2 else if ( target < array[mid] ) return bSearch( array, left, mid-1, target ); // recursive #1 else return bSearch( array, mid+1, right, target ); // recursive #2 } Call: bSearch( array, 0, n-1, target ) // array from 0 to (n-1)

22 Example 5 (Dry run 1)  Consider array = {2, 5, 6, 8, 9}, target = 8:  Initial Call = bSearch( array, 0, 4, 8 ) mid = 2, array[2] = 6 < 8 = target So, returns bSearch( array, 3, 4, 8 ) mid = 3, array[3] = 8 = target So, bSearch( array, 3, 4, 8 ) returns 3  Therefore, bSearch( array, 0, 4, 8 ) returns 3.

23 Example 5 (Dry run 2)  Consider array = {2, 5, 6, 8, 9}, target = 7:  Initial Call = bSearch( array, 0, 4, 7 ) mid = 2, array[2] = 6 < 7 = target So, returns bSearch( array, 3, 4, 7 ) mid = 3, array[3] = 8 > 7 = target So, returns bSearch( array, 3, 3, 7 ) mid = 3, array[3] = 8 > 7 = target So, returns bSearch( array, 3, 2, 7 ), which returns NOT_FOUND  Therefore, bSearch( array, 0, 4, 7 ) returns NOT_FOUND.

Department of Computer Science and Engineering, HKUST 24 Recursion Wrapper Function

25 Wrapper Function  Sometimes, it is not easy to use the recursive function. Eg. bSearch( array, 0, n-1, target ) The user of this function must provides an “0” in the second argument.  Sometimes, we would like to provide a wrapper function to call this function for us. int bSearch( int array[ ], int size, int target ) { // do nothing, but only call recursive function // with appropriate arguments return bSearch( array, 0, size-1, target ); }  Sometimes, we even name the internal function (the recursive one) with another name, so that the user will not mistakenly call this function. For example, rename it as bSearch_internal, _bSearch, or something else. The user will only use bSearch. bSearch will call bSearch_internal for the user.

Department of Computer Science and Engineering, HKUST 26 Recursion Program Call Stack in Recursion

27 Program Call Stack  Recall that calling functions will put activation records on the program call stack.  Recursive function will call itself. Several activation records are correspond to the same recursive function. But they are corresponds to different instances of function invocations (or function calls).

28 Program Call Stack (Cont’)  Consider the following recursive function: double exp(double x, int y){ double result = 0;// line A if(y==0) result = 1; else result = x * exp(x, y-1);// line B return result;// line C }  Now, trace the invocation of exp(3.2, 2).

29 Program Call Stack (Cont’) main exp x = 3.2 y = 2 result = 0 (Line A) main exp x = 3.2 y = 2 result = 0 exp x = 3.2 y = 1 result = 0 (Line A) (Line B) main exp x = 3.2 y = 2 result = 0 exp x = 3.2 y = 1 result = 0 exp x = 3.2 y = 0 result = 0 (Line A) (Line B)

30 Program Call Stack (Cont’) main exp x = 3.2 y = 2 result = 0 exp x = 3.2 y = 1 result = 0 exp x = 3.2 y = 0 result = 1 (Line B) (Line C) main exp x = 3.2 y = 2 result = (Line C) main exp x = 3.2 y = 2 result = 0 exp x = 3.2 y = 1 result = 3.2 (Line B) (Line C)

31 Program Call Stack in recursion  Some students misunderstood the concept of recursion because: They think that different instances of function cannot be co-exist, The value of the local variables are shared among all invocation of the functions.  Instead, these are not correct since Different invocations of the same function corresponds to different activation records in the program call stack. Local variables are stored in activation record, and hence are not shared among different invocation of the same function.

Department of Computer Science and Engineering, HKUST 32 Recursion Inline recursive function?

33 Inline Function  Recall that inlining a function can avoid the overhead of calling function.  However, based on the property of recursive function, C++ will not inline a recursive function. If it tries to inline a recursive function, it will copy the whole function to replace the recursive function, and then copy again and again … Infinite loop…   Therefore, even if you try to put the keyword inline in front of the function, it won’t inline recursive function.  inline keyword is only a hint (not a must) to the C++ compiler.

Department of Computer Science and Engineering, HKUST 34 Recursion Common Pitfalls in Recursion

35 Forget to write the base case int factorial( int n ){ //if ( n == 0 )// base case //return 1; // else return n * factorial(n-1);// recursive case }  This will loop infinitely: For example, factorial(0) will call factorial(-1), factorial(-10000) will call factorial(-10001), loops forever.

36 Inputs of recursive call does not change int factorial( int n ){ if ( n == 0 )// base case return 1; else return n * factorial(n);// recursive case }  This will also loop infinitely: Then, factorial(100) will call factorial(100), which will call factorial(100), … Some tricky questions in COMP251 says that this is not necessary to result in infinite loop.

37 Stack Overflow  Note that the program call stack is a limited resource. FYI: usually, we allocate 1MB or 2MB to stack, even on an 2GB RAM machine.  Recursive function will put many records on the stack.  easily to make it overflow.

38 Stack Overflow  Solutions: Minimize the use of local variables in recursive functions:  This can reduce the size of each activation record.  Note that local variables include function parameters (many students forget this). Don’t write recursive functions:  Will be covered in a minute. Implement a stack yourself:  Allocate a stack on the heap (2GB RAM machine will have ~2GB heap).  Push/pop the activation record (or simply the variable you needed to store) on/from the stack.

39 Stack Overflow (Solution 2) – Don’t write recursive functions  Many recursive function can be written in an iterative form. int factorial( int n ){ if ( n == 0 ) return 1; return n * factorial(n-1); } int factorial( int n ){ int result = 1; for (int i=2; i<=n; i++) result *= i; return result; }

40 Stack Overflow (Solution 2) – Don’t write recursive functions double exp(double x, int y){ if(y==0) return 1; return x * exp(x, y-1); } double exp(double x, int y){ double result = 1; for (int i=0; i<y; i++) result *= x; return result; }

41 Stack Overflow (Solution 2) – Don’t write recursive functions int zeros(int n){ if(n==0) return 1; if(n < 10) return 0; if(n%10 == 0) return 1 + zeros(n/10); else return zeros(n/10); } int zeros(int n){ int numZero = 0; do { if ( n%10 == 0 ) numZero += 1; n /= 10; } while ( n >= 10 ); return numZero; }

42 Stack Overflow (Solution 2) – Don’t write recursive functions  Convince yourself the following conversion is correct. int fibonacci( int n ){ if ( n <= 1 ) return 1; return fibonacci(n-1) + fibonacci(n-2); } int fibonacci( int n ){ int val[2]; val[0] = val[1] = 1; for (int i=2; i<=n; i++) val[i%2] = val[0] + val[1]; return val[n % 2]; }

43 Stack Overflow (Solution 2) – Don’t write recursive functions  Just as a practice, write the bSearch with the iterative method int linearSearch( int array[ ], int size, int target){ for (int i=0; i<size; i++) { if ( array[i] == target ) return i; } return NOT_FOUND; } int linearSearch( int array[], int left, int right, int target){ if ( left == right ) return NOT_FOUND; if ( array[left] == target ) return left; return linearSearch( array, left+1, right, target); }

44 Stack Overflow (Solution 2) – Don’t write recursive functions  It seems that all algorithms we have visited so far can be written as iterative. However, the iterative version is sometimes counter-intuitive.  The recursive version is usually simpler to understand. Not all recursive algorithms can be written as iterative (without using an additional stack).  You will learn more recursive algorithms, which may not be written in iterative form, in COMP171.

Department of Computer Science and Engineering, HKUST 45 Recursion When Function Declaration is necessary?

46 When Function Declaration is necessary?  Since function definition can act as function declaration, you can then move all function definitions to the front of the program, without any function declaration. It seems that it is not necessary to declare a function first.  However, considering the following scenario: int f( int x ) { if ( x == 0 ) return 0; return g( x – 1 ); } int g( int x ) { if ( x == 0 ) return 1; return f( x – 1 ); }  f(n) = 1 if n is an odd integer, otherwise it returns 0.

47 When Function Declaration is necessary?  However, the above code is not compliable. Note that function f calls the function g, but the compiler haven’t read the definition nor declaration of the function g. Similar result will be obtained even if you swap the order of the two functions definitions.  Solution: Declare the two functions first and then define them in any order. int f( int x ); int g( int x ); int f( int x ) { /*... */ } int g( int x ) { /*... */ }