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.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
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 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
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.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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.
Recursion A function that calls itself. Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recursion -- Introduction
CprE 185: Intro to Problem Solving (using C)
Recursion DRILL: Please take out your notes on Recursion
Abdulmotaleb El Saddik University of Ottawa
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Recursion Chapter 11.
Chapter 8: Recursion Java Software Solutions
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Unit 3 Test: Friday.
Recursion Chapter 11.
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
At the end of this session, learner will be able to:
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Java Software Solutions Foundations of Program Design Sixth Edition
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

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 is one that describes something in terms of simpler versions of itself.

3 Recursive Definitions Ordering property: "Simpler" means that we can order the versions by some criterion. Simplest version: Minimal / base case of the recursive object/procedure

4 Recursive Definitions - Examples Consider the following list of numbers: 24, 88, 40, 37 Such a list can be defined recursively: A LIST is a:number or a:number comma LIST That is, a LIST can be a number, or a number followed by a comma followed by a LIST The concept of a LIST is used to define itself

5 FIGURE 10.1 Tracing the recursive definition of a list

6 Recursive Definitions - Examples Consider the function f(n) = a n. Initial condition: f(0) = 1 Recursive rule: f(n) = a * f(n-1)

7 Recursive Definitions - Examples Consider the function f(n) = n! n! = 1 * 2 * 3 * … * (n-2) * (n-1) * n 0! = 1 by definition Initial condition: f(0) = 1 Recursive rule: f(n) = n * f(n-1)

8 Recursive Methods in Programming 1 A recursive method must have at least one base, or stopping, case Each successive call to itself must be a "smaller version of itself" so that a base case is eventually reached.

9 Recursive Methods in Programming - Example boolean BinarySearch(A, Start, End, P) find M = A[middle] If (M = P) return true <<<<< the base else if (End - Start = 0) return false <<<<< the base else if (P > M) BinarySearch(A, middle+1, End, P) else BinarySearch(A, Start, middle, P) Here the terminating condition is End - Start = 0 or M = P

10 Recursive Methods in Programming 2 A method in Java can invoke itself; if set up that way, it is called a recursive method The code of a recursive method must be structured to handle both the base case and the recursive case Each call sets up a new execution environment, with new parameters and new local variables As always, when the method completes, control returns to the method that invoked it

11 How to design a recursive method 1 A recursive method must have: one or more base cases one or more recursive cases which make the problem smaller General Idea: determine your base cases first (What can you solve?) determine how to simplify the problem to get to a base case

12 How to design a recursive method 2 The base case identifies a condition of the parameters in which the result can be immediately computed. The recursive case identifies the situation, in which the result must be expressed in terms of the solution to a simpler problem.

13 How to design a recursive method 3 To distinguish between the base case and the recursive case, a recursive method always can be expressed in the form of an if...else statement as follows: if (base case) // compute and return the result directly else // re-call the method to obtain the solution to a // simpler problem, and compute the result from // that solution

14 Infinite Recursion All recursive definitions must have a non- recursive part. If they don't, there is no way to terminate the recursive path A definition without a non-recursive part causes infinite recursion. This problem is similar to an infinite loop -- with the definition itself causing the infinite "looping"

15 How to avoid infinite recursion Take special care to ensure that the base case is properly implemented. Make sure that each subsequent recursive call represents progress toward the base case

16 Recursive Programming – Example 2 Consider the problem of computing the sum of all the numbers between 1 and N, inclusive If N is 5, the sum is This problem can be expressed recursively as: The sum of 1 to N is N plus the sum of 1 to N-1

17 FIGURE 10.2 The sum of the numbers 1 through N, defined recursively

18 Recursive Programming – the code public int sum (int num) { int result; if (num == 1) result = 1; else result = num + sum(num-1); return result; }

19 FIGURE 10.3 Recursive calls to the sum method

20 Indirect Recursion A method invoking itself is considered to be direct recursion A method could invoke another method, which invokes another, etc., until eventually the original method is invoked again For example, method m1 could invoke m2, which invokes m3, which invokes m1 again This is called indirect recursion It is often more difficult to trace and debug

21 FIGURE 10.4 Indirect recursion

22 Verifying Recursive Methods Trace the recursive method – difficult Draw diagram of the recursive calls – can be done for small examples only Use a method based on algorithm specification

23 Verifying Recursive Methods Develop a precise specification of what the algorithm has to do Check the base case – make sure the result meets the specification Check each recursive case and make sure it is for a smaller version of the problem, so that eventually the base case will be executed Assuming that the recursive call returns correct results, check to see if the recursive case meets the specifications

24 Recursion vs. Iteration Just because we can use recursion to solve a problem, doesn't mean we should For instance, we usually would not use recursion to solve the sum of 1 to N The iterative version is easier to understand (in fact there is a formula that is superior to both recursion and iteration in this case) You must be able to determine when recursion is the correct technique to use

25 Recursion vs. Iteration Every recursive solution has a corresponding iterative solution For example, the sum of the numbers between 1 and N can be calculated with a loop Recursion has the overhead of multiple method invocations However, for some problems recursive solutions are often more simple and elegant than iterative solutions

26 When to Use Recursion Recursion is preferred when each next call substantially reduces the input size Example: Recursive sum calls reduce the input by one: result = num + sum(num-1); Recursive binary search reduces the input by a factor of 2 if (P > M) BinarySearch (A, middle+1, End, P) else BinarySearch (A, Start, middle, P)

27 Example: Computing a n Compute 2 30 with 7 multiplications only (((2 2 *2 ) 2 * 2) 2 *2) 2 Iteration will require 29 multiplications Recursive solution: int pow(a,N) if N = 1 return a // terminating condition else if N is even return pow(a, N/2) * pow(a,N/2) else return a*pow(a,N/2)*pow(a,N/2)

28 Analysis of recursive methods Solving recurrence relations public int sum (int num) { if (num == 1) return 1; else return num + sum(num-1); } Recurrence relation: T(N) = T(N-1) + 1 The time to compute the sum of N integers is equal to the time to compute the sum of N-1 integers plus a constant

29 Solving recurrence relations T(N) = T(N-1) + 1 Telescoping: T(N) = T(N-1) + 1 T(N-1) = T(N-2) +1 ….. T(2) = T(1) +1 Add the left and the right sides T(N) + T(N-1) + …. T(2) = T(N-1) + T(N-2) +…T(1) …. + 1 After crossing the equal terms T(N) = T(1) … + 1 = N Therefore the complexity is O(N), same as with iteration