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.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
Recursion. Binary search example postponed to end of lecture.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
Programming with Recursion
C++ Plus Data Structures
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
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.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Chapter 3: Recursive Algorithms
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.
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.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
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.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Recursion CS 302 – Data Structures Chapter 7. What is recursion? smaller A technique that solves problem by solving smaller versions of the same problem!
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
Merge Sort. Stable vs. Non-Stable Sorts We frequently use sorting methods for items with multiple keys Sometimes we need to apply the sorting with different.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Contest Algorithms January 2016 Pseudo-code for divide and conquer, and three examples (binary exponentiation, binary search, and mergesort). 5. Divide.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
Chapter 6 Lists Plus Lecture 12. What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element.
Sorting and Searching Algorithms CS Sorting means... l The values stored in an array have keys of a type for which the relational operators are.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Pei Zheng, Michigan State University 1 Chapter 8 Recursion.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Recursion Powerful Tool
Programming with Recursion
Recursion.
Programming with Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Chapter 10 Sorting and Searching Algorithms
Hassan Khosravi / Geoffrey Tien
C++ Plus Data Structures
Yan Shi CS/SE 2630 Lecture Notes
CSE 373 Data Structures and Algorithms
Yan Shi CS/SE 2630 Lecture Notes
Chapter 18 Recursion.
Searching/Sorting/Searching
Recursive Algorithms 1 Building a Ruler: drawRuler()
Recursion.
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

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 the call. In other words, recursion occurs when a function calls itself! We must avoid making an infinite sequence of function calls (infinite recursion).

3 Finding a Recursive Solution Each successive recursive call should bring you closer to a situation in which the answer is known. A case for which the answer is known (and can be expressed without recursion) is called a base case. Each recursive algorithm must have at least one base case, as well as the general (recursive) case

4 General format for many recursive functions if (some condition for which answer is known) // base case solution statement else // general case recursive function call SOME EXAMPLES...

5 Writing a recursive function to find n factorial DISCUSSION The function call Factorial(4) should have value 24, because that is 4 * 3 * 2 * 1. For a situation in which the answer is known, the value of 0! is 1. So our base case could be along the lines of if ( number == 0 ) return 1;

6 Writing a recursive function to find Factorial(n) Now for the general case... The value of Factorial(n) can be written as n * the product of the numbers from (n - 1) to 1, that is, n * (n - 1) *... * 1 or, n * Factorial(n - 1) And notice that the recursive call Factorial(n - 1) gets us “closer” to the base case of Factorial(0).

7 Recursive Solution int Factorial ( int number ) // Pre: number is assigned and number >= 0. { if ( number == 0)// base case return 1 ; else// general case return number * Factorial ( number - 1 ) ; }

8 Three-Question Method of verifying recursive functions Base-Case Question: Is there a non-recursive way out of the function? Smaller-Caller Question: Does each recursive function call involve a smaller case of the original problem leading to the base case? General-Case Question: Assuming each recursive call works correctly, does the whole function work correctly?

9 Another example where recursion comes naturally From mathematics, we know that 2 0 = 1 and 2 5 = 2 * 2 4 In general, x 0 = 1 and x n = x * x n-1 for integer x, and integer n > 0. Here we are defining x n recursively, in terms of x n-1

// Recursive definition of power function int Power ( int x, int n ) // Pre: n >= 0. x, n are not both zero // Post: Function value = x raised to the power n. { if ( n == 0 ) return 1; // base case else // general case return ( x * Power ( x, n-1 ) ) ; } Of course, an alternative would have been to use looping instead of a recursive call in the function body. 10

11 “Why use recursion?” Those examples could have been written without recursion, using iteration instead. The iterative solution uses a loop, and the recursive solution uses an if statement. However, for certain problems the recursive solution is the most natural solution.

12 Use a recursive solution when: The depth of recursive calls is relatively “shallow” compared to the size of the problem. The recursive version does about the same amount of work as the nonrecursive version. The recursive version is shorter and simpler than the nonrecursive solution. SHALLOW DEPTH EFFICIENCY CLARITY

13 the quick sort algorithm A.. Z A.. L M.. Z A.. F G.. L M.. R S.. Z

14 Before call to function Split values[first] [last] splitVal = 9 GOAL: place splitVal in its proper position with all values less than or equal to splitVal on its left and all larger values on its right

15 After call to function Split values[first] [splitPoint] [last] splitVal = 9 smaller values larger values

// Recursive quick sort algorithm template void QuickSort ( ItemType values[ ], int first, int last ) // Pre: first <= last // Post: Sorts array values[ first..last ] into ascending order { if ( first < last ) // general case {int splitPoint ; Split ( values, first, last, splitPoint ) ; // values [ first ].. values[splitPoint - 1 ] <= splitVal // values [ splitPoint ] = splitVal // values [ splitPoint + 1 ].. values[ last ] > splitVal QuickSort( values, first, splitPoint - 1 ) ; QuickSort( values, splitPoint + 1, last ); } } 16

