You are on page 1of 12

Recursion

What is Recursion?
Recursive problems are problems that are
essentially solved by solving lots and lots of smaller
instances of the same problem. We do this by
recalling our function INSIDE our function. Sounds
weird, but recursion is one way to represent data

whose exact size the programmer doesnt know.


Each time we recall our function we are
breaking down our inputs into simpler ones in
such a way that eventually the base case must be
reached when we attain the simplest possible
form of the input.

The Three Pillars, REMEMBER THEM!


1. The function must CALL A CLONE OF ITSELF.
2. There must be a clear TERMINATING
CONDITION!
3. The code must MOVE TOWARD THIS
TERMINATING CONDITION!
Be prepared to see these as a test question. And be
prepared to hear them over and over. and
over..

So HOW am I supposed to DO this stuff?


You will be given a problem, and your first step will be to

break it down into the simplest case, or the case that will
signify you have reached your goal output. This is your
Terminating condition. An example of such is if you
were trying to use recursion to sum up all of the numbers is
a vector, you may go about this by adding it up one element
at a time, and then deleting the element you have just
added to the overall sum, or output. How would we
determine our base case here?
if isempty(vec) ! why? because when our vector is empty,
we have removed all that terms that have been added to the
overall sum, or our output.

Okay, terminating blah blah blah what now?


So now you have decided where you want to stop, at what

condition you have your final output now where do you


want to go? You need to work toward that terminating
condition! How? Call a clone! huh?
So back to adding up the elements in a vector. If our
terminating condition is:
if isempty(vector)
.. how can we move toward that condition? Well.. were
going to have to make our input vector, vec, shorter each
time we call a clone by removing the element we have
already added. In this case, say our function was called
mySum:
output = vector(1) + mySum(vector(2:end))
How does THAT work?

It takes the first element of vector, and adds it to the


recall of the function, but now your input is the second
through the end element of vector. So what we are adding
the first element of vector to, is the first element of
vector(2:end).
Doesnt make any sense? Well lets keep going and
hopefully this will become more clear when we move on to
tracing through a recursive function.
Now we have only one final step. What do I do once I
REACH that terminating condition thing?
Once we reach termination, we have our output, so we no
longer want to recall our function, so we know we wont have
that under our condition, and we know we have our sum
already, so we dont want to add anything else to it
so if isempty(vector)
output = 0;

Okay so what did all that amount to?


function output = mySum(vector)
if isempty(vector)
output = 0;
else
output = vector(1) + mySum(vec(2:end));
end

Lets Trace that thing so we UNDERSTAND it:


1. function output = mySum(vector)
2.
if isempty(vector)
3.
output= 0;
4.
else
5.
output= vector(1) + mySum(vector(2:end));
6.
end
Line 1: Function Header for mySum with vector as our input
Line2: This is our terminating condition
Line 3: our vector was empty so the value of our function call here was
zero. What you are thinking at this point is, well, once we are done adding,
wont this reset output to zero and erase all our work? NOPE! Why?
because . lets move to
Line 5: We need to sum up the values, so we take the first element,
vector(1) and add it to the next element by recalling the function changing
the input to vector(2:end). So when we recall the function, output is
vector(1) + the first element of our new input vector + mySum(the new

Look at it:
vector = [3, 5, 1, 8, 7];
out = mySum(vector);

we know that out will be 24 if we can add

Heres how it looks internally in MATLAB:


3 + mySum([5 1 8 7])
3 + 5 + mySum([1 8 7])

where [5 1 8 7] is vector 2:end right?


[1 8 7] is the new vector from above of 2:end

3 + 5 + 1 + mySum([8 7])
3 + 5 + 1 + 8 + mySum( 7 )
3 + 5 + 1 + 8 + 7 + mySum()
3 + 5 + 1 + 8 + 7 + ?????? what happens when our vector is empty? whats the
value of out? We set it to zero right? so we get:
3 + 5 + 1 + 8 + 7 + 0 and we didnt call another clone, so it has to stop here
correct? Yep. And the final value of out is: 3 + 5 + 1 + 8 + 7 + 0 = 24

Okay, that was a simple example, for understanding purposes, lets


do a harder one.
Problem Statement:

Solution:
function ret =

Go over that one.


IM LEAVING IT UP TO YOU TO GO OVER THAT
ONE YOURSELF. PLEASE MAKE SURE YOU
UNDERSTAND IT. Stereotypical examples of
recursive algorithms are the Fibonacci number
series and factorial (3!..). If you do not see these
on your homework and you probably will
writing code to produce the first 100 Fibonacci
numbers, and writing a code that will solve any
factorial would be great self exercises.
Recommendations: Do the homework for this one.

You might also like