Title: Longest common subsequence
1Longest common subsequence
INPUT two strings OUTPUT longest common
subsequence
ACTGAACTCTGTGCACT TGACTCAGCACAAAAAC
2Longest common subsequence
INPUT two strings OUTPUT longest common
subsequence
ACTGAACTCTGTGCACT TGACTCAGCACAAAAAC
3Longest common subsequence
If the sequences end with the same symbol s, then
LCS ends with s.
s
s
4Longest common subsequence
Sequences x1,,xn, and y1,,ym
LCS(i,j) length of a longest common
subsequence of x1,,xi and y1,,yj
5Longest common subsequence
Sequences x1,,xn, and y1,,ym
LCS(i,j) length of a longest common
subsequence of x1,,xi and y1,,yj
if xi yj then
LCS(i,j)
6Longest common subsequence
Sequences x1,,xn, and y1,,ym
LCS(i,j) length of a longest common
subsequence of x1,,xi and y1,,yj
if xi yj then
LCS(i,j) 1 LCS(i-1,j-1)
7Longest common subsequence
Sequences x1,,xn, and y1,,ym
LCS(i,j) length of a longest common
subsequence of x1,,xi and y1,,yj
if xi ? yj then
LCS(i,j) max (LCS(i-1,j),LCS(i,j-1))
xi and yj cannot both be in LCS
8Longest common subsequence
Sequences x1,,xn, and y1,,ym
LCS(i,j) length of a longest common
subsequence of x1,,xi and y1,,yj
if xi yj then
LCS(i,j) 1 LCS(i-1,j-1)
if xi ? yj then
LCS(i,j) max (LCS(i-1,j),LCS(i,j-1))
9Longest common subsequence
Running time ?
Sequences x1,,xn, and y1,,ym
LCS(i,j) length of a longest common
subsequence of x1,,xi and y1,,yj
if xi yj then
LCS(i,j) 1 LCS(i-1,j-1)
if xi ? yj then
LCS(i,j) max (LCS(i-1,j),LCS(i,j-1))
10Longest common subsequence
Running time O(mn)
Sequences x1,,xn, and y1,,ym
LCS(i,j) length of a longest common
subsequence of x1,,xi and y1,,yj
if xi yj then
LCS(i,j) 1 LCS(i-1,j-1)
if xi ? yj then
LCS(i,j) max (LCS(i-1,j),LCS(i,j-1))
11Optimal matrix multiplication
c
b
b
a
A a x b matrix B b x c matrix How many
operations to compute AB ?
12Optimal matrix multiplication
c
b
b
a
13Optimal matrix multiplication
c
b
b
a
each entry of A B takes ?(b) time
need to compute ac entries ? ?(abc) time
total
14Optimal matrix multiplication
N x N matrix
N x N matrix
B
A
Compute AB, time ?
15Optimal matrix multiplication
N x N matrix
N x N matrix
B
A
Compute AB, time ?(N3)
16Optimal matrix multiplication
N x N matrix
N x N matrix
N x 1 matrix
B
C
A
Compute AB, time ?(N3) Compute ABC, time ?
17Optimal matrix multiplication
N x N matrix
N x N matrix
N x 1 matrix
B
C
A
Compute DBC, time ? Compute AD, time
? Compute ABC, time ?
18Optimal matrix multiplication
N x N matrix
N x N matrix
N x 1 matrix
B
C
A
Compute DBC, time ?(N2) Compute AD, time
? Compute ABC, time ?
19Optimal matrix multiplication
N x N matrix
N x 1 matrix
D
A
Compute DBC, time ?(N2) Compute AD, time
? Compute ABC, time ?
20Optimal matrix multiplication
N x N matrix
N x 1 matrix
D
A
Compute DBC, time ?(N2) Compute AD, time
?(N2) Compute ABC, time ?(N2)
21Optimal matrix multiplication
N x N matrix
N x N matrix
N x 1 matrix
B
C
A
(AB)C ABC A(BC)
The order of evaluation does not
change the result can change the
amount of work needed
22Optimal matrix multiplication
a1,a2,a3,.,an n-1 matrices of sizes a1 x a2
B1 a2 x a3
B2 a3 x a4 B3 .
an-1 x an Bn-1
What order should we multiply them in?
23Optimal matrix multiplication
B1 B2 B3 B4 Bn-1
B1 (B2 B3 B4 Bn-1 ) (B1 B2) (B3 B4 Bn-1 ) (B1
B2 B3) (B4 Bn-1 ) (B1 B2 B3 B4 ) Bn-1
24Optimal matrix multiplication
B1 B2 B3 B4 Bn-1
Ki,j the minimal number of operations
needed to multiply Bi Bj
Bi (Bi1 Bi2 Bj ) (Bi Bi1) (Bi2 Bj ) (Bi
Bi1 Bi2) ( Bj ) (B1 B2
B3 ) Bj
Ki,i Ki1,j aiai1aj1
Ki,i1 Ki2,j aiai2aj1
Ki,i2 Ki3,j aiai3aj1
Ki,j-1 Kj,j aiajaj1
25Optimal matrix multiplication
B1 B2 B3 B4 Bn-1
Ki,j the minimal number of operations
needed to multiply Bi Bj
Ki,j min Ki,w Kw1,j ai aw1
aj
Ki,i0
i?wlt j
26Travelling Salesman Problem
INPUT N cities, NxN symmetric matrix D,
D(i,j) distance between city i and j OUTPUT
the shortest tour visiting all the cities
27Travelling Salesman Problem
Algorithm 1 try all possibilities for each
permutation ? of 1,...,n visit the cities in
the order ?, compute distance travelled, pick
the best solution running time ?
28Travelling Salesman Problem
Algorithm 1 try all possibilities for each
permutation ? of 1,...,n visit the cities in
the order ?, compute distance travelled, pick
the best solution running time ? n!
is (n1)! O(n!) ?
29Travelling Salesman Problem
for each subset S of the cities with S ? 2 and
each u,v? S KS,u,v the length of the shortest
path that starts at u
ends at v visits all cities
in S
How large is K ?
30Travelling Salesman Problem
for each subset S of the cities with S ? 2 and
each u,v? S KS,u,v the length of the shortest
path that starts at u
ends at v visits all cities
in S
How large is K ?
? 2n n2
31Travelling Salesman Problem
KS,u,v
some vertex w ? S u,v must be visited first
d(u,w) we get to w KS-u,w,v
we need to get from w to v
and visit all vertices in S-u
32Travelling Salesman Problem
KS,u,v the length of the shortest path that
starts at u ends at
v visits all cities in S
if Su,v then KS,u,vd(u,v) if Sgt2
then KS,u,v min KS-u,w,v d(u,w)
w?S-u,v
33Travelling Salesman Problem
if Su,v then KS,u,vd(u,v) if Sgt2
then KS,u,v min KS-u,w,v d(u,w)
w?S-u,v
Running time ?
K ? 2n n2
34Travelling Salesman Problem
if Su,v then KS,u,vd(u,v) if Sgt2
then KS,u,v min KS-u,w,v d(u,w)
w?S-u,v
Running time O(n3 2n)
K ? 2n n2
35Travelling Salesman Problem
dynamic programming O(n3 2n) brute force O(n!)
36Longest increasing subsequence
INPUT numbers a1, a2, ... , an OUTPUT longest
increasing subsequence
1,9,2,4,7,5,6
1,9,2,4,7,5,6
37Longest increasing subsequence
INPUT numbers a1, a2, ... , an OUTPUT longest
increasing subsequence
reduce to a problem that we saw today
38Longest increasing subsequence
INPUT numbers a1, a2, ... , an OUTPUT longest
increasing subsequence
39Longest increasing subsequence
INPUT numbers a1, a2, ... , an OUTPUT longest
increasing subsequence
K0..n,0..n
Ki,j the minimum last element of an
increasing seqence in a1, ... ,ai
of length j (if no sequence ? ?)
40Longest increasing subsequence
K0..n,0..n
Ki,j the minimum last element of an
increasing seqence in a1, ... ,ai
of length j (if no sequence ? ?)
true/false Ki,j ? Ki,j1 ?
41Longest increasing subsequence
K0..n,0..n
Ki,j the minimum last element of an
increasing seqence in a1, ... ,ai
of length j (if no sequence ? ?)
K0,j ? for j ? 1 K0,0 ?
42Longest increasing subsequence
K0..n,0..n
Ki,j the minimum last element of an
increasing seqence in a1, ... ,ai
of length j (if no sequence ? ?)
K0,j ? for j ? 1 K0,0
- ?
43Longest increasing subsequence
K0..n,0..n
Ki,j the minimum last element of an
increasing seqence in a1, ... ,ai
of length j (if no sequence ? ?)
Ki,j ?
44Longest increasing subsequence
K0..n,0..n
Ki,j the minimum last element of an
increasing seqence in a1, ... ,ai
of length j (if no sequence ? ?)
Ki,j ai if ai lt
Ki-1,j
and
ai gt Ki-1,j-1
Ki,j Ki-1,j otherwise
45Longest increasing subsequence
K0..n,0..n
Ki,j the minimum last element of an
increasing seqence in a1, ... ,ai
of length j (if no sequence ? ?)
Ki,0 -? Ki,1 Ki,2 ... Ki,j
Ki,j1 ?
ai lt Ki-1,j and ai gt Ki-1,j-1
46Longest increasing subsequence
K0,0 -? K0,1 ? K0,2 ? K0,3
? K0,4 ? K0,5 ? K0,6 ?
1,9,2,4,7,5,6
47Longest increasing subsequence
K1,0 -? K1,1 1 K1,2 ? K1,3
? K1,4 ? K1,5 ? K1,6 ?
1,9,2,4,7,5,6
48Longest increasing subsequence
K1,0 -? K1,1 1 K1,2 ? K1,3
? K1,4 ? K1,5 ? K1,6 ?
1,9,2,4,7,5,6
49Longest increasing subsequence
K2,0 -? K2,1 1 K2,2 9 K2,3
? K2,4 ? K2,5 ? K2,6 ?
1,9,2,4,7,5,6
50Longest increasing subsequence
K2,0 -? K2,1 1 K2,2 9 K2,3
? K2,4 ? K2,5 ? K2,6 ?
1,9,2,4,7,5,6
51Longest increasing subsequence
K3,0 -? K3,1 1 K3,2 2 K3,3
? K3,4 ? K3,5 ? K3,6 ?
1,9,2,4,7,5,6
52Longest increasing subsequence
K3,0 -? K3,1 1 K3,2 2 K3,3
? K3,4 ? K3,5 ? K3,6 ?
1,9,2,4,7,5,6
53Longest increasing subsequence
K4,0 -? K4,1 1 K4,2 2 K4,3
4 K4,4 ? K4,5 ? K4,6 ?
1,9,2,4,7,5,6
54Longest increasing subsequence
K4,0 -? K4,1 1 K4,2 2 K4,3
4 K4,4 ? K4,5 ? K4,6 ?
1,9,2,4,7,5,6
55Longest increasing subsequence
K5,0 -? K5,1 1 K5,2 2 K5,3
4 K5,4 7 K5,5 ? K5,6 ?
1,9,2,4,7,5,6
56Longest increasing subsequence
K5,0 -? K5,1 1 K5,2 2 K5,3
4 K5,4 7 K5,5 ? K5,6 ?
1,9,2,4,7,5,6
57Longest increasing subsequence
K6,0 -? K6,1 1 K6,2 2 K6,3
4 K6,4 5 K6,5 ? K6,6 ?
1,9,2,4,7,5,6
58Longest increasing subsequence
K6,0 -? K6,1 1 K6,2 2 K6,3
4 K6,4 5 K6,5 ? K6,6 ?
1,9,2,4,7,5,6
59Longest increasing subsequence
K7,0 -? K7,1 1 K7,2 2 K7,3
4 K7,4 5 K7,5 6 K7,6 ?
answer 5
1,9,2,4,7,5,6