Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Introduction to Recursion and Recursive Algorithms
Recursion.  Understand how the Fibonacci series is generated  Recursive Algorithms  Write simple recursive algorithms  Analyze simple recursive algorithms.
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
Recursion. Binary search example postponed to end of lecture.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CMPT 225 Recursion-part 3. Recursive Searching Linear Search Binary Search Find an element in an array, return its position (index) if found, or -1 if.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
COMPSCI 105 S Principles of Computer Science Recursion 3.
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 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
What is recursion? Imagine checking the dictionary for “apple”  a round fruit with a firm white flesh and a green, red or yellow skin  the usually sweet-tasting.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
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.
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.
Data Structures and Abstractions with Java, 4e Frank Carrano
Chapter 12 Recursion, Complexity, and Searching and Sorting
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
CSC 211 Data Structures Lecture 13
 O(1) – constant time  The time is independent of n  O(log n) – logarithmic time  Usually the log is to the base 2  O(n) – linear time  O(n*logn)
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
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.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
Data Structure Introduction.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
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.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Hassan Khosravi / Geoffrey Tien
More Computational Theory
Review: recursion Tree traversals
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Algorithm design and Analysis
CS201: Data Structures and Discrete Mathematics I
Basics of Recursion Programming with Recursion
Algorithms Recurrences.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Recursion

 Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack  Compute the result of simple recursive algorithms  Understand the drawbacks of recursion  Analyze the running times of recursive functions  Covered in the O Notation section John Edgar2

A certain man put a pair of rabbits in a place surrounded on all sides by a wall. How many pairs of rabbits can be produced from that pair in a year if it is supposed that every month each pair begets a new pair which from the second month on becomes productive? Liber Abaci, Leonardo Pisano Bigollo (aka Fibonacci), 1202 John Edgar3

 What happens if you put a pair of rabbits in a field?  More rabbits!  Assumptions  Rabbits take one month to reach maturity and  Each pair of rabbits produces another pair of rabbits one month after mating  Rabbits never die  Bunny heaven! John Edgar 4

 How many pairs of rabbits are there after 5 months?  Month 1: start – 1 pair  Month 2: the rabbits are now mature and mate – 1 pair  Month 3: – the first pair give birth to two babies – 2 pairs  Month 4: the first pair give birth to 2 babies, the pair born in month 3 are now mature – 3  Month 5: the 3 pairs from month 4, and 2 new pairs – 5 John Edgar 5

AAfter 5 months there are 5 pairs of rabbits ii.e. the number of pairs at 4 months (3) plus the number of pairs at 3 months (2) WWhy? WWhile there are 3 pairs of bunnies in month 4 only 2 of them are able to mate tthe ones alive in the previous month TThis series of numbers is called the Fibonacci series 6 month: pairs:

TThe n th number in the Fibonacci series, fib(n), is: 00 if n = 0, and 1 if n = 1 ffib(n – 1) + fib(n – 2) for any n > 1 ee.g. what is fib(23) EEasy if we only knew fib(22) and fib(21) TThe answer is fib(22) + fib(21) WWhat happens if we actually write a function to calculate Fibonacci numbers like this? John Edgar7

8

 Let's write a function just like the formula  fib(n) = 0 if n = 0, 1 if n = 1,  otherwise fib(n) = fib(n – 1) + fib(n – 2) John Edgar9 int fib(int n){ if(n == 0 || n == 1){ return n; }else{ return fib(n-1) + fib(n-2); } int fib(int n){ if(n == 0 || n == 1){ return n; }else{ return fib(n-1) + fib(n-2); } The function calls itself

 The Fibonacci function is recursive  A recursive function calls itself  Each call to a recursive method results in a separate call to the method, with its own input  Recursive functions are just like other functions  The invocation is pushed onto the call stack  And removed from the call stack when the end of a method or a return statement is reached  Execution returns to the previous method call John Edgar10

int fib(int n) if(n == 0 || n == 1) return n else return fib(n-1) + fib(n-2) John Edgar11 fib(5) fib(4) fib(3) fib(2) fib(1) fib(0) fib(2) fib(1) fib(0) fib(1) fib(2) fib(1) fib(0) fib(1) In the next section of the course we will explain that this is a hideously inefficient way of computing the series …

 When a function is called its data is pushed onto the call stack, and execution switches to the function  When a recursive function call is made, execution switches to the current invocation of that function  The call stack records the line number of the previous function where the call was made from ▪ Which will be the previous invocation of the same function  Once the function call ends it also returns to the previous invocation John Edgar12

 Recursive functions do not use loops to repeat instructions  But use recursive calls, in if statements  Recursive functions consist of two or more cases, there must be at least one  Base case, and one  Recursive case John Edgar13

 The base case is a smaller problem with a simpler solution  This problem’s solution must not be recursive ▪ Otherwise the function may never terminate  There can be more than one base case  And base cases may be implicit John Edgar14

 The recursive case is the same problem with smaller input  The recursive case must include a recursive function call  There can be more than one recursive case John Edgar15

 Define the problem in terms of a smaller problem of the same type  The recursive part  e.g. return fib(n-1) + fib(n-2);  And the base case where the solution can be easily calculated  This solution should not be recursive  e.g. if (n == 0 || n == 1) return n; John Edgar16

 How can the problem be defined in terms of smaller problems of the same type?  By how much does each recursive call reduce the problem size?  By 1, by half, …?  What is the base case that can be solved without recursion?  Will the base case be reached as the problem size is reduced? John Edgar17

