Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.

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

Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the.
Lecture Objectives  Learn how to think recursively  Trace a recursive method  Prove a recursive method is correct  Write recursive algorithms and methods.
Data Structures and Algorithms
Recursion Chapter 7.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion Chapter 5.
Computer Science II Recursion Professor: Evan Korth New York University.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Recursion Chapter 7.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.
Recursion Chapter 5.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
RECURSION Chapter 5. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
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.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
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.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Recursion. Midterm Exam Wednesday,November 4, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices Simple problem solving.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
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.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Recursion.
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Recursion Topic 5.
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Chapter 8: Recursion Java Software Solutions
Java Software Structures: John Lewis & Joseph Chase
Recursion Chapter 5.
7.2 Recursive Definitions of Mathematical Formulas
7.4 Problem Solving with Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Chapter 8: Recursion Java Software Solutions
Chapter 8: Recursion Java Software Solutions
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursive Thinking.
7.2 Recursive Definitions of Mathematical Formulas
Presentation transcript:

Recursion Chapter 7

Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how to write recursive algorithms and methods for searching arrays To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to –solve the Towers of Hanoi problem –to process two-dimensional images –to solve search problems such as finding a path through a maze using backtracking

Spring 2010CS 2253 Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways –Recursion provides a different way of thinking about a problem Recursion splits a problem into one or more simpler versions of itself

Spring 2010CS 2254 Recursive Thinking If there is one figure in the nest process the figure else process the outer figure process the nest inside the outer figure

Spring 2010CS 2255 When to Use a Recursive Algorithm There must be at least one case (the base case), for a small value of n, that can be solved directly A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case)

Spring 2010CS 2256 Designing a Recursive Algorithm Recognize the base case and provide a solution to it Devise a strategy to split the problem into smaller versions of itself while making progress toward the base case Combine the solutions of the smaller problems in such a way as to solve the larger problem

Spring 2010CS 2257 String Length Algorithm if the string is empty the length is 0 else length = 1 plus length of substring that excludes the first character

Spring 2010CS 2258 Proof by induction Prove the theorem is true for the base case Show that if the theorem is assumed true for n, then it must be true for n+1 See the slides on induction

Spring 2010CS 2259 Proving that a Recursive Method is Correct Recursive proof is similar to induction –Verify the base case is recognized and solved correctly –Verify that each recursive case makes progress towards the base case –Verify that if all smaller problems are solved correctly, then the original problem is also solved correctly

Spring 2010CS Tracing a Recursive Method

Spring 2010CS Recursive Definitions of Mathematical Formulas Mathematicians often use recursive definitions of formulas that lead very naturally to recursive algorithms Examples include: –Factorial –Powers –Greatest common divisor If a recursive function never reaches its base case, a stack overflow error occurs

Spring 2010CS Recursive Factorial Method Recursive definition n! = 1 if n=0 n * (n - 1)! if n>0 Method public static int factorial( int n) { if (n==0) return 1; else return n * factorial( n-1); }

Spring 2010CS Recursive Algorithm for x n Algorithm if n is 0 result is 1 else result is x * x n-1 Method public static double power( double x, int n) { if (n==0) return 1; else return x * power( x, n-1);

Spring 2010CS Greatest Common Divisor Euclid's algorithm (assumes m>=n) gcd(m, n) = n if n is a divisor of m gcd(m, n) = gcd( n, m % n)otherwise Method public static double gcd( int m, int n) { if (m%n==0) return n; else if (m<n); return gcd( n, m); else return gcd( n, m%n); }

Spring 2010CS Recursion Versus Iteration There are similarities between recursion and iteration In iteration, a loop repetition condition determines whether to repeat the loop body or exit from the loop In recursion, the condition usually tests for a base case You can always write an iterative solution to a problem that is solvable by recursion Recursive code may be simpler than an iterative algorithm and thus easier to write, read, and debug Iteration is usually more efficient

Spring 2010CS Iterative Factorial Method public static factorialIter( int n) { int result = 1; for k=1; k<=n; k++) result *= n; return result; }

Spring 2010CS Efficiency of Recursion Recursive methods often have slower execution times when compared to their iterative counterparts The overhead for loop repetition is smaller than the overhead for a method call and return If it is easier to conceptualize an algorithm using recursion, then you should code it as a recursive method –The reduction in efficiency does not outweigh the advantage of readable code that is easy to debug

Spring 2010CS Recursive Fibonacci Method Recursive definition fib n = 1 for n = 1,2 fib n-1 + fib n-2 for n>2 Method public static int fibonacci( int n) { if (n<=2) return 1; else return fibonacci(n-1) + fibonacci(n-2); }

Spring 2010CS Efficiency of Recursion: Exponential Fibonacci Inefficient

Spring 2010CS An O(n) Recursive Fibonacci Method To avoid excessive computation, remember the previous two values. Start the computation public static int fibonacciStart( int n) { return fibo( 1, 0, n); } Recursive method private static int fibo( int current, int previous, int n) if (n==1) return current; else return fibo( current + previous, current, n-1); }

Spring 2010CS Efficiency of Recursion: O(n) Fibonacci Efficient

