Saturday, 31 January 2015

Week 4: 'Re-re-re-re-...-cursion'

During the fourth week of lectures, we delved into the perennially dreaded topic of recursion (at least according to most people I know that have ever taken a computer science course). In essence, recursion works by defining something in terms of itself (this may occur multiple times until it is finally reduced to a base case for which a certain value is assigned). Recursion, then, can be distilled to two key elements: a base case for which the recursion 'ends' and an iterative case whereby the function passes a modified version of the argument through itself.

The most conspicuous examples of recursion occur in math, in both the Fibonacci sequence and the use of the factorial. In the case of the Fibonacci sequence, our 'base case' is simply the two numbers that start the sequence, which in this case are a_1 = 1 and a_2 = 1. From here, each successive number is derived by summing the previous two numbers in the sequence. As such, it is easy to see that if you wanted the 4th number in the sequence, you would sum a_1 and a_2 to get a_3 and then sum a_2 and a_3 to get a_4 (in this case, 1 + 1 = 2 = a_3 and 1 + 2 = 3 = a_4). With regards to the factorial, the following code explains recursion quite succinctly.

def factorial(n):
        if n == 0 or n == 1:
            return 1
        else:
            result = factorial(n-1) * n
            return result

As you can see, the base case occurs for n = 0, 1 which is defined to return 1 (since 0! = 1! = 1) and for all other cases, the function simply calls itself, but on a reduced argument (factorial(n-1) instead of factorial(n)) until n = 0 or n = 1 is reached, at which point the function terminates.

Although many students tend to find recursion a difficult concept to grasp, in reality, our brains are hard-wired to understand recursive thinking. For example, when you say someone is the 'great-great-great-great grandfather of person X', you are speaking recursively. The 'base case' occurs for the word 'grandfather' [0] which we all understand as 'the father of someone's father/mother'. Each successive addition of 'great' triggers a recursive call in which we simply apply 'father of' to our base case 'grandfather'. So, 'great-grandfather' [1] is the 'father of [0]'  and 'great-great-grandfather' [2] is the 'father of [1]', etc. So while we may find recursion difficult in a computer science sense, it really is a fundamental part of our everyday language that we rarely question or fail to understand.

As one last example of recursion in speech, we often define people/things in terms of other parameters. These parameters in turn, can be defined recursively as well in terms of other parameters. Although this type of recursion is not as clear as the example above, it serves to illustrate the point that our brains intuitively understand recursive thought. For example, take the following sentence:

'I went to see my mother today, whose car was parked next to a building, that had been recently bought by a millionaire, who owns three dogs.'

While it is a sentence few of us would ever be inclined to say, it's meaning is quite clear. The 'mother' is defined by the 'car next to a building' and the building, in turn, is defined by the face that it 'had been recently bought by a millionaire', and the millionaire, in turn, is defined by the fact that he/she 'owns three dogs'. So while this example is a tad convoluted at best, it elucidates the core principle of recursion as defining something in terms of something else.

Overall, this week's lessons in recursion were enjoyable as well as the lab which I found extremely helpful. Hopefully, recursion will become second nature by the time I am done with CSC148 since it is an extremely important and above all powerful tool in the arsenal of computer scientists.

PS: Hopefully the meaning of the title 'Re-re-re-re-...-cursion' is a bit more clear now.




Wednesday, 21 January 2015

Why Geeks Should Write

To be completely honest, one of the many 'perks' that pushed me into the direction of math and computer science was the fact that I would no longer have to 'write' (essays, reports, etc). However, I feel that 'writing' has been unduly monopolized in our everyday vernacular to represent a tedious, boring, and often fruitless task when it has the potential to be the complete opposite. Even for so-called 'geeks' who prefer to communicate using code, numbers, and more 'concrete' methods, writing is a process through which we break down, deconstruct, and analyze our thoughts. In so doing, we are not only distilling the crucial elements of new and often abstract ideas, but we are also making these concepts clearer to ourselves by connecting them with other ideas and experiences from our daily lives.

For example, in learning about vector spaces, the topics of 'linear independence' and 'spanning' were fundamental in understanding how to construct a basis for any vector space. At first, it was difficult to fully grasp the concept of a basis and how a finite number of vectors could represent an infinite number of items. Upon further reflection, the vectors of a basis seemed awfully similar to the blanks on any piece of identification. If we consider all humans as a vector space, each of us can be characterized by relatively few things--namely, our basis 'vectors' could be our age, height, nationality, gender, sexuality, income, location, etc. And in a broader sense, these few tidbits of information reveal a great deal about who we are, what our interests are, the people with whom we associate, etc. In addition, there is ample research that shows just how successful major companies are in marketing and advertising because they know the 'coefficients' of our particular basis 'vectors'.
By reflecting on what I had learned in class and connecting those fundamental ideas with structures in the 'real' world, I deepened not just my understanding of linear algebra but also my appreciation for it.

As such, I firmly believe that 'writing' in its most purest sense--reflection of concepts learned--can also greatly help 'geeks' to be able to both relate abstract concepts to the real world and to deepen their understanding of them. It is also important for 'geeks' to be able to relate their concepts to the real world since even the most abstract of ideas in computer science derive from problems in the real world (for example, data structures like stacks and queues and the principle of recursion are fundamental in computer science precisely because they are fundamental to how our own world works and functions). In conclusion, I challenge all self-proclaimed 'geeks' (myself included) to write and reflect about their experiences in their respective disciplines, not solely for the purpose of exercising the muscles of their hands but to exercise the muscles of their minds, in order to better connect the abstract with the practical and create the change and progress that we so desperately desire.