Assigned topic: Recursion.
At this point, i have had my first exposure to recursion, a powerful technique in computer programming.
According to wikipedia,
"Recursion
in computer science is a method where the solution to a problem depends
on solutions to smaller instances of the same problem (as opposed to
iteration)."
Simply put, recursion technique is seen when the
function being run calls itself within its own code (because the
function calls itself to start its work again on the given input).
The following code illustrates an example of recursion (stolen from week 4 lecture slides):
def rec_max(L):
return max([rec_max(x) if isinstance(x, list) else x for x in L])
This function sorts through a given list input and returns the greatest value within the list.
The
most important component of any recursive call is its "base case." This
is the condition in which the function has to reach so that it
terminates and stops running. Otherwise, python will return a "recursion
exceeds maximum depth" error.
The closest analogy that comes to
my mind is the concept of a "while loop," because it is also an
iteration technique that keeps running and running until it reaches a
condition in which it stops. Likewise, the while loop faces the same
problem of an "infinite loop" if its exit condition is not well defined.
So, it begs the question, what is the point of recursion when there already exists a similar technique that does the same job?
Refer to the rec_max(L) function above.
If
the given input is a list of [1, 2, 3, 4, 5], the function would return
5. A very simple test case which either the for loop or while loop can
easily accomplish.
However, what if the given input is something like: [1, [2, 3, 4], 5]?
A
for loop, for example, would iterate through: 1, the inner list, then
5, and return 5, with no regard for the content of the inner list - for
its assigned objective does not directly deal with such a disposition.
Even
if you were to install a command to the effect of "if you encounter
another list, start the iteration again," it is still not going to
account for an input which has 99 layers (an exaggerated point) of
nested lists.
This is where recursion comes in.
the 1 line
code above in english means "if you encounter another list, start the
iteration again." The code re-executes itself given the condition is
met. In the example of the nested list, the only (known) way to dealing
with an ADT that contains layers and layers of variables is to start the
count again, as if you were to do it manually.
This is where recursion becomes indispensable: when the input ADT consists of entries that are not of the same format.
Recursion,
at its base definition, is a form of loop. A loop is a command to the
computer to execute code over and over again until the condition is met.
However, recursion allows itself to start the iteration again if it
becomes necessary. But, in general, a recursion should always be used
when a big problem needs to be broken down into small problems and
solved step by step. ex. decomposing a big list into smaller lists and
iterating through each item to return the desired entry.
In brief,
recursion is a powerful tool in computer science that allows the
computer to solve complicated problems by breaking it down into smaller
ones via. starting the iteration again - very useful when the given
input ADT is not of a consistent format.
No comments:
Post a Comment