Programming with Recursion. What Is Recursion?

Similar documents
Recursion Solution. Counting Things. Searching an Array. Organizing Data. Backtracking. Defining Languages

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

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

Recursion. Chapter 5

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

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

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

CSE 230 Intermediate Programming in C and C++ Recursion

EE 368. Weeks 4 (Notes)

CSE 214 Computer Science II Recursion

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

1.7 Recursion. Department of CSE

Recursion continued. Programming using server Covers material done in Recitation. Part 2 Friday 8am to 4pm in CS110 lab

ADTs Stack and Queue. Outline

ADT Unsorted List. Outline

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Data Structures And Algorithms

Recursion: The Mirrors. (Walls & Mirrors - Chapter 2)

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

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

CS302 - Data Structures using C++

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

Solving problems by recursion

Chapter 19 Recursion and Method Redefinition

Recursion. Jordi Cortadella Department of Computer Science

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

Lecture 8 Recursion. The Mirrors

CS 161 Intro to CS I. Finish Pointers/Start Recursion

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

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

Chapter 15: Recursion

1 Recursion. 2 Recursive Algorithms. 2.1 Example: The Dictionary Search Problem. CSci 235 Software Design and Analysis II Introduction to Recursion

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Recursion. Example R1

UNIT 5A Recursion: Basics. Recursion

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

recursive algorithms 1

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis:

Recursive Definitions

Recursion. Recursion is: Recursion splits a problem:

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

CS 206 Introduction to Computer Science II

Chapter 2. Question 2 Write a box trace of the function given in Checkpoint Question 1. We trace the function with 4 as its argument (see next page).

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

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

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

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

CSE 2123 Recursion. Jeremy Morris

Chapter 10 Sorting and Searching Algorithms

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

Chapter 10: Recursive Problem Solving

CS200: Recursion and induction (recap from cs161)

Chapter 18 Recursion. Motivations

Programming II (CS300)

Recursion vs Induction. CS3330: Algorithms The University of Iowa

Goals: Define computational problem instance of a computational problem algorithm pseudocode Briefly discuss recursive procedure big-o notation

UNIT 5A Recursion: Basics. Recursion

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Control Structures of C++ Programming (2)

CMSC 150 LECTURE 7 RECURSION

moretosearch = (location < length);

Recursion. notes Chapter 8

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

Recursion vs Induction

Programming II (CS300)

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

CS103L SPRING 2017 UNIT 8: RECURSION

Counting Problems; and Recursion! CSCI 2824, Fall 2012!

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

Chapter 10 - Notes Applications of Arrays

ADTs Stack and Queue. Outline

C22a: Problem Solving using Recursion

Programming II (CS300)

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

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

Chapter 8. Binary Search Trees. Fall 2013 Yanjun Li CISC Trees. Owner Jake. Waitress Waiter Cook Helper Joyce Chris Max Len

Chapter 17 Recursion

CSC 1052 Algorithms & Data Structures II: Recursion

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

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

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

CS159. Nathan Sprague. November 9, 2015

CSCE 110 Notes on Recursive Algorithms (Part 12) Prof. Amr Goneid

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

Reduction & Recursion Overview

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS

Recursive Functions. Biostatistics 615 Lecture 5

COMP-202 Unit 8: Recursion. CONTENTS: Recursion in Java More complex examples

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CmpSci 187: Programming with Data Structures Spring 2015

CSCI 136 Data Structures & Advanced Programming. Lecture 9 Fall 2018 Instructors: Bills

Recursive Problem Solving

Chapter 6 Recursion. The Concept of Recursion

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Transcription:

Chapter 7 Programming with Recursion Fall 2017 Yanjun Li CS2200 1 What Is Recursion? How is recursion like a set of Russian dolls? Fall 2017 Yanjun Li CS2200 2

What Is Recursion? Recursive call A method call in which the method being called is the same as the one making the call A recursive function is a function that invokes itself directly or indirectly. Fall 2017 Yanjun Li CS2200 3 Example of Recursion Recursive definition A definition in which something is defined in terms of a smaller version of itself What is 3 factorial? Fall 2017 Yanjun Li CS2200 4

Example of Recursion Fall 2017 Yanjun Li CS2200 5 Examples of Recursion Fall 2017 Yanjun Li CS2200 6

Recursive Algorithm Base case The case for which the solution can be stated nonrecursively The case to stop recursion. General (recursive) case The case for which the solution is expressed in terms of a smaller version of itself Recursive algorithm A solution that is expressed in terms of (a) a smaller instance(s) of itself and (b) a base case(s) Fall 2017 Yanjun Li CS2200 7 Factorial Problem Base case : 0! = 1; General (recursive) case : n!= n* (n-1)!; Recursive algorithm //pre: n >=0 int factorial(int n) //n! if (n == 0) //base case return 1; else //recursive case return n * factorial(n-1); Fall 2017 Yanjun Li CS2200 8

