Those who cannot remember the past are condemned to repeat it
A DP is an algorithmic technique which is usually based on a recurrent formula and one (or some) starting states. A sub-solution of the problem is constructed from previously found ones. DP solutions have a polynomial complexity, which is more efficient than recursive function.
One main advantages of DP is that it can store the intermediate states for future use, which means it does not need to recompute some middle results like in the recursive function. So DP is much more efficient.
A small example is about Fibonacci numbers:
Fibonacci (n) = 1; if n = 0
Fibonacci (n) = 1; if n = 1
Fibonacci (n) = Fibonacci(n-1) + Fibonacci(n-2)
A code for pure recursion:
int fib (int n) {
if (n < 2)
return 1;
return fib(n-1) + fib(n-2);
}
A code for DP:
void fib () {
fibresult[0] = 1;
fibresult[1] = 1;
for (int i = 2; i<n; i++)
fibresult[i] = fibresult[i-1] + fibresult[i-2];
}
Next, I will give a basic example and an elementary example for introduction
1. Coin Change (basis)
Question
Given a list of N coins, their values (V1, V2, … , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or report that it’s not possible to select coins in such a way that they sum up to S.
Analysis
It’s a way to describe a situation, a sub-solution for the problem. For example a state would be the solution for sum i, where i≤S.
A smaller state than state i would be the solution for any sum j, where j
Set Min[i] equal to Infinity for all of i
Min[0]=0
For i = 1 to S
For j = 0 to N - 1
If (Vj<=i AND Min[i-Vj]+1<Min[i])
Then Min[i]=Min[i-Vj]+1
Output Min[S]
C++ code:
static int minCoins(int coins[], int m, int V)
{
// base case
if (V == 0) return 0;
// Initialize result
int res = Integer.MAX_VALUE;
// Try every coin that has smaller value than V
for (int i=0; i<m; i++)
{
if (coins[i] <= V)
{
int sub_res = minCoins(coins, m, V-coins[i]);
// Check for INT_MAX to avoid overflow and see if
// result can minimized
if (sub_res != Integer.MAX_VALUE && sub_res + 1 < res)
res = sub_res + 1;
}
}
return res;
}
2. Longest non-decreasing sequence (Elementary)
To this point, very simple examples have been discussed. Now let’s see how to find a way for passing from one state to another, for harder problems. For that we will introduce a new term called recurrent relation, which makes a connection between a lower and a greater state.
Question
Given a sequence of N numbers – A[1] , A[2] , …, A[N] . Find the length of the longest non-decreasing sequence.
As described above we must first find how to define a “state” which represents a sub-problem and thus we have to find a solution for it. Note that in most cases the states rely on lower states and are independent from greater states.
Concretely, we have to find the value of longest subsequences at every index point. Then largest LSi would be the longest subsequence in the given sequence. To begin LSi is assigned to be one since Ai is element of the sequence(Last element). Then for all j such that j
for i=0 to n-1
LS[i]=1;
for j=0 to i-1
if (a[i] > a[j] and LS[i]<LS[j]);
LS[i] = LS[j]+1
for i=0 to n-1
if (largest < LS[i])
largest = LS[i]
More examples on : Click me