Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of 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.
Problem Solving 4 Algorithms, Problem Solving and Recursion ICS-201 Introduction to Computing II Semester 071.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 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.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1Recursion. 2 Outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
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.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
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.
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
Chapter 17 Recursion Data Structures with Java © Rick Mercer Data Structures with Java © Rick Mercer.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
© 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.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Chapter Topics Chapter 16 discusses the following main topics:
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 9: introduction to recursion reading: 12.1
Data Structures with Java © Rick Mercer
Chapter 15 Recursion.
CprE 185: Intro to Problem Solving (using C)
Building Java Programs
Recursion DRILL: Please take out your notes on Recursion
Abdulmotaleb El Saddik University of Ottawa
Chapter 15 Recursion.
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.
slides adapted from Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Chapter 8: Recursion Java Software Solutions
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
slides adapted from Marty Stepp and Hélène Martin
slides created by Marty Stepp
Building Java Programs
Java Software Solutions Foundations of Program Design Sixth Edition
Recursion.
Presentation transcript:

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion

Copyright 2006 by Pearson Education 2 Chapter outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion tracing recursive methods greatest common divisor (GCD) recursive graphics (optional)

Copyright 2006 by Pearson Education 3 Recursive thinking and algorithms reading:

Copyright 2006 by Pearson Education 4 Recursive thinking recursion: a programming technique in which a method can call itself to solve a problem recursive definition: one which uses the word or concept being defined in the definition itself In some situations, a recursive definition can be an appropriate or elegant way to express a concept Before applying recursion to programming, it is best to practice thinking recursively

Copyright 2006 by Pearson Education 5 Recursive definitions Consider the following list of numbers: 24 -> 88 -> 40 -> 37 / A list can be defined recursively EitherLIST = null / orLIST = element -> LIST That is, a LIST is defined to be either empty (null), or an element followed by a LIST (The concept of a LIST is used to define itself) How would we confirm that null is a LIST? That one element is a LIST? That three elements are a LIST?

Copyright 2006 by Pearson Education 6 More recursive definitions An arithmetic expression is defined as: a numeric constant or variable identifier an arithmetic expression enclosed in parentheses 2 arithmetic expressions with a binary operator like + - / * % Note: The term arithmetic expression is defined by using the term arithmetic expression! (not the first bullet)

Copyright 2006 by Pearson Education 7 Recursive algorithms recursive algorithm: description for a way to solve a problem, that refers to itself Show everything in a folder and all it subfolders: 1. show everything in top folder 2. show everything in each subfolder in the same manner Look up a word in a dictionary: 1. look up the word using the alphabetical ordering. 2. if all words in the definition are known to you, stop. 3. else, for each unknown word in the definition, look up that word

Copyright 2006 by Pearson Education 8 A recursive algorithm Consider the task of finding out what place you are in a long line of people. If you cannot see the front of the line, you could ask the person in front of you. To answer your question, this person could ask the person in front of him/her, and so on.

Copyright 2006 by Pearson Education 9 A recursive algorithm Once the front person answers their place in line (first), this information is handed back, one person at a time, until it reaches you. This is the essence of recursive algorithms; many invocations of the same method each solve a small part of a large problem.

Copyright 2006 by Pearson Education 10 Recursive programming A method in Java can call itself; if written that way, it is called a recursive method A recursive method solves some problem. The code of a recursive method should be written to handle the problem in one of two ways: base case: a simple case of the problem that can be answered directly; does not use recursion. recursive case: a more complicated case of the problem, that isn't easy to answer directly, but can be expressed elegantly with recursion; makes a recursive call to help compute the overall answer

Copyright 2006 by Pearson Education 11 Factorial example The factorial for any positive integer N, written N!, is defined to be the product of all integers between 1 and N inclusive // not recursive public static long factorial(int n) { long product = 1; for (int i = 1; i <= n; i++) { product *= i; } return product; }

Copyright 2006 by Pearson Education 12 Recursive factorial factorial can also be defined recursively: A factorial is defined in terms of another factorial until the basic case of 0! is reached // recursive public static long factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); }

Copyright 2006 by Pearson Education 13 Recursion vs. iteration every recursive solution has a corresponding iterative solution For example, 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 you must be able to determine when recursion is appropriate

Copyright 2006 by Pearson Education 14 Recursive functions reading: 12.3

