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 at http://lacl.fr/~caubert/LR/.
Friday, February 10, 2017, 2:30 PM – 3:30 PM
Rhyne 161, Lenoir-Rhyne University, 625 7th Ave NE, Hickory, NC 28601.
Assume your colleague/classmate Bertrand asks you to review his (java
) code:
public class Example{
public static int f(int n){
int x = 0;
// Assume some code here, possibly changing the value of x
return x;
}
public static void main(String[] args){
System.out.println(f(35));
// Some other code.
// And then, after a while
System.out.println(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:
public class Example{
// You leave his function f unchanged,
public static int f(int n){
int x = 0;
// Assume some code here, possibly changing the value of x
return x;
}
public static void main(String[] args){
// But you store the value of f(35) into a variable:
int x = f(35);
// And then use that variable:
System.out.println(x);
// Some other code, unchaged
System.out.println(x + "is a nice number");
}
}
You send back your “optimized code” to Bertrand, but Bertrand starts complaining that you broke his program.
f
where your optimization would be correct.f
where you optimization wouldn’t be correct?f
and on the system for this transformation to be indeed an optimization?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.
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:
All the code showed during the lecture was in Java
. You can download it, or play on-line with it at rextester or at tutorialpoint (please fork it before editing it).
Numerous resources are dedicated to this question, cf. for instance the ~700 questions tagged “memoization” on stackoverflow.com. If you are interested in using memoization for a particular language, you may be interested in the following resources:
C++
, memoization is one of the common technique to optimize your program.C#
, this article will give you the main keys to memoize functions with multiple arguments.Haskell
, the wiki has a really complete page, with numerous examples and useful references.Java
, in complement of the code I gave, you can first look at a simple implementation before considering an interesting variation.Javascript
, a very complete article, including benchmarking and comparisons of implementation techniques, should get you started.Perl
, “memoize” is already a part of the core modules, and its regex engine actually uses memoization! For an excellent explanation of this latter point, in Perl
and linking it to automaton, cf. this article.Python
, this page will provide a gentle introduction to memoization, and on how to perform it using decorators.R
, consult James Balamuta’s slides on Functions, Recursion, Benchmarking, and Memoization.Ruby
, this article will give you some hints on how to use memoization, but the bottom line is: use memoist (which relies on ActiveSupport::Memoizable, that happens to be sometimes counter-productive!)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.
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.
C++
compilators!pdf
version or the md
source of this page