Introduction To Memoization

From a previous post on recursion we see that sometimes the sub problems of a recursive solution are related or overlapping in certain ways, the Fibonacci number sequence has sub-problems which overlap. From the recursion tree we notice that most calculations repeat, which causes inefficiency in the algorithm.

Recursion Tree:
recursion tree

The solution for Fibonacci 4, 3, 2, 1 and 0 is repeated on the left and right of the recursion tree, which makes the algorithm very inefficiently. What if there was a way we could store values for previously calculated Fibonacci numbers so that in the event that we need those solutions we could find them O(1) without doing any calculations.

In computingmemoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously processed inputs. Memoization has also been used in other contexts (and for purposes other than speed gains), such as in simple mutually recursive descent parsing[1] in a general top-down parsingalgorithm[2][3] that accommodates ambiguity and left recursion in polynomial time and space. Although related to caching, memoization refers to a specific case of this optimization, distinguishing it from forms of caching such as buffering or page replacement. In the context of some logic programming languages, memoization is also known as tabling;[4] see also lookup table more…

Original Algorithm:

def fib(num):
    if num < 2:return num     if num > 0 :return fib(num-1) + fib(num-2)

Memoized Version:

cache = {}
def fib(num):
    if num in cache:
        return cache[num]
        cache[num] = num if num < 2 else fib(num-1) + fib(num-2)
        return cache[num]

Pruned Recursion Tree:
The red lines on the above diagram shows the pruning on the recursion tree caused by the memoization of the algorithm. The algorithm is now much more efficient because it no longer needs to traverse the recursion tree for a value which has already been calculated. Until next time have fun 🙂



Recursion in Computer Science

Recursion, though important is frequently overlooked by programmers who do not understand its potential and place in algorithm design. It allows the expression of some problems in a very elegant and succinct manner, problems such as the famous Fibonacci number sequence are better understood and written with the aid of recursion. We will go through a few algorithms that employ It in an attempt to explain its function in a clear and concise manner.

I will be using python in this post because its a scripting language and therefore there is no need to recompile while testing my code. If you do not have python on you machine you can get it here python;  python IDE .

“A picture is worth a thousand words” is certainly true for this photo, in order for recursion to work there has to be at least one base case. The algorithm can have multiple base cases each of which is responsible for a particular state in which the algorithm may be in at each step. So lets look at our first example of a simple recursive function written in python.

Counting backwards from 10 to 0

def countDescending(num):
     print num
     if num == 0: return
     countDescending(num - 1)


  1. The countDescending accepts a number as input then prints that number
  2. Base case: When num = 0 return from the function.
  3. Recursive step:The function calls itself with the input being reduced by 1

NB* All recursive solutions the initial problem must be one that can be broken down into simpler  problems, which eventually leads to the base case. Also to note, each function  call along  with scoped non-static variables are held on the stack until the base case is reached at which point the stack is unwound and the resources released.

Fibonacci Number

def fib(num):
    if num < 2:return num
    if num > 0 :return fib(num-1) + fib(num-2)


Fibonacci Number sequence: 0, 1, 1, 2, 3, 5, 8
Recursive formula: \displaystyle f_{n} = \sum_{n=1}^{8} (n-1)+(n-2)
Base case: If input is less than 2 return the original input.
Recursive step: If input greater than 0 call fib with number – 1 and number – 2
Recursion Tree:

A recursion tree allows us to gain insight into the workings of a recursive algorithm, the above diagram shows the recursion tree for the Fibonacci number  8. From it we note that there is lot of repetition in calculation, this could be avoided with the use momoization which we will discuss in a future article.