Sunday, March 2, 2014

Recursion -- Exciting but Complicated!

    Recursion is the process of breaking a problem into similar smaller components. In computer science, a recursive function is any function which calls itself within its body, therefore making a loop. A real world world example of recursion is the nested images formed when two mirrors are parallel to each other; in this case, we have an infinite recursion. When defining a recursive function, we must define a base case in order to prevent an infinite recursion. This base case does not lead to a recursive call; without it, the function will run infinitely and a "RuntimeError" will occur.

    A really good example of recursion is the Fibonacci sequence. In this sequence, every number is defined by the sum of the two previous numbers. [n = (n-1) + (n-2)]. in this way, each number can be broken down into its two previous numbers; those two numbers can be broken down into their two previous components and so on, until the first two numbers (0 & 1) are encountered. In the case of Fibonacci numbers, the two first numbers of the sequence are the base case and need to be known.

    Trees also tend to be recursive. A Tree is made of a root and one or more branches. Every branch of a Tree can recursively be broken down into its components -- root and branches. Therefore, in a recursive function, a Tree is defined as a root plus its branches, which are themselves Trees.

    I find recursion one of the most useful and interesting concepts in computer science and mathematics. It could be difficult when first working with recursion to think of how it works, but with time, it will get easier!