0% found this document useful (0 votes)
13 views

Chapter Four - Dynamic Programming

Programing language lecture note

Uploaded by

Solomonic Eman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chapter Four - Dynamic Programming

Programing language lecture note

Uploaded by

Solomonic Eman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 85

Chapter Four

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:

F(n) = F(n-2) + F(n-1)

with seed values F(1) = 1, F(2) = 1


or F(0) = 0, F(1) = 1

What would a series look like:

0, 1, 1, 2, 3, 4, 5, 8, 13, 21, 34, 55, 89, 144, …

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

What’s the problem?

Compiled by Abebe Z.
Memoization:

Fib(n)
{
if (n == 0)
return M[0];

if (n == 1)
return M[1];

if (Fib(n-2) is not already calculated)


call Fib(n-2);

if(Fib(n-1) is already calculated)


call Fib(n-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

• Given a weighted directed graph,


<u,v,t,x,z> is a path of weight 29 from u to z.
<u,v,w,x,y,z> is another path from u to z; it has weight 16 and
is the shortest path from u to z.

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}.

Thus, (i, j) = dij(n). Also, dij(0) = aij .

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

dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1)}

intermediate vertices inZ. {1, 2, …, k}


Compiled by Abebe
Recursive Formulation
If we do not use intermediate nodes, i.e., when k=0, then
dij(0) = wij
If k>0, then
dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1)}

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

• At each step k dij satisfies the criteria


• Path begins at i & ends at j
• Intermediate vertices on the path come from the set {1, 2,
…, k}

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

● Step 1:- Consider all paths which contain 1 as a


intermediate vertex
■ 2,1,3 is the only path
■ dij(1) = min{dij(0) , dik(0) + dkj(0)}
■ d23(1) = min{d23(0) , d21(0) + d13(0)};
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

● Step 1:- Consider all paths which contain 1 as a


intermediate vertex
■ 2,1,3 is the only path
■ dij(1) = min{dij(0) , dik(0) + dkj(0)}
■ d23(1) = min{d23(0) , d21(0) + d13(0)} = 8
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

● Step 2:- Consider all paths which contain 2 as a


intermediate vertex
■ 3,2,1 is the only path
■ dij(2) = min{dij(1) , dik(1) + dkj(1)}
■ d31(2) = min{d31(1) , d32(1) + d21(1)};
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

● Step 2:- Consider all paths which contain 2 as a


intermediate vertex
■ 3,2,1 is the only path
■ dij(2) = min{dij(1) , dik(1) + dkj(1)}
■ d31(2) = min{d31(1) , d32(1) + d21(1)} = 5
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

● Step 3:- Consider all paths which contain 3 as a


intermediate vertex
■ 1,3,2 is the only path
■ dij(3) = min{dij(2) , dik(2) + dkj(2)}
■ d12(3) = min{d12(2) , d13(2) + d32(2)};
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

● Step 3:- Consider all paths which contain 3 as a


intermediate vertex
■ 1,3,2 is the only path
■ dij(3) = min{dij(2) , dik(2) + dkj(2)}
■ d12(3) = min{d12(2) , d13(2) + d32(2)} = 7
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

Final Result

● We have obtained the value of an optimal solution.


● To obtain the actual solution i.e. the shortest path
between each pair of vertices, use the predecessor
matrix
Compiled by Abebe Z.
Predecessor Matrix
• pij(0) = NIL if i = j or wij = ∞
i if i ≠ j & wij < ∞

• pij(k) = pij(k-1) if dij(k-1) ≤ dik(k-1) + dkj(k-1)


pkj(k-1) if dij(k-1) > dik(k-1) + dkj(k-1)

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

 Being a smart kid, you


apply dynamic
programming.

Compiled by Abebe Z.
Solution

Compiled by Abebe Z.
Sensitivity Analysis
 Valuables are not very valuable when camping, pick
richer people to steal from.

 Suddenly a valuable item of $15 that weighs 11kg is


