Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Recursion in Python. Recursion Problems in every area of life can be defined recursively, that is, they can be described in terms of themselves. An English.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science II Recursion Professor: Evan Korth New York University.
Recursion. Binary search example postponed to end of lecture.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CS0007: Introduction to Computer Programming File IO and Recursion.
Recursion Fall 2008 Dr. David A. Gaitros
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
What is recursion? Imagine checking the dictionary for “apple”  a round fruit with a firm white flesh and a green, red or yellow skin  the usually sweet-tasting.
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 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 9, 2015)
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Recursion.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
CS2852 Week 6, Class 2 Today Class exercise: Implementing a recursive method Binary Search Trees Tomorrow: Quiz at start of lab Implementing a recursive.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Introduction to Recursion
Recursion DRILL: Please take out your notes on Recursion
Decrease-and-Conquer Approach
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Chapter 9 Recursion.
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm design and Analysis
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Basics of Recursion Programming with Recursion
Recursion Taken from notes by Dr. Neil Moore
Yan Shi CS/SE 2630 Lecture Notes
Last Class We Covered Recursion Stacks Parts of a recursive function:
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Lecture 6 - Recursion.
Presentation transcript:

Recursion

What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that calls itself! Another way to repeat code Takes advantage of the “activation stack”

You just moved in to a new house on a dead end street… In celebration you shout: “Woo hoo, my new house is awesome and the house number is 1282!”

You just moved in to a new house on a dead end street… But you don’t know the house number! You do know that your house number is 2 plus the address of your neighbor to the left So you decide to call your neighbor…

Call your neighbor and ask them for their house number. Add two to get your house number. You

Call your neighbor and ask them for their house number. Howdy neighbor. What is your house number? Neighbor 1 You

Wait for the neighbor to give you an answer. 42. Neighbor 1 You

Kindly thank them and hang up. Sweet, thanks! You Neighbor 1

Add two to your neighbor’s address to get your house number. So my address is = 44!

What if your neighbor doesn’t know their address?

Call your neighbor and ask for their house number. Howdy neighbor. What is your house number? Neighbor 1 You

But they don’t know it! Hm, that’s a good question. Hold please! Neighbor 1 You

Neighbor 1 puts you on hold and calls their neighbor. Neighbor 1 Neighbor 2You

Neighbor 1 puts you on hold and calls their neighbor. Howdy neighbor. What is your house number? Neighbor 1 Neighbor 2You

But their neighbor doesn’t know their house number either! (Seriously, who are these people?!) Hm, that’s a good question. Hold please! Neighbor 1 Neighbor 2You

Neighbor 2 puts neighbor 1 on hold and calls neighbor 3. Howdy neighbor. What is your house number? Neighbor 1Neighbor 2YouNeighbor 3

Neighbor 3 tells neighbor 2 their house number (phew!) 22. Neighbor 1Neighbor 2YouNeighbor 3

Neighbor 2 thanks neighbor 3 and hangs up. Sweet, thanks! Neighbor 1Neighbor 2YouNeighbor 3

Neighbor 2 adds two to get address. So my address is = 24! Neighbor 1Neighbor 2You

Neighbor 2 takes neighbor 3 off of hold and tells them their address. 24. Neighbor 1Neighbor 2You

Neighbor 1 thanks neighbor 2 and hangs up. Sweet, thanks! Neighbor 1Neighbor 2You

Your neighbor adds two to get their address. So my address is = 26! Neighbor 1

Finally your neighbor takes you off hold and tells you their address (what took them so long?!) Sweet, thanks! Neighbor 1

You add two to your neighbors address to get your house number! So my address is = 28!

Let’s analyze what just happened How many phone calls were made? When did the calling chain finally stop? From “your” perspective, what happened when you called your neighbor?

Algorithm in writing If you already know your house number, say it loud! If you don’t know your house number… – Call neighbor to your left and ask for their house number – When you get the number, add two to it, then say it loud!

But… What if you don’t have any neighbors?

Algorithm in writing If you already know your house number, say it loud If you don’t have a neighbor to the left and don’t know your house number, say 0 If you don’t know your house number… – Call neighbor to your left and ask for their house number – When you get the number, add two to it, then say it loud!

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22You

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2 N3

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2 N3

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N2 N3 22

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N = 24

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N1 N = 24 24

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N = 26

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! - If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You N = 26 26

- If you already know your house number, say it loud - If you don’t have a neighbor to the left and don’t know your house number, say 0 - If you don’t know your house number… -Call neighbor to your left and ask for their house number -When you get the number, add two to it, then say it loud! 22 You = 28

22 You

But… What if no-one on your street knows their house number?

But… What if no-one on your street knows their house number? – Think about it….don’t we cover this case by adding a check to see if you have a neighbor to the left and returning 0 if you don’t?

