Chapter Four - Dynamic Programming
Chapter Four - Dynamic Programming
Dynamic Programming
Compiled by Abebe Z.
Dynamic Programming
• Dynamic programming is a very powerful, general tool
for solving optimization problems.
• Once understood it is relatively easy to apply, but
many people have trouble understanding it.
Compiled by Abebe Z.
Greedy Algorithms
• Greedy algorithms focus on making the best local
choice at each decision point.
• For example, a natural way to compute a shortest
path from x to y might be to walk out of x, repeatedly
following the cheapest edge until we get to y.
WRONG!
• In the absence of a correctness proof greedy
algorithms are very likely to fail.
Compiled by Abebe Z.
Problem:
Let’s consider the calculation of Fibonacci numbers:
Compiled by Abebe Z.
Recursive Algorithm:
Fib(n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
Return Fib(n-1)+Fib(n-2)
}
Compiled by Abebe Z.
Recursive Algorithm:
Fib(n)
{ It has a serious issue!
if (n == 0)
return 0;
if (n == 1)
return 1;
Return Fib(n-1)+Fib(n-2)
}
Compiled by Abebe Z.
Recursion tree
Compiled by Abebe Z.
Memoization:
Fib(n)
{
if (n == 0)
return M[0];
if (n == 1)
return M[1];
//Store the ${n}^{th}$ Fibonacci no. in memory & use previous results.
M[n] = M[n-1] + M[n-2]
Return M[n];
}
Compiled by Abebe Z.
already calculated …
Compiled by Abebe Z.
Shortest Path
Floyd-Warshall Algorithm
Compiled by Abebe Z.
Shortest Paths Problems
1
v t
5 4 9
8 13
u 3 x 4 y
2 10
1 1 2
w z
6
Compiled by Abebe Z.
Shortest Path
• Problem: given a weighted directed graph G, find the minimum-
weight path from a given source vertex s to another vertex v
• “Shortest-path” = minimum weight
• Weight of path is sum of edges
• E.g., a road map: what is the shortest path from Chapel Hill to Charlottesville?
Compiled by Abebe Z.
More Shortest Paths Variants
1. All weights are non-negative.
2. Weights may be negative, but no negative cycles
(A cycle is a path from a vertex to itself.)
3. Negative cycles allowed.
Algorithm reports “∞” if there is a negative cycle on path from source to
destination
• (2) and (3) seem to be harder than (1).
v
v 5 u v w
5 u v w
u -3 2 u 0 ∞ ∞
u 3 2 u 0 5 8
v -3 0 3
6
v ∞ ∞ ∞
-6 w -6 -1 0 w
w Compiled by Abebe Z.
w ∞ ∞ ∞
Floyd-Warshall Algorithm
• Dynamic programming solution that solves the problem in O(|V| 3)
time.
• Negative weight edges may be present
• Assumption:- No negative weight cycles
Compiled by Abebe Z.
Intermediate Vertices
Without loss of generality, we will assume that V={1,2,…,n}, i.e., that
the vertices of the graph are numbered from 1 to n.
Given a path p=(1,2,…,m) in the graph, we will call the vertices k with k
in {2,…,m-1} the intermediate vertices of p.
Compiled by Abebe Z. 15
Structure of the Shortest Path
Let dij(k) denote the length of the shortest path from i to
j such that all intermediate vertices are contained in the
set {1,…,k}.
Compiled by Abebe Z. 16
Remark 1
A shortest path does not contain any vertex twice, as this would imply
that the path contains a cycle. By assumption, cycles in the graph have
a positive weight, so removing the cycle would result in a shorter path,
which is impossible.
Compiled by Abebe Z.
Remark 2
Consider a shortest path p from i to j such that the intermediate
vertices are from the set {1,…,k}.
• If the vertex k is not an intermediate vertex on p, then dij(k) = dij(k-1)
• If the vertex k is an intermediate vertex on p, then dij(k) = dik(k-1) + dkj(k-1)
Interestingly, in either case, the subpaths contain merely nodes from {1,…,k-1}.
Compiled by Abebe Z.
Remark 2
Therefore, we can conclude that
Compiled by Abebe Z. 20
The Floyd-Warshall Algorithm
Floyd-Warshall(W)
n = # of rows of W;
D(0) = W;
for k = 1 to n do
for i = 1 to n do
for j = 1 to n do
dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1)};
return D(n);
Compiled by Abebe Z. 21
Time and Space Requirements
The running time is obviously O(n3).
However, in this version, the space requirements are high. One can
reduce the space from O(n3) to O(n2) by using a single array d.
Compiled by Abebe Z. 22
Example
5 1 2 3
D(0)
1 3
1 0 8 5
8
3 2 2 3 0 ∞
2 3 ∞ 2 0
Compiled by Abebe Z.
Example
5 1 2 3
D(0)
1 3
1 0 8 5
8
3 2 2 3 0 ∞
2 3 ∞ 2 0
Final Result
Compiled by Abebe Z.
Example
5 1 2 3
D(1)
1 3
1 0 8 5
8
3 2 2 3 0 8
2 3 ∞ 2 0
1 2 3 1 2 3
P(0) P(1)
1 N 1 1 1 N 1 1
2 2 N N 2 2 N 1
3 N 3 N 3 N 3 N
Compiled by Abebe Z.
Example
5 1 2 3
D(2)
1 3
1 0 8 5
8
3 2 2 3 0 8
2 3 5 2 0
1 2 3
P(2)
1 N 1 1
2 2 N 1
3 2 3 N
Compiled by Abebe Z.
Example
5 1 2 3
D(3)
1 3
1 0 7 5
8
3 2 2 3 0 8
2 3 5 2 0
1 2 3
P(3)
Use this final predecessor 1 N 3 1
matrix to obtain the
optimal result 2 2 N 1
3 2 3 N
Compiled by Abebe Z.
Printing the Shortest Path between vertices i & j
print( P, i, j)
{
if i = j then print i
else if pij = NIL then print “No Path”
else{
print(P, i, pij)
print j
}
}
Compiled by Abebe Z.
0-1 Knapsack problem
Compiled by Abebe Z.
Knapsack Problem
You are a mischievous child camping
in the woods. You would like to steal
items from other campers, but you
can only carry so much mass in your
knapsack. You see seven items worth
stealing. Each item has a certain
mass and monetary value. How do
you maximize your profit so you
can buy more video games
later?
Compiled by Abebe Z.
Knapsack Problem
Value and mass of each
item is given
Maximize profit
Subject to mass
constraint of knapsack:
15 kg
Compiled by Abebe Z.
Solution
Compiled by Abebe Z.
Sensitivity Analysis
Valuables are not very valuable when camping, pick
richer people to steal from.
Compiled by Abebe Z.
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 W.
So we must consider weights of items as well as
their values.
2 3
This is a knapsack 3 4
Max weight: W = 20 4 5
5 8
W = 20
9 10
Compiled by Abebe Z.
Knapsack problem
There are two versions of the problem:
1. “0-1 knapsack problem”
• Items are indivisible; you either take an item or not. Some
special instances can be solved with dynamic programming
Compiled by Abebe Z.
0-1 Knapsack problem
• Given a knapsack with maximum capacity W, and a set S consisting of
n items
• Each item i has some weight wi and benefit value bi (all wi, bi and W
are integer values)
Compiled by Abebe Z.
Brute-force approach
Let’s first solve this problem with a
straightforward algorithm
• Since there are n items, there are 2n possible
combinations of items.
• We go through all combinations and find the one
with maximum value and with total weight less
or equal to W
• Running time will be O(2n)
Compiled by Abebe Z.
Dynamic programming approach
Compiled by Abebe Z.
Defining a Subproblem
If items are labeled 1..n, then a subproblem would be to find
an optimal solution for Sk = {items labeled 1, 2, .. k}
• The best set of items from {I0, I1, I2} is {I0, I1, I2}
• BUT the best set of items from {I0, I1, I2, I3} is {I0, I2, I3}.
• In this example, note that this optimal solution, {I0, I2, I3}, does
NOT build upon the previous optimal solution, {I0, I1, I2}.
• (Instead it builds upon the solution,
Compiled by{IAbebe
0, I2}, which is really the optimal
Z.
Defining a Subproblem
• Let’s add another parameter: w, which will represent
the maximum weight for each subset of items
Compiled by Abebe Z.
Recursive Formula
V [i 1, w] if wi w
V [i, w]
max{V [i 1, w],V [i 1, w wi ] bi } else
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
Compiled by Abebe Z.
Example
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1
2
3
4
for w = 0 to W
V[0,w] = 0
Compiled by Abebe Z.
Example
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
for i = 1 to n
V[i,0] = 0
Compiled by Abebe Z.
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=1 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=3
1 0 0
wi=2
2 0
3 0 w=1
4 0 w-wi =-1
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z. V[1,1] = V[0,1]
else V[i,w] = V[i-1,w] // w > w = 0
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=1 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=3
1 0 0 3
wi=2
2 0
3 0 w=2
4 0 w-wi =0
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 3+V[0,0] > V[0,2]
3+0 > 0
V[i,w] = bi + V[i-1,w- wi] V[1,2] = 3
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=1 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=3
1 0 0 3 3
wi=2
2 0
3 0 w=3
4 0 w-wi =1
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 3+V[0,1] > V[0,3]
3+0 > 0
V[i,w] = bi + V[i-1,w- wi]
V[1,3] = 3
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
else V[i,w] = V[i-1,w] // w > w
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=1 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=3
1 0 0 3 3 3
wi=2
2 0
3 0 w=4
4 0 w-wi =2
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 3+V[0,2] > V[0,4]
3+0 > 0
V[i,w] = bi + V[i-1,w- wi]
V[1,4] = 3
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
else V[i,w] = V[i-1,w] // wi > w
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=1 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=3
1 0 0 3 3 3 3
wi=2
2 0
3 0 w=5
4 0 w-wi =3
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 3+V[0,3] > V[0,5]
V[i,w] = bi + V[i-1,w- wi] 3+0 > 0
else V[1,5] = 3
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=2 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=4
1 0 0 3 3 3 3
wi=3
2 0 0
3 0 w=1
4 0 w-wi =-2
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
V[2,1] = V[1,1]
= 0
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=2 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3
3 0 w=2
4 0 w-wi =-1
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z. V[2,2] = V[1,2]
= 3
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=2 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4
3 0 w=3
4 0 w-wi =0
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 4+V[1,0] > V[1,3]
4+0 > 3
V[i,w] = bi + V[i-1,w- wi]
V[2,3] = 4
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
else V[i,w] = V[i-1,w] // w > w
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=2 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4 4
3 0 w=4
4 0 w-wi =1
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 4+V[1,1] > V[1,4]
4+0 > 3
V[i,w] = bi + V[i-1,w- wi]
V[2,4] = 4
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=2 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4 4 7
3 0 w=5
4 0 w-wi =2
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 4+V[1,2] > V[1,5]
V[i,w] = bi + V[i-1,w- wi] 4+3 > 3
else V[2,5] = 7
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
else V[i,w] = V[i-1,w] // w > w
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=3 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
3 0 0 3 4 w= 1..3
4 0
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z. V[3,1] = V[2,3]
V[3,2]
V[3,3] V[2,1]
V[2,2]
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=3 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
3 0 0 3 4 5 w= 4
4 0 w- wi=0
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 5+V[2,0] > V[2,4]
V[i,w] = bi + V[i-1,w- wi] 5+0 > 4
V[3,4] = 5
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
else V[i,w] = V[i-1,w] // w > w
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=3 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
3 0 0 3 4 5 7 w= 5
4 0 w- wi=1
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 5+V[2,1] > V[2,5]
V[i,w] = bi + V[i-1,w- wi] 5+0 > 7
V[3,4] = 7
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=4 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=6
1 0 0 3 3 3 3
wi=5
2 0 0 3 4 4 7
3 0 0 3 4 5 7 w= 1..4
4 0 0 3 4 5
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z. V[4,3]
V[4,1] = V[3,4]
V[4,4]
V[4,2] V[3,3]
V[3,1]
V[3,2]
= 54
0
Items:
i wi bi
Example 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=4 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
bi=6
1 0 0 3 3 3 3
wi=5
2 0 0 3 4 4 7
3 0 0 3 4 5 7 w= 5
4 0 0 3 4 5 7 w- wi=0
for i = 1 to n
for w = 1 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w] 6+V[3,1] > V[3,5]
V[i,w] = bi + V[i-1,w- wi] 6+0 > 7
V[4,5] = 7
else
V[i,w] = V[i-1,w]
Compiled by Abebe Z.
else V[i,w] = V[i-1,w] // w > w
Exercise
Compiled by Abebe Z.
How to find actual Knapsack Items
• All of the information we need is in the table.
• V[n,W] is the maximal value of items that can be
placed in the Knapsack.
• Let i=n and k=W
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 // Assume the ith item is not in the knapsack
// Could it be in the optimally packed
knapsack? Compiled by Abebe Z.
Items:
i wi bi
Finding the Items 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=4 3: (4, 5)
0 0 0 0 0 0 0 k= 5 4: (5, 6)
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] =
4 0 0 3 4 5 7 V[i1,k] =
i=n, k=W
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 Compiled by Abebe Z.
Items:
i wi bi
Finding the Items 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=4 3: (4, 5)
0 0 0 0 0 0 0 k= 5 4: (5, 6)
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = V[4,5]=7
V[i,k]
4 0 0 3 4 5 7 V[i1,k]
V[i 1,k]=V[3,5]
= =7
i=n, k=W
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 Compiled by Abebe Z.
Items:
i wi bi
Finding the Items 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=3 3: (4, 5)
0 0 0 0 0 0 0 k= 5 4: (5, 6)
1 0 0 3 3 3 3 bi=5
2 0 0 3 4 4 7 wi=4
3 0 0 3 4 5 7 V[i,k] ==V[3,5]=7
V[i,k]
4 0 0 3 4 5 7 V[i1,k]=V[2,5]
V[i 1,k] = =7
i=n, k=W
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 Compiled by Abebe Z.
Items:
i wi bi
Finding the Items 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=2 3: (4, 5)
0 0 0 0 0 0 0 k= 5 4: (5, 6)
1 0 0 3 3 3 3 bi=4
0 0 3 4 4 7 wi=3
2
3 0 0 3 4 5 7 V[i,k]
V[i,k]==V[2,5]=7
4 0 0 3 4 5 7 V[i1,k]=V[1,5]
V[i 1,k] = =3
i=n, k=W
k wi= 5-3=2
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsack
i =2-1 =1
i = i1, k = k-wi k =5-3 =2
else
W=5
i = i1 Compiled by Abebe Z.
Items:
i wi bi
Finding the Items 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=2 3: (4, 5)
0 0 0 0 0 0 0 k= 5 4: (5, 6)
1 0 0 3 3 3 3 bi=4
0 0 3 4 4 7 wi=3
22
3 0 0 3 4 5 7 V[i,k] = V[2,5]=7
V[i1,k]=V[1,5] =3
4 0 0 3 4 5 7
k wi= 5-3=2
i=n, k=W
while i,k > 0
if V[i,k] V[i1,k] then
mark the ith item as in the knapsacki =2-1 =1
i = i1, k = k-wi k =5-3 =2
else
W=5
i = i1 Compiled by Abebe Z.
Items:
i wi bi
Finding the Items 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=1 3: (4, 5)
0 0 0 0 0 0 0 k= 2 4: (5, 6)
1 0 0 3 3 3 3 bi=3
2 0 0 3 4 4 7 wi=2
3 0 0 3 4 5 7 V[i,k] ==V[1,2]=3
V[i,k]
V[i1,k]=V[0,2]
V[i 1,k] = =0
4 0 0 3 4 5 7
k wi=0
i=n, k=W
while i,k > 0
if V[i,k] V[i1,k] then
2 mark the ith item as in the knapsack
I =1-1 =0
i = i1, k = k-wi K =2-2 =0
else
W=5
i = i1 Compiled by Abebe Z.
Items:
i wi bi
Finding the Items 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=1 3: (4, 5)
0 0 0 0 0 0 0 k= 2 4: (5, 6)
11 0 0 3 3 3 3 bi=3
2 0 0 3 4 4 7 wi=2
3 0 0 3 4 5 7 V[i,k] ==V[1,2]=3
V[i,k]
V[i1,k]=V[0,2]
V[i 1,k] = =0
4 0 0 3 4 5 7
k wi=0
i=n, k=W
while i,k > 0
if V[i,k] V[i1,k] then
2 mark the ith item as in the knapsack
I =1-1 =0
i = i1, k = k-wi K =2-2 =0
else
W=5
i = i1 Compiled by Abebe Z.
Items:
i wi bi
Finding the Items 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 i=0 3: (4, 5)
0 0 0 0 0 0 0 k= 0 4: (5, 6)
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7 The optimal
knapsack
4 0 0 3 4 5 7
should contain
i=n, k=W {1, 2}
while i,k > 0
1: (2, 3) if V[i,k] V[i1,k] then
2:2 (3, 4)
1 mark the nth item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 Compiled by Abebe Z.
Items:
i wi bi
Finding the Items 1: (2, 3)
2: (3, 4)
i\W 0 1 2 3 4 5 3: (4, 5)
0 0 0 0 0 0 0 4: (5, 6)
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7 The optimal
knapsack
4 0 0 3 4 5 7
should contain
i=n, k=W {1, 2}
while i,k > 0
1: (2, 3) if V[i,k] V[i1,k] then
2: (3, 4) mark the nth item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 Compiled by Abebe Z.
Memorization (Memory Function Method)
• Goal:
• Solve only subproblems that are necessary and solve it only once
• Memorization is another way to deal with overlapping subproblems
in dynamic programming
• With memorization, we implement the algorithm recursively:
• If we encounter a new subproblem, we compute and store the solution.
• If we encounter a subproblem we have seen, we look up the answer
• Most useful when the algorithm is easiest to implement recursively
• Especially if we do not need solutions to all subproblems.
Compiled by Abebe Z.
Conclusion
• Dynamic programming is a useful technique of solving
certain kind of problems
• When the solution can be recursively described in terms of
partial solutions, we can store these partial solutions and
re-use them as necessary (memorization)
• Running time of dynamic programming algorithm vs. naïve
algorithm:
• 0-1 Knapsack problem: O(W*n) vs. O(2n)
Compiled by Abebe Z.