Chapter 7 - Dynamic Programming
Chapter 7 - Dynamic Programming
¨ Introduction
¨ Shortest Path Problem
¨ Knapsack Problem
¨ Extended Knapsack Problem
¨ Key Reference: Chapter 12, Taha’s book
1. Dynamic Programming
¨ Dynamic Programming is a technique that can
be used to solve many optimization problems.
In most applications, dynamic programming
obtains solutions by working backward from
the end of problem toward the beginning, the
breaking up a large, unwieldy problem into a
series of smaller, more tractable problems.
4 Steps in Dynamic Programming
¨ Divide the Problem into stages
¨ Solve the last stage first
¨ Work backwards solving stages
¨ Optimal solution obtained from all solved
stages
TYPES OF DP PROBLEMS
4
1 4
S A B 5
T
5 6
2 5 13
S B E T
16 2
5
C 2
F
¨ d(J, I) = 4
11 4
11 7 7 0
8 6
¨ Example:
A knapsack can hold some limited items. Given a set of
items, each with a weight and a value, determine the
count of each item to include in a collection so that the
total weight is less than or equal to a given limit and the
total value is as large as possible
Mathematical Model
¨ Wi = weight of item i = 1, 2, …, n.
¨ Vi = value of item i = 1, 2, …, n.
¨ C = capacity of knapsack
¨ Xi = 1 if item i is selected;
𝑛 0, otherwise
¨ Model: 𝑀𝑎𝑥 𝑍=∑ 𝑉 𝑖 𝑋 𝑖
𝑖=1
Subject to:
={0,1}
KNAPSACK PROBLEM CLASSIFY
¨ There are many types of Knapsack problem in practice.
¨ Examples:
- Choosing goods to carry on for delivery.
- Some scheduling problems, for example, there are some
tasks that need to be completed in the next 2 weeks, the
2-week duration can be considered as knapsack, the
tasks must be scheduled so that profit can be maximized
or the cost can be minimized; the constraints are the
working days, or hours in this 2 weeks.
Formula for Sub-problems
No add k
Add k
Example 1: Knapsack Problem
Given some items, pack the knapsack to get the
maximum total value. Each item has some weight and
some value. Total weight that we can carry is no more
than some fixed number C = 8. So we must consider
weights of items as well as their values.
0 0 - 0 0
1 1 15
1 0 - 0 0 2 5 10
2 0 - 0 0 3 3 9
3 0 - 0 0
4 4 5
4 0 5+0 5 1
C=8
5 0 5+0 5 1
6 0 5+0 5 1
7 0 5+0 5 1
0 5+0 5
Example 1 Solution
¨ Stage i = 3:
f3(s3) =max[v3x3+f4(s4)]=max[9x3+ f4(s3-3x3)];
s3 9x3+ f4(s3-3x3) Optimal Solution
x3=0 x3=1 f3(s3) x*3 Item W V
0 0+0 - 0 0
1 1 15
1 0+0 - 0 0 2 5 10
2 0+0 - 0 0 3 3 9
3 0+0 9+0 9 1
4 4 5
4 0+5 9+0 9 1
C=8
5 0+5 9+0 9 1
6 0+5 9+0 9 1
7 0+5 9+5 14 1
0+5 9+5 14
Example 1 Solution
¨ Stage i = 2:
f2(s2) =max[v2x2+f3(s3)]=max[10x2+ f3(s2-5x2)];
s2 10x2+ f3(s2-5x2) Optimal Solution
x2 = 0 x2 = 1 f2(s2) x*2 Item W V
0 0+0 - 0 0
1 1 15
1 0+0 - 0 0 2 5 10
2 0+0 - 0 0 3 3 9
3 0+9 - 9 0
4 4 5
4 0+9 - 9 0
C=8
5 0+9 10+0 10 1
6 0+9 10+0 10 1
7 0+14 10+0 14 0
0+14 10+9 19
Example 1 Solution
¨ Stage i = 1:
f1(s1) =max[v1x1+f2(s2)]=max[15x1+ f2(s1-x1)];
s1 15x1+ f2(s1-x1) Optimal Solution
x1 = 0 x1= 1 f1(s1) x*1 Item W V
0 0+0 - 0 0
1 1 15
1 0+0 15+0 15 1 2 5 10
2 0+0 15+0 15 1 3 3 9
3 0+9 15+0 15 1
4 4 5
4 0+9 15+9 24 1
C=8
5 0+10 15+9 24 1
6 0+10 15+10 25 1
7 0+14 15+10 25 1
0+19 15+14 29
Example 1 Solution
¨ Optimal Solution: given C =8
¨ At Stage 1, f1(s1) =29, s1=8, x*1 = 1 f2(s2)
= 14 ; s2=s1-x1=8-1=7
¨ At stage 2, f2(s2) = 14 at s2= 7, x*2 = 0 f3(s3)
=14 ;s3=7
¨ At stage 3, f3(s3) =14 at s3=7, x*3 =1
f4(s4) = 5 ;s4=4
¨ At stage 4, f4(s4) = 5 at s4 =4, x*4 =1.
¨ Optimal solution is (1,0,1,1) Objective
4. Extended Model
¨ Wi = weight of item i = 1, 2, …, n.
¨ Vi = value of item i = 1, 2, …, n.
¨ C = capacity of knapsack
¨ Xi 0 and integers 𝑛
¨ Model: 𝑀𝑎𝑥 𝑍=∑ 𝑉 𝑖 𝑋 𝑖
𝑖=1
Subject to:
0, and integers
Solution
¨ Stage: i is number of item, i =1,2,.., n
¨ Alternatives at stage i: xi =0,1,… number of selected items
¨ Stage si : total weight assigned/consumed to stages i, i+1,…,n
¨ fi(si) = the maximum value at stages i, i +1, .., n given si
¨ Backward recursive formula: start from item i =n.
The weight used at stage i is: ()=.
Therefore:
f
fn+1(sn+1)=0
Note: s ~ g in previous method
Example 2: Extended Knapsack Problem
A vessel has capacity C = 4 tons. So we must consider
to load among 3 items as the following table
0 14+0 28 42 - 42 C=4
3 3
4 0 14+0 28 42 56 56 4
Example 2 Solution
¨ Stage i = 2: x2 =={0,1}
f2(s2) =max[v2x2+f3(s3)]= max[47x2+ f3(s2-3x2)];
4 0 + 56 47 + 14 61 1
Example 2 Solution
¨ Stage i = 1: x1 =={0,1,2}
f1(s1) =max[v1x1+f2(s2)]= max[31x1+ f2(s1-2x1)];
4 0 + 61 31 + 28 62 + 0 62 2
Example 2 Solution
¨ Optimal Solution: given C =4
¨ At Stage 1, f1(s1) =62, s1=4, x*1 = 2 f2(s2)
= 0;s2=0=s1-2*x1
¨ At stage 2, f2(s2) = 0 at s2= 0, x*2 = 0 f3(s3)
=0 ;s3=0
¨ At stage 3, f3(s3) =0 at s3=0, x*3 =0.
¨ Optimal solution is (2,0,0) Objective value
= 62
Practice
¨ Let’s run our algorithm on the following data:
n = 4 (# of elements)
W = 5 (max weight)
¨ Elements (weight, benefit): (2,3), (3,4), (4,5), (5,6)
¨ How to pack the knapsack to achieve maximum
total value of packed items?
Practice Solution
¨ Stage i = 4: s4 = w4x4 =5x4 ≤ C = 5
f4(s4) =max[v4x4+f5(s5)]=max[6x4]; f5(s5)=0;
s4 6x4+f5(s4-5x4)] Optimal
Solution Item W V
x4=0 x4=1 f4(s4) x*4
1 2 3
0 0 - 0 0
2 3 4
1 0 - 0 0
3 4 5
2 0 - 0 0
0 - 0 4 5 6
3 0
4 0 - 0 0 C=5
5 0 6 6 1
Practice Solution
¨ Stage i = 3: s3 = w3x3 =4x3 ≤ C = 5
f3(s3) =max[v3x3+f4(s4)]=max[5x3+f4(s3-4x3)]=;
s3 5x4+f4(s3-4x3)] Optimal
Solution Item W V
x3=0 x3=1 f3(s3) x*3
1 2 3
0 0+0 - 0 0
2 3 4
1 0+0 - 0 0
3 4 5
2 0+0 - 0 0
0+0 - 0 4 5 6
3 0
0+0 4+0 4 4 5 6
3 1
0+0 3+0 - 3 4 5 6
3 1
0 - 0 4 5 6
3 0
4 0 - 0 0 C=5
5 0 6 6 1
Practice Solution
¨ Stage i = 7:Q7 = 3x7 ≤ C = 5
f7(s7) =min[v7x7+f8(s8)]=max[5x3+f4(s3-4x3)]=;
s3 5x4+f4(s3-4x3)] Optimal
Solution Item W V
x3=0 x3=1 f3(s3) x*3
1 2 3
0 0+0 - 0 0
2 3 4
1 0+0 - 0 0
3 4 5
2 0+0 - 0 0
0+0 - 0 4 5 6
3 0
0+0 4+0 0 4 5 6
3 0
0+0 3+0 - 3 4 5 6
3 1