Sunday, 1 March 2015

SLOG: Week 7 Recursion Summary

After doing so many labs and exercises on recursion, I think I almost at the point of mastering this technique. As a reminder, recursion is basically when a function calls upon itself which can effectively break down a seemingly big complicated problem into many simpler and smaller cases such that when we combine these cases together, we can form a solution to the problem. This is much like how we solve optimization problems in calculus where we pick out key details, form equations, take the derivative, check where the derivative is equal to zero, and then substitute our results back into the original equation. On another note, I sort of like to think of recursion as a machine where everything operates together to accomplish a task; in the case of recursion, that task would be finding a solution.

One of the fundamental aspects for recursion to be successful is the base case. The base case is what signifies the end of the recursive call and without it, the function would keep calling upon itself infinitely to no end which would certainly get us nowhere closer to the solution we desire. In addtion, quite often there are more than one base cases as shown in assignment two where the minimax function has the following base cases:

if game is over and current player has won:
    return a score of 1
elif game is over and current player has lost:
    return a score of -1
elif game is over:
    return a score of 0


The key to finding the base cases of a problem is to break the problem into the simplest form possible. These are the cases where no absolutely no recursion is required which is, as you can see, why the recursive call would end.

The next part of recursion is what to do if the conditions of the base cases are not fulfilled. This is usually where the recursive call on your function will occur. To illustrate this concept more clearly, we can use the Fibonacci Sequence. The Fibonacci Sequence is a sequence in which the first two terms are 0 and 1 and every term after that is determined by adding the previous two terms together.

For example the first 8 terms of the sequence are: 0, 1, 1, 2, 3, 5, 8, 13

We can find the n'th term in the sequence where n is the first term using recursion. Right off the bat, we can easily identify the base cases which require no recursion at all. This is the case where we are asked to identify the first or second term.



def fibonacci(n):

    if n == 0 or n == 1:
        return n



Now comes what happens if the base cases are not fulfilled. In this case it is when the term we are finding is neither the first or the second term in the sequence. We know that the n'th term is determined by adding the two terms before it and notice:


To get the third term: we add 0 + 1 = 1
In other words, fibonacci(0) + fibonacci(1) = 3rd term in the sequence

SO MAYBE..... fibonacci(2) = fibonacci(2-2) + fibonacci(2-1) and since n = 2

                      -> fibonacci(n) = fibonacci(n-2) + fibonacci(n-1)



Thus we come to the conclusion that our function to determine the n'th term in the fibonacci sequence should be:


def fibonacci(n):

    if n == 0 or n == 1:
        return n
 
    else:
        return fibonacci(n-2) + fibonacci(n-1)



Let's test our function:

For the 5th term: (n=4)

None of the base cases are fulfilled so we call fibonacci(2) + fibonacci(3)

fibonacci(2) does not fulfil base cases so it calls fibonacci(0) and fibonacci(1)
These two fulfill the base cases and return 0 + 1 which gives fibonacci(2) a value of 1

Next, fibonacci(3) does not fulfil base cases so it calls fibonacci(1) and fibonacci(2)
fibonacci(1) fulfills the base case and gets a value of 1 and we already know fibonacci(2) gives a value of 1. Thus fibonacci(3) has a value of 2

Thus the 5th term = fibonacci(2) + fibonacci(3) = 1 + 2 = 3

This is indeed the 5th term in the sequence which proves that our function is right!


As illustrated, recursion can be a very powerful tool for solving problems. Had we been finding the 9000'th term in the sequence instead of just the 5th, it would most likely tire our brains out whereas the computer could compute it in seconds.

In Summary:

The key to recursion is to.....

1) Find the base cases in which absolutely no recursion is needed and a value can be returned immediately. This should be the condition of your "if" statement.

2) Find what you need to do to make your function work towards the base cases when it is called upon itself, as demonstrated by the Fibonacci sequence

3) Check to see whether your recursive call is not missing anything!

No comments:

Post a Comment