In my 100 days of Fibonacci project I have now shown three idioms for implementing algorithms: Direct recursion, accumulated recursion, and an iterative approach. Today I diving into a technique for optimizing function with many repetitive calls.
Memoization can be added to an already implemented function. When used correctly is provides a radical speedup for a little extra memory consumption. This I have showcased in Java on the directly recursive Fibonacci implementation.
Day 2 - Java
Here I have implemented Fibonacci in Java using dynamic programming. The idea is to check whether the result has already been calculated. If it has so, then we return the already calculated the result, otherwise we calculate the result, save it, and return.
To keep the type signature to that of the other implementations, we build an overloaded wrapper method, which initializes a hash table, runs the recursive function and returns its result.
The code is as usual available and can be found on Github.
Fibonacci and Dynamic Programming
Today I introduced dynamic programming. We have two ways to apply dynamic programming, either by memoization or tabulation. Memoization is the process of remembering intermediate results. This is exactly what I did in the earlier example. Here we might cache some results we never use. Tabulation is the process of pre-calculating a table of intermediate results and then afterwards aggregate the result afterwards.
Dynamic programming can also be seen as a method of reducing the number of unfolds. In this example the Fibonacci implementation is called linear number of times to n. In the direct recursive method, it would have been exponentially in n.
Java, Java, Java
Java is one of the top languages used today. It is compiled to an intermediate representation, Java byte code, before it is ran on a Java virtual machine. This makes it very portable without sharing the Java source code. Furthermore there is a big body of libraries and articles written solving various problems in the language.
When having an enterprise it is certainly also a big plus that most programmers program in Java. It is easy to find people who are able to read and write Java code.