Questions tagged [dynamic-programming]

Questions about problems that can be solved by combining recursively obtained solutions of subproblems.

Dynamic programming is a technique for building efficient algorithms, in cases where a recursive algorithm might be very slow.

Before asking us to show you how to solve a dynamic programming exercise for you, it may be helpful to refer to our general guidance on strategies for constructing dynamic programming algorithms:

If you're not sure how to solve a dynamic programming exercise, tell us in the question how you've tried to apply those techniques, what you've tried so far, and what progress you've made; this helps us give you better answers.

983 questions
69
votes
3 answers

Knapsack problem -- NP-complete despite dynamic programming solution?

Knapsack problems are easily solved by dynamic programming. Dynamic programming runs in polynomial time; that is why we do it, right? I have read it is actually an NP-complete problem, though, which would mean that solving the problem in polynomial…
Strin
  • 1,515
  • 1
  • 11
  • 16
43
votes
3 answers

Deciding on Sub-Problems for Dynamic Programming

I have used the technique of dynamic programming multiple times however today a friend asked me how I go about defining my sub-problems, I realized I had no way of providing an objective formal answer. How do you formally define a sub-problem for a…
daniel gratzer
  • 1,500
  • 1
  • 13
  • 22
38
votes
2 answers

Is there a difference between top-down and bottom-up dynamic programming?

Is there a fundamental difference between top-down and bottom-up dynamic programming? In particular, is there a problem which can be solved bottom-up but not top-down? Or is the bottom-up approach just an unwinding of the recurrence in the top-down…
user695652
  • 783
  • 1
  • 5
  • 14
36
votes
4 answers

What is dynamic programming about?

Sorry in advance if this question sounds dumb... As far as I know, building an algorithm using dynamic programming works this way: express the problem as a recurrence relation; implement the recurrence relation either via memoization or via a…
hey hey
  • 463
  • 4
  • 5
20
votes
5 answers

A Case Distinction on Dynamic Programming: Example Needed!

I have been working on dynamic programming for some time. The canonical way to evaluate a dynamic programming recursion is by creating a table of all necessary values and filling it row by row. See for example Cormen, Leiserson et al: "Introduction…
Raphael
  • 73,212
  • 30
  • 182
  • 400
18
votes
6 answers

How is Dynamic programming different from Brute force

I was reading up on Dynamic Programming when I came across the following quote A dynamic programming algorithm will examine all possible ways to solve the problem and will pick the best solution. Therefore, we can roughly think of dynamic…
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
17
votes
3 answers

dynamic programming exercise on cutting strings

I have been working on the following problem from this book. A certain string-processing language offers a primitive operation which splits a string into two pieces. Since this operation involves copying the original string, it takes n units of…
Mark
  • 373
  • 1
  • 3
  • 7
17
votes
5 answers

What is "dynamic" about dynamic programming?

One of my seniors had a job interview and he was asked why it is called dynamic. He couldn't answer and after he gave up the interviewer said that there's nothing dynamic about it, its just called like that. That is hard for me to believe. Does it…
kintoki
  • 281
  • 2
  • 8
16
votes
2 answers

Largest sum divisible by n

I asked this question on StackOverflow, but I think here is a more appropriate place. This is a problem from Introduction to algorithms course: You have an array $a$ with $n$ positive integers (the array doesn't need to be sorted or the elements…
15
votes
2 answers

Why is the dynamic programming algorithm of the knapsack problem not polynomial?

The dynamic programming algorithm for the knapsack problem has a time complexity of $O(nW)$ where $n$ is the number of items and $W$ is the capacity of the knapsack. Why is this not a polynomial-time algorithm? I have read that one needs $\lg W$…
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…
14
votes
3 answers

What is a naive method?

I was researching dynamic programming and read the following: Often when using a more naive method, many of the subproblems are generated and solved many times. What is a naive method?
chopper draw lion4
  • 467
  • 1
  • 6
  • 9
14
votes
2 answers

When can I use dynamic programming to reduce the time complexity of my recursive algorithm?

Dynamic programming can reduce the time needed to perform a recursive algorithm. I know that dynamic programming can help reduce the time complexity of algorithms. Are the general conditions such that if satisfied by a recursive algorithm would…
13
votes
2 answers

Matrix chain multiplication and exponentiation

If I have two matrices $A$ and $B$, of dimensions $1000\times2$ and $2\times1000$, respectively, and want to compute $(AB)^{5000}$, it's more efficient to first rewrite the expression as $A(BA)^{4999}B$ and only then evaluate numerically, because…
isekaijin
  • 405
  • 2
  • 9
1
2 3
65 66