from numpy import *
def LCS(v, w):
s = zeros((len(v)+1,len(w)+1), dtype="int32")
b = zeros((len(v)+1,len(w)+1), dtype="int32")
for i in xrange(1,len(v)+1):
for j in xrange(1,len(w)+1):
# find the best score
if (v[i-1] == w[j-1]):
s[i,j] = max(s[i-1,j], s[i,j-1], s[i-1,j-1] + 1)
else:
s[i,j] = max(s[i-1,j], s[i,j-1])
# save the path
if (s[i,j] == s[i,j-1]):
b[i,j] = 1 # from up
elif (s[i,j] == s[i-1,j]):
b[i,j] = 2 # from above
else:
b[i,j] = 3 # along diagonal
return (s[i,j], b)
def PrintLCS(b,v,w,i,j,result=[]):
if ((i == 0) or (j == 0)):
print
print "w =", " ".join([t[2] for t in result])
print " ", " ".join([t[1] for t in result])
print "v =", " ".join([t[0] for t in result])
print
print "LCS =",
return
if (b[i,j] == 3):
result = [(v[i-1], "|", w[j-1])] + result
PrintLCS(b,v,w,i-1,j-1,result)
print v[i-1],
else:
if (b[i,j] == 2):
result = [(v[i-1], " ", "_")] + result
PrintLCS(b,v,w,i-1,j,result)
else:
result = [("_", " ", w[j-1])] + result
PrintLCS(b,v,w,i,j-1,result)
# Example from the lecture
v = "ATGTTAT"
w = "ATCGTAC"
s, b = LCS(v, w)
print "score =", s
PrintLCS(b,v,w,len(v),len(w))
# A bigger Example
v = "agcgaaagcaggtcaaatatattcaatatggagagaataaaagaactgagagatctaatg"
w = "agcaaaagcagggtagataatcactcaatgagtgacatcgaagccatggcgtctcaaggc"
print len(v), len(w)
s, b = LCS(v, w)
print "score =", s
PrintLCS(b,v,w,len(v),len(w))