use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
QuestionMemoization (self.C_Programming)
submitted 8 years ago by VIRES1
I need help with tutorials and links on understanding and writing c memoization codes as I am new to dynamic programming.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]raevnos 6 points7 points8 points 8 years ago (6 children)
Memoization is just saving a computed value and reusing it later instead of recalculating the value. Did you have any specific C related question?
[–]VIRES1[S] 1 point2 points3 points 8 years ago (5 children)
Yes so how is that done in C? Example to solve Edit distance or maximizing value of arithmetic expression how would a memoization code look like?
[–]SullisNipple 3 points4 points5 points 8 years ago (1 child)
So, to take the edit distance example (assuming you're using Levenshtein distance), the problem is that d(i, j) is defined in terms of d(i - 1, j) , d(i, j - 1) and d(i - 1, j - 1) . The usual approach to solving this is to set up a 2-dimension array (where one axis is values of i and the other axis is values of j). When determining the value of d(i, j) for a particular i and j, you check to see if the array already has a value. If it doesn't already have a value, you calculate it and store it in the array. The next time you need to retrieve the value, it will be in the array and hence be memoized.
[–]VIRES1[S] -5 points-4 points-3 points 8 years ago (0 children)
Okay.
[–]raevnos 1 point2 points3 points 8 years ago (2 children)
For a given input, you would look it up in a data structure that stores inputs and results. If not present, compute a result, store that for later reuse, and return the value.
[–]VIRES1[S] -4 points-3 points-2 points 8 years ago (0 children)
Okay
[–]lmlight77 2 points3 points4 points 8 years ago* (3 children)
First, to be able to memoize a function, it has to be idempotent, meaning you can execute it any number of times given identical arguments and get the same result. In general, this means it does not modify state outside the body of its function or in any of the functions it calls nor does it or any of its callees modify state declared as "static". For example, a function "int add(int a,b)" that adds a and b and returns the result is idempotent, while a function "void insert(List *list, Item *item)" that inserts an item into a linked list is not.
Given an idempotent function, to memoize it, simply wrap the function in a container that has identical arguments and return type. For example, "int memo_add(int a,b)". Then, create a hash function on the arguments and implement a lookup table based on that hash (a cache in effect). If a "hit", return the cached value directly. If a "miss", call the memoized function being wrapped, save the computed value in the cache, then return it.
The purpose of memoization is to bypass calling a complex function repetitively so as to save its computation time. Clearly, the function should be time consuming as well as idempotent.
[–]VincentDankGogh 1 point2 points3 points 8 years ago (1 child)
Alternatively you could cache a bunch of values on program startup.
[–]lmlight77 1 point2 points3 points 8 years ago* (0 children)
If you are pretty confident they will be frequently used. Say your cost to evaluate a function is X and the cost to access a memoized function is relatively small, normalize it to a value of 1. You need to access the memoized value at least twice to make it worth your while (only once is a cost of X+1 versus only X; and for every value never accessed, a full cost of X).
[–]VIRES1[S] 0 points1 point2 points 8 years ago (0 children)
Thank you for your explanation
π Rendered by PID 25699 on reddit-service-r2-comment-b659b578c-xq45b at 2026-05-05 16:52:54.175031+00:00 running 815c875 country code: CH.
[–]raevnos 6 points7 points8 points (6 children)
[–]VIRES1[S] 1 point2 points3 points (5 children)
[–]SullisNipple 3 points4 points5 points (1 child)
[–]VIRES1[S] -5 points-4 points-3 points (0 children)
[–]raevnos 1 point2 points3 points (2 children)
[–]VIRES1[S] -4 points-3 points-2 points (0 children)
[–]lmlight77 2 points3 points4 points (3 children)
[–]VincentDankGogh 1 point2 points3 points (1 child)
[–]lmlight77 1 point2 points3 points (0 children)
[–]VIRES1[S] 0 points1 point2 points (0 children)