Memoization

You know when you jot down some phone numbers in the little memo near your phone? Well, not sure how many people still do that, but whenever they do it they are doing what Computer Scientists call Memoization: the process of annotating data in a memo for quick reference in the future.

Memoization is the technique primarily used in a field of Algorithms called "Dynamic Programming", commonly known as DP.

DP is hard to grasp. At least for me. It is a different paradigm compared to Recursion, and similarly to recursion, you need to solve hundreds of problems before getting any good at DP.

I'm not good at DP at all.

But I do want to exemplify a simple use of memoization, using a generalization of the second most famous DP problem (the first most famous being Factorial): Fibonacci.

As you may recall, Fibonacci is a simple recursive formula that describes the growth in population for a certain species of rats. It is defined as follows:

Fib(n) = n (for n<=1)
Fib(n) = Fib(n-2) + Fib(n-1) (otherwise)

A generalization of Fibonacci requires us to pass to the function a second parameter, which indicates the number of immediate predecessors terms to add for the calculation of Fib(n). We're going to call that second parameter T (for "Terms to be added"). The generalized Fibonacci function then becomes:

FibGeneralized(n,T) = n (for n<T)
FibGeneralized(n,T) = FibGeneralized(n-T) + FibGeneralized(n-T-1) + ... + FibGeneralized(n-1) (otherwise)

Hence we can see the Fib(n) = FibGeneralized(n,2), which means that Fibonacci requires as to sum the last two immediate predecessor terms.

Now let's suppose that our goal is to find the following (big) number:

FibGeneralized(100, 10) = ?

Hence we want the 100th term in the generalized Fibonacci function where we always add the last ten terms.

If we don't use memoization and simply try the code up above, we can quickly write it:

static BigInteger SumLastN(int index, int n)
{
if (index < n) return index;
BigInteger sum = 0;
for (int i = n; i >= 1; i--)
{
sum += SumLastN(index - i, n);
}
return sum;
}

And then all we have to do is call SumLastN(100, 10). However, this is an incredibly slow way of doing it since there are multiple repeating computations: F(90, 10), for instance will eventually calculate F(10,10). But F(89, 10) will also eventually pass thru F(10,10). And so on so forth. We end up wasting a ton of time re-computing the same values. I actually took a short video of this code running, check it out:


With some patience, it eventually prints out SumLastN(38, 10). But it won't get anywhere close to our goal of SumLastN(100, 10).

Memoization comes handy here. The insight is the following: anytime that you want to compute SumLastN(p, T), do this:

1) First, check whether you already have that value in your memo - in our case the memo will be a simple hash-table
2) If it is not there, sure, calculate it as before, but right after calculating it add it back to your memo (hash-table)

That should do it. Take a look at the modified code below:

static BigInteger SumLastNMemoization(int index, int n, Hashtable htMemo)
{
if (index < n) return index;
if (htMemo.ContainsKey(index)) return (BigInteger)htMemo[index];

BigInteger sum = 0;
for (int i = n; i >= 1; i--)
{
sum += GetAndSetMemo(index - i, n, htMemo, -1);
}

return GetAndSetMemo(index, n, htMemo, sum);
}

static BigInteger GetAndSetMemo(int index, int n, Hashtable htMemo, BigInteger value)
{
if (!htMemo.ContainsKey(index))
{
if (value != -1)
{
htMemo.Add(index, value);
}
else
{
BigInteger result = SumLastNMemoization(index, n, htMemo);
if (!htMemo.ContainsKey(index))
{
htMemo.Add(index, result);
}
}
}
return (BigInteger)htMemo[index];
}

Here is the video showing that one in action (sorry for the background noise and bad video quality):


And there you have the answer we were looking for, in a blink of an eye:

FibGeneralized(100, 10) = 52383321763167120645301410147

Cheers,
Marcelo

Comments

  1. Thanks for your explanation. It really helped me understand the concept better.

    Reply Delete
  2. Thanks for the write-up Marcelo! Since my brain is too small for DP, I almost always go from recurrent relation to recursive solution, then add @lru_cache decorator (if it's python) or explicit dictionary or array (if cache is dense and has limited key range) to cache the results and, only if it's not enough, use the heavy artillery - bottom up solution. Sometimes implementation using memoization is actually faster than bottom-up alternative, since a lot of subproblems don't have to be solved at all, but bottom-up algorithm will have to calculate them anyways. At the same time if algorithm is such that most or all subproblems have to be solved, then bottom-up algorithm is faster due to lower overhead, which is especially noticeable on the micro-level, since there are less wrong CPU branch predictions.

    Reply Delete

Post a Comment

[フレーム]

Popular posts from this blog

Quasi FSM (Finite State Machine) problem + Vibe

Not really an FSM problem since the state isn't changing, it is just defined by the current input. Simply following the instructions should do it. Using VSCode IDE you can also engage the help of Cline or Copilot for a combo of coding and vibe coding, see below screenshot. Cheers, ACC. Process String with Special Operations I - LeetCode You are given a string  s  consisting of lowercase English letters and the special characters:  * ,  # , and  % . Build a new string  result  by processing  s  according to the following rules from left to right: If the letter is a  lowercase  English letter append it to  result . A  '*'   removes  the last character from  result , if it exists. A  '#'   duplicates  the current  result  and  appends  it to itself. A  '%'   reverses  the current  result . Return the final string  result  after processing all char...

Shortest Bridge – A BFS Story (with a Twist)

Here's another one from the Google 30 Days challenge on LeetCode — 934. Shortest Bridge . The goal? Given a 2D binary grid where two islands (groups of 1s) are separated by water (0s), flip the fewest number of 0s to 1s to connect them. Easy to describe. Sneaky to implement well. 🧭 My Approach My solution follows a two-phase Breadth-First Search (BFS) strategy: Find and mark one island : I start by scanning the grid until I find the first 1 , then use BFS to mark all connected land cells as 2 . I store their positions for later use. Bridge-building BFS : For each cell in the marked island, I run a BFS looking for the second island. Each BFS stops as soon as it hits a cell with value 1 . The minimum distance across all these searches gives the shortest bridge. 🔍 Code Snippet Here's the core logic simplified: public int ShortestBridge(int[][] grid) { // 1. Mark one island as '2' and gather its coordinates List<int> island = FindAndMark...

Classic Dynamic Programming IX

A bit of vibe code together with OpenAI O3. I asked O3 to just generate the sieve due to laziness. Sieve is used to calculate the first M primes (when I was using Miller-Rabin, was giving me TLE). The DP follows from that in a straightforward way: calculate the numbers from i..n-1, then n follows by calculating the min over all M primes. Notice that I made use of Goldbach's Conjecture as a way to optimize the code too. Goldbach's Conjecture estates that any even number greater than 2 is the sum of 2 primes. The conjecture is applied in the highlighted line. Cheers, ACC. PS: the prompt for the sieve was the following, again using Open AI O3 Advanced Reasoning: " give me a sieve to find the first M prime numbers in C#. The code should produce a List<int> with the first M primes " Minimum Number of Primes to Sum to Target - LeetCode You are given two integers  n  and  m . You have to select a multiset of  prime numbers  from the  first   m  pri...