C22a: Problem Solving using Recursion

Similar documents
q To develop recursive methods for recursive mathematical functions ( ).

q To develop recursive methods for recursive mathematical functions ( ).

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

CS/CE 2336 Computer Science II

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

CSE 143 Lecture 11. Decimal Numbers

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CSE 2123 Recursion. Jeremy Morris

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Building Java Programs

Chapter 18 Recursion. Motivations

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

Recursion. Recursion [Bono] 1

Programming II (CS300)

Programming II (CS300)

Programming II (CS300)

CIS265 - Spring Exam 2 First Name Last Name CSU#

CSE 143 Lecture 10. Recursive Programming. reading: slides adapted from Marty Stepp and Hélène Martin

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Fun facts about recursion

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

C12a: The Object Superclass and Selected Methods

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

CS212 Midterm. 1. Read the following code fragments and answer the questions.

Recursive Problem Solving

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

CS 3410 Ch 7 Recursion

C27a: Stack and Queue

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

Complexity of Algorithms

We cover recursion in 150. Why do it again in 151?

Chapter 10: Recursive Problem Solving

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

Reduction & Recursion Overview

CS205: Scalable Software Systems

CS 180 Final Exam Review 12/(11, 12)/08

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

CSE 143. Computer Programming II

Lecture 19: Recursion

Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

Solving problems by recursion

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting

Chapter 6 Recursion. The Concept of Recursion

1.7 Recursion. Department of CSE

Lecture 6: Recursion RECURSION

C18a: Abstract Class and Method

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Recursive Definitions

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

2.3 Recursion 7/23/2015 3:06:35 PM

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Recursion. Chapter 5

Chapter 15: Recursion

CS1 Lecture 15 Feb. 19, 2018

Sorting & Searching (and a Tower)

COMP 110 MORE TYPES. Instructor: Sasa Junuzovic

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

CS18000: Programming I

C16b: Exception Handling

Recursion. Thinking Recursively. Tracing the Recursive Definition of List. CMPT 126: Lecture 10. Recursion

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

Koch snowflake. Fractal Fern

CSC 273 Data Structures

Objectives. Recursion. One Possible Way. How do you look up a name in the phone book? Recursive Methods Must Eventually Terminate.

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

Pace University. Fundamental Concepts of CS121 1

= otherwise W-4 W-1. Overview. CSE 142 Computer Programming I. Overview. Factorial Function

C06: Java API & Libraries

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Searching and Sorting (Savitch, Chapter 7.4)

Recursion. Fundamentals of Computer Science

C02: Overview of Software Development and Java

What is an algorithm?

A Useful Case. A Fruitful Application of Recursion: n-queen problem

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Recursion. Chapter Simple Recursion. Goals Trace recursive algorithms Implement recursive algorithms

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

RECURSION. Many Slides from Ken Birman, Cornell University

Programming Problems 22nd Annual Computer Science Programming Contest

C06: Memory Management

Recursion. Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

CSE 214 Computer Science II Recursion

Computer Science 1 Ah

! New mode of thinking. ! Powerful programming paradigm. ! Mergesort, FFT, gcd. ! Linked data structures.

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Search,Sort,Recursion

Recursion. Java recursive programming: Writing methods that call themselves to solve problems recursively.

2.3 Recursion. Overview. Mathematical Induction. What is recursion? When one function calls itself directly or indirectly.

C20a: Selected Interfaces in Java API

Transcription:

CISC 3115 TY3 C22a: Problem Solving using Recursion Hui Chen Department of Computer & Information Science CUNY Brooklyn College 11/6/2018 CUNY Brooklyn College 1

Outline Characteristics of recursion Recursion as problem solving strategy Examples Recursive helper method/function Tail and non-tail recursion 11/6/2018 CUNY Brooklyn College 2

Characteristics of Recursion All recursive methods have the following characteristics: One or more base cases (the simplest case) are used to stop recursion. Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. 11/6/2018 CUNY Brooklyn College 3

