Dynamic Programming
Dynamic Programming
TECHNIQUES
Recursion & Dynamic
Programming
3/26/23 NHMinh@FIT 2
Dynamic Programming
o Dynamic Programming is an algorithm design technique for
optimization problems (minimize/maximize)
o DP can be used when the solution to a problem may be
viewed as the result of a sequence of decisions
o DP reduces computation by
n Solving subproblems in a bottom-up fashion.
n Storing solution to a subproblem the first time it is solved.
n Looking up the solution when subproblem is encountered again.
o Key: determine structure of optimal solutions
3/26/23 NHMinh@FIT 3
Dynamic Programming History
o Bellman. Pioneered the systematic study of
dynamic programming in the 1950s.
o Etymology.
n Dynamic programming = planning over time.
n Secretary of Defense was hostile to mathematical
research.
n Bellman sought an impressive name to avoid
confrontation.
o "it's impossible to use dynamic in a pejorative sense"
o "something not even a Congressman could object to"
3/26/23 NHMinh@FIT 4
Steps in Dynamic Programming
1. Define the problem and identify its optimal structure.
n Optimal structure: the optimal solution to the problem can be
obtained by combining the optimal solutions to its
subproblems.
2. Formulate a recursive solution (top-down).
3. Compute the value of an optimal solution in a bottom-
up fashion.
n Memorize the recursive solutions by storing the results of
previous computation in a table.
n Convert the recursive solution to an iterative one.
3/26/23 NHMinh@FIT 6
Fibonacci Numbers
o Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, …
𝐹! = 𝑖 if 𝑖 ≤ 1
𝐹! = 𝐹!"# + 𝐹!"$ if 𝑖 > 1
Solved by a recursive program:
int Fib(int n) Fn
{
if (n <= 1)
return n; Fn-1 + Fn-2
else
return Fib(n - 1)
+ Fib(n - 2); Fn-2 + Fn-3 Fn-3 + Fn-4
}
Fib(4) Fib(3)
+ +
Fib(1) Fib(0)
3/26/23 NHMinh@FIT 9
Fibonacci Numbers
#include <iostream>
int* memo = new int[n+1];
int Fib (int n)
{
if (n <= 1)
return 1; Each 𝐹𝑖 is calculated
if(memo[n] != 0) only once
return memo[n];
int result = Fib(n - 1) + Fib(n - 2);
memo[n] = result;
return result;
} But it is still inefficient because
Resursive calls are called
multiple times for an n
3/26/23 NHMinh@FIT 10
Fibonacci Numbers
n Using a bottom-up approach can solve this problem!
n F(0) = 0
n F(1) = 1
n F(2) = 1+0 = 1
n …
n F(n-2) =
n F(n-1) =
n F(n) = F(n-1) + F(n-2)
3/26/23 NHMinh@FIT 11
Fibonacci Numbers – DP
#include <iostream>
int Fib_DP(int n)
{
/* Declare an array to store Fibonacci numbers. */
int *f = new int[n+1];//1 extra to handle case, n
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
f[i] = f[i-1] + f[i-2];
return f[n];
}
3/26/23 NHMinh@FIT 12
Fibonacci Numbers – DP
#include <iostream>
Space Optimization
int Fib_DP(int n)
{
int a = 0, b = 1, c, i;
if(n == 0)
return 0;
for (i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}
3/26/23 NHMinh@FIT 13
EXAMPLE 2: THE
KNAPSACK PROBLEM
3/26/23 NHMinh@FIT 14
The Knapsack problem
o Problem statement:
n A thief is robbing a museum and he only has a single
knapsack to carry all the items he steals.
n The knapsack has a capacity for the amount of weight it can
hold. Each item in the museum has a weight and a value
associated with it.
$7 $10
2kg 4kg
10kg
$12 $8
5kg 3kg
3/26/23 NHMinh@FIT 15
The Knapsack problem - Variation
o 0/1 Knapsack problem
n Each item is chosen at most once.
n Decision variable for each item is a binary value (0 or 1)
o Multiple-choice Knapsack problem
n Each item can be put to the knapsack multiple times.
n Decision variable for each item is an integer value.
o Bounded Knapsack problem
n Same with multiple-choice but each item has the max number of
times it can be chosen.
o Knapsack problem with fractional items
o Knapsack problem with multiple constraint
o …
3/26/23 NHMinh@FIT 16
0/1 Knapsack problem – Example
o Knapsack’s capacity: 10kg
2
o 5 items can be chosen:
• Item 1: $6 (2 kg)
1
• Item 2: $10 (2 kg) 3
3/26/23 NHMinh@FIT 18
The Knapsack problem – Recursive
//Returns the maximum value that can be put in a knapsack of //capacity W
int KnapSack(int n, int wt[], int val[], int W) {
if (n == 0 || W == 0) // Base Case
return 0;
// If weight of the nth item is more than Knapsack capacity W,
//then this item cannot be included in the optimal solution
if (wt[n - 1] > W)
return KnapSack(n - 1, wt, val, W);
𝒇 𝒏 − 𝟏, 𝑾 − 𝒘𝒏 + 𝒙𝒏
// else: Return the maximum of two cases:
// (1) nth item included // (2) not included
return max( val[n-1] + KnapSack(n-1, wt, val, W-wt[n-1]) ,
KnapSack(n-1, wt, val, W) );
}
𝒇 𝒏 − 𝟏, 𝑾
3/26/23 NHMinh@FIT 19
0/1 Knapsack problem – DP
o We can also use a bottom-up approach and memorize the
solutions to subproblems to a table à Dynamic Programming
n Row: items
n Column: remaining weight capacity of the knapsack
n We fill the table using the following recurrence relation:
𝒇 𝑾, 𝒊 = 𝐦𝐚𝐱 𝒇 𝒊 − 𝟏, 𝑾 − 𝒘𝒊 + 𝒙𝒊 , 𝒇 𝒊 − 𝟏, 𝑾
0kg 1kg 2kg 3kg 4kg 5kg 6kg 7kg 8kg 9kg 10kg
1
2
3
4
5
3/26/23 NHMinh@FIT 20
0/1 Knapsack problem – DP
oUse only item 1:
à 𝑓(1,1) = 0, 𝑓(1,2) = 6, 𝑓(1,3) = 6, … , 𝑓(1,10) = 6
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10
3 3kg $12
4 4kg $16
5 5kg $20
3/26/23 NHMinh@FIT 21
0/1 Knapsack problem – DP
oUse item 1 & 2:
à 𝑓 2,2 = max 𝑓 1,0 + 10, 𝑓 1,2 = 10, …
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10
3 3kg $12
4 4kg $16
5 5kg $20
3/26/23 NHMinh@FIT 22
0/1 Knapsack problem – DP
oUse item 1 & 2:
à 𝑓 2,3 = max 𝑓 1,1 + 10, 𝑓 1,3 = 10, …
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10
3 3kg $12
4 4kg $16
5 5kg $20
3/26/23 NHMinh@FIT 23
0/1 Knapsack problem – DP
oUse item 1 & 2:
à 𝑓 2,4 = max 𝑓 1,2 + 10, 𝑓 1,4 = 16, …
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10 16
3 3kg $12
4 4kg $16
5 5kg $20
3/26/23 NHMinh@FIT 24
0/1 Knapsack problem – DP
oUse item 1 & 2:
à 𝑓 2, 𝑖 = max 𝑓 1, 𝑊 − 2 + 10, 𝑓 1, 𝑊
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10 16 16 16 16 16 16 16
3 3kg $12
4 4kg $16
5 5kg $20
3/26/23 NHMinh@FIT 25
0/1 Knapsack problem – DP
oUse item 1, 2, 3:
à 𝑓 3,3 = max 𝑓 2,0 + 12, 𝑓 2,3 = 12
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10 16 16 16 16 16 16 16
3 3kg $12 0 10 12
4 4kg $16
5 5kg $20
3/26/23 NHMinh@FIT 26
0/1 Knapsack problem – DP
oUse item 1, 2, 3:
à 𝑓 3,4 = max 𝑓 2,1 + 12, 𝑓 2,4 = 16
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10 16 16 16 16 16 16 16
3 3kg $12 0 10 12 16
4 4kg $16
5 5kg $20
3/26/23 NHMinh@FIT 27
0/1 Knapsack problem – DP
oUse item 1, 2, 3:
à 𝑓 3,5 = max 𝑓 2,2 + 12, 𝑓 2,5 = 22
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10 16 16 16 16 16 16 16
3 3kg $12 0 10 12 16 22
4 4kg $16
5 5kg $20
3/26/23 NHMinh@FIT 28
0/1 Knapsack problem – DP
oUse item 1, 2, 3:
à 𝑓 3, 𝑖 = max 𝑓 2, 𝑊 − 3 + 12, 𝑓 2, 𝑊
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10 16 16 16 16 16 16 16
3 3kg $12 0 10 12 16 22 22 28 28 28 28
4 4kg $16
5 5kg $20
3/26/23 NHMinh@FIT 29
0/1 Knapsack problem – DP
oUse item 1, 2, 3, 4:
à 𝑓 4, 𝑖 = max 𝑓 3, 𝑊 − 4 + 16, 𝑓 3, 𝑊
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10 16 16 16 16 16 16 16
3 3kg $12 0 10 12 16 22 22 28 28 28 28
4 4kg $16 0 10 12 16 22 26 28 32 38 38
5 5kg $20
3/26/23 NHMinh@FIT 30
0/1 Knapsack problem – DP
oUse item 1, 2, 3, 4, 5:
à 𝑓 5,10 = max 𝑓 4,5 + 20, 𝑓 4,10 = 42
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10 16 16 16 16 16 16 16
3 3kg $12 0 10 12 16 22 22 28 28 28 28
4 4kg $16 0 10 12 16 22 26 28 32 38 38
5 5kg $20 0 10 12 16 22 26 30 32 38 42
3/26/23 NHMinh@FIT 31
0/1 Knapsack problem – DP
o Solution:
n item 5 + item 3 + item 2 à $42 – 10kg
0|1 2 3 4 5 6 7 8 9 10
1 2kg $6 0 6 6 6 6 6 6 6 6 6
2 2kg $10 0 10 10 16 16 16 16 16 16 16
3 3kg $12 0 10 12 16 22 22 28 28 28 28
4 4kg $16 0 10 12 16 22 26 28 32 38 38
5 5kg $20 0 10 12 16 22 26 30 32 38 42
3/26/23 NHMinh@FIT 32
The Knapsack problem – DP
int KnapSack(int n, int wt[], int val[], int W)
{
int i, w;
//Create a table K to store solutions of subproblems
int** K = new int*[n + 1];
for(i = 0; i <= n; i++)
K[i] = new int[W + 1];
for (i = 0; i <= n; i++) //Build table K[][] in bottom up manner
for (w = 0; w <= W; w++) {
if (i==0 || w==0)
K[i][w] = 0;
𝒇 𝒊 − 𝟏, 𝑾 − 𝒘𝒊 + 𝒙𝒊
else if (wt[i-1] <= w)
K[i][w] = K[i-1][w];
else
K[i][w] = max(K[i-1][w-wt[i-1]] + val[i-1], K[i-1][w] );
}
return K[n][W]; 𝒇 𝒊 − 𝟏, 𝑾
} 3/26/23 NHMinh@FIT 33
Multi-choice Knapsack problem
o Knapsack’s capacity: 10kg
2
o 3 items can be chosen: 2
• Item 1: $5 (3 kg)
3
• Item 2: $7 (4 kg) 1
3
• Item 3: $8 (5 kg) 1 1
3
à 1 item can be picked 1 10kg
many times
3/26/23 NHMinh@FIT 35
Multi-choice Knapsack problem
oUse only item 1:
à 𝑓(1,3) = 5, 𝑓(1,6) = 10, 𝑓(1,9) = 15
0 1 2 3 4 5 6 7 8 9 10
1 3kg $5 0 0 0 5 5 5 10 10 10 15 15
2 4kg $7
3 5kg $8
𝟏×𝒙𝟏 𝟐×𝒙𝟏 𝟑×𝒙𝟏
3/26/23 NHMinh@FIT 36
Multi-choice Knapsack problem
oUse only item 1 & 2:
à 𝑓 2, 𝑊 = max 𝑓 1, 𝑊 − 4𝑘 + 7𝑘, 𝑓 1, 𝑊
0 1 2 3 4 5 6 7 8 9 10
1 3kg $5 0 0 0 5 5 5 10 10 10 15 15
2 4kg $7 0 0 0 5 7 7 10 12 14 15 17
3 5kg $8
3/26/23 NHMinh@FIT 37
Multi-choice Knapsack problem
oUse item 1, 2, and 3:
à 𝑓 3, 𝑊 = max 𝑓 2, 𝑊 − 5𝑘 + 8𝑘, 𝑓 2, 𝑊
0 1 2 3 4 5 6 7 8 9 10
1 3kg $5 0 0 0 5 5 5 10 10 10 15 15
2 4kg $7 0 0 0 5 7 7 10 12 14 15 17
3 5kg $8 0 0 0 5 7 8 10 12 14 15 17
3/26/23 NHMinh@FIT 38
Multi-choice Knapsack problem
o Solution:
n item 2 + 2 x item 1 à $17 – 10kg
0 1 2 3 4 5 6 7 8 9 10
1 3kg $5 0 0 0 5 5 5 10 10 10 15 15
2 4kg $7 0 0 0 5 7 7 10 12 14 15 17
3 5kg $8 0 0 0 5 7 8 10 12 14 15 17
3/26/23 NHMinh@FIT 39
Multi-choice Knapsack problem
o As an exercise, rewrite the Knapsack function
to solve multi-choice Knapsack problem:
n Using Recursion (top-down)
n Using Dynamic Programming (bottom-up)
3/26/23 NHMinh@FIT 40
Properties of Dynamic Programming
o There are 2 main properties of a problem that
suggest that the given problem can be solved
using Dynamic programming:
1. Overlapping Subproblems
à solutions of same subproblems are needed again
and again
2. Optimal Structures
à optimal solution of the given problem can be
obtained by using optimal solutions of its
subproblems.
3/26/23 NHMinh@FIT 41
Dynamic Programming Applications
o Area o Some famous dynamic
n Bioinformatics. programming algorithms.
n Viterbi for hidden Markov
n Control theory. models.
n Information theory. n Unix diff for comparing two
n Operations research. files.
n Smith-Waterman for
n Computer science: sequence alignment.
theory, graphics, AI, n Bellman-Ford for shortest
systems, …. path routing in networks.
n Cocke-Kasami-Younger for
parsing context free
grammars.
3/26/23 NHMinh@FIT 42
More Reading
o The best way to get a feel for this is through some
more examples.
1. Longest Common Subsequence
2. Longest Increasing Subsequence
3. Matrix Chain Multiplication
4. Partition problem
5. Rod Cutting
6. Coin change problem
7. Word Break Problem
8. …
3/26/23 NHMinh@FIT 43