slides created by Marty Stepp and Alyssa Harding

Slides:



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

Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursion … just in case you didn’t love loops enough …
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
1Recursion. 2 Outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Nested for loops.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
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
Introduction to Recursion
Building Java Programs
MSc/ICY Software Workshop , Semester 2
Recursion DRILL: Please take out your notes on Recursion
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
More on Recursion.
Chapter 5: Control Structures II
CS1371 Introduction to Computing for Engineers
Recursion
Java Software Structures: John Lewis & Joseph Chase
Building Java Programs
Building Java Programs
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Applied Algorithms (Lecture 17) Recursion Fall-23
slides adapted from Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Building Java Programs
CSE 143 Lecture 11 Recursive Programming reading:
Building Java Programs
Recursion.
Building Java Programs
Recursion based on slides by Alyssa Harding
CS302 - Data Structures using C++
Recursion Based on slides by Alyssa Harding
Lecture 14: Strings and Recursion AP Computer Science Principles
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Recursion Taken from notes by Dr. Neil Moore
Java Programming: Chapter 9: Recursion Second Edition
Building Java Programs
CSE 143 Lecture 13 Recursive Programming reading:
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 13 Recursive Programming reading:
Building Java Programs
Building Java Programs
slides created by Marty Stepp
Building Java Programs
Recursive Thinking.
Lecture 6 - Recursion.
Presentation transcript:

slides created by Marty Stepp and Alyssa Harding CSE 143 Lecture 10 Recursion slides created by Marty Stepp and Alyssa Harding http://www.cs.washington.edu/143/

Recursion Iteration: a programming technique in which you describe actions to be repeated using a loop Recursion: a programming technique in which you describe actions to be repeated using a method that calls itself Both approaches can be used to solve the same problems Some problems are easier solved iteratively Some problems are easier solved recursively Sometimes the recursive solution is a LOT simpler than the iterative solution!

Exercise How many students total are directly behind you in your "column" of the classroom? You have poor vision, so you can see only the people right next to you. So you can't just look back and count. But you are allowed to ask questions of the person next to you. How can we solve this problem? (recursively )

The idea Recursion is all about breaking a big problem into smaller occurrences of that same problem. Each person can solve a small part of the problem. What is a small version of the problem that would be easy to answer? What information from a neighbor might help me?

Recursive algorithm Number of people behind me: If there is someone behind me, ask him/her how many people are behind him/her. When they respond with a value N, then I will answer N + 1. If there is nobody behind me, I will answer 0.

Recursion and cases Every recursive algorithm involves at least 2 cases: base case: A simple occurrence that can be answered directly. recursive case: A more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem. Some recursive algorithms have more than one base or recursive case, but all have at least one of each. A crucial part of recursive programming is identifying these cases.

Example: stairs You want to walk down a flight of stairs. Iterative approach: “Let me count the number of stairs there are, and then take that that many steps!” 1 2 3

Example: stairs You want to walk down a flight of stairs. Recursive approach: “If I’m at the bottom, I stop. Otherwise, I take a step down and repeat.” step and repeat… step and repeat… step and repeat… stop!

Recursion in Java Consider the following method to print a line of * characters: // Prints a line containing the given number of stars. // Precondition: n >= 0 public static void printStars(int n) { for (int i = 0; i < n; i++) { System.out.print("*"); } System.out.println(); // end the line of output Write a recursive version of this method (that calls itself). Solve the problem without using any loops. Hint: Your solution should print just one star at a time.

A basic case What are the cases to consider? What is a very easy number of stars to print without a loop? public static void printStars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); } else { ... }

Handling more cases Handling additional cases, with no loops (in a bad way): public static void printStars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); } else if (n == 2) { System.out.print("*"); } else if (n == 3) { } else if (n == 4) { } else ... }

