Remember to Use Memoization!

Seminar at Rose-Hulman

Clément Aubert



Memoization is an optimization technique that trades (a bit of) space for (a lot of) time. It comes in multiple forms, and you may already use it without knowing it. This technique cannot always be applied, but when properly used, it can speedup the running time from exponential to linear!

We’ll motivate the introduction of memoization with some examples, introduces to its origins, and mention the limitations of this technique. Although not necessary to understand this lecture, some preliminary material, as well as exercises, will be posted Saturday 01/21 at

When and Where?


Monday, January 23, 2017, 4:20 PM – 5:10 PM


Room O257, Olin Hall, Rose-Hulman institute, 5500 Wabash Avenue Terre Haute, IN .

Preliminary Material

Motivational Example

Assume your colleague/classmate Bertrand asks you to review his (python) code:

def f(n):
    # Assume some clever code here

print f(35)

# Some other code
# And then, after a while,

print f(35), "is a nice number"

You feel like Bertrand is uselessly computing f(35) twice, and decide to “improve” his code by doing the following:

# You leave his function f unchanged,
def f(n):
    # Assume some clever code here

# But you store the value of f(35) into a variable:
x = f(35)

# And then use that variable:
print x

# Some other code, unchanged

print x, "is a nice number"

You send back your “optimized code” to Bertrand, but Bertrand starts complaining that you broke his program.

  1. Is Bertrand right, could it be that you broke his program?
  2. Give two examples of definition of the function f where your optimization would be correct.
  3. Can you give three examples of definition of the function f where you optimization wouldn’t be correct?
  4. What are the condition on f and on the system for this transformation to be indeed an optimization?


Automata Theory

To study memoization, we’ll have a peek at the context in which it was created: automata theory. It is not needed to have an extensive knowledge of this topic to understand the lecture, but the following should help you get some familiarities with the main concepts.

Definition of Automata

Generally speaking, an automaton (a.k.a. “finite automaton”, or “finite state machine”) is

Intuitively, the automaton is given an input word n ⊆ Σn, and then it reads the first character of n and applies its transition function: according to its initial state si, it changes its state, and move to the next character. The transition function is then applied until it is not defined for the couple (current character, current state). When there is no more character to read in n, or when the transition function can’t be applied, the automaton halts. If the automaton stops in the state sa, then we say that the automaton accepts the word n, otherwise we say that it rejects the word n.

There are plenty of variations on automaton. We are going to study two of them:

  1. The addition of the capacity to move back and forth on the input word,
  2. The addition of a “pushdown stack”.


  1. Try to write the definition of a “Two-way finite automata”, i.e., of an automaton capable of moving back-and-forth on the input word. Hint: in a first approximation, you only need to change the co-domain of the transition function, and the definition of halting.
  2. You want to prevent your automaton from “falling out of the input”, i.e., to read an instruction “go fetch the previous character” when you’re already at the first character. How to do that? Hint: suppose the input word is surrounded by “end markers”, that tell the automaton that it reaches the beginning or end of the word, and impose a condition on the transition function.
  3. Now, you supposedly have the correct definition of a “Two-way finite automaton”. Compare your definition with the one on Wikipedia.

Code Used During the Lecture

All the code showed during the lecture was in Python. You can download it, or play on-line with it at rextester or at tutorialpoint (please fork it before editing it).

To Go Further

How to Use Memoization with your Favorite Language?

Numerous resources are dedicated to this question, cf. for instance the ~700 questions tagged “memoization” on If you are interested in using memoization for a particular language, you may be interested in the following resources:

Memoization is virtually available for every programming language. If you feel like I forgot your favorite language, if you find a better technique that the ones listed above, feel free to send a line! If you have an account on Codewars, you can also browse the memoization tag to get some hand-on experience. You can also browse the memoization tag on codereview to get a practical sense of when is this method used.

Research Papers

As a warm-up, to understand how memoization is used to prove that the halting problem for Pushdown Automata (called PDA) is decidable in polynomial time, this answer is probably well-suited. It is clear and compact, but assume some knowledge of automata and languages.