Hello all, I hope everyone is having a
great week after finishing our first term test and assignment. This week’s
learning material is one of my favourite, recursion. This is not the first time
that I have seen recursion, since I have learned it in high school. Yet, I can
still recall how astonished I was when I was first introduced to recursion.
Calling yourself recursively and then you can solve a complicated problem? This
is mind blowing. Now, I have come to understand the concept of recursion and
how powerful it can be to solve problems in a simple and elegant way. So, why
don’t we start a journey to discover the beauty of recursion?
First of all, what exactly is recursion?
Recursion involves a function calling itself inside the function body. Its
basic concept is to break a complicated problem into smaller and smaller sub
problems until you get to a simple enough problem that can be solved easily. This
idea sounds magical, but to understand recursion fully, we need to work through
an actual example. Consider the code below.
Tracing recursion can get really bothersome
if we start with a random example. The key point is that we need to trace
recursive calls progressively, which means starting with depth level 1(or the
simplest situation), then moves our way up to depth level 2, 3, and so on.
Here, the simplest example is when L is an
integer instead of a list. Under this condition, nm simply returns what L is.
This is also called the base/general case, since it provides a condition to
stop the recursive callings.
>>> nm(6)
6
Then, we look at what would happen if L is
a list with depth one.
>>> nm([1, 3, 7, 5, 2])
è max([nm(1), nm(3), nm(7), nm(5), nm(2)]) #we know how nm performs on an integer, so we
get
è max([1, 3, 7, 5, 2])
7
Now, trace the function when L is a
depth-two list
>>> nm([1, [3, 7], 5, 2])
è max([nm(1), nm([3, 7]), nm(5), nm(2)]) #we know how nm performs on a
depth-one list, it returns the maximum number in the list.
è max([1, 7, 5, 2])
7
Continue to trace a depth-three list.
>>> nm([1, [3, [2, 7], 4], 6])
è max([nm(1), nm([3, [2, 7], 4]), nm(6)] #we know how nm performs on a
depth-two list, it returns the maximum number
è max([1, 7. 6])
7
As you can see, the most important part in
this tracing process is that, we trace to the point when we know what would be
returned instead of going to the base case every time. This saves us from
getting really messy recursive calls, and helps with the understanding of how
to come up with a recursive solution. The recursive thinking process somehow assemblies
the mathematic concept of induction, which is to solve the problem with order
n, assuming the order n-1 case works. Therefore, when you implement a recursive
solution, you want your calling to change the problem state to one
that is a little easier to solve every time. By doing this multiple of times
until we reach the simplest problem that we have an answer for, the original
problem can be solved by working our way backward through a series of calls.
(This is why we start tracing from the simplest problem, then go up with depth 2,
3, 4…) In this sense, the base/general case plays a crucial part in recursion,
since it identifies the order 1 case and stops recursion when necessary.
Without the base case, recursion will be an infinite loop that has no meaning.
To summarize everything, there are three
rules that programmers need to follow when implementing a recursive solution
(I visited one of my classmates' slog and found a useful website, these rules are taken from that website, the full article is here, the slog that I steal this from is here)
1.
A recursive algorithm must have
a base case
2.
A recursive algorithm must
change its state and move toward the base case
3.
A recursive algorithm calls
itself, recursively
Tracing recursion is not hard to understand
if you follow the process I did above. Once you do the tracing for a couple of
different examples by yourself, you will understand recursion more thoroughly
(like me after lab4). Recursion is such a powerful tool that can solve a really
complicated problem by repeatedly solving a simple version of the problem. It
is by far the most elegant solution I have ever seen in computer science, and
I am excited to discover more about it. Below are images created by
recursion. If you are interested in this stunning concept, start tracing
today!!






No comments:
Post a Comment