Dynamic Programming
Dynamic Programming
PROGRAMMING
ALGORITHM PARADIGMS
Greedy: Makes a series of choices, each of which looks best
at the moment (locally optimal choice), with the hope that
these local optima lead to a globally optimal solution.
Divide-and-conquer: Break up a problem into independent
subproblems; solve each subproblem; combine solutions to
subproblems to form solution to the original problem.
Dynamic programming: Break up a problem into a series of
overlapping subproblems; combine solutions to smaller
subproblems to form solution to large subproblem
DYNAMIC PROGRAMMING
Main idea:
• set up a recurrence relating a solution
to a larger instance to solutions of
some smaller instances
• solve smaller instances once
• record solutions in a table
• extract the solution to the initial
instance from that table
FIBONACCI NUMBERS
Pseudocode:
Recall the definition of
Fibonacci numbers: fib(n)
{
If(n==0)
F(n) = F(n-1) + F(n-2)
return 0;
F(0) = 0
if ( n<=2 )
F(1) = 1
return 1;
return (fib(n-1) + fib(n-2) );
}
FLAW IN RECURSION
• The same subproblem is
being recomputed
repeatedly
• This can be avoided by
storing the results of
subproblems and reusing it
● Memoization: Store
intermediate results to reuse
● Memoized Algorithm
ED mfib(n):
if n in memo: return
SOLUTIO memo[n]
N if n<=2: f=1
else: f=mfib(n-1)+mfib(n-2)
memo[n]=f
return f
• Use an iterative method
dynamic_fib(n){
int f[n+1];
BOTTOM f[1]=f[2]=1;
-UP for (int i=3; i<=n;i++)
APPROA f[i]=f[i-1]+f[i-2];
CH return f[n];
}
• Cost is O(n) (both time and
space)
REQUIREMENTS
● Simple Subproblem: Must be able to divide problems into simple
subproblems each similar to the original
- Usually, there are polynomial number of subproblems
● Subproblem Optimality: The optimal solution to the global
problem must be composition of optimal subproblem solutions
using a simple combining operation
● Subproblem Overlap: Optimal solutions to unrelated sub-
problems can contain subproblems in common
5 EASY STEPS
● Define subproblems
● Guess part of the solution
● Relate subproblems
● Recurse and Memoize or build a DP table
● Solve the final problem
COIN CHANGE PROBLEM
● You are given some amount N and a set of
denominations C={c1,c2,..,cd}, and each
denominations has infinite coins.
● The goal is to make change for the amount
n using the given denominations.
- Minimize the total number of coins
returned for a particular quantity of change.
GREEDY APPROACH
Example: Example:
Denominations available: {1, 5, 6, 9}
Denominations available:
Amount: N = 11 {4,10,25}
• 11 - 9 = 2
Amount: N = 41
• 2-1=1
• 41 – 25 = 16
• 1–1
• 16 – 10 = 6
Number of coins requires to make
change to 11 = 3 • 6–4=2
- Coin set = {9, 1, 1}
Solution does not exist
RECURSIVE APPROACH
DYNAMIC
PROGRAMMING
Denominations available: {1, 5, 6, 9}
Amount: N = 10
0 1 2 3 4 5 6 7 8 9 10
1
5
6
9
c[i-1][j] if D[i]>j
c[i][j] =
min{ c[i-1][j], 1+c[i][ j-D[i] ] } otherwise
Denominations available: {2, 3, 5, 10}
Amount: N = 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2
3
5
10
0-1 KNAPSACK
PROBLEM
OPTIMIZAT
ION
PROBLEM
DP
SOLUTION
● Relating
subproblems
● Best subset of Sk that has total
weight w is either best subset Sk-1
Weight
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 1 1 1 1 1 1 1 1 1
Items
2 0
3 0
4 0
Total Capacity = 10
N=4
KP(4,10)
Yes No
KP(3,10) KP(3,5)
Yes No
KP(2,10) KP(2,6)
Yes No
KP(1,10) KP(1,8)
Yes No Yes No