Lecture 9: introduction to recursion reading: 12.1

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

The Singleton Pattern II Recursive Linked Structures.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursion … just in case you didn’t love loops enough …
Building Java Programs
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
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.
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 ,
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
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.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
1 CSE 142 Final Exam Review Problems. 2 Question Types expressions array mystery inheritance mystery file processing array programming Critters classes.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Recursion.
CS314 – Section 5 Recitation 9
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.
Building Java Programs
MSc/ICY Software Workshop , Semester 2
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.
Recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
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.
Recursion Copyright (c) Pearson All rights reserved.
Building Java Programs
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
Building Java Programs
The Basics of Recursion
slides created by Marty Stepp and Alyssa Harding
Recursion based on slides by Alyssa Harding
CS302 - Data Structures using C++
Topic 6 loops, figures, constants
Building Java Programs
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.
Java Programming: Chapter 9: Recursion Second Edition
Building Java Programs
Building Java Programs
This Lecture Substitution model
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
Chapter 18 Recursion.
Building Java Programs
Building Java Programs
slides created by Marty Stepp
Building Java Programs
Recursive Thinking.
Presentation transcript:

Lecture 9: introduction to recursion reading: 12.1 CSE 143 Lecture 9: introduction to recursion reading: 12.1

Exercise (To a student in the front row) 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 recursion: The definition of an operation in terms of itself. Solving a problem using recursion depends on solving smaller occurrences of the same problem. recursive programming: Writing methods that call themselves to solve problems recursively. An equally powerful substitute for iteration (loops) Particularly well-suited to solving certain types of problems

Linked Lists are Self-Similar a linked list is: null a node whose next field references a list recursive data structure: a data structure partially composed of smaller or simpler instances of the same data structure

Why learn recursion? "Cultural experience" – think differently about problems Solves some problems more naturally than iteration Can lead to elegant, simplistic, short code (when used well) Many programming languages ("functional" languages such as Scheme, ML, and Haskell) use recursion exclusively (no loops) A key component of many of our assignments in CSE 143

Getting down stairs Need to know two things: Most code will look like: Getting down one stair Recognizing the bottom Most code will look like: if (simplest case) { compute and return solution } else { divide into similar subproblem(s) solve each subproblem recursively assemble the overall solution }

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.

Another recursive task How can we remove exactly half of the M&M's in a large bowl, without dumping them all out or being able to count them? What if multiple people help out with solving the problem? Can each person do a small part of the work? What is a number of M&M's that it is easy to double, even if you can't count? (What is a "base case"?)

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)

Exercise Write a recursive method reverseLines that accepts a file Scanner and prints the lines of the file in reverse order. Example input file: Expected console output: I have eaten the icebox the plums that were in that were in the plums the icebox I have eaten What are the cases to consider? How can we solve a small part of the problem at a time? What is a file that is very easy to reverse?

Reversal pseudocode Reversing the lines of a file: Read a line L from the file. Print the rest of the lines in reverse order. Print the line L. If only we had a way to reverse the rest of the lines of the file....

Reversal solution public static void reverseLines(Scanner input) { if (input.hasNextLine()) { // recursive case String line = input.nextLine(); reverseLines(input); System.out.println(line); } Where is the base case?

Tracing our algorithm call stack: The method invocations currently running reverseLines(new Scanner("poem.txt")); public static void reverseLines(Scanner input) { if (input.hasNextLine()) { String line = input.nextLine(); // "I have eaten" reverseLines(input); System.out.println(line); } public static void reverseLines(Scanner input) { if (input.hasNextLine()) { String line = input.nextLine(); // "the plums" reverseLines(input); System.out.println(line); } public static void reverseLines(Scanner input) { if (input.hasNextLine()) { String line = input.nextLine(); // "that were in" reverseLines(input); System.out.println(line); } public static void reverseLines(Scanner input) { if (input.hasNextLine()) { String line = input.nextLine(); // "the icebox" reverseLines(input); System.out.println(line); } public static void reverseLines(Scanner input) { if (input.hasNextLine()) { // false ... } input file: output: I have eaten the plums that were in the icebox the icebox that were in the plums I have eaten

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?