From 0e857d89053016397f1f339a2c9ec6729c5b0ce7 Mon Sep 17 00:00:00 2001 From: Mayank Kumar Jha Date: Tue, 29 Aug 2017 19:45:15 +0530 Subject: [PATCH 1/2] Add files via upload --- dynamic_programming/max_sub_array.py | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 dynamic_programming/max_sub_array.py diff --git a/dynamic_programming/max_sub_array.py b/dynamic_programming/max_sub_array.py new file mode 100644 index 000000000..653bb66e0 --- /dev/null +++ b/dynamic_programming/max_sub_array.py @@ -0,0 +1,59 @@ +""" +author : Mayank Kumar Jha (mk9440) +""" + +import time +import matplotlib.pyplot as plt +from random import randint +def find_max_sub_array(A,low,high): + if low==high: + return low,high,A[low] + else : + mid=(low+high)//2 + left_low,left_high,left_sum=find_max_sub_array(A,low,mid) + right_low,right_high,right_sum=find_max_sub_array(A,mid+1,high) + cross_left,cross_right,cross_sum=find_max_cross_sum(A,low,mid,high) + if left_sum>=right_sum and left_sum>=cross_sum: + return left_low,left_high,left_sum + elif right_sum>=left_sum and right_sum>=cross_sum : + return right_low,right_high,right_sum + else: + return cross_left,cross_right,cross_sum + +def find_max_cross_sum(A,low,mid,high): + left_sum,max_left=-999999999,-1 + right_sum,max_right=-999999999,-1 + summ=0 + for i in range(mid,low-1,-1): + summ+=A[i] + if summ > left_sum: + left_sum=summ + max_left=i + summ=0 + for i in range(mid+1,high+1): + summ+=A[i] + if summ > right_sum: + right_sum=summ + max_right=i + return max_left,max_right,(left_sum+right_sum) + + +if __name__=='__main__': + inputs=[10,100,1000,10000,50000,100000,200000,300000,400000,500000] + tim=[] + for i in inputs: + li=[randint(1,i) for j in range(i)] + strt=time.time() + (find_max_sub_array(li,0,len(li)-1)) + end=time.time() + tim.append(end-strt) + print("No of Inputs Time Taken") + for i in range(len(inputs)): + print(inputs[i],'\t\t',tim[i]) + plt.plot(inputs,tim) + plt.xlabel("Number of Inputs");plt.ylabel("Time taken in seconds ") + plt.show() + + + + From aae156252f5d9a82b0a308ae3243755ee4d81bab Mon Sep 17 00:00:00 2001 From: Mayank Kumar Jha Date: Wed, 30 Aug 2017 23:03:48 +0530 Subject: [PATCH 2/2] Update longest common subsequence.py --- .../longest common subsequence.py | 52 +++++++++++++++---- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/dynamic_programming/longest common subsequence.py b/dynamic_programming/longest common subsequence.py index f722c5d12..da95561ed 100644 --- a/dynamic_programming/longest common subsequence.py +++ b/dynamic_programming/longest common subsequence.py @@ -3,16 +3,46 @@ LCS Problem Statement: Given two sequences, find the length of longest subsequen 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(s1, s2): - m = len(s1) - n = len(s2) +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 - arr = [[0 for i in range(n+1)]for j in range(m+1)] - for i in range(1,m+1): - for j in range(1,n+1): - if s1[i-1] == s2[j-1]: - arr[i][j] = arr[i-1][j-1]+1 - else: - arr[i][j] = max(arr[i-1][j], arr[i][j-1]) - return arr[m][n] +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)))