1

I'm learning dynamic programming and I've been having a great deal of trouble understanding more complex problems. When given a problem, I've been taught to find a recursive algorithm, memoize the recursive algorithm and then create an iterative, bottom-up version. At almost every step I have an issue. In terms of the recursive algorithm, I write different different ways to do recursive algorithms, but only one is often optimal for use in dynamic programming and I can't distinguish what aspects of a recursive algorithm make memoization easier. In terms of memoization, I don't understand which values to use for indices. For conversion to a bottom-up version, I can't figure out which order to fill the array/double array.

This is what I understand: - it should be possible to split the main problem to subproblems

In terms of the problem mentioned, I've come up with a recursive algorithm that has these important lines of code:

int optionOne = values[i] + find(values, i+1, limit - values[i]);
int optionTwo = find(values, i+1, limit);

If I'm unclear or this is not the correct qa site, let me know.

Edit:

Example: Given array x: [4,5,6,9,11] and max value m: 20

Maximum subsequence in x under or equal to m would be [4,5,11] as 4+5+11 = 20

2
  • It's best if you give an example an show us exactly what you mean by "maximum subsequence below a certain value". Commented Oct 13, 2013 at 18:52
  • I think the problem you are describing is a 0-1 knapsack problem where values equal weights. Dynamic programming solution is given here.en.wikipedia.org/wiki/Knapsack_problem Commented Oct 13, 2013 at 21:19

1 Answer 1

1

I think this problem is NP-hard, meaning that unless P = NP there isn't a polynomial-time algorithm for solving the problem.

There's a simple reduction from the subset-sum problem to this problem. In subset-sum, you're given a set of n numbers and a target number k and want to determine whether there's a subset of those numbers that adds up to exactly k. You can solve subset-sum with a solver for your problem as follows: create an array of the numbers in the set and find the largest subsequence whose sum is less than or equal to k. If that adds up to exactly k, the set has a subset that adds up to k. Otherwise, it does not.

This reduction takes polynomial time, so because subset-sum is NP-hard, your problem is NP-hard as well. Therefore, I doubt there's a polynomial-time algorithm.

That said - there is a pseudopolynomial-time algorithm for subset-sum, which is described on Wikipedia. This algorithm uses DP in two variables and isn't strictly polynomial time, but it will probably work in your case.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.