Saturday, 28 February 2015

Reconsidering Recursion - Week 7 (Mark this!)

I already wrote about my impressions on recursion, but I figured it has been given enough focus in class and labs to warrant another post. As I talked about earlier, I think that the reason recursion is initially unintuitive is because it feels like the problem you are solving is not precisely resolved by the code you write. What I mean is that, until now, the code we have written has been written to directly perform some function or solve some problem. With recursion, since the data structure being considered is typically arbitrary in nature (eg. a list with unknown depth, a tree with unknown branches and children), we are unable to know exactly what we are working with.

To deal with this, we define what we want the function to do when it reaches a base case, typically after the most simple object in the data structure has been reached (eg. working down a tree to get to a leaf, reaching the objects contained in a list of arbitrary depths). If we have not reached the base case yet, the function executes a different line of code, typically asking the function to recursively perform itself on objects within the original data structure. As long as the base case has not been reached, this code continues to execute.

Considering the approach listed above, it appears that the initially unintuitive nature of recursion is due to the fact that the solution is often simpler than the problem itself. Since the problem itself can be infinitely complex by human standards, it is typically counterproductive to consider the problem as a whole and instead think about how the problem is structured; what is the simplest case and how the problem can become more complex. Instead of directly telling the function how it is going to solve the problem, you are defining how the function will navigate the structure of the problem, since this structure can be extremely simple or infinitely convoluted.

Knowing this, the usefulness of recursion is even more apparent. It is a type of problem solving where considering the problem as a whole can be overwhelming and the structure itself is more important. You are not solving the problem per say, you are providing the function with an algorithm by which it will navigate and solve the problem. As we begin to consider slightly more complex recursive problems, like in assignment 2, I am sure an economic approach to coding will be more important than ever as increasingly large problems exponentially increase the amount of code that needs to be processed. Therefore, any attempts to make the code more efficient may lead to drastic differences in processing time as large problems are considered.

No comments:

Post a Comment