recursion. what is recursion? solving a problem in terms of itself or repeating objects in a...
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! <Hang up>
YouNeighbor 1
Add two to your neighbor’s address to get your house number.
So my address is 42 + 2 =
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.
<holding…>
Neighbor 1Neighbor 2 You
Neighbor 1 puts you on hold and calls their neighbor.
Howdy neighbor. What is your
house number?
<holding…>
Neighbor 1Neighbor 2 You
But their neighbor doesn’t know their house number either! (Seriously, who are these people?!)
Hm, that’s a good question. Hold
please!
<holding…>
Neighbor 1Neighbor 2 You
Neighbor 2 puts neighbor 1 on hold and calls neighbor 3.
<holding…><holding…>
Howdy neighbor. What is your
house number?
Neighbor 1Neighbor 2 YouNeighbor 3
Neighbor 3 tells neighbor 2 their house number (phew!)
<holding…><holding…>
22.
Neighbor 1Neighbor 2 YouNeighbor 3
Neighbor 2 thanks neighbor 3 and hangs up.
<holding…><holding…>
Sweet, thanks! <Hang up>
Neighbor 1Neighbor 2 YouNeighbor 3
Neighbor 2 adds two to get address.
<holding…><holding…>
So my address is 22 + 2 =
24!
Neighbor 1Neighbor 2 You
Neighbor 2 takes neighbor 3 off of hold and tells them their address.
<holding…>
24.
Neighbor 1Neighbor 2 You
Neighbor 1 thanks neighbor 2 and hangs up.
<holding…>
Sweet, thanks! <Hang up>
Neighbor 1Neighbor 2 You
Your neighbor adds two to get their address.
<holding…>
So my address is 24 + 2 =
26!
Neighbor 1
Finally your neighbor takes you off hold and tells you their address (what took them so long?!)
Sweet, thanks! <Hang up>
Neighbor 1
You add two to your neighbors address to get your house number!
So my address is 26 + 2 =
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!
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!
22 You <calling neighbor, waiting for answer>
- 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 <calling neighbor, waiting for answer>
N1<answers call, decides what to do>
- 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 <calling neighbor, waiting for answer>
N1<calling neighbor, waiting for answer>
- 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 <calling neighbor, waiting for answer>
N1<calling neighbor, waiting for answer>
N2<answers call, decides what to do>
- 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 <calling neighbor, waiting for answer>
N1<calling neighbor, waiting for answer>
N2<calling neighbor, waiting for answer>
- 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 <calling neighbor, waiting for answer>
N1<calling neighbor, waiting for answer>
N2<calling neighbor, waiting for answer>
N3<answers call, decides what to do>
- 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 <calling neighbor, waiting for answer>
N1<calling neighbor, waiting for answer>
N2<calling neighbor, waiting for answer>
N3<knows answer! Tells N2 and hangs up>
- 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 <calling neighbor, waiting for answer>
N1<calling neighbor, waiting for answer>
N2<calling neighbor, waiting for answer>
N3<knows answer! Tells N2 and hangs up>
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 <calling neighbor, waiting for answer>
N1<calling neighbor, waiting for answer>
N2<adds two to neighbor’s answer>
22 + 2 = 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 <calling neighbor, waiting for answer>
N1<calling neighbor, waiting for answer>
N2<knows answer! Tells N1 and hangs up>
22 + 2 = 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 <calling neighbor, waiting for answer>
N1<adds two to neighbor’s answer>
24 + 2 = 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 <calling neighbor, waiting for answer>
N1<knows answer! Tells you and hangs up>
24 + 2 = 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 <add two to neighbor’s answer>
26 + 2 = 28
22 You <done! Your address is 28>
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 andmove towardterm 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 = 1else
ans = 3 * fact(3-1)
>> myans = fact(3)
fact(3)if 3 == 1 | 3==0
ans = 1else
ans = 3 * fact(3-1)
Cal fact(3) from command window…
>> myans = fact(3)
fact(3)if 3 == 1 | 3==0
ans = 1else
ans = 3 * fact(3-1)
Cal fact(3) from command window…
>> myans = fact(3) fact(2)if 2 == 1 | 2==0
ans = 1else
ans = 2 * fact(2-1)
fact(3)if 3 == 1 | 3==0
ans = 1else
ans = 3 * fact(3-1)
Cal fact(3) from command window…
>> myans = fact(3) fact(2)if 2 == 1 | 2==0
ans = 1else
ans = 2 * fact(2-1)
fact(3)if 3 == 1 | 3==0
ans = 1else
ans = 3 * fact(3-1)
Cal fact(3) from command window…
>> myans = fact(3) fact(2)if 2 == 1 | 2==0
ans = 1else
ans = 2 * fact(2-1)
fact(1)if 1 == 1 | 1==0
ans = 1else
ans = 1 * fact(1-1)
fact(3)if 3 == 1 | 3==0
ans = 1else
ans = 3 * fact(3-1)
Cal fact(3) from command window…
>> myans = fact(3) fact(2)if 2 == 1 | 2==0
ans = 1else
ans = 2 * fact(2-1)
fact(1)if 1 == 1 | 1==0
ans = 1else
ans = 1 * fact(1-1)1
fact(3)if 3 == 1 | 3==0
ans = 1else
ans = 3 * fact(3-1)
Cal fact(3) from command window…
>> myans = fact(3) fact(2)if 2 == 1 | 2==0
ans = 1else
ans = 2 * fact(2-1)
1
fact(3)if 3 == 1 | 3==0
ans = 1else
ans = 3 * fact(3-1)
Cal fact(3) from command window…
>> myans = fact(3) fact(2)if 2 == 1 | 2==0
ans = 1else
ans = 2 * fact(2-1)
12
fact(3)if 3 == 1 | 3==0
ans = 1else
ans = 3 * fact(3-1)
Cal fact(3) from command window…
>> myans = fact(3)
2
fact(3)if 3 == 1 | 3==0
ans = 1else
ans = 3 * fact(3-1)
Cal fact(3) from command window…
>> myans = fact(3)
26
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
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 Matlabfunction 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 endend
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