Linear Search Binary Search John Edgar18

John Edgar19 int linSearch(int arr[], int sz, int x){ for (int i=0; i < sz; i++){ if(x == arr[i]){ return i; } } //for return -1; //target not found } int linSearch(int arr[], int sz, int x){ for (int i=0; i < sz; i++){ if(x == arr[i]){ return i; } } //for return -1; //target not found } The algorithm searches an array one element at a time using a for loop

 Base cases  Target is found, or the end of the array is reached  Recursive case  Target not found John Edgar20 int recLinSearch(int arr[], int next, int sz, int x){ if (next >= sz){ return -1; } else if (x == arr[i]){ return i; } else return recLinSearch(arr, sz, next + 1, x); } int recLinSearch(int arr[], int next, int sz, int x){ if (next >= sz){ return -1; } else if (x == arr[i]){ return i; } else return recLinSearch(arr, sz, next + 1, x); }

 Requires that the array is sorted  In either ascending or descending order  Make sure you know which!  A divide and conquer algorithm  Each iteration divides the problem space in half  Ends when the target is found or the problem space consists of one element John Edgar21

John Edgar22 The array is sorted, and contains 16 items indexed from 0 to 15 The array is sorted, and contains 16 items indexed from 0 to 15 Guess that the target item is in the middle, that is index = 15 / 2 = 7 Guess that the target item is in the middle, that is index = 15 / 2 = 7 value index Search for 32

John Edgar23 45 is greater than 32 so the target must be in the lower half of the array 45 is greater than 32 so the target must be in the lower half of the array value index Search for 32 Repeat the search, guessing the mid point of the lower sub-array (6 / 2 = 3) Repeat the search, guessing the mid point of the lower sub-array (6 / 2 = 3) Everything in the upper half of the array can be ignored, halving the search space Everything in the upper half of the array can be ignored, halving the search space

John Edgar24 21 is less than 32 so the target must be in the upper half of the subarray 21 is less than 32 so the target must be in the upper half of the subarray value index Search for 32 Repeat the search, guessing the mid point of the new search space, 5 Repeat the search, guessing the mid point of the new search space, 5 The mid point = (lower subarray index + upper index) / 2 The mid point = (lower subarray index + upper index) / 2 The target is found so the search can terminate The target is found so the search can terminate

 Each sub-problem searches a sub-array  Differs only in the upper and lower array indices that define the sub-array  Each sub-problem is smaller than the last one  In the case of binary search, half the size  There are two base cases  When the target item is found and  When the problem space consists of one item ▪ Make sure that this last item is checked John Edgar25

John Edgar26 int binSearch( int arr[], int sz, int lo, int hi, int x){ int mid = (lo + hi) / 2; if (lo > hi){ return - 1; //base case } else if(arr[mid] == x){ return mid; //second base case } else if(arr[mid] < x){ return binSearch(arr, sz, mid + 1, hi, x); } else { //arr[mid] > target return binSearch(arr, sz, lo, mid - 1, x); } int binSearch( int arr[], int sz, int lo, int hi, int x){ int mid = (lo + hi) / 2; if (lo > hi){ return - 1; //base case } else if(arr[mid] == x){ return mid; //second base case } else if(arr[mid] < x){ return binSearch(arr, sz, mid + 1, hi, x); } else { //arr[mid] > target return binSearch(arr, sz, lo, mid - 1, x); }

John Edgar27

 Some recursive algorithms are inherently inefficient  e.g. the recursive Fibonacci algorithm which repeats the same calculation again and again  Look at the number of times fib(2) is called  Such algorithms should be implemented iteratively  Even if the solution was determined using recursion John Edgar28

 Recursive algorithms have more overhead than similar iterative algorithms  Because of the repeated function calls  Each function call has to be inserted on the stack  It is often useful to derive a solution using recursion and implement it iteratively  Sometimes this can be quite challenging! John Edgar29

 Some recursive functions result in the call stack becoming full  Which usually results in an error and the termination of the program  This happens when function calls are repeatedly pushed onto the stack  And not removed from the stack until the process is complete John Edgar30

 This function works  But try running it to sum the numbers from 1 to 8,ooo  It will probably result in a stack overflow ▪ Since 8,000 function calls have to be pushed onto the stack! John Edgar31 int sum(int x){ if (x == 0 || x == 1){ return x; } else { return x + sum(x – 1); } int sum(int x){ if (x == 0 || x == 1){ return x; } else { return x + sum(x – 1); }

 This function won't result in a stack overflow  Why not? ▪ Hint – think how fast factorials increase John Edgar32 int fact (int x){ if (x == 0 || x == 1){ return 1; } else { return x * fact(x – 1); } int fact (int x){ if (x == 0 || x == 1){ return 1; } else { return x * fact(x – 1); }

 What happens when we run fact(5)?  Each function call returns a value to a calculation in a previous call  Like this  fact(5) ▪ fact(4) ▪ fact(3)  fact(2)  fact(1)  1 returned to fact(2)  2 returned to fact(3) ▪ 6 returned to fact(4) ▪ 24 returned to fact(5)  120 returned from fact(5)  The final answer is only computed in the final return statement John Edgar33 int fact (int x){ if (x == 0 || x == 1){ return 1; } else { return x * fact(x – 1); } int fact (int x){ if (x == 0 || x == 1){ return 1; } else { return x * fact(x – 1); }

 Another recursive factorial function  The recursive call is the last statement in the algorithm and  The final result of the recursive call is the final result of the function  The function has a second parameter that contains the result John Edgar34 int factTail (int x, int result){ if (x <= 1){ return result; } else { return factTail(x-1, result * x); } int factTail (int x, int result){ if (x <= 1){ return result; } else { return factTail(x-1, result * x); }

 Here is the trace of factTail(5, 1)  factTail(5, 1) ▪ factTail(4, 5) ▪ factTail(3, 20)  factTail(2, 60)  factTail(1, 120)  120 returned to fact(2)  120 returned to fact(3) ▪ 120 returned to fact(4) ▪ 120 returned to fact(5)  120 returned from fact(5)  The final answer is returned through each of the recursive calls, and is calculated at the bottom of the recursion tree John Edgar35 int factTail (int x, int result){ if (x <= 1){ return result; } else { return factTail(x-1, result * x); } int factTail (int x, int result){ if (x <= 1){ return result; } else { return factTail(x-1, result * x); }

int factTail (int x, int result){ if (x <= 1){ return result; } else { return factTail(x-1, result * x); } int factTail (int x, int result){ if (x <= 1){ return result; } else { return factTail(x-1, result * x); }  Tail recursive functions can be easily converted into iterative versions  This is done automatically by some compilers John Edgar36 int factIter (int x){ int result = 1; while (x > 1){ result = result * x; x--; } return result; } int factIter (int x){ int result = 1; while (x > 1){ result = result * x; x--; } return result; }

 It is useful to trace through the sequence of recursive calls  This can be done using a recursion tree  Recursion trees can be used to determine the running time of algorithms  Annotate the tree to indicate how much work is performed at each level of the tree  And then determine how many levels of the tree there are John Edgar37

John Edgar38

 Mathematical induction is a method for performing mathematical proofs  An inductive proof consists of two steps  A base case that proves the formula hold for some small value (usually 1 or 0)  An inductive step that proves if the formula holds for some value n, it also holds for n+1  The inductive step starts with an inductive hypothesis  An assumption that the formula holds for n John Edgar39

 Recursion is similar to induction  Recursion solves a problem by  Specifying a solution for the base case and  Using a recursive case to derive solutions of any size from solutions to smaller problems  Induction proves a property by  Proving it is true for a base case and  Proving that it is true for some number, n, if it is true for all numbers less than n John Edgar40

 Prove, using induction that the algorithm returns the values  fact(0) = 0! = 1  fact(n) = n! = n * (n – 1) * … * 1 if n > 0 John Edgar41 int fact (int x){ if (x == 0){ return 1; } else { return x * fact(x – 1); } int fact (int x){ if (x == 0){ return 1; } else { return x * fact(x – 1); }

 Base case: Show that the property is true for n = 0, i.e. that fact(0) returns 1  This is true by definition as fact(0) is the base case of the algorithm and returns 1  Establish that the property is true for an arbitrary k implies that it is also true for k + 1  Inductive hypothesis: Assume that the property is true for n = k, that is assume that  fact(k) = k * ( k – 1) * ( k – 2) * … * 2 * 1 John Edgar42

IInductive conclusion: Show that the property is true for n = k + 1, i.e., that fact(k + 1) returns ((k + 1) * k * (k – 1) * (k – 2) * … * 2 * 1 BBy definition of the function: fact(k + 1) returns ((k + 1) * fact(k) – the recursive case AAnd by the inductive hypothesis: fact(k) returns kk * (k – 1) * (k – 2) * … * 2 * 1 TTherefore fact(k + 1) must return ((k + 1) * k * (k – 1) * (k – 2) * … * 2 * 1 WWhich completes the inductive proof John Edgar43

John Edgar44

 Towers of Hanoi  Eight Queens problem  Sorting  Mergesort  Quicksort John Edgar45

 Linked Lists are recursive data structures  They are defined in terms of themselves  There are recursive solutions to many list methods  List traversal can be performed recursively  Recursion allows elegant solutions of problems that are hard to implement iteratively ▪ Such as printing a list backwards John Edgar46