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.

968 questions
5
votes
1 answer

Is dynamic programming restricted to optimization problems?

The usual criteria used to decide if a problem can be solved using dynamic programming is (1) if it has optimal sub-problems and (2) if it has overlapping sub-problems. Does the word "optimal" mean that DP can only be used to solve optimization…
Alan Evangelista
  • 257
  • 1
  • 2
  • 10
4
votes
0 answers

What is the relationship between the Markov property and optimal substructure?

Dynamic programming can only be applied to problems with optimal substructure. The Markov property (e.g. in Markov Decision Processes, MDPs) means that the distribution of one state $x_{k+1}$ only depends on the state directly before ($x_k$, and the…
Martin Thoma
  • 2,360
  • 1
  • 22
  • 41
4
votes
0 answers

When not to use dynamic programming

I was reading about dynamic programming and I understood that we should not be using dynamic programming approach if the optimal solution of a problem does not contain the optimal solution of the subproblem. The Longest path problem is very clear…
4
votes
1 answer

Binomial coefficient to approach multi-way choices DP problem?

I'm trying to understand this dynamic programming related problem, adapted from Kleinberg's Algorithm Design book. Not homework: i've already a solution, just considering if i'm ok with the theory. Suppose we want to replicate a file over a…
kentilla
  • 302
  • 1
  • 13
3
votes
0 answers

Sequence matching

Let $T$ and $P$ be respectively two sequences $t_1, · · · , t_n$ and $p_1, · · · , p_k$ of characters such that $k ≤ n$. The characters range over a finite alphabet Σ. With each position of $T$, we associate a non-negative value. Let $v(i)$ denote…
locvju
  • 31
  • 2
3
votes
1 answer

Length of longest arithmetic progression in an array

I was reading an article on Longest Arithmetic Progression. The solution given has S(n)=$O(n^2)$. Can't I solve it in $O(1)$ space? To find the three elements, we first fix an element as middle element and search for other two (one smaller and one…
shiwang
  • 481
  • 1
  • 9
  • 24
3
votes
1 answer

Select optimal subintervals from array

I have an input array and I have to select an indefinite number of intervals from it so that the "profit" is maximal and I have exactly T elements selected in total, where T is given. Profit means the sum of all the elements of an interval, except…
Johnny
  • 31
  • 2
2
votes
2 answers

Print (not count) all possible path classic climbing stair problem)

I came across this classic question and found may many solution to it. for loop and DP/ reclusive + memorization. Also found a twisted version of the questions asking to print all possible path instead of counting. Wondering for the twisted…
Maxfield
  • 227
  • 1
  • 7
2
votes
1 answer

Longest subsequence accepted by DFA (Dynamic Prog algorithm)

Problem: Given a string $s$ and a DFA $D$, compute the longest subsequence of $s$ such that the subsequence is accepted by $D$, or report that no such subsequence exists. This problem has a runtime of $O(qn)$ where $q$ is the number of states of…
fraiser
  • 133
  • 5
2
votes
1 answer

Can there exist more than one optimal solution in a dynamic programming problem?

In any dynamic programming problem can there exist more than one optimal state ? If so how would I enumerate all of them ? For example: in the subset sum problem for the given set $\{-3, -2, 7, 5\}$ and target sum $0$. There exists two ways of…
ng.newbie
  • 215
  • 2
  • 10
2
votes
2 answers

Given a Tree decomposition, How to find the tree decomposition of its subgraph?

You are given a tree decomposition of a large graph with not so small bounded tree-width. Suppose now you need to solve a dynamic programming problem inside a subgraph and does not have time to do another costly tree decomposition. How do I find the…
R. S.
  • 167
  • 1
  • 8
2
votes
1 answer

Knapsack with an even number of items.

I was asked in an interview to create a knapsack of only even items in n*C time, c being the capacity, n being the number of items to choose from. I tried approaching it with a third dimension, where it would be of height n and signify at each ij…
Lola1984
  • 185
  • 1
  • 8
2
votes
2 answers

Given a set of numbers (negative or positive), and a maximum weight w, find a subset that is maximal whose sum is less than w

The aim of this problem is to find a subset (need not be consecutive) of a given set such that the sum is maximal and less than some given number $w$. (Note, we are trying to find a subset that is less than or equal to $w$ and not closest to…
athul777
  • 133
  • 5
2
votes
1 answer

Solving a dynamic programming problem?

Alex writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down? My one friend said to me that this problem can be solved by dynamic programming.But I can't understand…
2
votes
1 answer

Knapsack problems with two sums and two sets

For an algorithm homework, I'm asked to give a solution for a knapsack problem. I have in input a list of products. A product is assigned a cost and a number of calories. So I have two sets, one of money and one of kcal. My goal is to choose…
Moiap13
  • 23
  • 5
1
2 3 4 5