Handling more cases 2 Taking advantage of the repeated pattern (somewhat better): public static void printStars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); } else if (n == 2) { System.out.print("*"); printStars(1); // prints "*" } else if (n == 3) { printStars(2); // prints "**" } else if (n == 4) { printStars(3); // prints "***" } else ... }

Using recursion properly Condensing the recursive cases into a single case: public static void printStars(int n) { if (n == 1) { // base case; just print one star System.out.println("*"); } else { // recursive case; print one more star System.out.print("*"); printStars(n - 1); }

"Recursion Zen" The real, even simpler, base case is an n of 0, not 1: public static void printStars(int n) { if (n == 0) { // base case; just end the line of output System.out.println(); } else { // recursive case; print one more star System.out.print("*"); printStars(n - 1); } Recursion Zen: The art of properly identifying the best set of cases for a recursive algorithm and expressing them elegantly. (A CSE 143 informal term)

Recursive tracing Consider the following recursive method: public static int mystery(int n) { if (n < 10) { return n; } else { int a = n / 10; int b = n % 10; return mystery(a + b); } What is the result of the following call? mystery(648)

A recursive trace mystery(648): int a = 648 / 10; // 64 int b = 648 % 10; // 8 return mystery(a + b); // mystery(72) mystery(72): int a = 72 / 10; // 7 int b = 72 % 10; // 2 return mystery(a + b); // mystery(9) mystery(9): return 9;

Recursive tracing 2 Consider the following recursive method: public static int mystery(int n) { if (n < 10) { return (10 * n) + n; } else { int a = mystery(n / 10); int b = mystery(n % 10); return (100 * a) + b; } What is the result of the following call? mystery(348)

A recursive trace 2 mystery(348) int a = mystery(34); return (10 * 3) + 3; // 33 int b = mystery(4); return (10 * 4) + 4; // 44 return (100 * 33) + 44; // 3344 int b = mystery(8); return (10 * 8) + 8; // 88 return (100 * 3344) + 88; // 334488 What is this method really doing?

Example: reverse Now we’ll look at a problem that’s hard to solve iteratively, but easier with recursion Given a Scanner as input, print the lines in reverse How would you solve this iteratively? Loop while there are more lines Requires additional storage, like a List or a Stack

Example: reverse Writing reverse recursively: What is the base case? public static void reverse(Scanner input) { // base case: no more lines if ( !input.hasNextLine() ) { // do nothing } else { … } This is a good base case, but we don’t need to do anything in this case

Example: reverse Writing reverse recursively: What is the base case? public static void reverse(Scanner input) { // base case: no more lines // recursive case if ( input.hasNextLine() ) { … } It’s better style not to have an empty if statement.

Example: reverse Writing reverse recursively: What is the recursive case’s work? public static void reverse(Scanner input) { // base case: no more lines // recursive case if ( input.hasNextLine() ) { String line = input.nextLine(); // reverse the rest of the input System.out.println(line); } We made a little progress, how do we do the rest?

Example: reverse Writing reverse recursively: What is the recursive case’s work? public static void reverse(Scanner input) { // base case: no more lines // recursive case if ( input.hasNextLine() ) { String line = input.nextLine(); reverse(input); System.out.println(line); } We recursively call the method with the easier problem!

Example: reverse Input: student that loves recursion Output: recursion public static void main (String[] args) { Scanner input = new Scanner(new File("recursion.txt")); reverse(input); } public static void reverse(Scanner input) { if ( input.hasNextLine() ) { String line = input.nextLine(); // student reverse(input); System.out.println(line); } public static void reverse(Scanner input) { if ( input.hasNextLine() ) { String line = input.nextLine(); // that reverse(input); System.out.println(line); } public static void reverse(Scanner input) { if ( input.hasNextLine() ) { String line = input.nextLine(); // loves reverse(input); System.out.println(line); } public static void reverse(Scanner input) { if ( input.hasNextLine() ) { String line = input.nextLine(); // recursion reverse(input); System.out.println(line); } public static void reverse(Scanner input) { if ( input.hasNextLine() ) { // false! String line = input.nextLine(); reverse(input); System.out.println(line); } Input: student that loves recursion Output: recursion loves that student