Chapter 18 Recursion "To iterate is human, to recurse divine.", L. Peter Deutsch.

Slides:



Advertisements
Similar presentations
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 13 – Recursion.
Advertisements

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 13 – Recursion.
Recursion - see Recursion. RHS – SOC 2 Recursion We know that: –We can define classes –We can define methods on classes –Mehtods can call other methods.
CHAPTER 13 Recursion. Recursive Solution A recursive solution  solves a problem by solving a smaller instance of the problem. Example  How do we go.
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.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Cis304 RECURSION. Recursion A recursive computation solves a problem by using the solution of the same problem with simpler input For recursion to terminate,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
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.
CHAPTER 17 RECURSION CHAPTER GOALS –To learn about the method of recursion –To understand the relationship between recursion and iteration –To analysis.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
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.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Recursion.
Recursive Methods Noter ch.2. QUIZ What is the result of the method call foo(4) ? 1.Prints 4 2.Prints 1 3.Prints Prints Compiler error:
Recursion James Wright St Peter’s Secondary School 733 Parkhill Road West Peterborough,Ontario K9J-8M4
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eleven: Recursion Slides by Evan Gallagher.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 13 - Recursion.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 13 - Recursion.
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.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
COSC2007 Data Structures II
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
AP Computer Science Instructor Alabama School of Fine Arts
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Lecture 10 Recursion. public class recursionDemo { public static void main(String[] args) { System.out.println("TriCount for n = 5 is... " + triCount(5));
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Thirteen: Recursion.
CHAPTER 17 RECURSION. CHAPTER GOALS To learn about the method of recursion To understand the relationship between recursion and iteration To analysis.
Chapter 18 Recursion. Chapter Goals To learn about the method of recursion To understand the relationship between recursion and iteration To analyze problems.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion Lecture 6 Dr. Musab.
Copyright © 2013 by John Wiley & Sons. All rights reserved. RECURSION CHAPTER Slides by Rick Giles 13.
ICOM 4015: Advanced Programming Lecture 13 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Thirteen:
Mutual Recursion A set of methods calls each other cooperatively and repeatedly Expression is a term, or sum or difference of terms Term is a factor, or.
Recursion Chapter 11. How it works “A recursive computation solves a problem by using the solution of the same problem with simpler inputs” Big Java,
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
Copyright © 2013 by John Wiley & Sons. All rights reserved. RECURSION CHAPTER 11 May 27, 2013 Slides by James Tam Department of Computer Science, University.
Recursion - see Recursion
Recursion Version 1.0.
Chapter 13 Recursion.
Topic 6 Recursion.
Department of Computer Science,
Chapter 5: Control Structures II
Chapter 13 – Recursion.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion For some problems, it’s useful to have a method call itself.
Recursion - see Recursion
Advanced Java Programming
Unit 3 Test: Friday.
Chapter 13 – Recursion Big Java by Cay Horstmann
Chapter 18 Recursion.
Java Programming: Chapter 9: Recursion Second Edition
Presentation transcript:

Chapter 18 Recursion "To iterate is human, to recurse divine.", L. Peter Deutsch

Recursion A recursive computation solves a problem by using the solution of the same problem with simpler values For recursion to terminate, there must be special cases for the simplest inputs.

Recursion Two key requirements for recursion success: ▫Every recursive call must simplify the computation in some way ▫There must be special cases to handle the simplest computations directly

Permutations Design a class that will list all permutations of a string A permutation is a rearrangement of the letters The string "eat" has six permutations "eat" “ate” "eta" “aet" "tea” "tae"

Public Interface of PermutationGenerator public class PermutationGenerator { public PermutationGenerator(String aWord) {... } ArrayList getPermutations() {... } }

File PermutationGeneratorTester.java 01: import java.util.ArrayList; 02: 03: /** 04: This program tests the permutation generator. 05: */ 06: public class PermutationGeneratorTester 07: { 08: public static void main(String[] args) 09: { 10: PermutationGenerator generator 11: = new PermutationGenerator("eat"); 12: ArrayList permutations = generator.getPermutations(); 13: for (String s : permutations) 14: { 15: System.out.println(s); 16: } Continued 17: } 18: } 19:

File PermutationGeneratorTester.java eat eta aet ate tea tae Output

To Generate All Permutations getPermutations: loop through all positions in the word to be permuted For each position, compute the shorter word obtained by removing ith letter: String shorterWord = word.substring(0, i) + word.substring(i + 1); Continued

To Generate All Permutations Generate all permutations that start with 'e', then 'a' then 't' To generate permutations starting with 'e', we need to find all permutations of "at" This is the same problem with simpler inputs. Use recursion

To Generate All Permutations Construct a permutation generator to get permutations of the shorter word PermutationGenerator shorterPermutationGenerator = new PermutationGenerator(shorterWord); ArrayList shorterWordPermutations = shorterPermutationGenerator.getPermutations();

To Generate All Permutations Finally, add the removed letter to front of all permutations of the shorter word Special case: simplest possible string is the empty string; single permutation, itself for (String s : shorterWordPermutations) { result.add(word.charAt(i) + s); }

File PermutationGenerator.java 01: import java.util.ArrayList; 02: 03: /** 04: This class generates permutations of a word. 05: */ 06: public class PermutationGenerator 07: { 08: /** 09: Constructs a permutation generator. aWord the word to permute 11: */ 12: public PermutationGenerator(String aWord) 13: { 14: word = aWord; 15: } 16: Continued

File PermutationGenerator.java 17: /** 18: Gets all permutations of a given word. 19: */ 20: public ArrayList getPermutations() 21: { 22: ArrayList result = new ArrayList (); 23: 24: // The empty string has a single permutation: itself 25: if (word.length() == 0) 26: { 27: result.add(word); 28: return result; 29: } 30: 31: // Loop through all character positions 32: for (int i = 0; i < word.length(); i++) 33: { Continued

File PermutationGenerator.java 34: // Form a simpler word by removing the ith character 35: String shorterWord = word.substring(0, i) 36: + word.substring(i + 1); 37: 38: // Generate all permutations of the simpler word 39: PermutationGenerator shorterPermutationGenerator 40: = new PermutationGenerator(shorterWord); 41: ArrayList shorterWordPermutations 42: = shorterPermutationGenerator.getPermutations(); 43: 44: // Add the removed character to the front of 45: // each permutation of the simpler word, 46: for (String s : shorterWordPermutations) 47: { 48: result.add(word.charAt(i) + s); 49: } 50: } Continued

File PermutationGenerator.java 51: // Return all permutations 52: return result; 53: } 54: 55: private String word; 56: }

Self Check 3.What are all permutations of the four-letter word beat? 4.Our recursion for the permutation generator stops at the empty string. What simple modification would make the recursion stop at strings of length 0 or 1?

Answers 3.They are b followed by the six permutations of eat, e followed by the six permutations of bat, a followed by the six permutations of bet, and t followed by the six permutations of bea. 4.Simply change because a word with a single letter is also its sole permutation. if (word.length() == 0) to if (word.length() <= 1)

Thinking Recursively Problem: test whether a sentence is a palindrome Palindrome: a string that is equal to itself when you reverse all characters ▫A man, a plan, a canal–Panama! ▫Go hang a salami, I'm a lasagna hog ▫Madam, I'm Adam

Implement isPalindrome Method public class Sentence { /** Constructs a aText a string containing all characters of the sentence */ public Sentence(String aText) { text = aText; } Continued

Implement isPalindrome Method /** Tests whether this sentence is a true if this sentence is a palindrome, false otherwise */ public boolean isPalindrome() {... } private String text; }

Thinking Recursively: Step-by-Step 1.Consider various ways to simplify inputs Here are several possibilities: ▫Remove the first character ▫Remove the last character ▫Remove both the first and last characters ▫Remove a character from the middle

Thinking Recursively: Step-by-Step 2.Combine solutions with simpler inputs into a solution of the original problem ▫Most promising simplification: remove first and last characters "adam, I'm Ada", is a palindrome too! ▫Thus, a word is a palindrome if  The first and last letters match, and  Word obtained by removing the first and last letters is a palindrome Continued

Thinking Recursively: Step-by-Step 2.Combine solutions with simpler inputs into a solution of the original problem ▫What if first or last character is not a letter? Ignore it  If the first and last characters are letters, check whether they match; if so, remove both and test shorter string  If last character isn't a letter, remove it and test shorter string  If first character isn't a letter, remove it and test shorter string

Thinking Recursively: Step-by-Step 3.Find solutions to the simplest inputs ▫Strings with two characters  No special case required; step two still applies ▫Strings with a single character  They are palindromes ▫The empty string  It is a palindrome

Thinking Recursively: Step-by-Step 4.Implement the solution by combining the simple cases and the reduction step public boolean isPalindrome() { int length = text.length(); // Separate case for shortest strings. if (length <= 1) return true; // Get first and last characters, converted to lowercase. char first = Character.toLowerCase(text.charAt(0)); char last = Character.toLowerCase(text.charAt(length - 1)); Continued

Thinking Recursively: Step-by-Step if (Character.isLetter(first) && Character.isLetter(last)) { // Both are letters. if (first == last) { // Remove both first and last character. Sentence shorter = new Sentence(text.substring(1, length - 1)); return shorter.isPalindrome(); } else return false; } Continued

Thinking Recursively: Step-by-Step else if (!Character.isLetter(last)) { // Remove last character. Sentence shorter = new Sentence(text.substring(0, length - 1)); return shorter.isPalindrome(); } else { // Remove first character. Sentence shorter = new Sentence(text.substring(1)); return shorter.isPalindrome(); }

Recursive Helper Methods Sometimes it is easier to find a recursive solution if you make a slight change to the original problem Consider the palindrome test of previous slide It is a bit inefficient to construct new Sentence objects in every step Continued

Recursive Helper Methods Rather than testing whether the sentence is a palindrome, check whether a substring is a palindrome: /** Tests whether a substring of the sentence is a start the index of the first character of the end the index of the last character of the true if the substring is a palindrome */ public boolean isPalindrome(int start, int end)

Recursive Helper Methods Then, simply call the helper method with positions that test the entire string: public boolean isPalindrome() { return isPalindrome(0, text.length() - 1); }

Recursive Helper Methods: isPalindrome public boolean isPalindrome(int start, int end) { // Separate case for substrings of length 0 and 1. if (start >= end) return true; // Get first and last characters, converted to lowercase. char first = Character.toLowerCase(text.charAt(start)); char last = Character.toLowerCase(text.charAt(end)); Continued

Recursive Helper Methods: isPalindrome if (Character.isLetter(first) && Character.isLetter(last)) { if (first == last) { // Test substring that doesn’t contain the matching letters. return isPalindrome(start + 1, end - 1); } else return false; } else if (!Character.isLetter(last)) { // Test substring that doesn’t contain the last character. return isPalindrome(start, end - 1); } Continued

Recursive Helper Methods: isPalindrome else { // Test substring that doesn’t contain the first character. return isPalindrome(start + 1, end); }

The Efficiency of Recursion Recursive methods can be less efficient than their iterative counterparts Consider computing Fibonacci numbers fib(n) = fib(n-1) + fib(n-2)

Call Tree for Computing fib(6) Figure 2: Call Tree of the Recursive fib method See Jeliot animation of fib()

The Efficiency of Recursion Method takes so long because it computes the same values over and over The computation of fib(6) calls fib(3) three times Imitate the pencil-and-paper process to avoid computing the values more than once

The Efficiency of Recursion Occasionally, a recursive solution runs much slower than its iterative counterpart In most cases, the recursive solution is only slightly slower The iterative isPalindrome performs only slightly better than recursive solution ▫Each recursive method call takes a certain amount of processor time

The Efficiency of Recursion Smart compilers can avoid recursive method calls if they follow simple patterns Most compilers don't do that In many cases, a recursive solution is easier to understand and implement correctly than an iterative solution "To iterate is human, to recurse divine.", L. Peter Deutsch

Using Mutual Recursions Problem: to compute the value of arithmetic expressions such as Computing expression is complicated ▫* and / bind more strongly than + and - ▫parentheses can be used to group subexpressions * 5 (3 + 4) * (2 - (3 - (4 - 5)))

Syntax Diagram for Evaluating an Expression Figure 5: Syntax Diagrams for Evaluating an Expression

Using Mutual Recursions An expression can broken down into a sequence of terms, separated by + or - Each term is broken down into a sequence of factors, separated by * or / Each factor is either a parenthesized expression or a number The syntax trees represent which operations should be carried out first

Syntax Tree for Two Expressions Figure 6: Syntax Trees for Two Expressions

Mutually Recursive Methods In a mutual recursion, a set of cooperating methods calls each other repeatedly To compute the value of an expression, implement 3 methods that call each other recursively ▫ getExpressionValue ▫ getTermValue ▫ getFactorValue

Using Mutual Recursions To see the mutual recursion clearly, trace through the expression (3+4)*5 : getExpressionValue calls getTermValue ▫ getTermValue calls getFactorValue  getFactorValue consumes the ( input  getFactorValue calls getExpressionValue  getExpressionValue returns eventually with the value of 7, having consumed This is the recursive call.  getFactorValue consumes the ) input  getFactorValue returns 7 Continued

Using Mutual Recursions ▫ getTermValue consumes the inputs * and 5 and returns 35 getExpressionValue returns 35