Recursion, pt. 2: Thinking it Through. What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Introduction to Recursion and Recursive Algorithms
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
HST 952 Computing for Biomedical Scientists Lecture 9.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort Quicksort     29  9.
Recursion.
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
Sorting21 Recursive sorting algorithms Oh no, not again!
Recursion Chapter 11 Chapter 11.
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
Recursion.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Algorithm Efficiency and Sorting
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.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
(c) , University of Washington
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
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 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
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.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
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.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
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.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
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 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
Recursion, pt. 3: The Towers of Hanoi. Towers of Hanoi One classic recursive problem is that of the Towers of Hanoi.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
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:
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion Powerful Tool
Sections 4.1 & 4.2 Recursive Definitions,
Recursion DRILL: Please take out your notes on Recursion
Decrease-and-Conquer Approach
Applied Algorithms (Lecture 17) Recursion Fall-23
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Yan Shi CS/SE 2630 Lecture Notes
CSC 143 Binary Search Trees.
Recursion.
ITEC324 Principle of CS III
Presentation transcript:

Recursion, pt. 2: Thinking it Through

What is Recursion? Recursion is the idea of solving a problem in terms of solving a smaller instance of the same problem – For some problems, it may not possible to find a direct solution.

What is Recursion? One famous problem which is solved in a recursive manner: the factorial. – n! = 1 for n = 0, n =1… – n! = n * (n-1)!, n > 1. Note that aside from the n=0, n=1 cases, the factorial’s solution is stated in terms of a reduced form of itself.

The Reduction Process There are these two main elements to a recursive solution: – The base case: the form (or forms) of the problem for which an exact solution is provided. – The recursive step: the reduction of the problem to a simpler form.

The Reduction Process Once the base case has been reached, the solution obtained for the simplified case is modified, step by step, to yield the correct answer for the original case.

Sorting One classic application for recursion is for use in sorting. – What might be some strategies we could use for recursively sorting? During the first week, I gave a rough overview of quicksort. There are other divide-and-conquer type strategies.

Merge Sort One technique, called merge sort, operates on the idea of merging two pre-sorted arrays. – How could such a technique help us?

Merge Sort One technique, called merge sort, operates on the idea of merging two pre-sorted arrays. – If we have two presorted arrays, then the smallest overall element must be in the first slot of one of the two arrays. It dramatically reduces the effort needed to find each element in its proper order.

Merge Sort Problem: when presented with a fresh array, with items strewn about in random order within it… how can we use this idea of “merging” to sort the array? – Hint: think with recursion! – Base case: n = 1 – a single item is automatically a “sorted” list.

Merge Sort We’ve found our base case, but what would be our recursive step? – Given the following two sorted arrays, how would we merge them into a single sorted array? [13] => [13 42] [42]

Merge Sort Given the following two sorted arrays, how would we merge them into a single sorted array? [-2, 47] => [-2, 47, 57, 101] [57, 101]

Merge Sort Given the following two sorted arrays, how would we merge them into a single sorted array? [13, 42] => [7, 101] [,,, ] 101 7, 13 42

Merge Sort Can we find a pattern that we can use to make a complete recursive step?

Merge Sort Given the following two sorted arrays, how would we merge them into a single sorted array? [13, 42] => [7, 101] [,,, ] 101 7, 13 42

Merge Sort In words: – If both lists still have remaining elements, pick the smaller of the first elements and consider that element removed. – If only one list has remaining elements, copy the remaining elements into place. This is the recursive step of merge sort.

Merge Sort [ ],[ ] [13 32],[77 55],[43 1],[42 88] | | | [13 32],[55 77],[ 1 43],[42 88] [ ],[ ] [ ]

Merge Sort Note that for each element insertion into the new array, only one element needs to be examined from each of the two old arrays. – It’s possible because the two arrays are presorted. – The “merge” operation thus takes O(N) time.

Questions? Homework: implement mergesort

Recursive Structure: Binary Tree

Recursive Structures One data structure we have yet to examine is that of the binary tree. How could we create a method that prints out the values within a binary tree in sorted order?

Binary Tree

Binary Tree Code template class TreeNode { public: K key; V value; TreeNode * left; TreeNode * right; }

Binary Tree What can we note about binary trees that can help us print them in sorted order?

