Beruflich Dokumente
Kultur Dokumente
Programming
1
Dynamic Programming
2
Optimal substructure:
We say that a problem exhibits optimal
substructure if an optimal solution to the
problem contains within its optimal solution to
subproblems.
3
Subtleties
One should be careful not to assume that optimal substructure
applies when it does not. consider the following two problems in
which we are given a directed graph G = (V, E) and vertices u, v
V.
Unweighted shortest path:
Find a path from u to v consisting of the fewest edges.
Good for Dynamic programming.
Unweighted longest simple path:
Find a simple path from u to v consisting of the most edges.
Not good for Dynamic programming.
4
Steps in developing a dynamic
programming algorithm
Problem:
We are given a sequence(chain)
<A1, A2, A3,…An> of n matrices
to be multiplied, we wish to compute
the product A1A2….An.using least work.
6
Since matrix multiplication is associative so all ways
of parenthesization will yield the same answer.
(A1(A2(A3A4)))
(A1((A2A3)A4))
((A1A2)(A3A4))
((A1(A2A3))A4)
(((A1A2)A3)A4)
7
Although answer by each one will be same, but the
cost of evaluating varies drastically.
8
P (k ) P (n k ) ifn 2
P(n) = ∑k=1n-1P(k)P(n-k)
9
Let us use Dynamic programming approach
10
To evaluate Ai..j We must find an index k
Ai..j = Ai..k Ak+1..j for some k in between i and j.
Total cost to get Ai..j =
11
Recursive solution
0 if i=j
m[i ,j] =
min{m[i ,k] + m[k+1 ,j] + pi-1pkpj
13
To keep track of successive decisions let us use
S[i j] to denote the value of index k from where
sequence Ai…Aj is to be partitioned.
14
for finding m[i ,j]
optimal results for Ai..k and Ak+1..j are used
and
size of Ai..k is k-i+1 which is less than j-i+1
Also
size ofAk+1..j is j-k which is also less than j-i+1
15
Matrix-chain-order (p)
1 n ← len[p] - 1
2 For i ← 1 to n
3 do m[i ,i] ← 0
4 for i ← 2 to n
5 do for l ← 1 to n- i +1
6 do j ← i+l -1
7 m[i,j] ← α
8 for k ← I to j – 1
9 do q ← m[I , k] + m [ k+1 ,j] +
pi-1pkpj
16
10 If q < m[I,j]
11 then m[I,j] ← q
12 s[i,j] ← k
13 Return m , s
17
MatrixDimension
A1 30 x 35
A2 35 x 15
A3 15 x 5
A4 5 x 10
A5 10 x 20
A6 20 x 25
m[2,2] + m[3,5] + p1p2p5 = 13000
M[2,5] = min m[2,3] +m[4,5] + p1p3p5 = 7125
m[2,4] + m[5,5] + p1p4p5 = 11375
= 7125
18
M
1
6 i
15125 2
5 3
10500
11875
j 4 5375
7125 4
9375 5
3500
3 2500 6
4375 5000
7875 1000
750 0
2 2625 0
15750
0
0
1 0 0
19
S
1
6 i
2
3
3
5 3
j 3
4 3 4
3
3 5 5
3 3
1 3 5
4
2 2 3
1
20
Optimal solution
Optimal way to multiplication of these six
matrices
((A1(A2A3))((A4A5)A6))
21
Example
Let us begin with a simple capital budgeting
problem.
22
An Example of
Dynamic
Programming
23
Courses
A student has to take examination A B C
in three courses, A,B,C. He days
has three days available for
study. He feels it would be best 0 0 1 0
to devote a whole day to study
of the same course, so that he
may study a course for one 1 1 1 1
day, two days, or three days or
not at all. His estimates of the
grades he may get by study
are as follows: 2 1 3 3
How should he study so
that he maximizes the 3 3 4 3
sum of his grades
24
Let u1, u2,u3 be the no of days he should study
the courses A,B,C resp.
25
let us introduces the state variables xj as:
X3 = u 1 + u 2 + u 3 ≤ 3
X2 = u 1 + u 2 = x 3 – u 3
X1 = u 1 = x2 – u2
Xj denotes no of days available to study
courses from course 1 to course j
Fj(xj)= max [fj(uj) + Fj-1(x j-1)] X j-1 = xj - uj
F1(x1) = f1(u1)
F3 (x3) = max [ f1(u1) + f2(u2) +f3(u3)
26
F2(3)=max( f2(0)+ F1(3-0), f2(1)+F1(3-1),f2(2)+F1(3-2),f2(3)+F1(3-3)
F1(x1)
Cours
0 1 - - - 1
es A B C
days 1 2 1 - - 2
0 0 1 0
2 2 2 3 - 3
1 1 1 1
2 1 3 3 3 4 2 4 4 4
3 3 4 3 27
Cours F2(u +F1(x1 F2(x
es A B C 2) ) 2)
U2
days x2
0 1 2 3
0 0 1 0 0 1 - - - 1
1 1 1 1 1 2 1 - - 2
2 1 3 3 2 2 2 3 - 3
3 3 4 3 3 4 2 4 4 4
F3(u3)
+F2(x2) F3(x
3)
U3 0 1 2 3
x3
0 1 - - - 1
1 2 2 - - 2
2 3 3 4 - 4
28
3 4 4 5 4 5
Thus the optimal way to study
u3 = 2
F2(3-2)=F2[1] obtained by decision u2 = 0
F1(3-2-0)=F1[1] =1 obtained by taking u1 = 1
30
Optimal Binary search Tree
It is one of the most important data structures in
Computer Science
for for
do int
do while
if while
int
if
32
General case with different
probabilities
Let us take the set of n distinct keys {k 1, k2….kn}
Where k1< k2 < …<.kn
And
p(i)= prob. of ki
some searches may not be in K, so we have k+1 dummy
keys d0,d1,…dn representing values not in K.
let
do - all values less than k1
dn all values greater than kn
di all values between ki and ki+1
33
Optimal Binary search trees
cost:2.75
cost:2.80
optimal!!
34
n n
i 1
pi q
i 0
i 1
we assume that the actual cost of a search is the number of nodes examined .i.e. the depth of the
node found by the search in T plus one
n n
E[search cost in T] (depth T (ki ) 1) pi (depth T (d i ) 1) qi
i 1 i 0
n n
1 depth T (ki ) pi depth T (d i ) qi
i 1 i 0
35
For a given set of probabilities, our goal is
to construct a binary search tree whose
expected search is smallest. We call such a
tree an optimal binary search tree. 36
Note: optimal binary search tree need not be
of least height
37
The structure of an optimal binary search
tree
38
If an optimal binary search tree T has a subtree
T' containing keys ki, ..., kj,
39
Given keys ki,… kj one of the keys say kr will be
root of optimal tree of these keys.
left subtree of it will contain keys
ki … kr-1,
di-1,… dr-1
While right subtree will have
kr+1… .kj
dr,… dj
Note for the subtree rooted at ki
Left subtree will have di-1 only
While for the tree rooted at kj will contain only dj as
right subtree.
40
e[i,j] = expected cost of searching in optimal
binary search tree for ki,…kj
Note
if j = I-1
e[I,r-1]
e[r+1,j]
42
l j l j
w(i, j ) pl
l i
ql
l i 1
43
Step 2: A recursive solution
j j
w(i, j ) pl ql
l i l i 1
w(i, j ) w(i, j 1) pj qj
qi 1 if j i -1,
e[i, j ]
min {e[i, r 1] e[r 1, j ] w(i, j )} if i j.
ir j
44
computing the expected search cost of an
optimal binary search tree
OPTIMAL-BST(p,q,n)
1 for i 1 to n + 1
2 do e[i, i – 1] qi-1
3 w[i, i – 1] qi-1
4 for l 1 to n
5 do for i 1 to n – l + 1
6 do j i + l – 1
7 e[i, j]
8 w[i, j] w[i, j – 1] + pj+qj 45
9 for r i to j
10 do t e[i, r –1]+e[r +1, j]+w[i, j]
11 if t < e[i, j]
12 then e[i, j] t
13 root[i, j] r
14 return e and root
the OPTIMAL-BST procedure takes (n3), just
like MATRIX-CHAIN-ORDER
46
The table e[i,j], w[i,j], and root[i,j] computer by
OPTIMAL-BST on the key distribution.
47
Root[i,j] = index r for which kr is root of the
optimal binary search tree for ki,….kj
48
Knuth has shown that there are always roots of
optimal subtrees such that root[i, j –1] root[i+1, j]
for all 1 i j n. We can use this fact to modify
Optimal-BST procedure to run in (n2) time.
49
Draw all binary search trees for set
{ while ,if and do} and find expected costs
while if
if do while
do
a b
50
do
do while
if while
do
while if if
c d e
51
Ex1: pi = 1/7 =qi
Ex2 p = (0.5, 0.1 , 0.05)
q = (0.15, 0.1, 0.05, 0.05)
ex1 ex2
a 19/7 3.0
b 17/7 2.25
c 19/7 1.85
d 19/7 2.40
e 19/7 1.95
52
To apply dynamic programming approach we
need to view the construction of such a tree
as the result of a sequence of decisions.
A possible approach would be to make a
decision as to which of the ki’ s should be
assigned to the root node of the tree.
If we choose kr as root then internal nodes k1,
k2, …kk-1 as well as external nodes for the
classes d0, d1,…dk-1 will lie in the left subtree
“l”of the root. Remaining will be be in the
right subtree “r.”
53
Plant1 Plant2 Plant 3
Proposal C1 r1 C2 r2 C3 r3
1 0 0 0 0 0 0
2 1 5 2 8 1 4
3 2 6 3 9 - -
4 - - - -
4 12
54
Each plant will only be permitted to work on one of
its proposals.
The goal is to maximize the firm’s revenue resulting
from the allocation of the $ 5 million.
A straight forward way to solve this is to try all
possibilities and choose the best.
In this case there are only 3 4 2 = 24 ways of
allocating the money.
Many of these are infeasible.
55
Let us break the problem into three stages:
Each stage represents the money allocated
to a single plant.
So stage one represents the money
allocated to plant 1
Stage 2 the money to plant 2
Stage 3 the money allocated to plant 3
Each stage is divided into states.A state
encompasses the information required to
go from one stage to the next
56
{0,1,2,3,4,5}: the amount of money spent on
plant 1.
{0,1,2,3,4,5}: the amount of money spent on
plants 1 and 2
{5}: the amount of money spent on plants 1,2
and 3.
57
The easy possibility is stage 1
58
We are now ready to compute for stage 2.
In this case we want to find the best solution
for both plants 1 and 2.
We want to calculate the best revenue for a
given amount for plants 1 and 2.
We simply go through all the plant 2
proposals, allocate the given amount of
fund to it and use the above table to see
how plant 1 will spend the remainder.
59
uppose we want to determine best allocation for state
value 4.
proposal 1 of 2 gives revenue 0, leaves 4 for plant 1
which returns 6: total:6
proposal 2 of 2 gives revenue 8 leaves 2 for plant 1
which returns 6: total:14
proposal 3 of 2 gives revenue 9, leaves 1 for plant 1
which returns 5: total:6
proposal 4 of 2 gives revenue 12, leaves 0 for plant 1
which returns 0: total:12
60
If available Then the And the
capital x2 optimal revenue for
proposal is stages 1
and 2 is
0 1 0
1 1 5
2 2 8
3 2 13
4 2 or 3 14
5 4 17
61
We can now go on to stage 3.
The only value we are interested in is x3 = 5.
One again we go through all the proposals
for this stage, determine the amount of
money remaining and use the above table
to decide the value for previous stages.
Proposal 1 of plant 3 gives revenue 0 and
leaves 5 to prev. stage giving revenue = 17
Proposal 2 of plant 3 gives revenue 4 leaves
4 prev stages give 14 so total revenue =
18
62
Here calculations are done recursively.
Stage 2 calculations are based on stage 1,
Stage 3 only on stage 2.
In fact given at a state all future decisions
are made independent of how you got to
the state.
This is principle of optimality.
Dynamic programming rests on this
assumption.
63
Principle of Optimality
an optimal sequence of decisions has
the property that whatever the initial
state and decisions are, the remaining
decisions must constitute an optimal
decision sequence with regard to the
state resulting from the first
decision.
64
All pair shortest paths
The Floyd Warshall Algorithm
67
Let
Ak(i , j) represent the length of a shortest path from i
to j going through no vertex of index greater than k,
A(i, j) represent shortest distance between i and j
A(i,j) = min {min{Ak-1(i , k) + A k-1 ( k , j)},
cost (i, j)}
1 kn
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^ 4 3 4 5 ^
81
A P
0 1 -3 2 -4
3 0 -4 1 -1 ^ 3 4 5 1
7 4 0 5 3 4 ^ 4 2 1
2 -1 -5 0 -2 4 3 ^ 2 1
8 5 1 6 0 4 3 4 ^ 1
4 3 4 5 ^
82
Path from node 1 to node 3
83
Warshall’s Algorithm
86
Warshall’s Algorithm
(a) Digraph
(b) Adjacency matrix
kth matrix
directed path in the digraph
Element rij(k ) is 1 only if there is a path from
ith vertex to the jth vertex with intermediate
vertices numbered not higher than k
88
Warshall’s Algorithm
Recurrence Relation
(k )
rij is 1
vi, list of vertices numbered not higher
than k, vj ( k 1)
rij
2 possibilities
- List does not contain kth vertex , it
( k 1)
r
contains vertices numbered not ik higher
( k 1)
than k-1. Then kj is 1
r
89
- List contains kth vertex. Then
vi, vertices numbered ≤ k-1, vk, vertices
numbered ≤ k-1, vj
=> and are both 1
90
Warshall’s Algorithm
If an element rij is 1 in ( k 1)
, it remains 1 in .
R
(k )
If an element rij is 0 in , it has to be changed to 1 in if
R
and only if the element in its row( k i1) and column k and the
element in its column j and rowR k are both 1’s in .
Basis: Adjacency matrix A
(k )
R
91
Warshall’s Algorithm
1 2 3 4
1 0 1 0 0
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0
1 2 3 4
1 0 1 0 0
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0 92
Warshall’s Algorithm
1 2 3 4
1 0 1 0 0
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0
1 2 3 4
1 0 1 1 0
2 0 0 1 0
3 0 0 0 1 93
4 0 0 0 0
Warshall’s Algorithm
1 2 3 4
1 0 1 1 0
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0
1 2 3 4
1 0 1 1 1
2 0 0 1 1
3 0 0 0 1
94
4 0 0 0 0
Warshall’s Algorithm
1 2 3 4
1 0 1 1 1
2 0 0 1 1
3 0 0 0 1
4 0 0 0 0
1 2 3 4
1 0 1 1 1
2 0 0 1 1
3 0 0 0 1
95
4 0 0 0 0
Warshall’s Algorithm
96
Shortest path problem
Suppose we have a graph that represents the high
way system of a state or country. In this graph ,
the vertices represent cities and the edges
represent sections of the high way. Each edge
has a weight representing the distance between
the two cities connected by the edge.
A motorist wishing to derive from city A to city b
would be interested answers to the following
questions:
Is there a path from A to B?
If there are more than one path from A to B which
path is the shortest.
97
The problem of finding shortest path from a specified
Vertex S to another specified vertex T can be stated as
follows:
A simple weighted digraph G on vertex is described
by an N*N , weight matrix D where
dij=length of the directed edge from vertex i to
vertex j, dij >= 0
dij =0
dij = α if there is no edge from i to j
98
In general dij �dji ,and triangle inequality need not be
Satisfied , ie dij + djk may be less than dik .
99
Dijkstra’s Algorithm
d[i,j] – Weight of the arc from i to j
Distance[i] – keeps the distance of the shortest path known
thus far from s to i.
Initially distance[s]=0 and distance[i] = α for all i �
≠ s
100
Initially only member of perm is s.
As soon as t becomes a member of perm, algorithm termin
The algorithm maintains a variable “current “ which is the
node that has been added to perm, most recently. Initially
Current =s. Whenever a node current is added to perm,
distance must be recomputed for all vertices which
are successors of current.
102
2
a d
8
7 2
2 1
10
3
b 2
e g
4
1 4
7
c 3 f
103
A B C D E F G
α 0 α α α α α
7 0 1 α α α α
7 0 1 α α α α
4 0 1 α 5 4 α
4 0 1 α 5 4 α
4 0 1 14 5 4 11
4 0 1 14 5 4 11
4 0 1 12 5 4 11
4 0 1 12 5 4 11
4 0 1 12 5 4 7
4 0 1 12 5 4 7
104
Path can also determined moving backward.
Start back from node G till we find a place where
present value of distance is obtained. At that place
the current gives the node which precedes G.
BCEG
105
Bellman Ford Algorithm
106
107
TheBellman Ford algorithm solves the single
source shortest paths problem in general
case in which edge weights may be negative.
Algorithm returns a boolean value indicating
whether or not there is a negative weigh
cycle that is reachable from the source.
108
When there is no cycle of negative length,
there is a shortest path between any two
vertices of an n- vertex graph that has at most
n-1 edges in it.
Distl [u] be length of a shortest path between
source v to a vertex u under the constraint
that sh.path contains at most l edges.
Distn-1 [u] is the length of shortest path from v
to u
109
Recursive relation
110
6 2 -1 5
1 -2 1 3
5 3
5 -2 3
6
4
-1
111
distk [1..7]
K 1 2 3 4 5 6 7
1 0 6 5 5 ∞ ∞ ∞
2 0 3 3 5 5 4 ∞
3 0 1 3 5 2 4 7
4 0 1 3 5 0 4 5
5 0 1 3 5 0 4 3
6 0 1 3 5 0 4 3
112
113
114
115
116
117
118
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
119
120
dist pred
S 0 s
T 6, s
X α
Y 7 s
Z α
S y x t
7 -3 -2 125
Problem: find shortest paths from A to all others adding
edges in the order
be, db, bd, ab, ac, dc, bc, ed
B
2
-1
2
3 1 E
A
-3
D
4
C 5
126
Review: Dynamic programming
DP is a method for solving certain kind of
problems
DP can be applied when the solution of a
problem includes solutions to subproblems
We need to find a recursive formula for the
solution
We can recursively solve subproblems, starting
from the trivial case, and save their solutions in
memory
In the end we’ll get the solution of the whole
problem
127
be solved with dynamic
programming
Simple Subproblems
We should be able to break the original problem to
smaller subproblems that have the same structure
Optimal Substructure of the problems
The solution to the problem must be a composition
of subproblem solutions
Subproblem Overlap
Optimal subproblems to unrelated problems can
contain subproblems in common
128
Longest Common Subsequence
X = < A, B, C, B, D, A, B >
Y = < B, D, C, A, B, A >
< B, C, A > is a common subsequence of
both X and Y.
< B, C, B, A > or < B, C, A, B > is the longest
common subsequence of X and Y.
129
Longest-common-subsequence
problem:
We are given two sequences X =
<x1,x2,...,xm> and Y = <y1,y2,...,yn> and wish to
find a maximum length common
subsequence of X and Y.
130
(Optimal substructure of LCS)
Let X = <x1,x2,...,xm> and Y = <y1,y2,...,yn> be the
sequences, and let Z = <z1,z2,...,zk> be any LCS of X
and Y.
1. If xm = yn
then zk = xm = yn and Zk-1 is an LCS of Xm-1 and Yn-1.
2. If xm yn
then zk xm implies Z is an LCS of Xm-1 and Y.
3. If xm yn
then zk yn implies Z is an LCS of X and Yn-1.
131
A recursive solution to
subproblem
Define c [i, j] is the length of the LCS of Xi
and Yj .
0 if i=0 or j=0
c[i, j ] c[i 1, j 1] 1 if i,j>0 and xi=y j
max{c[i, j 1], c[i 1, j ]} if i,j>0 and x y
i j
132
Computing the length of an LCS
LCS_LENGTH(X,Y)
1 m length[X]
2 n length[Y]
3 for i 1 to m
4 do c[i, 0] 0
5 for j 1 to n
6 do c[0, j] 0
7 for i 1 to m
8 do for j 1 to n
133
9 do if xi = yj
10 then c[i, j] c[i-1, j-1]+1
11 b[i, j] “”
12 else if c[i–1, j] c[i, j-1]
13 then c[i, j] c[i-1, j]
14 b[i, j] “”
15 else c[i, j] c[i, j-1]
16 b[i, j] “”
17 return c and b
134
Complexity: O(mn)
135
PRINT_LCS
PRINT_LCS(b, X, c, j )
1 if i = 0 or j = 0
2 then return
Complexity: O(m+n)
3 if b[i, j] = “”
4 then PRINT_LCS(b, X, i-1, j-1)
5 print xi
6 else if b[i, j] = “”
7 then PRINT_LCS(b, X, i-1, j)
8 then PRINT_LCS(b, X, i, j-1)
136
Review: Longest Common
Subsequence (LCS)
Problem: how to find the longest pattern of
characters that is common to two text strings X
and Y
Dynamic programming algorithm: solve
subproblems until we get the final solution
Subproblem: first find the LCS of prefixes of X
and Y.
this problem has optimal substructure: LCS of
two prefixes is always a part of LCS of bigger
strings
137
Review: Longest Common
Subsequence (LCS) continued
Define Xi, Yj to be prefixes of X and Y of
length i and j; m = |X|, n = |Y|
We store the length of LCS(Xi, Yj) in c[i,j]
Trivial cases: LCS(X0 , Yj ) and LCS(Xi, Y0) is
empty (so c[0,j] = c[i,0] = 0 )
Recursive formula for c[i,j]:
c[i 1, j 1] 1 if x[i ] y[ j ],
c[i, j ]
max(c[i, j 1], c[i 1, j ]) otherwise
c[m,n] is the final solution 138
Review: Longest Common
Subsequence (LCS)
After we have filled the array c[ ], we can use
this data to find the characters that constitute
the Longest Common Subsequence
139
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?
140
0-1 Knapsack problem:
a picture
Weight Benefit value
wi bi
Items
3
2
This is a knapsack 3 4
Max weight: W = 20 4 5
5 8
W = 20
9 10
141
0-1 Knapsack problem
Problem, in other words, is to find
max bi subject to wi W
iT iT
The problem is called a “0-1” problem,
because each item must be entirely accepted
or rejected.
Just another version of this problem is the
“Fractional Knapsack Problem”, where we
can take fractions of items.
142
0-1 Knapsack problem: 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 the most total value and with total
weight less or equal to W
Running time will be O(2n)
143
0-1 Knapsack problem: brute-
force approach
Can we do better?
Yes, with an algorithm based on dynamic
programming
We need to carefully identify the
subproblems
Let’s try this:
If items are labeled 1..n, then a subproblem
would be to find an optimal solution for
Sk = {items labeled 1, 2, .. k}
144
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 valid subproblem definition.
The question is: can we describe the final
solution (Sn ) in terms of subproblems (Sk)?
Unfortunately, we can’t do that. Explanation
follows….
145
Defining a Subproblem
w1 =2 w2 =4 w3 =5 w4 =3 Weight Benefit
b1 =3 b2 =5 b3 =8 b4 =4 Item wi bi
? #
1 2 3
Max weight: W = 20 S4 2 3 4
For S4:
S5
Total weight: 14; 3 4 5
total benefit: 20 4 5 8
5 9 10
w1 =2 w2 =4 w3 =5 w4 =9
b1 =3 b2 =5 b3 =8 b4 =10
For S5:
Solution for S4 is
Total weight: 20 not part of the
total benefit: 26 solution for S5!!! 146
Defining a Subproblem
(continued)
As we have seen, the solution for S4 is not part
of the solution for S5
So our definition of a subproblem is flawed
and we need another one!
Let’s add another parameter: w, which will
represent the exact weight for each subset of
items
The subproblem then will be to compute
B[k,w]
147
Recursive Formula for
subproblems
Recursive formula for subproblems:
B[k 1, w] if wk w
B[k , w]
max{B[k 1, w], B[k 1, w wk ] bk } else
It means, that the best subset of Sk that has
total weight w is one of the two:
1) the best subset of Sk-1 that has total weight w,
or
2) the best subset of Sk-1 that has total weight w-
wk plus the item k
148
Recursive Formula
B[k 1, w] if wk w
B[k , w]
max{B[k 1, w], B[k 1, w wk ] bk } else
The best subset of Sk that has the total weight
w, either contains item k or not.
First case: wk>w. Item k can’t be part of the
solution, since if it was, the total weight would
be > w, which is unacceptable
Second case: wk <=w. Then the item k can be
in the solution, and we choose the case with
greater value
149
0-1 Knapsack Algorithm
for w = 0 to W
B[0,w] = 0
for i = 0 to n
B[i,0] = 0
for w = 0 to W
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
150
Running time
for w = 0 to W
O(W)
B[0,w] = 0
for i = 0 to n
Repeat n times
B[i,0] = 0
for w = 0 to W
O(W)
< the rest of the code >
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
152
Example
i 0 1 2 3 4
W
0 0
1 0
2 0
3 0
4 0
5 0
for w = 0 to W
B[0,w] = 0
153
Example
i 0 1 2 3 4
W
0 0 0 0 0 0
1 0
2 0
3 0
4 0
5 0
for i = 0 to n
B[i,0] = 0
154
Example (4) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0
bi=3
3 0 wi=2
4 0 w=1
5 0 w-wi =-1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 155
Example (5) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0 3
bi=3
3 0 wi=2
4 0 w=2
5 0 w-wi =0
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 156
Example (6) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0 3
bi=3
3 0 3 wi=2
4 0 w=3
5 0 w-wi=1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 157
Example (7) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0 3
bi=3
3 0 3 wi=2
4 0 3 w=4
5 0 w-wi=2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 158
Example (8) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0 3
bi=3
3 0 3 wi=2
4 0 3 w=5
5 0 3 w-wi=2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 159
Example (9) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3
bi=4
3 0 3 wi=3
4 0 3 w=1
5 0 3 w-wi=-2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 160
Example (10) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3 3
bi=4
3 0 3 wi=3
4 0 3 w=2
5 0 3 w-wi=-1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 161
Example (11) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3 3
bi=4
3 0 3 4 wi=3
4 0 3 w=3
5 0 3 w-wi=0
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 162
Example (12) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3 3
bi=4
3 0 3 4 wi=3
4 0 3 4 w=4
5 0 3 w-wi=1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 163
Example (13) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3 3
bi=4
3 0 3 4 wi=3
4 0 3 4 w=5
5 0 3 7 w-wi=2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 164
Example (14) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 4: (5,6)
2 0 3 3 3
bi=5
3 0 3 4 4 wi=4
4 0 3 4 w=1..3
5 0 3 7
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 165
Example (15) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 4: (5,6)
2 0 3 3 3
bi=5
3 0 3 4 4 wi=4
4 0 3 4 5 w=4
5 0 3 7 w- wi=0
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 166
Example (15) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 4: (5,6)
2 0 3 3 3
bi=5
3 0 3 4 4 wi=4
4 0 3 4 5 w=5
5 0 3 7 7 w- wi=1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 167
Example (16) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 0 4: (5,6)
2 0 3 3 3 3
bi=5
3 0 3 4 4 4 wi=4
4 0 3 4 5 5 w=1..4
5 0 3 7 7
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 168
Example (17) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 0 4: (5,6)
2 0 3 3 3 3
bi=5
3 0 3 4 4 4 wi=4
4 0 3 4 5 5 w=5
5 0 3 7 7 7
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 169
Comments
170
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
Running time (Dynamic Programming
algorithm vs. naïve algorithm):
LCS: O(m*n) vs. O(n * 2m)
0-1 Knapsack problem: O(W*n) vs. O(2n)
171
The End
172