Friday, 4 March 2016

Week 7: Recursion

Last week we also started looking at recursion, so here’s my breakdown of the topic:

Recursion
     Recursion is when a function calls itself. What does that mean? It means that when the function is defined, the function itself is in that definition. In programming this means that in the body of a function, a call is made to that function. For instance, if you defined a function f that summed up the numbers 1 to n where n is an argument of f, then you could implement this in python through a loop or through recursion. Lets look at each of these cases in python.

Loop
f(n):
     sum = 0
     for i in range(n + 1):
          sum += i 
     # equivalent to sum(range(n))
     return sum

Let’s trace the function in the case n = 3
sum = 0 
sum += 1 -> sum = 0 + 1 = 1 
sum += 2 -> sum = 1 + 2 = 3
sum += 3 -> sum = 3 + 3 = 6
Therefore f(3) = 6

Now let’s look at the recursive implementation of this function:

Recursion
f(n):
     if(n > 0)
          return(n + f(n - 1))
     else:
          return(n)

Once again, let’s trace the function in the case n = 3
f(3): 3 > 0 -> 3 + f(2)
f(2): 2 > 0 -> 2 + f(1)
f(1): 1 > 0 -> 1 + f(0)
f(0): 0 = 0 -> 0
Therefore f(3) = 3 + (2 + (1 + 0))) = 6

     As you can see, they do the same thing! As a general rule anything that can be done through recursion can be done with a loop as well.

Let’s break down what a recursive function has:
Recursive Case: Obviously, a recursive function has a recursive call to itself. This can’t be the only part of a recursive function though! Most recursive functions will call themselves forever until python gives up. For this reason we need a base case, where the function is not called again.
Base Case: This case is essentially the stopping point of the function. In the above example the base case very clearly defined by the if; else statements, where the n > 0 is the recursive case, and the else is the base case.
     These two building blocks can be played around with and changed (multiple of each, etc.), but without both of them a function isn’t recursive.

At this point the most prominent question is probably "Why would I ever use recursion if a loop can accomplish the same thing?"
     The answer is that sometimes it’s way easier to use loops and sometimes is way easier to use recursion. The above example is harder to understand using recursion than a loop, and is just as long, so it’s a case where a loop is superior. But say you were give a graph and two nodes, and were asked whether  their was a path between those node. It is theoretically possible to come up with a function that uses while loops to answer the question, but it would be very difficult to understand. With a recursive function the process becomes much easier: just check whether the current is the final node and if not, then call the function on all the nodes the current node leads to.

Recursion is a very powerful tool, and it's fun to play with, so I'm glad were finally learning it!

No comments:

Post a Comment