Questions tagged [memoization]

A technique in which function return values are saved so they do not need to be recalculated if the function is called again with the same arguments.

40 questions
17
votes
4 answers

Dynamic Programming vs Memoization

I am having trouble to understand dynamic programming. Mainly because of its name. As far as I understand, it's just another name of memoization or any tricks utilizing memoization. Am I understanding correctly? Or is DP something else?
Eonil
  • 291
  • 1
  • 2
  • 8
14
votes
3 answers

Memoization without array

In Cormen et al.'s Introduction to algorithms, section 15.3 Elements of dynamic programming explains memoization as follow: A memoized recursive algorithm maintains an entry in a table for the solution to each subproblem. Each table entry initially…
4
votes
2 answers

Algorithm to find "truncatable words"?

My son who is just learning to read loves the concept of hiding one letter of a word with his finger and asking what the rest of the word spells. His favorite is turning "her" into "he" and "then" into "the." As he's starting to learn bigger words I…
user112052
  • 41
  • 2
3
votes
1 answer

What's the runtime complexity of this algorithm for breaking up string into words?

I am given a input string $s$ ("bedbathandbeyond") and a set of words {"bed", "bath", "beyond", "bat", "hand", "and"}. I need to divide the input string $s$ into a series of words in the dictionary. In this case, the two allowed outputs would be…
3
votes
1 answer

How to convert a recursive function to a non recursive one using stack while keeping memoization?

Let's say I want to count the number of ways a string can be decoded, once encoding algorithm follows this map: 'a'=>'1', 'b'=>'2', ... 'z'=>'26'. I could simply count it using a recursive function as follows: def num_ways(s: str) -> int: if…
2
votes
0 answers

Find worst-time complexity for a problem on matching a simple regular expression?

I am not looking for the complexity of following algorithm but rather how to think about the problem and calculate complexity of a given solution? One way is to perhaps use The Master Method for recursive functions, but that hides the "how?" part. I…
2
votes
1 answer

Control of the combinatorial aspects of a dynamic programming solution

I am exploring how a Dynamic Programming design approach relates to the underlying combinatorial properties of problems. For this, I am looking at the canonical instance of the coin exchange problem: Let S = [d_1, d_2, ..., d_m] and n > 0 be a…
2
votes
1 answer

Find the $k$-th lexicographically smallest hamiltonian circuit

Let's say we have given unweighted directed graph with $N$ nodes and $M$ edges, and we want to find the $K$-th hamiltonian circuit, ordered in lexicographical order. For example, if we have complete graph with $4$ nodes, and $12$ edges. The smallest…
2
votes
2 answers

With Memoization Are Time Complexity & Space Complexity Always the Same?

I am studying Dynamic Programming using both iterative and recursive functions. With recursion, the trick of using Memoization the cache results will often dramatically improve the time complexity of the problem. When evaluating the space…
2
votes
2 answers

Can I use the set of "used arguments values" as a memoization key for a deterministic function?

I have a deterministic function $f(x_1, x_2, ..., x_n)$ that takes $n$ arguments. Given a set of arguments $X = (x_i)$, I can compute $U_X = \{ i \in [1, n] : x_i \text{ was read during the evaluation of } f(X) \}$ Would it be valid to use the set…
Jean Hominal
  • 123
  • 3
2
votes
3 answers

Why would a function be tabulated in advance and then retrieved?

Given some function and assuming no concern for the time to compute the co-domain for its domain, when might it be preferable to compute and tabulate in advance the co-domain results of the function, and then retrieve it from the table rather than…
gallygator
  • 23
  • 2
2
votes
2 answers

Zero sum game: dp recursion strategy

Trying to solve the zero-sum problem described here, where two opponent players at each turn can choose to collect 1, 2 or 3 stones with different values, with the objective of getting more points at the end of the game (and assuming each player is…
2
votes
1 answer

How do I calculate the time complexity of this memoized algorithm?

The problem is: count all increasing subsequence of s. def main(): print(count([3,2,4,5,4])) def count(s: list) -> int: n = len(s) counter = 0 result = {} if n == 0: return 0 for i in range(n): counter +=…
2
votes
2 answers

Runtime of weighted interval scheduling dynamic programming algorithm

Consider this implementation of a dynamic programming algorithm for weighted interval scheduling: M-Compute-Opt(j) If j=0 then Return 0 Else if M[j] is not empty then Return M[j] Else Define M[j] = max(v_j +…
kanso37
  • 445
  • 2
  • 14
2
votes
1 answer

Can memoization be applied to any recursive algorithm?

I am new to the concepts of recursion, backtracking and dynamic programming. I am having a hard time understanding if at all I can apply memoization to a particular recursive algorithm and if there is a relation between memoization being…
Spindoctor
  • 131
  • 2
1
2 3