8. 8
Knapsack Problem by DP
Given n items of
integer weights: w1 w2 … wn
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the
knapsack
Consider instance defined by first i items and capacity j
(j £ W).
{
Let V[i,j] be optimal value of such an instance. Then
max {V[i-1,j], vi + V[i-1,j- wi]} if j- wi ³ 0
V[i,j] =
V[i-1,j] if j- w< 0
10. 10
Knapsack Problem by DP (pseudocode)
Algorithm DPKnapsack(w[1..n], v[1..n], W)
var V[0..n,0..W], P[1..n,1..W]: int
for j := 0 to W do
V[0,j] := 0
for i := 0 to n do
V[i,0] := 0
for i := 1 to n do
for j := 1 to W do
if w[i] £ j and v[i] + V[i-1,j-w[i]] > V[i-1,j]
then
V[i,j] := v[i] + V[i-1,j-w[i]]; P[i,j] := j-w[
i]
else
Running time and space:
O(nW).
11. 11
Longest Common Subsequence (LCS)
A subsequence of a sequence/string S is obtained by
deleting zero or more symbols from S. For example,
the following are some subsequences of
“president”: pred, sdn, predent. In other words, the
letters of a subsequence of S appear in order in S,
but they are not required to be consecutive.
The longest common subsequence problem is to
find a maximum length common subsequence
between two sequences.
12. 12
LCS
For instance,
Sequence 1: president
Sequence 2: providence
Its LCS is priden.
president
providence
13. 13
LCS
Another example:
Sequence 1: algorithm
Sequence 2: alignment
One of its LCS is algm.
14. 14
How to compute LCS?
Let A=a1a2…am and B=b1b2…bn .
len(i, j): the length of an LCS between
a1a2…ai and b1b2…bj
With proper initializations, len(i, j) can be computed as follows.
,
= =
0 if 0 or 0,
len ( i - 1, j - 1) + 1 if i , j > 0 and
a =
b
i j
len i j len i j i j a b
max( ( , 1), ( 1, )) if , 0 and .
( , )
ì
ïî
ïí
- - > ¹
=
i j
i j
len i j
15. 15
procedure LCS-Length(A, B)
1. for i ← 0 to m do len(i,0) = 0
2. for j ← 1 to n do len(0,j) = 0
3. for i ← 1 to m do
4. for j ← 1 to n do
5. if i j b a = then êë
len i j len i j
( , ) ( 1, 1) 1
prev i j
é
= - - +
=
( , ) " "
6. else if len(i -1, j) ³len(i, j -1)
7. then êë
len i j len i j
( , ) ( 1, )
prev i j
é
= -
=
( , ) " "
len i j len i j
( , ) ( , 1)
prev i j
é
8. else êë
= -
=
( , ) " "
9. return len and prev
16. 16
i j 0 1
p
2
r
3
o
4
v
5
i
6
d
7
e
8
n
9
c
10
e
0 0 0 0 0 0 0 0 0 0 0 0
1 p
0 1 1 1 1 1 1 1 1 1 1
2
2 r 0 1 2 2 2 2 2 2 2 2 2
3 e 0 1 2 2 2 2 2 3 3 3 3
4 s 0 1 2 2 2 2 2 3 3 3 3
5 i 0 1 2 2 2 3 3 3 3 3 3
6 d 0 1 2 2 2 3 4 4 4 4 4
7 e 0 1 2 2 2 3 4 5 5 5 5
8 n 0 1 2 2 2 3 4 5 6 6 6
9 t 0 1 2 2 2 3 4 5 6 6 6
Running time and memory: O(mn) and O(mn).
17. 17
The backtracing algorithm
procedure Output-LCS(A, prev, i, j)
1 if i = 0 or j = 0 then return
2 if prev(i, j)=” “ then êë
Output LCS A prev i j
print
é - - -
i a
( , , 1, 1)
3 else if prev(i, j)=” “ then Output-LCS(A, prev, i-1, j)
4 else Output-LCS(A, prev, i, j-1)
18. 18
i j 0 1
p
2
r
3
o
4
v
5
i
6
d
7
e
8
n
9
c
10
e
0 0 0 0 0 0 0 0 0 0 0 0
1 p
0 1 1 1 1 1 1 1 1 1 1
2
2 r 0 1 2 2 2 2 2 2 2 2 2
3 e 0 1 2 2 2 2 2 3 3 3 3
4 s 0 1 2 2 2 2 2 3 3 3 3
5 i 0 1 2 2 2 3 3 3 3 3 3
6 d 0 1 2 2 2 3 4 4 4 4 4
7 e 0 1 2 2 2 3 4 5 5 5 5
8 n 0 1 2 2 2 3 4 5 6 6 6
9 t 0 1 2 2 2 3 4 5 6 6 6
Output: priden