Writing Recursive Solutions Algorithm for writing recursive solutions Determine the size of the problem Size is the factor that is getting smaller Size is usually a parameter to the problem Identify the base case(s) The case(s) for which you know the answer Identify the general case(s) The case(s) that can be expressed as a smaller version of the size Fall 2017 Yanjun Li CS2200 9 Writing Recursive Solutions Let s try it Problem: Calculate X n (X to the nth power) What is the size of the problem? Which case do you know the answer to? Which case can you express as a smaller version of the size? Recursive formulation: X n =X*(X n-1 ) Fall 2017 Yanjun Li CS2200 10

Writing Recursive Solutions int Power(int number, int exponent) if (exponent == 0) //base case return 1; else //recursive case return number * Power(number, exponent - 1); Do we need a precondition? Fall 2017 Yanjun Li CS2200 11 Writing Recursive Solutions Pattern of solution if (some condition for which answer is known) solution statement else function call Fall 2017 Yanjun Li CS2200 12

Writing Recursive Solutions Shall we try it again? Problem: Calculate Nth item in Fibonacci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 What is the next number? What is the size of the problem? Which case do you know the answer to? Which case can you express as a smaller version of the size? Fall 2017 Yanjun Li CS2200 13 Writing Recursive Solutions //pre: n>=0 int Fibonacci(int n) if (n == 0 n == 1) return n; else return Fibonacci(n-2) + Fibonacci(n-1); ) Fall 2017 Yanjun Li CS2200 14

Verifying Recursive Solutions How can we prove that the solution works? Base-Case Question: Is there a non-recursive way out of the function? Smaller-Caller Question: Does each recursive function call involve a smaller case of the original problem leading to the base case? General-Case Question: Assuming each recursive call works correctly, does the whole function work correctly? Can you answer yes to all three questions? Fall 2017 Yanjun Li CS2200 15 Verifying Recursive Solutions int Power(int number, int exponent) if (exponent == 0) return 1; else return number * Power(number, exponent - 1); Is there a non-recursive way out? Does each call involve a small case of original? Assuming recursive call works, does the entire function work? Fall 2017 Yanjun Li CS2200 16

Writing Recursive Solutions Shall we try it again? Problem: Search a list of integers for a value and return true if it is in the list and false otherwise What is the size of the problem? Fall 2017 Yanjun Li CS2200 17 Writing Recursive Solutions bool ValueInList(ListType data, int value, int startindex); Which case do you know the answer to? Which case can you express as a smaller version of the size? Fall 2017 Yanjun Li CS2200 18

Writing Recursive Solutions bool ValueInList(ListType list, int value, int startindex) if (list.info[startindex] == value) return true; Base Case 1 else if (startindex == list.length -1) return false; Base Case 2 else return ValueInList(list, value, startindex + 1); Fall 2017 Yanjun Li CS2200 19 Writing Recursive Solutions Why use recursion? True, these examples could more easily be solved using iteration. However, a recursive solution is a natural solution in certain cases, especially when pointers are involved. Fall 2017 Yanjun Li CS2200 20

Writing Recursive Solutions Printing a list in order Size? Base case? Recursive (general) case? Fall 2017 Yanjun Li CS2200 21 Writing Recursive Solutions struct NodeType int info; NodeType * next; ; void Print(NodeType* listptr) if (listptr!= NULL) cout << listptr->info << endl; Print(listPrt->next); Where is the base case? Fall 2017 Yanjun Li CS2200 22

Writing Recursive Solutions Printing a list in reverse order What must be changed from the in-order print to make the code print in reverse order? Fall 2017 Yanjun Li CS2200 23 Writing Recursive Solutions void RevPrint(NodeType* listptr) if (listptr!= NULL) RevPrint(listPrt->next); cout << listptr->info << endl; Fall 2017 Yanjun Li CS2200 24

Writing Recursive Solutions Can you verify the RevPrint algorithm using the three-question method? Base-Case question? Smaller-Caller Question? General-Case Question? Fall 2017 Yanjun Li CS2200 25 Binary Search binarysearch (an anarray of ArrayType, a value of ItemType): if (anarray is of size 1) Determine if anarray s item is equal to value else Find the midpoint of anarray Determine which half of anarray contains value if (value is in the first half of anarray) binarysearch(first half of anarray, value) else binarysearch(second half of anarray, value) Fall 2017 Yanjun Li CS2200 26

