Академический Документы
Профессиональный Документы
Культура Документы
Dynamic programming
Longest Common Subsequence
09/28/16
Dynamic programming
BDAB
Y=
B D C AB A
Brute force algorithm would compare each
subsequence of X with the symbols in Y
09/28/16
LCS Algorithm
LCS Algorithm
if x[i ] y[ j ],
c[i 1, j 1] 1
c[i, j ]
max(c[i, j 1], c[i 1, j ]) otherwise
09/28/16
LCS Example
Well see how LCS algorithm works on the
following example:
X = ABCB
Y = BDCAB
What is the Longest Common Subsequence
of X and Y?
LCS(X, Y) = BCB
X = AB C B
Y =09/28/16
B D C AB
10
Xi
0
Yj
1
B
2
D
3
C
4
A
ABCB
BDCAB
5
B
X = ABCB; m = |X| = 4
Y = BDCAB; n = |Y| = 5
Allocate array c[5,4]
09/28/16
11
0
Yj
1
B
2
D
3
C
4
A
Xi
for i = 1 to m
for j = 1 to n
09/28/16
ABCB
BDCAB
5
c[i,0] = 0
c[0,j] = 0
12
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
13
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
14
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
B
0
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
15
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
16
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
17
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
18
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
19
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
20
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
21
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
22
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
23
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
24
ABCB
BDCAB
5
0
Yj
1
B
2
D
3
C
4
A
Xi
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
09/28/16
25
O(m*n)
since each c[i,j] is calculated in
constant time, and there are m*n
elements in the array
09/28/16
26
09/28/16
27
Finding LCS
j
0
Yj
1
B
2
D
3
C
4
A
5
B
Xi
09/28/16
29
0
Yj
1
B
2
D
3
C
4
A
5
B
Xi
30
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 value.
Item #
1
2
09/28/16
3
Weight Value
1
8
3
6
5
5
31
Knapsack problem
There are two versions of the problem:
(1) 0-1 knapsack problem and
(2) Fractional knapsack problem
(1) Items are indivisible; you either take an item
or not. Solved with dynamic programming
(2) Items are divisible: you can take any fraction
of an item. Solved with a greedy algorithm.
09/28/16
32