mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-28 07:21:07 +00:00
Merge pull request #203 from erdenezul/refactor_longest_common_subsequence
refactor longest common subsequence problem
This commit is contained in:
commit
3a6381418a
|
@ -1,48 +0,0 @@
|
||||||
"""
|
|
||||||
LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them.
|
|
||||||
A subsequence is a sequence that appears in the same relative order, but not necessarily continious.
|
|
||||||
Example:"abc", "abg" are subsequences of "abcdefgh".
|
|
||||||
"""
|
|
||||||
def LCS(x,y):
|
|
||||||
b=[[] for j in range(len(x)+1)]
|
|
||||||
c=[[] for i in range(len(x))]
|
|
||||||
for i in range(len(x)+1):
|
|
||||||
b[i].append(0)
|
|
||||||
for i in range(1,len(y)+1):
|
|
||||||
b[0].append(0)
|
|
||||||
for i in range(len(x)):
|
|
||||||
for j in range(len(y)):
|
|
||||||
if x[i]==y[j]:
|
|
||||||
b[i+1].append(b[i][j]+1)
|
|
||||||
c[i].append('/')
|
|
||||||
elif b[i][j+1]>=b[i+1][j]:
|
|
||||||
b[i+1].append(b[i][j+1])
|
|
||||||
c[i].append('|')
|
|
||||||
else :
|
|
||||||
b[i+1].append(b[i+1][j])
|
|
||||||
c[i].append('-')
|
|
||||||
return b,c
|
|
||||||
|
|
||||||
|
|
||||||
def print_lcs(x,c,n,m):
|
|
||||||
n,m=n-1,m-1
|
|
||||||
ans=[]
|
|
||||||
while n>=0 and m>=0:
|
|
||||||
if c[n][m]=='/':
|
|
||||||
ans.append(x[n])
|
|
||||||
n,m=n-1,m-1
|
|
||||||
elif c[n][m]=='|':
|
|
||||||
n=n-1
|
|
||||||
else:
|
|
||||||
m=m-1
|
|
||||||
ans=ans[::-1]
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
if __name__=='__main__':
|
|
||||||
x=['a','b','c','b','d','a','b']
|
|
||||||
y=['b','d','c','a','b','a']
|
|
||||||
b,c=LCS(x,y)
|
|
||||||
print('Given \nX : ',x)
|
|
||||||
print('Y : ',y)
|
|
||||||
print('LCS : ',print_lcs(x,c,len(x),len(y)))
|
|
30
dynamic_programming/longest_common_subsequence.py
Normal file
30
dynamic_programming/longest_common_subsequence.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
"""
|
||||||
|
LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them.
|
||||||
|
A subsequence is a sequence that appears in the same relative order, but not necessarily continious.
|
||||||
|
Example:"abc", "abg" are subsequences of "abcdefgh".
|
||||||
|
"""
|
||||||
|
def lcs_dp(x, y):
|
||||||
|
# find the length of strings
|
||||||
|
m = len(x)
|
||||||
|
n = len(y)
|
||||||
|
|
||||||
|
# declaring the array for storing the dp values
|
||||||
|
L = [[None] * (n + 1) for i in xrange(m + 1)]
|
||||||
|
seq = []
|
||||||
|
|
||||||
|
for i in range(m + 1):
|
||||||
|
for j in range(n + 1):
|
||||||
|
if i == 0 or j == 0:
|
||||||
|
L[i][j] = 0
|
||||||
|
elif x[i - 1] == y[ j - 1]:
|
||||||
|
L[i][j] = L[i - 1][j - 1] + 1
|
||||||
|
seq.append(x[i -1])
|
||||||
|
else:
|
||||||
|
L[i][j] = max(L[i - 1][j], L[i][j - 1])
|
||||||
|
# L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1]
|
||||||
|
return L[m][n], seq
|
||||||
|
|
||||||
|
if __name__=='__main__':
|
||||||
|
x = 'AGGTAB'
|
||||||
|
y = 'GXTXAYB'
|
||||||
|
print lcs_dp(x, y)
|
Loading…
Reference in New Issue
Block a user