Recursion as Problem Solving Strategy Break the problem into subproblems such that one or more subproblems resembles the original problem These subproblems resembling the original problem is almost the same as the original problem in nature with a smaller size. Apply the same approach to solve the subproblem recursively to reach the base case 11/6/2018 CUNY Brooklyn College 4

Problem Solving Example: Print Message Many Times Problem: print a message n times. Can we solve it using recursion? Original problem: print a message n times Subproblems with smaller size print a message 1 time print a message (n-1) times (the same problem as the original problem, but smaller size) Base case: print the message 0 times (or 1 time) 11/6/2018 CUNY Brooklyn College 5

Problem Solving Example: Print Message Many Times: Solution Base case: n = 0 (how about the n = 1 base case? Which one yields better code?) public class PrintMsg { public static void main(string[] args) { nprintmsg("hello, World!", 5); public static void nprintmsg(string msg, int n) { if (n == 0) return; // base case System.out.println(msg); // subproblem 1 nprintmsg(msg, n-1); // subproblem 2 11/6/2018 CUNY Brooklyn College 6

Problem Solving Example: Print Message Many Times: Revisit Wait! Can we just write a loop to print a message n times? (solving problem iteratively) Remarks However, it is sometimes easier to think recursively to solve a complex problems. Many problems we solve iteratively can also be solved recursively. Example: the palindrome problem (e.g., madam, nursesrun) 11/6/2018 CUNY Brooklyn College 7

Problem Solving Example: Is It a Palindrome? Problem: is a given string a palindrome? Recursive solution: 1) Compare the first and last character of the string. If not equal, not palindrome; 2) otherwise, repeat for the substring less the first and the last character (the same problem whose size is the original size 2) Base case: a single character or empty string, and the single character string or the empty string is always a palindrome. 11/6/2018 CUNY Brooklyn College 8

Problem Solving Example: Is It a Palindrome? Solution An example realization of the solution public static boolean ispalindrome(string s) { // base case if (s.length() <= 1) return true; // subproblem 1 if (s.charat(0)!= s.charat(s.length()-1)) return false; // subproblem 2 return ispalindrome(s.substring(1, s.length()-1)); 11/6/2018 CUNY Brooklyn College 9

Problem Solving Example: Is It a Palindrome? Discussion Is the solution efficient, in particular, when the string is very long? Hint: a Java string is immutable? How many string objects are being coreated? public static boolean ispalindrome(string s) { // base case if (s.length() <= 1) return true; // subproblem 1 if (s.charat(0)!= s.charat(s.length()-1)) return false; // subproblem 2 return ispalindrome(s.substring(1, s.length()-1)); 11/6/2018 CUNY Brooklyn College 10

Problem Solving Example: Is It a Palindrome? Recursive Helper Rewrite it by introducing a new method that uses parameters to indicate subproblem size public static boolean ispalindrome(string s) { return ispalindrome(s, 0, s.length()-1); // The recursive helper method public static boolean ispalindrome(string s, int beginindex, int endindex) { if (endindex - beginindex <= 1) return true; // base case if (s.charat(beginindex)!= s.charat(endindex)) return false; // subproblem 1 return ispalindrome(s, beginindex+1, endindex-1); // subproblem 2 11/6/2018 CUNY Brooklyn College 11

Problem Solving Example: Selection Sort Problem: sort a list Recursive solution: divide the problem into two subproblems 1. Find the smallest number in the list and swaps it with the first number. 2. Ignore the first number and sort the remaining smaller list recursively (subproblem is the same problem as the original problem with the size 1). 11/6/2018 CUNY Brooklyn College 12

Problem Solving Example: Selection Sort: Solution The sample solution includes two realizations Sort integers Sort any objects with the Comparator interface 11/6/2018 CUNY Brooklyn College 13

Problem Solving Example: Searching Problem: search an item (using its key) in a sorted list Recursive solution: divide the problem into subproblems, one or more are essentially the original problem 1. Find the middle element in the list 2. The list becomes three parts. Determine which part contains or may contain the item. Search the item the part that may contain the item (the subproblem identical to the original problem but with smaller size) Case 1: If the key is less than the middle element, recursively search the key in the first half of the list. Case 2: If the key is equal to the middle element, the search ends with a match. Case 3: If the key is greater than the middle element, recursively search the key in the second half of the array. Base case: the list becomes empty (not found); or it is the middle element (found). 11/6/2018 CUNY Brooklyn College 14

Problem Solving Example: Searching: Implementation An example implementation using a helper method public static int search(int[] numbers, int key) { return search(numbers, key, 0, numbers.length-1); private static int search(int[] numbers, int key, int beginindex, int endindex) { int mid = (endindex + beginindex) / 2; // observe when mistakenly wrote - instead if (beginindex > endindex) return - beginindex - 1; // base case (not foudn) if (numbers[mid] == key) return mid; // base case (found) if (key < numbers[mid]) { // subproblem, the same problem but smaller size return search(numbers, key, beginindex, mid-1); else { // subproblem, the same problem but smaller size return search(numbers, key, mid+1, endindex); 11/6/2018 CUNY Brooklyn College 15

Iteration or Recursion? Some problems appear to be easily solved using iteration, while others recursion. Question: can you solve preceding examples using iteration? Example problems (recursion is easier) Search files containing a word in a directory (the search file problem, already discussed) Find directory size (the total size in bytes of all files under a directory, a revision of the search file problem) Solve the Tower of Hanoi problem Quick sort 11/6/2018 CUNY Brooklyn College 16

Problem Solving Example: Directory Size Problem: to find the size of a directory, i.e., the sum of the sizes of all files in the directory. The challenge: a directory may contain subdirectories and files. directory f1 f 2... f m d 1 d 2... d n 11/6/2018 CUNY Brooklyn College 17

Directory Size: Thinking Recursively The size of the directory can be defined recursively as follows, size d) = size( f ) + size( f ) +... + size( f ) + size( d ) + size( d ) +... + ( 1 2 m 1 2 size( dn ) 11/6/2018 CUNY Brooklyn College 18

Problem Solving Example: Tower of Hanoi Problem: There are n disks labeled 1, 2, 3,..., n, and three towers labeled A, B, and C. All the disks are initially placed on tower A. No disk can be on top of a smaller disk at any time. Only one disk can be moved at a time, and it must be the top disk on the tower. See https://liveexample.pearsoncmg.com/dsanimatio n/towerofhanoi.html 11/6/2018 CUNY Brooklyn College 19

Examine it at Size = 3 11/6/2018 CUNY Brooklyn College 20

How about Large Size? Starting with n disks on tower A. The Tower of Hanoi problem can be decomposed into three subproblems: Move n-1 disks from tower A to tower C Move disk n from tower A to tower B Move n-1 disks from tower C to tower B 11/6/2018 CUNY Brooklyn College 21

How about Large Size? 11/6/2018 CUNY Brooklyn College 22

Questions? Problem solving using recursion Divide big problem into smaller subproblems some of which are the same problem as the original one with smaller size Examples Sorting, searching, and others More examples in the textbook 11/6/2018 CUNY Brooklyn College 23

Tail Recursion A recursive method is said to be tail recursive if there are no pending operations to be performed on return from a recursive call. Tail recursions can be realized by complier efficiently. 11/6/2018 CUNY Brooklyn College 24

Tail and Non-tail Recursion: Compute Factorial Non-tail recursion public static int factorial(int n) { if (n == 0) { // base case return 1; else { // recursive call or method invocation // non-tail recursion, because we have to multiple factorial(n-1) by n, a pending operation return n * factorial(n - 1); Tail recursion public static int factorial(int n) { return factorial(n, 1); private static int factorial(int n, int result) { if (n == 0) { // base case return result; else { // recursive call // tail recursion, no pending operation after returning from the recursive call return factorial(n - 1, n * result); 11/6/2018 CUNY Brooklyn College 25

Questions Concept of tail and non-tail recursions Can you identify non-tail/tail-recursive methods in preceding examples? Write tail-recursive methods 11/6/2018 CUNY Brooklyn College 26