Lec08 Dynamic Programming2024
Lec08 Dynamic Programming2024
DYNAMIC PROGRAMMING
What is Dynamic Programming?
Dynamic Programming: from Wikipedia — “method
for solving complex problems by breaking them down
into simpler subproblems”
Used when problem breaks down into recurring small
sub-problems and the recursive solution results in
identical recursive calls that are executed more than
once
Examples:
Matrix chain multiplication
Optimal binary search tree
There can be many solution, but we wish to find a
solution with optimal value
Dynamic vs Divide & Concur
Dividing
problem into
sub-problems
Independent Overlapping
sub-problems sub-problems
Classification of Dynamic Programming
Dynamic
programming
Top-down Bottom-up
approach approach
Steps to develop the algorithms
Steps of dynamic programming (DP):
Conversion of the given problem into a search problem
Search would be from a large search space
Search space is generally segregated into various sub-spaces
Need the partition algorithm which can be accomplished through recursive
algorithm
Recursively define the value of an optimal solution
Compute the optimal solution, typically bottom-up
Construct the path of an optimal solution (if desired)
Recursive calls are characterised and a non-recursive procedure is
developed for filling the table (the calculations can be used at a later
stage)
Storing value in table, in case the value is needed again, no re-
computation is needed
Thus, two key ingredients of DP:
Optimal substructure
Overlapping subproblems
Bottom up algorithms usually outperform top-down ones by a constant factor
Disadvantages of Dynamic Programming
A C G T
0 0 0 0 0
A 0
G 0
C 0
T 0
A 0
11
Longest Common Subsequence
Initialise
Since A = A, row
1st
and column to 0 values
get the
diagonal value
LCS[0,0]+1: 0+1=1
A C G T
0 0 0 0 0
A 0 0+1=1
G 0
C 0
T 0
A 0
12
Longest Common Subsequence
0 0 0 0 0
A 0 1 1
G 0
C 0
T 0
A 0
13
Longest Common Subsequence
Final result
A C G T
0 0 0 0 0
A 0 1 1 1 1
G 0 1 1 2 2
C 0 1 2 2 2
T 0 1 2 2 3
A 0 1 2 2 3
The LCS is 3.
14
Optimal substructure & Overlapping sub
problems
Optimal substructure
Occurs when the optimal solution contains within it
optimal solutions to subproblems
Matrix-chain multiplication uses two
subproblems, the two sub chains
Overlapping sub problems
Occurs when a recursive algorithm revisit the same
problem repeatedly
Dynamic programming solve each subproblem only
once and stores the result
In LCS, reduce exponential problem to O(mn)
ROD CUTTING PROBLEM
Rod Cutting Problem
Given a rod of length n with n-1 cutting points
Challenge: Find the best cutting for the rod that maximise the
revenue
For e.g., if the rod length is 8, cut it into length 2 and 6 will give a
revenue 5+17 = 22
Rod Cutting Problem
Possible cutting of rod:
The min length is 1, so the number of cut is n-1
Assume that no cost incur for the cut, if n = 4, we will have:
=
max
Pn-1 + V1
Sub problems can be defined as recurrence relationship to solve the rod
cutting problem. However, it gives exponential timePncomplexity
+ V0
Rod Cutting Problem ~ Recursive Tree
cutRod (n=4)
cutRod(2) 2 = 21
cutRod(1) 4 = 22
cutRod(0) 8 = 23
Pseudocode of Rod Cutting ~ Dynamic
BOTTOM_UP_ROD_CUT(p, n)
4 q = -∞
5 for i = 0 to j< i
e
A C
d i i,j d
f
Number of Multiplication
A= B= We have 1*1
8*7
9*5
Total 3 multiplication
We have 6 final result,
each result consists of 3
multiplication
No of multiplications for AB = 3 * 3 * 2 = 18
Matrix Chain-Products
Matrix Chain-Product:
Compute A=A0*A1*…*An-1
Ai is di × di+1
Problem: How to parenthesize?
Example
B is 3 × 100
C is 100 × 5
D is 5 × 5
(B*C)*D takes (3 x 100 x 5) + (3 x 5 x 5) = 1500 +
75 = 1575 ops
B*(C*D) takes (3 x 100 x 5) + (100 x 5 x 5) = 1500
+ 2500 = 4000 ops
28
29
Matrix Chain Multiplication
A
We S
B, CImatters:
G
will show that the way we group matrices when multiplying
DiA,ff
Let A be a N
Lete
2x10I
B be a 10x50F
matrix
I
r
Let C bee
nc A N C
matrix
a 50x20 matrix
e
Consider computing A(BC):!!! T
# multiplications for (BC) = 10x50x20 = 10000, creating a 10x20
answer matrix
# multiplications for A(BC) = 2x10x20 = 400
Total multiplications = 10000 + 400 = 10400.
Say What?
If (A (B ((CD) (EF)) ) ) is optimal
Then (B ((CD) (EF)) ) is optimal as well
Proof on the next slide…
Matrix Chain Multiplication
Assume that we are calculating ABCDEF and that
the following parenthesization is optimal:
(A (B ((CD) (EF)) ) )
Then it is necessarily the case that
(B ((CD) (EF)) )
is the optimal parenthesization of BCDEF.
In essence, there is exactly one value of k for which we should "split" our work
into two separate cases so that we get an optimal result.
Here is a list of the cases to choose from:
(A0) (A1 Ak+2 ... An-1)
(A0 A1) (A2 Ak+2 ... An-1)
(A0 A1A2) (A3 Ak+2 ... An-1)
...
(A0 A1 ... An-3) (An-2 An-1)
(A0 A1 ... An-2) (An-1)
Basically, count the number of multiplications in each of these choices and pick
the minimum.
One other point to notice is that you have to account for the minimum number of
multiplications in each of the two products.
Matrix Chain Multiplication
Consider the case multiplying these 4 matrices:
A: 2x4
B: 4x2
C: 2x3
D: 3x1
1 0
120 264 1080 1344
2 X 0
360 1320 1350
Check
3 the work:
X X 0
720 1140
(A1 * A2 )* ((A3 * A4) * A5)
4 X X X 0
1680
The resulting
5 Xmatrix: X (4*3)* X(3*20 20*
X 7) 0
Number of multiplication: (120) (720)
(3*20 20* 7) We have the selected k is 2 when
(3*7) M(1,3).
(420) Thus, we have (A1*A2) and
(4 * 7) another group will be
(84) (A3*A4*A5).
From (A3*A4*A5), we have the
120+720+420+84=1 selected k is 4. Then we have
344 (A3*A4), this forms the solution:
((A3*A4)*A5)
51
Example
i\ j 1 2 3 4 5
1 0
120 264 1080 1344
2 X 0
360 1320 1350
3 X X 0 1140
720
4 X X X 0
1680
5 X X X X 0
We focus on
the selected k
values to
determine
where to put
the parenthesis
53
54
5*4*6=120
55
4*6*2=48
56
6*2*7=84
57
m[2,3] = 48;+40 m[1,2]
= 120;+60
A1(A2xA3 ) We want the minimum,
=48+(5*4*2)=88 or and thus, 88 is the
(A1xA2)A3 = answer.
120+5*6*2=180
58
A2X(A3xA4 )
=84+(4*6*7)=252
or
(A2xA3)xA4 =
48+4*2*7=104
59
A1x(A2XA3xA4)
104+5*4*7=160
or
(A1xA2)x(A3xA4)
120+84+5*6*7=414
Or
(A1XA2xA3)XA4
88+5*2*7=158
60
Algorithm matrixChain(S):
Input: sequence S of n matrices to be multiplied
Output: number of operations in an optimal
paranethization of S
for i 0 to n-1 do
Ni,i 0
for b 1 to n-1 do
for i 0 to n-b-1 do
j i+b
Ni,j +infinity
for k i to j-1 do
Ni,j min{Ni,j , Ni,k +Nk+1,j +di
dk+1 dj+1}
0/1 KNAPSACK
The 0/1 Knapsack Problem
Given: A set S of n items, with each item i having
bi - a positive benefit
wi - a positive weight
Goal: Choose items with maximum total benefit but with
weight at most W.
If we are not allowed to take fractional amounts, then this
is the 0/1 knapsack problem.
In this case, we let T denote the set of items we take
Objective: maximize b
iT
i
Constraint: w
iT
i W
Greedy Example
Items: Solution:
• 5 (2 in)
1 2 3 4 5 • 1 (4 in)
• 3 (2 in)
Weight: 4 in 2 in 2 in 6 in 2 in
W = 9 in w = 8 in
Benefit: $20 $3 $6 $25 $80
B = $106
Value: 5 1.5 3 4.17 40
Greedy Example
Items: Solution:
• 5 (2 in)
1 2 3 4 5 • 4 (6 in)
Weight: 4 in 2 in 2 in 6 in 2 in
W = 9 in w = 8 in
Benefit: $20 $3 $6 $27 $80
B = $107
Value: 5 1.5 3 4.5
A 0/1 Knapsack Algorithm, First Attempt
Items: 1 2 3 4 5
Weight (kg): 1 2 5 6 7
Benefit: 1 6 18 22 28
A 0/1 Knapsack Algorithm, Second Attempt
Items: 1 2 3 4 5
Weight (kg): 1 2 5 6 7
Benefit: 1 6 18 22 28
The 0/1 Knapsack Algorithm
W = 11 kg
k represents the item
Values in the cell, is B[k,w]
0 1 2 3 4 5 6 7 8 9 10 11
ø 0 0 0 0 0 0 0 0 0 0 0 0
{1} 0 1 1 1 1 1 1 1 1 1 1 1
{1,2} 0
{1,2,3} 0
{1,2,3,4} 0
{1,2,3,4,5} 0
Items: 1 2 3 4 5
Weight (kg): 1 2 5 6 7
Benefit: 1 6 18 22 28
The 0/1 Knapsack Algorithm
, so we get 1
W = 11 kg
0 1 2 3 4 5 6 7 8 9 10 11
ø 0 0 0 0 0 0 0 0 0 0 0 0
{1} 0 1 1 1 1 1 1 1 1 1 1 1
k=2,{1,2} 0 1 6 7 7 7 7 7 7 7 7 7
{1,2,3} 0
{1,2,3,4} 0
{1,2,3,4,5} 0
b2 = 6, We get 6 Items: 1 2 3 4 5
(it means that we have only item 2, no item 1.
Weight (kg): 1 2 5 6 7
Benefit: 1 6 18 22 28
The 0/1 Knapsack Algorithm
W = 11 kg
0 1 2 3 4 5 6 7 8 9 10 11
ø 0 0 0 0 0 0 0 0 0 0 0 0
{1} 0 1 1 1 1 1 1 1 1 1 1 1
{1,2} 0 1 6 7 7 7 7 7 7 7 7 7
{1,2,3} 0 1 6 7 7 18 19 24 25 25 25 25
{1,2,3,4} 0
{1,2,3,4,5} 0
Items: 1 2 3 4 5
Weight (kg): 1 2 5 6 7
Benefit: 1 6 18 22 28
The 0/1 Knapsack Algorithm
W = 11 kg
0 1 2 3 4 5 6 7 8 9 10 11
ø 0 0 0 0 0 0 0 0 0 0 0 0
{1} 0 1 1 1 1 1 1 1 1 1 1 1
{1,2} 0 1 6 7 7 7 7 7 7 7 7 7
{1,2,3} 0 1 6 7 7 18 19 24 25 25 25 25
{1,2,3,4} 0 1 6 7 7 18 22 24 28 29 29 40
{1,2,3,4,5} 0
Items: 1 2 3 4 5
Weight (kg): 1 2 5 6 7
Benefit: 1 6 18 22 28
The 0/1 Knapsack Algorithm
W = 11 kg
0 1 2 3 4 5 6 7 8 9 10 11
ø 0 0 0 0 0 0 0 0 0 0 0 0
{1} 0 1 1 1 1 1 1 1 1 1 1 1
{1,2} 0 1 6 7 7 7 7 7 7 7 7 7
{1,2,3} 0 1 6 7 7 18 19 24 25 25 25 25
{1,2,3,4} 0 1 6 7 7 18 22 24 28 29 29 40
{1,2,3,4,5} 0 1 6 7 7 18 22 28 29 34 35 40
Items: 1 2 3 4 5
Weight (kg): 1 2 5 6 7
Benefit: 1 6 18 22 28
The 0/1 Knapsack Algorithm
W = 11 kg
0 1 2 3 4 5 6 7 8 9 10 11
ø 0 0 0 0 0 0 0 0 0 0 0 0
{1} 0 1 1 1 1 1 1 1 1 1 1 1
{1,2} 0 1 6 7 7 7 7 7 7 7 7 7
{1,2,3} 0 1 6 7 7 18 19 24 25 25 25 25
{1,2,3,4} 0 1 6 7 7 18 22 24 28 29 29 40
{1,2,3,4,5} 0 1 6 7 7 18 22 28 29 34 35 40
Items: 1 2 3 4 5
Weight (kg): 1 2 5 6 7
Benefit: 1 6 18 22 28
The 0/1 Knapsack Algorithm
W = 11 kg
0 1 2 3 4 5 6 7 8 9 10 11
ø 0 0 0 0 0 0 0 0 0 0 0 0
{1} 0 1 1 1 1 1 1 1 1 1 1 1
{1,2} 0 1 6 7 7 7 7 7 7 7 7 7
{1,2,3} 0 1 6 7 7 18 19 24 25 25 25 25
{1,2,3,4} 0 1 6 7 7 18 22 24 28 29 29 40
{1,2,3,4,5} 0 1 6 7 7 18 22 28 29 34 35 40
Items: 1 2 3 4 5
Weight (kg): 1 2 5 6 7
Benefit: 1 6 18 22 28
Algorithm 01Knapsack(S, W):
Input: set S of items w/ benefit bi
and weight wi; max.
weight W
Output: benefit of best subset with
weight at most W
for w 0 to W do
B[w] 0
for k 1 to n do
for w W downto wk do
if B[w-wk]+bk > B[w] then
B[w] B[w-wk]+bk
Exercise