Merge Sort Algorithm

Divide and Conquer We can use a type of a divide and conquer algorithm to sort an array We'll use what's called a merge sort We'll divide the portion of the array we are currently working on into two parts, and sort the parts, and then merge them together to finish the deal

Merge Sort Algorithm The merge sort algorithm is roughly as follows mergeSort(array, start, end) middle = start + end / 2 mergesort(arr, start, middle) mergesort(arr, middle+1, end) merge(arr, start, middle, end) Find a middle, sort the two halves, then merge them together

Merge Sort Algorithm What's missing? We need a base case, and there are two options 1) When the size of the part of the array we are working on is 1, we can consider this sorted 2) When the size of the part of the array we are working on is "small enough", use a "slower" sort to finish that portion (since recursive calls are expensive) We'll just use 1, for purposes of easier algorithm analysis, and we don't lose much anyway

Sorting - Merge Sort Use merge sort on the array A = [ 5, 2, 4, 6, 1, 3, 2, 6 ] Calling “merge_sort” method

Sorting - Merge Sort Merging operation on sorted sequence –lengths of the sorted sequence being merged increased as the algorithm progress from bottom to top merge merge sort on the array A = [ 5, 2, 4, 6, 1, 3, 2, 6 ]

Sorting - Merge Sort Algorithm: divide-and-conquer merge_sort(A, i, j) { if ( i< j ) { compute k = ( i + j ) / 2 merge_sort(A, i, k) merge_sort(A, k+1, j) return( merge the two sorted sequences to form one sorted sequence) } A ij A i k A k+1j

Merge Step The merge step algorithm should run as fast as possible The merge function will get the start, middle, and end of the array What it can do is create a secondary array to copy values into, and then copy those back into the original array

Merge Step Algorithm merge(array, start, middle, end) make temp array while neither array is empty if array[start] > array[middle] copy array[start++] into temp else copy array[middle++] into temp while not finished array is not empty copy all elements into temp copy elements of temp into array

Merge Step Runtime What is the runtime of the merge step? If we look at the while loops, the first one either goes from start to middle OR middle to end, with some portion of the other, while the 2nd while loop finishes the job Thus, combined, the while loops take a total time of end - start, which, in the worse case, is O(n)

Merge Step Code private static void merge(int[] arr, int start, int mid, int end) { int[] temp = new int[end-start+1]; int first1 = start, last1 = mid, first2 = mid+1, last2 = end; int index = 0; while (first1 <= last1 && first2 <= last2) if (arr[first1] < arr[first2]) temp[index++] = arr[first1++]; else temp[index++] = arr[first2++]; while (first1 <= last1) temp[index++] = arr[first1++]; while (first2 <= last2) temp[index++] = arr[first2++]; for (int i = 0, j = start; i < end-start+1; i++, j++) arr[j] = temp[i]; }

Merge Sort Code public static void mergeSort(int[] arr) { mergeSort(arr, 0, arr.length-1); } private static void mergeSort(int[] arr, int start, int end) { if (start < end) { int mid = (start + end) / 2; mergeSort(arr, start, mid); mergeSort(arr, mid+1, end); merge(arr, start, mid, end); } }

Merge Sort Analysis The base case is T(1) = 1, what is the recursive case for the recurrence relation? The merge step is O(n) (as we figured out before), there are two recursive calls, and 1 (insignificant) other statement T(n) = 2*T(n/2) + O(n) + 1 We showed in the last set of slides that this is O(n log n)

Other D&C Algorithms Quicksort (a D&C with a worst case of n 2 ) Tree traversals (linear time; this will be discussed later on in the course)

Summary Divide and conquer gives us recursive solutions to problems We attempt to inch closer to the solution by dividing the problem in half and doing something interesting with the result of solving the halves As with the merge sort and maximum subsequence sum algorithms, we may have to do something beyond looking at the results of the halves We can analyze divide and conquer algorithms very easily by looking at recurrence relations

32 Function BinarySearch( ) BinarySearch takes sorted array info, and two subscripts, fromLoc and toLoc, and item as arguments. It returns false if item is not found in the elements info[fromLoc…toLoc]. Otherwise, it returns true. BinarySearch can be written using iteration, or using recursion.

33 found = BinarySearch(info, 25, 0, 14 ); item fromLoc toLoc indexes info NOTE: denotes element examined

// Recursive definition template bool BinarySearch ( ItemType info[ ], ItemType item, int fromLoc, int toLoc ) // Pre: info [ fromLoc.. toLoc ] sorted in ascending order // Post: Function value = ( item in info [ fromLoc.. toLoc] ) {int mid ; if ( fromLoc > toLoc ) // base case -- not found return false ; else { mid = ( fromLoc + toLoc ) / 2 ; if ( info [ mid ] == item ) // base case-- found at mid return true ; else if ( item < info [ mid ] ) // search lower half return BinarySearch ( info, item, fromLoc, mid-1 ) ; else // search upper half return BinarySearch( info, item, mid + 1, toLoc ) ; } } 34