Spring 2010CS Recursive Array Search Searching an array can be accomplished using recursion Simplest way to search is a linear search –Examine one element at a time starting with the first element and ending with the last Base case for recursive search is an empty array –Result is negative one Another base case would be when the array element being examined matches the target Recursive step is to search the rest of the array, excluding the element just examined

Spring 2010CS Algorithm for Recursive Linear Array Search

Spring 2010CS Implementation of Recursive Linear Search

Spring 2010CS Design of Binary Search Algorithm Binary search can be performed only on an array that has been sorted Stop cases –The array is empty –Element being examined matches the target Checks the middle element for a match with the target Throw away the half of the array in which the target cannot be

Spring 2010CS Binary Search Algorithm

Spring 2010CS Trace of a Binary Search

Spring 2010CS Implementation of Binary Search

Spring 2010CS Implementation of Binary Search

Spring 2010CS Efficiency of Binary Search At each recursive call we eliminate half the array elements from consideration The number of times you can divide N by 2 before getting to 0 is log 2 N Therefore the efficiency is O(log 2 n)

Spring 2010CS Comparable Interface Classes that implement the Comparable interface must define a compareTo method that enables its objects to be compared in a standard way –compareTo allows one to define the ordering of elements of a class

Spring 2010CS Method Arrays.binarySearch Java API class Arrays contains a binarySearch method –Can be called with sorted arrays of primitive types or with sorted arrays of objects –If the objects in the array are not mutually comparable or if the array is not sorted, the results are undefined –If there are multiple copies of the target value in the array, there is no guarantee which one will be found –Throws ClassCastException if the target is not comparable to the array elements

Spring 2010CS Recursive Data Structures Computer scientists often encounter data structures that are defined recursively –Trees (Chapter 6) are defined recursively Linked list can be described as a recursive data structure Recursive methods provide a very natural mechanism for processing recursive data structures The first language developed for artificial intelligence research was a recursive language called LISP

Spring 2010CS Recursive Definition of a Linked List A non-empty linked list is a collection of nodes such that each node references another linked list consisting of the nodes that follow it in the list The last node references an empty list Recursive definition –A linked list is either empty, or it consists of a node (called the list head) that stores data and a reference to a linked list

Spring 2010CS Recursive Size Method

Spring 2010CS Recursive toString Method

Spring 2010CS Recursive Replace Method

Spring 2010CS Recursive Add Method

Spring 2010CS Recursive Remove Method

Spring 2010CS Recursive Remove Method

Spring 2010CS Problem Solving with Recursion Will look at two problems –Towers of Hanoi –Counting cells in a blob

Spring 2010CS Towers of Hanoi

Spring 2010CS Towers of Hanoi

Spring 2010CS Algorithm for Towers of Hanoi

Spring 2010CS Algorithm for Towers of Hanoi

Spring 2010CS Algorithm for Towers of Hanoi

Spring 2010CS Recursive Algorithm for Towers of Hanoi

Spring 2010CS Implementation of Recursive Towers of Hanoi

Spring 2010CS Counting Cells in a Blob Consider how we might process an image that is presented as a two-dimensional array of color values Information in the image may come from –X-Ray –MRI –Satellite imagery –Etc. Goal is to determine the size of any area in the image that is considered abnormal because of its color values

Spring 2010CS Classes for Counting Cells in a Blob

Spring 2010CS Algorithm for Counting Cells in a Blob if the cell is outside the grid result is 0 if color is not abnormal result is 0 else set color to marker color result = 1 + number of cells in blob that includes a neighbor

Spring 2010CS Implementation

Spring 2010CS Implementation

Spring 2010CS Counting Cells in a Blob

Spring 2010CS Backtracking Backtracking is an approach to using trial and error in a search for a solution –Backtracking is a systematic approach to trying alternative paths and eliminating them if they don’t work For example: finding a path through a maze –Start by walking down a path as far as you can go Eventually, you will reach your destination or you won’t be able to go any farther If you can’t go any farther, you will need to retrace your steps You need a way to keep track of what you've already tried

Spring 2010CS Backtracking Never try the exact same path more than once, and you will eventually find a solution path if one exists Problems that are solved by backtracking can be described as a set of choices made by some method Recursion allows us to implement backtracking in a relatively straightforward manner –Each activation frame is used to remember the choice that was made at that particular decision point A program that plays chess may involve some kind of backtracking algorithm

Spring 2010CS Backtracking

Spring 2010CS Recursive Algorithm for Finding Maze Path

Spring 2010CS Maze Implementation

Spring 2010CS Maze Implementation

Spring 2010CS Maze Implementation

Spring 2010CS Chapter Review A recursive method has a standard form To prove that a recursive algorithm is correct, you must –Verify that the base case is recognized and solved correctly –Verify that each recursive case makes progress toward the base case –Verify that if all smaller problems are solved correctly, then the original problem must also be solved correctly The run-time stack uses activation frames to keep track of argument values and return points during recursive method calls

Spring 2010CS Chapter Review Mathematical Sequences and formulas that are defined recursively can be implemented naturally as recursive methods Recursive data structures are data structures that have a component that is the same data structure Towers of Hanoi and counting cells in a blob can both be solved with recursion Backtracking is a technique that enables you to write programs that can be used to explore different alternative paths in a search for a solution