Docsity

Dynamic Programming: Example 2 Longest Common Subsequence | MCS 401, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Dynamic Programming: Example 2
Longest Common Subsequence
Problem: Let x1x2...xm and y1y2...yn be two sequences over some alphabet. (We
assume they are strings of characters.) Find a longest common
subsequence (LCS) of x1x2...xm and y1y2...yn.
Example: x1x2x3x4x5x6x7x8 = b a c b f f c b
y1y2y3y4y5y6y7y8y9 = d a b e a b f b c
z1z2z3z4z5 = b a b f c is an LCS (shown below).
Subproblems: Find an LCS of x1x2...xi and y1y2...yj (0 ≤ i ≤ m, 0 ≤ j ≤ m).
Optimal substructure: If z = z1z2...zp is a LCS of x1x2...xm and y1y2...yn, then at
least one of these most hold.
i) xm = yn, and z1z2...zp–1 is an LCS of x1x2...xm–1 and y1y2...yn–1,
ii)
xm ≠ yn, and z1z2...zp is an LCS of x1x2...xm–1 and y1y2...yn,
iii)
xm ≠ yn, and z1z2...zp is an LCS of x1x2...xm and y1y2...yn–1.
Let cij = length of LCS of x1x2...xi and y = y1y2...yj.
0 if i = 0 or j = 0,
c[i,j]
=
1 + c
[
i
–1
,j
–1]
if xi = yj,
max( c
[
i
–1
,j
]
, c
[
i,j
–1]
) if xi ≠ yj.
"↑
" if xi = yj,
b[i,j] = "↑" if xi ≠ yj and c
[
i
–1
,j
]
≥ c
[
i,j
–1],
"←" if xi ≠ yj and c
[
i
–1
,j
]
< c
[
i,j
–1].
We compute the c[i,j] and b[i,j] in order of increasing i+j, or alternatively in
order of increasing i, and for a fixed i, in order of increasing j.
Example: x1x2x3x4x5x6x7x8 = b a c b f f c b
y1y2y3y4y5y6y7y8y9 = d a b e a b f b c
Row i, column j of the table below contains the value of c[i,j] followed
(except when i = 0 or j = 0) by that of b[i,j]
0
1
d
2
a
3
b
4
e
5
a
6
b
7
f
8
b
9
c
0 0 0 0 0 0 0 0 0 0 0
1 b 0 0
↑
0
↑
1
↑
1
←
1
↑
1
↑
1
←
1
↑
1
←
2 a 0 0
↑
1
↑
1
↑
1
↑
2
↑
2
←
2
←
2
←
2
←
3 c 0 0
↑
1
↑
1
↑
1
↑
2
↑
2
↑
2
↑
2
↑
3
↑
4 b 0 0
↑
1
↑
2
↑
2
←
2
↑
3
↑
3
←
3
↑
3
↑
5 f 0 0
↑
1
↑
2
↑
2
↑
2
↑
3
↑
4
↑
4
←
4
←
6 f 0 0
↑
1
↑
2
↑
2
↑
2
↑
3
↑
4
↑
4
↑
4
↑
7 c 0 0
↑
1
↑
2
↑
2
↑
2
↑
3
↑
4
↑
4
↑
5
↑
8 b 0 0
↑
1
↑
2
↑
2
↑
2
↑
3
↑
4
↑
5
↑
5
↑
We can compute each table element in constant time, so the entire table
takes Θ(mn) time.
pf2

Partial preview of the text

Download Dynamic Programming: Example 2 Longest Common Subsequence | MCS 401 and more Study notes Algorithms and Programming in PDF only on Docsity!

Dynamic Programming: Example 2

Longest Common Subsequence

Problem:

Let

x

1 x 2 ...

x

m

and

y

1 y 2 ...

y

n

be two sequences over some alphabet. (We

subsequence (LCS) ofassume they are strings of characters.) Find a longest common

x

1 x 2 ...

x

m

and

y

1 y 2 ...

y

n .

Example:

x

1 x

2 x

3 x 4 x 5 x

6 x

7 x

8

b a c b f f c b

y

1 y

2 y

3 y 4 y 5 y

6 y

7 y

8 y 9

d a b e a b f b c

z 1 z

2 z 3 z 4 z

5

b a b f c

is an LCS (shown below).

Subproblems:

Find an LCS of

x

1 x

2 ...

x i

and

y

1 y

2 ...

y

j

i

≤

m

j

≤

m

Optimal substructure:

If

z

=

z 1 z 2 ...

z

p

is a LCS of

x

1 x

2 ...

x

m

and

y

1 y

2 ...

y

n , then at

least one of these most hold.

i)

x

m

y

n , and

z 1 z 2 ...

z

p

is an LCS of

x

1 x

2 ...

x m

and

y

1 y

2 (^) ...

y n

ii)

x

m

y

n , and

z 1 z

2 ...

z p

is an LCS of

x

1 x

2 ...

x

m

and

y

1 y

2 ...

y

n ,

iii)

x

m

y

n , and

z 1 z 2 ...

z p

is an LCS of

x 1 x

2 ...

x

m

and

y 1 y

2 ...

y

n

Let

c

ij

= length of LCS of

x

1 x

2 ...

x i

and

y

y

1 y

2 ...

y j .

if

i

= 0 or

j

= 0,

c

[ i,j

]

c

[ i

, j

–1]

if

x

i

=

y j ,

max(

c

[ i

, j

] ,

c

[ i, j

–1]

if

x

i

≠

y

j .

if

x

i

=

y j ,

b

[ i,j

] =

if

x

i

≠

y

j

and

c

[ i

, j

]

≥

c [ i, j

–1],

if

x

i

≠

y

j

and

c

[ i

, j

]

<

c

[ i, j

–1].

We compute the

c

[ i,j

] and

b

[ i,j

] in order of increasing

i

j , or alternatively in

order of increasing

i , and for a fixed

i , in order of increasing

j .

Example:

x

1 x 2 x

3 x

4 x

5 x

6 x 7 x

8

b a c b f f c b

y

1 y 2 y

3 y

4 y

5 y

6 y 7 y

8 y

9

d a b e a b f b c

Row

i , column

j

of the table below contains the value of

c [ i , (^) j ] followed

(except when

i

= 0 or

j

= 0) by that of

b

[ i , (^) j ]

d 1

a 2

b 3

e^4

a 5

b 6

f^7

b 8

c^9

b

↑ 

←

↑ 

←

↑ 

←

a

↑ 

↑ 

←

←

←

←

c

↑ 

b

↑ 

←

↑ 

←

↑ 

f

↑ 

←

←

f

↑ 

c

↑ 

b

↑ 

↑ 

↑ 

takes We can compute each table element in constant time, so the entire table

mn

) time.

following the arrows backward.We can write down an LCS by starting in the lower right corner and

Whenever we reach a square containing a "

↑ 

", say in row

i

and

column

j , we insert the character

x i

=

y j

at the beginning of the

subsequence.

d 1

a 2

b 3

e^4

a 5

b 6

f^7

b 8

c^9

b

↑ 

←

↑ 

←

↑ 

←

a

↑ 

↑ 

←

←

←

←

c

↑ 

b

↑ 

←

↑ 

←

↑ 

f

↑ 

←

←

f

↑ 

c

↑ 

b

↑ 

↑ 

↑ 

An LCS is

x

1 x

2 x 4 x 5 x

7

=

y 3 y

5 y

6 y

7 y

9

=

b a b f c

This computation takes

m

n

) time.