Binary Search Implementation issues: How will you pass half of anarray to the recursive calls to binarysearch? How do you determine which half of the array contains value? What should the base case(s) be? How will binarysearch indicate the result of the search? Fall 2017 Yanjun Li CS2200 27 int Mystery( ItemType info[ ], ItemType item, int fromloc, int toloc ) int mid; if ( fromloc > toloc ) return -1; //base case else mid = ( fromloc + toloc ) / 2 ; if (info[mid] == item) return mid; else if (item < info[mid]) return Mystery (info,item,fromloc,mid-1 ); else return Mystery(info,item,mid + 1,toLoc) ; What does this function return? Fall 2017 Yanjun Li CS2200 28

Mr. Spock s Dilemma (Choosing r out of n Things) Problem How many different choices are possible for exploring r planets out of n planets in a solar system? A combination problem Fall 2017 Yanjun Li CS2200 29 Mr. Spock s Dilemma (Choosing r out of n Things) Let c(n, r) be the number of groups of r planets chosen from n In terms of Planet X: c(n, r) = the number of groups of r planets that include Planet X + the number of groups of r planets that do not include Planet X Fall 2017 Yanjun Li CS2200 30

Mr. Spock s Dilemma (Choosing r out of n Things) The number of ways to choose r out of n things is the sum of The number of groups of r planets that include Planet X The number of ways to choose r 1 out of n 1 things since X is chosen already c(n 1, r 1) And the number of groups of r planets that do not include Planet X The number of ways to choose r out of n 1 things c(n 1, r) c(n, r) = c(n 1, r 1) + c(n 1, r) Fall 2017 Yanjun Li CS2200 31 Mr. Spock s Dilemma (Choosing r out of n Things) Base cases There is one group of everything c(r, r) = 1 There is one group of nothing c(n, 0) = 1 Although r cannot exceed n here, we want our solution to be general c(n, r) = 0 if r > n Fall 2017 Yanjun Li CS2200 32

Mr. Spock s Dilemma (Choosing r out of n Things) Recursive solution c(n, k) = 1 if r = 0 1 if r = n 0 if r > n c(n 1, r 1) + c(n 1, r) if 0 < r < n Fall 2017 Yanjun Li CS2200 33 Mr. Spock s Dilemma (Choosing r out of n Things) Figure 2-12 The recursive calls that c(4, 2) generates Fall 2017 Yanjun Li CS2200 34

Organizing Data: The Towers of Hanoi The goal is to move all the discs from the left peg to the right one. Only one disc may be moved at a time. A disc can be placed either on an empty peg or on top of a larger disc. Try to use the smallest number of moves possible. http://www.dynamicdrive.com/dynamicindex12/towerhanoi.htm Fall 2017 Yanjun Li CS2200 35 Organizing Data: The Towers of Hanoi Figure 2-19a and b (a) The initial state; (b) move n - 1 disks from A to C Fall 2017 Yanjun Li CS2200 36

The Towers of Hanoi Figure 2-19c and d (c) move one disk from A to B; (d) move n - 1 disks from C to B Fall 2017 Yanjun Li CS2200 37 The Towers of Hanoi solvetowers( n, source, destination, spare) //move n from A to C if (n is 1) Move a disk directly from source to destination //move 1 from A to C else solvetowers(n-1, source, spare, destination) //move n-1 from A to B solvetowers(1, source, destination, spare) //move 1 from A to C solvetowers(n-1, spare, destination, source) //move n-1 from B to C //end if Fall 2017 Yanjun Li CS2200 38

The Towers of Hanoi If T N is the minimum number of moves needed to solve the puzzle with N disks: T 0 = 0 //base case T N <=T N-1 +1+T N-1 = 2T N-1 +1 when N>0 Move the top N-1 disks from A to B (using C as a spare peg) // T N-1 Move the bottom disk from A to C // 1 Move N-1 disks from B to C (using A as a spare peg) // T N-1 Fall 2017 Yanjun Li CS2200 39 Recursion and Efficiency Some recursive solutions are so inefficient that they should not be used Factors that contribute to the inefficiency of some recursive solutions Overhead associated with function calls Inherent inefficiency of some recursive algorithms (combination problem) Fall 2017 Yanjun Li CS2200 40

Recursion and Efficiency Do not use a recursive solution if it is inefficient and there is a clear, efficient iterative solution Fall 2017 Yanjun Li CS2200 41 When To Use Recursion Depth of recursive calls is relatively shallow compared to the size of the problem Recursive version does about the same amount of work as the non-recursive version (same Big-O) The recursive version is shorter and simpler than the non-recursive solution SHALLOW DEPTH EFFICIENCY CLARITY Fall 2017 Yanjun Li CS2200 42

Reference Reproduced from C++ Plus Data Structures, 4 th edition by Nell Dale. Reproduced by permission of Jones and Bartlett Publishers International. Fall 2017 Yanjun Li CS2200 43