Binary Tree Note that for a given node, anything in the left subtree comes (in sorted order) before the node. On the other hand, anything in the right subtree comes after the node.

Binary Tree Recursion So, to print out the binary tree… void print(TreeNode * node) { if(node == 0) return; print(node.left); cout << node.value << “ ”; print(node.right); }

Binary Tree Recursion Calling print() with the tree’s root node will then print out the entire tree. – Note that we’re dumping the values to the console because it’s simpler for teaching purposes. We should instead set up either an iterator which returns each item in the tree, one at a time, in proper order… or a custom operator <<.

Binary Tree

Questions?

Analysis 1.We started at n and reduced the problem to 1!. – Is there a reason we couldn’t start from 1 and move up to n? 2.The actual computation was done entirely at the end of the method, after it returned from recursion. – Could we do some of this calculation on the way, before the return?

Using Recursion Note that the core reduction of the problem is still the same, no matter how we handle the issues raised by #1 and #2. How we choose to code this reduction, however, can vary greatly, and can even make a difference in efficiency.

Using Recursion Let’s examine the issue raised by #1: that of starting from the reduced form and moving to the actual answer we want.

Coding Recursion int factorial(int n) { if(n<0) throw Exception(); if(???) return ?; else return n * factorial(n+1); } Hmm. We’re missing something.

Coding Recursion How will we know when we reach the desired value of n? – Also, isn’t this method modifying the actual value of n? Maybe we need… another parameter.

Coding Recursion int factorial(int i, int n) { if(i<0) throw exception(); if(???) return ?; else return i * factorial(i+1, n); } That looks better. When do we stop?

Coding Recursion int factorial(int i, int n) { if(i<0) throw exception(); if(i >= n) return n; else return i * factorial(i+1, n); } Well, almost. It might need cleaning up.

Helper Methods Unfortunately, writing the methods in this way does leave a certain… design flaw in place. – We’re expecting the caller of these methods to know the correct initial values to place in the parameters. – We’ve left part of our overall method implementation exposed. This could cause issues.

Helper Methods A better solution would be to write a helper method solution. – It’s so named because its sole reason to exist is to help setup the needed parameters and such for the true, underlying method.

Coding Recursion int factorial(int i, int n) { if(i >= n) return n; else return i * factorial(i+1, n); }

Helper Methods int factorialStarter(int n) { if(n < 0) throw exception(); else if(n==0) return 1; else return factorial(1, n); }

Helper Methods Note how factorialStarter performs the error-checking and sets up the special recursive parameter. – This would be the method that should be called for true factorial functionality. – The original factorial method would then be its helper method, aiding the originally-called method perform its tasks.

Helper Methods Note that we wish for only factorialStarter to be generally accessible – to be public. – Assuming, of course, that these methods are class methods. – The basic factorial method is somewhat exposed. – The solution? Make factorial private !

Questions?

Using Recursion Let’s now turn our attention to the issue raised by #2: that of when the main efforts of computation occur. – For this version, we’ll return to starting at “n” and counting down to 1.

Using Recursion How can we perform most of the computation before reaching the base case of our problem? – 5!= 5 * 4! = 5 * 4 * 3! = … = 5 * 4 * 3 * 2 * 1!

Using Recursion How can we perform most of the computation before reaching the base case of our problem? – 5!= 5 * 4! = 5 * 4 * 3! = … = 5 * 4 * 3 * 2 * 1! – We could keep track of this multiplier across our recursive calls.

Coding Recursion int factorial(int part, int n) { if(n == 0 || n == 1) return part; else return factorial (part * n, n-1); }

Coding Recursion int factorial(int n) { return factorial(1, n); } Using a separate method to start our computation allows us to hide the additional internal parameter.

Tail Recursion A tail-recursive method is one in which all of the computation is done during the initial method calls. – When a method is tail-recursive, the final, full desired answer may be obtained once the base case is reached. – In such conditions, the answer is merely forward back through the chain of remaining “return” statements.

Tail Recursion int factorial(int part, int n) { if(n == 0 || n == 1) return part; else return factorial(part * n, n-1); } Note how in the recursive step, the “reduced” problem’s return value is instantly returned.

Tail Recursion For methods which are tail-recursive, compilers can shortcut the entire return statement chain and instead return directly from the original, first call of the method. – In essence, a well-written compiler can reduce tail-recursive methods to mere iteration.

Questions?