Three “Pillars” of Recursion A recursive function… – Calls clone of itself – Has terminating condition(s) – Moves toward terminating condition(s)

House number problem components Calling clone of itself? Terminating condition(s)? Moving toward the terminating conditions? If you already know your house number, say it loud If you don’t have a neighbor to the left and don’t know your house number, say 0 If you don’t know your house number… – Call neighbor to your left and ask for their house number – When you get the number, add two to it, then say it loud!

House number problem components Calling clone of itself? Terminating condition(s)? Moving toward the terminating conditions? If you already know your house number, say it loud If you don’t have a neighbor to the left and don’t know your house number, say 0 If you don’t know your house number… – Call neighbor to your left and ask for their house number – When you get the number, add two to it, then say it loud! Terminating conditions Call clone and move toward term cond

Another problem: Factorial How do you calculate 5! 5*4*3*2*1

Another problem: Factorial How do you calculate 5! 5! = 5*4! 4! = 4*3! 3! = 3*2! 2! = 2*1! 1! = 1 (what about 0!)

In general… n! = n * (n-1)! If we had a factorial function instead of !, it would be fact(n) = n * fact(n-1)

Factorial Recursive Algorithm What are are terminating (aka base) conditions? What is the simplest factorial to calculate? What inputs does the function need? How do we move toward the terminating condition?

Factorial Recursive Algorithm If number is 0 or 1 – Answer is 1 Else – Answer is number * factorial of number-1

Call fact(3) from command window… >> myans = fact(3)

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) >> myans = fact(3)

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3)

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1)

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1)

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1) fact(1) if 1 == 1 | 1==0 ans = 1 else ans = 1 * fact(1-1)

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1) fact(1) if 1 == 1 | 1==0 ans = 1 else ans = 1 * fact(1-1) 1

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1) 1

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) fact(2) if 2 == 1 | 2==0 ans = 1 else ans = 2 * fact(2-1) 1 2

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) 2

fact(3) if 3 == 1 | 3==0 ans = 1 else ans = 3 * fact(3-1) Cal fact(3) from command window… >> myans = fact(3) 2 6

Cal fact(3) from command window… >> myans = fact(3) 6

Cal fact(3) from command window… >> myans = fact(3) myans = 6 >> 6

Answer is returned to place that called it, which in this case was the command window!

What about bad inputs? What happens if input is -1? If input is -5?

What about bad inputs? What happens if input is -1? If input is -5? We theoretically recurse forever! (though our activation stack keeping track of all the temporary workspaces will eventually run out of memory and we will get an error)

Wrapper Functions Use a “wrapper function” to preprocess your data! A wrapper function checks input(s), and if they are value, makes the first call to the recursive function Captures result from recursive function and then returns it back to where called the wrapper function originally Note, the wrapper function should usually only run once per call (unlike the recursive helper function)

Why recursion? In general, any recursive function could be written as a loop (likely a while loop) Loops are usually faster (because Matlab doesn’t have to make all those temporary workspaces and copy the parameter values in) But some problems are solved more simply using recursion – Printing out structures (e.g. nested structures) – Listing out directories and files – Crawling the web

Recursively List Contents of Directory

Given a directory, we want to list all items in that folder, including items in Don’t even worry about matlab yet…let’s think about the steps involved in general Given a directory (you, yourself), what would you do to start listing out the filenames? What would you do when you encountered another folder?

Recursively Listing Directories Open folder to see list of items For each item listed in folder – Check to see if it is a directory If it is a directory, we need to search it (start whole process over) Else, if not a directory (meaning it’s just a file name), print it out to command window and continue on

Translating to Matlab function mydir(dirname) % recursive dir traversal lst = dir(dirname); % returns a structure array for pos = 3:length(lst) %skip over first two items, as they are always '.' and '..' if lst(pos).isdir % check if current is a directory mydir([dirname '/' lst(pos).name]) else fprintf('%s\n', lst(pos).name); % print out file name end

When you shouldn’t use recursion Consider Fibonacci numbers fib(n) = fib(n-1) + fib(n-2) fib(0) = 0 fib(1) = 1

Redundant Calculations To compute fib(n), we recursively compute fib(n-1). When that recursive call returns, we compute fib(n-2) using another recursive call – We have already computed fib(n-2) in the process of computing fib(n-1) – We make two calls to fib(n-2)

Redundant Calculations Making two method calls would double the running time Compounding effect: each recursive call does more and more redundant work – Each call to fib(n-1) and each call to fib(n-2) makes a call to fib(n-3); there are 3 calls to fib(n-3) – Each call to fib(n-2) or fib(n-3) results in a call to fib(n-4), so 5 calls to fib(n-4)

Redundant Calculations III The recursive routine fib is exponential – Meaning the amount of work done grows exponentially based on the input value n