Copyright 2006 by Pearson Education 15 Recursive power example Write method pow that takes integers x and y as parameters and returns x y. x y = x * x * x *... * x (y times, in total) An iterative solution: // not recursive public static int pow(int x, int y) { int product = 1; for (int i = 0; i < y; i++) { product = product * x; } return product; }

Copyright 2006 by Pearson Education 16 Recursive power function Another way to define the power function: pow(x, 0) = 1 pow(x, y) = x * pow(x, y-1), y > 0 // recursive public static int pow(int x, int y) { if (y == 0) { return 1; } else { return x * pow(x, y - 1); }

Copyright 2006 by Pearson Education 17 How recursion works each call sets up a new instance of all the parameters and the local variables as always, when the method completes, control returns to the method that invoked it (which might be another invocation of the same method) pow(4, 3) = 4 * pow(4, 2) = 4 * 4 * pow(4, 1) = 4 * 4 * 4 * pow(4, 0) = 4 * 4 * 4 * 1 = 64

Copyright 2006 by Pearson Education 18 Infinite recursion a definition with a missing or badly written base case causes infinite recursion, similar to an infinite loop avoided by making sure that the recursive call gets closer to the solution (moving toward the base case) public static int pow(int x, int y) { return x * pow(x, y - 1); // Oops! Forgot base case } pow(4, 3) = 4 * pow(4, 2) = 4 * 4 * pow(4, 1) = 4 * 4 * 4 * pow(4, 0) = 4 * 4 * 4 * 4 * pow(4, -1) = 4 * 4 * 4 * 4 * 4 * pow(4, -2) =... crashes: Stack Overflow Error!

Copyright 2006 by Pearson Education 19 Activation records activation record: memory that Java allocates to store information about each running method return point ("RP"), argument values, local variable values Java stacks up the records as methods are called; a method's activation record exists until it returns drawing the act. records helps us trace the behavior of a recursive method _ | x = [ 4 ] y = [ 0 ] | pow(4, 0) | RP = [pow(4,1)] | | x = [ 4 ] y = [ 1 ] | pow(4, 1) | RP = [pow(4,2)] | | x = [ 4 ] y = [ 2 ] | pow(4, 2) | RP = [pow(4,3)] | | x = [ 4 ] y = [ 3 ] | pow(4, 3) | RP = [main] | | | main

Copyright 2006 by Pearson Education 20 Tracing recursive methods Consider the following method: public static int mystery1(int x, int y) { if (x < y) { return x; } else { return mystery1(x - y, y); } For each call below, indicate what value is returned: mystery1(6, 13)____________ mystery1(14, 10)____________ mystery1(37, 10)____________ mystery1(8, 2)____________ mystery1(50, 7)____________

Copyright 2006 by Pearson Education 21 Tracing recursive methods public static void mystery2(int n) { if (n <= 1) { System.out.print(n); } else { mystery2(n / 2); System.out.print(", " + n); } For each call below, indicate what output is printed: mystery2(1)____________ mystery2(2)____________ mystery2(3)____________ mystery2(4)____________ mystery2(16)____________ mystery2(30)____________ mystery2(100)____________

Copyright 2006 by Pearson Education 22 Tracing recursive methods public static int mystery3(int n) { if (n < 0) { return -mystery3(-n); } else if (n < 10) { return n; } else { return mystery3(n/10 + n % 10); } For each call below, indicate what value is returned: mystery3(6)____________ mystery3(17)____________ mystery3(259)____________ mystery3(977)____________ mystery3(-479)____________

Copyright 2006 by Pearson Education 23 Tracing recursive methods public static void mystery4(String s) { if (s.length() > 0) { System.out.print(s.charAt(0)); if (s.length() % 2 == 0) { mystery4(s.substring(0, s.length() - 1)); } else { mystery4(s.substring(1, s.length())); } System.out.print(s.charAt(s.length() - 1)); } For each call below, indicate what output is printed: mystery4("") ____________ mystery4("a") ____________ mystery4("ab") ____________ mystery4("bc") ____________ mystery4("abcd") ____________

Copyright 2006 by Pearson Education 24 Recursive numeric problems Problem: Given a decimal integer n and a base b, print n in base b. (Hint: consider the / and % operators to divide n.) Problem: Given integers a and b where a >= b, find their greatest common divisor ("GCD"), which is the largest number that is a factor of both a and b. Use Euclid's formula, which states that: GCD(a, b) = GCD(b, a MOD b) GCD(a, 0) = a (Hint: What should the base case be?)

Copyright 2006 by Pearson Education 25 Recursive printing problem Problem: Write a method starString that takes an integer n as an argument and returns a string of stars (asterisks) 2 n long (i.e., 2 to the nth power). For example: starString(0) should return "*" (because 2^0 == 1) starString(1) should return "**" (because 2^1 == 2) starString(2) should return "****" (because 2^2 == 4) starString(3) should return "********" (because 2^3 == 8) starString(4) should return "****************" (2^4 == 16)

Copyright 2006 by Pearson Education 26 Recursive string problems Problem: Write a recursive method isPalindrome that takes a string and returns whether the string is the same forwards as backwards. (Hint: examine the end letters.) Problem: Write a recursive method areAnagrams that takes two strings w1 and w2 and returns whether they are anagrams of each other; that is, whether the letters of w1 can be rearranged to form the word w2.

Copyright 2006 by Pearson Education 27 Recursion can perform badly The Fibonacci numbers are a sequence of numbers F 0, F 1,... F n such that: F 0 = F 1 = 1 F i = F i-1 + F i-2 for any i > 1 Problem: Write a method fib that, when given an integer i, computes the i th Fibonacci number. Why might a recursive solution to this problem be a bad idea? (Let's write it...) Can we fix it? If so, how?

Copyright 2006 by Pearson Education 28 Revisiting Fibonacci... recursive Fibonacci was expensive because it made many, many recursive calls fibonacci(n) recomputed fibonacci(n ) many times in finding its answer! this is a common case of "overlapping subproblems" or "divide poorly and reconquer", where the subtasks handled by the recursion are redundant with each other and get recomputed

Copyright 2006 by Pearson Education 29 Recursive graphics reading: 12.4

Copyright 2006 by Pearson Education 30 Fractal images fractal: A mathematically generated, self-similar image. Created by B. Mandelbrot in 1975 Many can be drawn elegantly using recursive algorithms

Copyright 2006 by Pearson Education 31 A fractal can be drawn at many different levels. Each level is another layer of self-similarity. The larger figure is decomposed into smaller occurrences of the same figure. The smaller figures can themselves be decomposed, and so on. Let's write a program to draw the fractal below, an image called the Sierpinski Triangle. Fractal levels

Copyright 2006 by Pearson Education 32 Fractal code We can write a recursive method to draw the triangle figure at a certain level. public static void drawFigure(int level, Graphics g) {... } The recursive aspect is that drawFigure for a given level should call itself for other levels as part of its work. But these smaller levels appear at different positions, so we should require the triangle's 3 corner points as parameters. public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) {... }

Copyright 2006 by Pearson Education 33 The base case As usual, we begin with the base case. The easiest figure to draw is a triangle at level 1. We can use the fillPolygon method of Graphics g to do this. public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) { if (level == 1) { // base case: simple triangle Polygon p = new Polygon(); p.addPoint(p1.x, p1.y); p.addPoint(p2.x, p2.y); p.addPoint(p3.x, p3.y); g.fillPolygon(p); } else { // recursive case, split into 3 triangles... }

Copyright 2006 by Pearson Education 34 Thinking recursively A key observation: The end points needed to draw the smaller triangles (p4, p5, p6) are the midpoints between the larger triangle's endpoints (p1, p2, p3). We can write a method to compute the midpoint between two Point objects. // returns the point halfway between p1 and p2 public static Point midpoint(Point p1, Point p2) { return new Point( (p1.x + p2.x) / 2, (p1.y + p2.y) / 2); }

Copyright 2006 by Pearson Education 35 Complete solution method public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) { if (level == 1) { // base case: simple triangle Polygon p = new Polygon(); p.addPoint(p1.x, p1.y); p.addPoint(p2.x, p2.y); p.addPoint(p3.x, p3.y); g.fillPolygon(p); } else { // recursive case, split into 3 triangles Point p4 = midpoint(p1, p2); Point p5 = midpoint(p2, p3); Point p6 = midpoint(p1, p3); // recurse on 3 triangular areas drawFigure(level - 1, g, p1, p4, p6); drawFigure(level - 1, g, p4, p2, p5); drawFigure(level - 1, g, p6, p5, p3); }