available, will the optimal solution change?
Yes, the maximum profit becomes $23

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.

Item #(i) Weight(wi) Value(bi)


1 1 8
2 3 6
3 5 5
Compiled by Abebe Z.
0-1 Knapsack problem

Weight Benefit value


Items wi bi

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

2. “Fractional knapsack problem”


• Items are divisible: you can take any fraction of an item

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)

Problem: How to pack the knapsack to achieve


maximum total value of packed items?
Compiled by Abebe Z.
0-1 Knapsack problem
• Problem, in other words, is to find
max  bi subject to w i W
iT iT

 The problem is called a “0-1” problem,


because each item must be entirely
accepted or rejected.

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

• We can do better with an algorithm based


on dynamic programming

• We need to carefully identify the


subproblems

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}

• This is a reasonable subproblem definition.


• The question is:
Can we describe the final solution (SK+1 ) in terms of subproblems (Sk)?

• Unfortunately, we can’t do that.


Basically, the solution to the optimization problem for Sk+1 might NOT
contain the optimal solution from problem Sk.
Compiled by Abebe Z.
Knapsack 0-1 Problem
• Let’s illustrate that point with an example:
Item Weight Value
I0 3 10
n o fa d we
nitio wed a n
I1 8 4 efi
o o ur d m is fla !
S
ro ble r one
I2 9 9 p
sub anothe
I3 8 11 need

• The maximum weight the knapsack can hold is 20.

• 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

• The subproblem then will be to compute V[k,w], i.e.,


to find an optimal solution for Sk = {items labeled 1,
2, .. k} in a knapsack of size w.
• Assuming knowing V[i, j], where i=0,1, 2, … k-1,
j=0,1,2, …w, how to derive V[k,w]?

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

• The best subset of Si that has the total weight  w, either


contains item i or not.
• First case: wi>w. Item i can’t be part of the solution, since if it
was, the total weight would be > w, which is unacceptable.
• Second case: wi  w. Then the item i can be in the solution, and
we choose the case with greater value.
0-1 Knapsack Algorithm
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 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.
else V[i,w] = V[i-1,w] // wi > w
Running Time What is the running time of this
algorithm? O(n*W)
for w = 0 to W
O(W)
V[0,w] = 0
for i = 1 to n O(n)
V[i,0] = 0
for i = 1 to n Repeat n times
for w = 1 to W
O(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]
Remember that the
else
V[i,w] = V[i-1,w]
brute-force algorithm
else V[i,w] = V[i-1,w] // wi > w Compiled by Abebe Z.
takes O(2n)
Example

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)
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

How to find out which items are in the optimal subset?


Comments
• This algorithm only finds the max possible value that can be carried in
the knapsack
• i.e., the value in V[n,W]

To know the items that make this maximum value, an


addition to this algorithm is necessary

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[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 // 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[i1,k] =
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 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[i1,k]
V[i 1,k]=V[3,5]
= =7
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 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[i1,k]=V[2,5]
V[i 1,k] = =7
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 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[i1,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[i1,k] then
mark the ith item as in the knapsack
i =2-1 =1
i = i1, k = k-wi k =5-3 =2
else
W=5
i = i1 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[i1,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[i1,k] then
mark the ith item as in the knapsacki =2-1 =1
i = i1, k = k-wi k =5-3 =2
else
W=5
i = i1 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[i1,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[i1,k] then
2 mark the ith item as in the knapsack
I =1-1 =0
i = i1, k = k-wi K =2-2 =0
else
W=5
i = i1 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[i1,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[i1,k] then
2 mark the ith item as in the knapsack
I =1-1 =0
i = i1, k = k-wi K =2-2 =0
else
W=5
i = i1 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[i1,k] then
2:2 (3, 4)
1 mark the nth item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 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[i1,k] then
2: (3, 4) mark the nth item as in the knapsack
i = i1, k = k-wi
else
W=5
i = i1 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.

You might also like