CSE 143 Lecture 9 Recursion slides created by Alyssa Harding

Slides:



Advertisements
Similar presentations
Lecture 12 Recursion part 1
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Computer Science II Recursion Professor: Evan Korth New York University.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Recursion … just in case you didn’t love loops enough …
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
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
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
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.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Recursion AP Computer Science A Mr. Langner By: Thomas Robbins.
Chapter 5 Loops.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion Pepper. Recursion Definition English –procedure that can repeat a version of itself indefinitely – such as mirrors looking at each other. Math.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 5: Control Structures II
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
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.
Lecture 12: Final Review Tami Meredith. Programming Requires 1. Identification of the problem Understanding it, identifying correct solutions 2. Solving.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
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.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Recursion Version 1.0.
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
Introduction to Recursion
Building Java Programs
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.
Chapter 5: Control Structures II
Recursion
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
CSE 143 Lecture 11 Recursive Programming reading:
Recursion Pepper.
slides created by Marty Stepp and Alyssa Harding
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Recursion based on slides by Alyssa Harding
CS302 - Data Structures using C++
Recursion Based on slides by Alyssa Harding
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
CIS 110: Introduction to Computer Programming
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:
Recursion Pepper.
Building Java Programs
slides created by Marty Stepp
Building Java Programs
Recursion.
CIS 110: Introduction to Computer Programming
Lecture 6 - Recursion.
Presentation transcript:

CSE 143 Lecture 9 Recursion slides created by Alyssa Harding

2 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 many of the same problems –Some problems are easier solved iteratively –Some problems are easier solved recursively

3 Example: row Imagine that you’re a robot and I ask you which row you’re sitting in: So far, you’re programmed to take an iterative approach int count = 0; while ( moreRowsLeft() ) { count++; }

4 Example: row What if you’re a robot who can’t see well? What row AM I in? …Do more rows exist? …Do I exist? …What is the meaning of life?

5 Example: row What if you’re have a room full of other robots? You can ask them questions to help solve your problem! …but not that question! We need to make progress each time What row is he in? What row am I in? What row is he in?

6 Example: row We can ask what row they are in to figure out our own row: What row are you in? …ok, I’m row 3! I’m in the front, so row 1! What row are you in? …ok, I’m row 2!

7 Case analysis Iteratively, we think of the loop bounds Recursively, we think of the cases Base case: –Easiest, simplest case where we know exactly what work to do –Example: “If I’m in the front row, I’m in row 1.” Recursive case: –We do a little bit of work and ask someone else a simpler version of the same question –Example: “Otherwise, I ask the person in front of me what row they are in and add 1!”

8 Case analysis Key questions to ask: Identifying the base case: –What is the easiest case? –When do I know that I’m done? Working out the recursive case: –What’s a small bit of work that I can do? –What progress can I make towards my goal? –Is there a repeated pattern?

9 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 3 2

10 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… stop!

11 Example: writeStars Here’s an iterative approach to making a method that writes out n stars: public static void writeStars(int n) { for (int i = 0; i < n; i++) System.out.print("*"); System.out.println(); }

12 Example: writeStars Let’s transform it to be recursive! What is the base case? public static void writeStars2(int n) { if ( n == 1 ) { System.out.println(“*”); } else {... } } Printing 1 star is easy, but printing 0 is even easier!

13 Example: writeStars Let’s transform it to be recursive! What is the base case? public static void writeStars2(int n) { if ( n == 0 ) { System.out.println(); } else {... } } Here’s our simplest base case.

14 Example: writeStars Let’s transform it to be recursive! What is the recursive case? public static void writeStars2(int n) { if ( n == 0 ) { System.out.println(); } else { for (int i = 0; i < n; i++ ) { System.out.println(“*”); } } } We’re a lazy robot! We just want to make a small amount of progress.

15 Example: writeStars Let’s transform it to be recursive! What is the recursive case? public static void writeStars2(int n) { if ( n == 0 ) { System.out.println(); } else { System.out.println(“*”); writeStars2(n – 1); } } We make a little progress… We ask another robot to do the rest. We have to trust that we’re writing the method well!

16 Example: writeStars We can trace its progress as it goes: writeStars2(3) System.out.print(“*”) writeStars2(2) System.out.print(“*”) writeStars2(1) System.out.print(“*”) writeStars2(0) System.out.println()

17 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

18 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

19 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.

20 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?

21 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!

22 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); } Example: reverse Output: recursion loves that student 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); } Input: student that loves recursion 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); }

23 Example: stutter Our favorite problem: stutter! Given an int as input, stutter the digits –Example: stutter(348) returns So far we’ve only printed inside of our recursive methods, but we can return values as well

24 Example: stutter What is the base case? public static int stutter(int n) { if ( n < 10 ) { return n*11; } else {... } } Any single digit number can be stuttered easily.

25 Example: stutter What is the recursive case? public static int stutter(int n) { if ( n < 10 ) { return n*11; } else {... } } We can make a smaller problem by breaking the number down: n = 348 n/10 -> 34 n%10 -> 8 and recurse by stuttering both parts: stutter(n/10) -> 3344 stutter(n%10) -> 88

26 Example: stutter What is the recursive case? public static int stutter(int n) { if ( n < 10 ) { return n*11; } else { return stutter(n/10)*100 + stutter(n%10); } } To put them back into one number, we can’t just add. We need to shift the first digits to the right: stutter(n/10)*100 + stutter(n%10) 3344*

27 Example: stutter What about negative numbers? public static int stutter(int n) { if ( n < 0 ) { return –stutter(-n); } else if ( n < 10 ) { return n*11; } else { return stutter(n/10)*100 + stutter(n%10); } } We deal with them first and trust the recursion to take care of the rest.