 2002-2010 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.

Slides:



Advertisements
Similar presentations
Starting Out with Java: From Control Structures through Objects
Advertisements

Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: 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.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
 2002 Prentice Hall. All rights reserved. 1 Chapter 6 - Methods Outline Note: Inconsistent with textbook subsection numbering […] 6.14Recursion 6.15 Example.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
FIT FIT1002 Computer Programming Unit 20 Recursion.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
 Pearson Education, Inc. All rights reserved Recursion.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
 2005 Pearson Education, Inc. All rights reserved Recursion.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
C++ Programming Lecture 12 Functions – Part IV
Java How to Program, 10/e Late Objects Version © Copyright by Pearson Education, Inc. All Rights Reserved.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
RECURSION.
Chapter 15 Recursion.
Java Software Structures: John Lewis & Joseph Chase
CS1120: Recursion.
Recursion Data Structures.
Functions Recursion CSCI 230
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without an explicit message). Slides added by L.Lilien are © Leszek T. Lilien. Permision to use for non-commercial purposes slides added by L.Lilien’s will be gladly granted upon a written (e.g., ed) request.

 Prentice Hall. All rights reserved. 2 Recursion Recursive methods - call themselves: –Directly –Indirectly Call others methods which call it Continually breaks problem down to simpler forms –Each method call divides the problem into two pieces: A piece that the method knows how to do A recursive call (a.k.a. recursion step) that solves a smaller problem In other words, each recursive method: –Waits till recursive call made by it finishes –Then finishes itself The method must converge in order to end recursion –Must include a “terminating condition” (see below)

 Prentice Hall. All rights reserved. 3 Recursion Example 1: Factorials Fig | Recursive evaluation of 5! Recursive formula for factorial calculation: n! = n. (n – 1)! Termination conditions: 1! = 1 0! = 1

 Prentice Hall. All rights reserved. 4 import javax.swing.JOptionPane; /** This program demonstrates the recursive factorial method. */ public class Factorial { public static void main(String[] args) { String input; // To hold user input int number; // To hold a number // Get a number from the user. input = JOptionPane.showInputDialog("Enter a " + "nonnegative integer:"); number = Integer.parseInt(input); // Display the factorial of the number. JOptionPane.showMessageDialog(null, number + "! is " + factorial(number)); System.exit(0); } /** The factorial method uses recursion to calculate the factorial of its argument, which is assumed to be a nonnegative n The number to use in the The factorial of n. */ private static int factorial(int n) { if (n == 0) return 1; // Base case else return n * factorial(n - 1); } } // end of class FactorialDemo Sample I/O: The recursion ends when the number is 0 Main calls the factorial method to get the result Factorial calls itself recursively here Must be static since it is called by a static method ( main ). Recall that a static method can only use/call static fields/methods.

 Prentice Hall. All rights reserved. 5 Run Factorial in Eclipse & watch Call Stack in the Debug window 1) Open Debug perspective (using icon on the perspective bar) 2) Set up breakpoints: a)In Line 22: “ JOptionPane.showMessageDialog(…); “ b)In Line 39: “ return n * factorial(n - 1); “ (to do this, right click just to the left of the line number, then select Toggle Breakpoint) 3) Select Run>>Debug 4) Enter input (e.g., 4) into the pop-up window and click “OK” 5) Program progresses to the first breakpoint 6) Look at value of input and number in the Variables window 7) Select the Resume button in the main toolbar. 8) Program progresses to the next breakpoint 9) Look at value of n in the Variables window 10-N) Repeat 7-9 till execution finishes © Leszek T. Lilien

 Prentice Hall. All rights reserved. 6 Final notes for FactorialDemo I bet that you could write a program calculating n! for n = 0, 1, …, 10 without using recursion that is faster and requires less memory (a more efficient program) We use recursion here (in this case less efficient) for educational purposes So that you can learn how to use recursion in these cases when using recursion is the best way to solve a problem © Leszek T. Lilien

 Prentice Hall. All rights reserved. 7 ** READ LATER** Recursion (Cont.) Common Programming Error Infinite recursion (error!) occurs when the programmer either: 1)omits the base case (terminating condition), or 2)writes the recursion step incorrectly so that it does not converge on the base case Infinite recursion will eventually exhaust available memory. This error is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution.

 Prentice Hall. All rights reserved. 8 Recursion Example 2: The Fibonacci Numbers Fibonacci Numbers (a.k.a. Fibonacci Sequence) –Recursion used to calculate F(n): F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) Example – –Set of recursive calls to method Fibonacci [from Fig (ed.1)] return 1return 0 F( 1 )F( 0 )return 1 F( 3 ) F( 2 )F( 1 ) + return + Two terminating conditions. }

 Prentice Hall. All rights reserved. import javax.swing.JOptionPane; /** This program demonstrates the recursive fib method. */ public class FibNumbers { public static void main(String[] args) { String input; // To hold user input int number; // To hold a number // Get a number from the user. input = JOptionPane.showInputDialog("Enter a nonnegative integer:"); number = Integer.parseInt(input); // Display the Fibonacci number for the input. JOptionPane.showMessageDialog(null, "Fibonacci(" + number + ") is " + fib(number)); System.exit(0); } /** The fib method calculates the nth number in the Fibonacci n The nth number to The nth number. */ public static int fib(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return fib(n - 1) + fib(n - 2); } } // end of class FibNumbers 9 Main calls the recursive fib method to get result The recursion ends (“return”) when the number is 0 or 1 Fibonacci calls itself twice, to get the result as the sum of the two previous Fibonacci numbers Must be static since it is called by a static method ( main ). Sample I/O:

 Prentice Hall. All rights reserved. 10 Output example 1 (same as on the previous page): Output example 2: © Leszek T. Lilien

 Prentice Hall. All rights reserved RUN ON YOUR OWN ++ Run FibNumbers in Eclipse & watch Call Stack in the Debug window 1) Open Debug perspective (using icon on the perspective bar) 2) Set up breakpoint in Line 33: “ return fib(n - 1) + fib(n - 2 );“ (to do this, right click just to the left of the line number, then select Toggle Breakpoint) 3) Select Run>>Debug 4) Enter input (e.g., 6) into the pop-up window and click “OK” 5) Program progresses to the first breakpoint 6) Look at value of n in the Variables window 7) Select the Resume button in the main toolbar. 8-N) Repeat 5-7 till execution finishes © Leszek T. Lilien

 Prentice Hall. All rights reserved. 12 Final notes for FibNumbers (analogous as for Factorial) I bet that you could write a program calculating Fibonacci(n) without using recursion that is faster and requires less memory (a more efficient program) We use recursion here (in this case less efficient) for educational purposes So that you can learn how to use recursion in these cases when using recursion is the best way to solve a problem © Leszek T. Lilien

 Prentice Hall. All rights reserved. 13 Recursion vs. Iteration Iteration –Uses repetition structures while, do/while, for, foreach –Continues until counter fails repetition condition Recursion –Uses selection structures if, if/else, switch –Repetition through method calls –Continues until a termination case reached (e.g., fib(0) or fib(1 ) for Fibonacci numbers progam) –Creates duplicates of the variables Can consume a lot of memory and processor speed

 Prentice Hall. All rights reserved. 14 Recursion Example 3: Towers of Hanoi Background: –In a temple in the Far East, priests are attempting to move a stack of 64 disks from one peg to another –The legend: The world will end when the priests complete their task –The initial stack of 64 disks is threaded onto Peg 1 and arranged from bottom to top by decreasing size –The priests are attempting to move the stack from Peg 1 to Peg 3 under the constraints that: Exactly one disk is moved at a time and A larger disk may never be placed above a smaller disk –Peg 2 is available for temporarily holding disks

 Prentice Hall. All rights reserved. 15 Towers of Hanoi – Cont. Write an application to solve the Towers of Hanoi problem. Allow the user to enter the number of disks. Use a recursive Tower method with four parameters: a) the number of disks to be moved, b) the id of the initial peg (on which these disks are initially threaded), c) the id of the final peg (to which this stack of disks is to be moved), d) the id of the temporary peg (to be used as a temporary holding area). The application should display the precise instructions it will take to move the disks from the starting peg to the destination peg. — Example: To move a stack of 3 disks from Peg 1 to Peg 3, the application should display the following series of moves: 1 --> 3 (This notation means “Move the top disk from Peg 1 to Peg 3.”) 1--> > > > > > 3

 Prentice Hall. All rights reserved. 16 Towers of Hanoi – Cont. Developing an algorithm that displays the precise sequence of peg-to-peg disk transfers If we were to approach this problem with conventional methods (e.g. using iteration), we would rapidly find ourselves hopelessly knotted up in managing the disks If we attack the problem with recursion in mind, it immediately becomes quite easy to solve

 Prentice Hall. All rights reserved. 17 Towers of Hanoi – Cont. Design -- Approach: 1) Moving n disks from Peg 1 to Peg 3, using Peg 2 as a temporary peg can be viewed recursively in terms of moving only n – 1 disks: a) Move n – 1 disks from Peg 1 to Peg 2, using Peg 3 as a temporary peg b) Move the last disk (the largest) from Peg 1 to Peg 3 c) Move the n – 1 disks from Peg 2 to Peg 3, using Peg 1 as a temporary peg 2) Terminating condition (base case ) : The process ends when the last task requires moving n = 1 disks This last task is accomplished by simply moving the disk (from the initial to the final peg) without the need for a temporary holding area

 Prentice Hall. All rights reserved. 18 Method main calls the recursive method Hanoi to get result import java.util.Scanner; /** Application solves the Towers of Hanoi problem using recursion. */ public class TowerOfHanoi { static public void main(String[] args) { // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Get the number of disks from the user. System.out.print("Enter the number of disks for Towers of Hanoi: "); int nrDisks = keyboard.nextInt(); // Call the recursive method ToH (Towers of Hanoi) ToH(nrDisks, 1, 3, 2); } // end method main /** The ToH method displays all disk num The number of discs to fromPeg The peg to move toPeg The peg to move tempPeg The temporary peg. */ private static void ToH(int num, int fromPeg, int toPeg, int tempPeg) { if (num > 0) { ToH(num - 1, fromPeg, tempPeg, toPeg); System.out.println("Move a disc from peg " + fromPeg + " to peg " + toPeg); ToH(num - 1, tempPeg, toPeg, fromPeg); } } // end method ToH } // end class TowerOfHanoi Towers of Hanoi Code main calls the recursive ToH method to get the sequence of disk moves. ToH calls itself twice, to get the result s (see pseudocode) RECALL (the previous page): Moving n disks from Peg 1 to Peg 3, using Peg 2 as a temporary peg : a) Move n – 1 disks from Peg 1 to Peg 2, using Peg 3 as a temporary peg b) Move the last disk (the largest) from Peg 1 to Peg 3 c) Move the n – 1 disks from Peg 2 to Peg 3, using Peg 1 as a temporary peg

 Prentice Hall. All rights reserved. 19 Peg A Peg B Peg C Towers of Hanoi – 2 Sample Outputs © Leszek T. Lilien

 Prentice Hall. All rights reserved. 20 Towers of Hanoi – Cont. Note: Don’t try to run the following program for values larger than 12 – you will use a lot of time and display (or printout) space. We’ll learn later how complexity theory explains this © Leszek T. Lilien

 Prentice Hall. All rights reserved. 21 Final notes for Towers of Hanoi I bet that you could not write a program solving Towers of Hanoi without using recursion (even a less efficient one) We use recursion here not just for educational purposes This is an example of a problem when using recursion is the best way to solve a problem © Leszek T. Lilien

 Prentice Hall. All rights reserved. 22 The End