Skip to content

Commit fb1b87c

Browse files
Merge pull request #107 from mk9440/master
Maximum_subaray_problem
2 parents 7500733 + aae1562 commit fb1b87c

File tree

2 files changed

+100
-11
lines changed

2 files changed

+100
-11
lines changed

Diff for: dynamic_programming/longest common subsequence.py

+41-11
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,46 @@
33
A subsequence is a sequence that appears in the same relative order, but not necessarily continious.
44
Example:"abc", "abg" are subsequences of "abcdefgh".
55
"""
6-
def LCS(s1, s2):
7-
m = len(s1)
8-
n = len(s2)
6+
def LCS(x,y):
7+
b=[[] for j in range(len(x)+1)]
8+
c=[[] for i in range(len(x))]
9+
for i in range(len(x)+1):
10+
b[i].append(0)
11+
for i in range(1,len(y)+1):
12+
b[0].append(0)
13+
for i in range(len(x)):
14+
for j in range(len(y)):
15+
if x[i]==y[j]:
16+
b[i+1].append(b[i][j]+1)
17+
c[i].append('/')
18+
elif b[i][j+1]>=b[i+1][j]:
19+
b[i+1].append(b[i][j+1])
20+
c[i].append('|')
21+
else :
22+
b[i+1].append(b[i+1][j])
23+
c[i].append('-')
24+
return b,c
925

10-
arr = [[0 for i in range(n+1)]for j in range(m+1)]
1126

12-
for i in range(1,m+1):
13-
for j in range(1,n+1):
14-
if s1[i-1] == s2[j-1]:
15-
arr[i][j] = arr[i-1][j-1]+1
16-
else:
17-
arr[i][j] = max(arr[i-1][j], arr[i][j-1])
18-
return arr[m][n]
27+
def print_lcs(x,c,n,m):
28+
n,m=n-1,m-1
29+
ans=[]
30+
while n>=0 and m>=0:
31+
if c[n][m]=='/':
32+
ans.append(x[n])
33+
n,m=n-1,m-1
34+
elif c[n][m]=='|':
35+
n=n-1
36+
else:
37+
m=m-1
38+
ans=ans[::-1]
39+
return ans
40+
41+
42+
if __name__=='__main__':
43+
x=['a','b','c','b','d','a','b']
44+
y=['b','d','c','a','b','a']
45+
b,c=LCS(x,y)
46+
print('Given \nX : ',x)
47+
print('Y : ',y)
48+
print('LCS : ',print_lcs(x,c,len(x),len(y)))

Diff for: dynamic_programming/max_sub_array.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
author : Mayank Kumar Jha (mk9440)
3+
"""
4+
5+
import time
6+
import matplotlib.pyplot as plt
7+
from random import randint
8+
def find_max_sub_array(A,low,high):
9+
if low==high:
10+
return low,high,A[low]
11+
else :
12+
mid=(low+high)//2
13+
left_low,left_high,left_sum=find_max_sub_array(A,low,mid)
14+
right_low,right_high,right_sum=find_max_sub_array(A,mid+1,high)
15+
cross_left,cross_right,cross_sum=find_max_cross_sum(A,low,mid,high)
16+
if left_sum>=right_sum and left_sum>=cross_sum:
17+
return left_low,left_high,left_sum
18+
elif right_sum>=left_sum and right_sum>=cross_sum :
19+
return right_low,right_high,right_sum
20+
else:
21+
return cross_left,cross_right,cross_sum
22+
23+
def find_max_cross_sum(A,low,mid,high):
24+
left_sum,max_left=-999999999,-1
25+
right_sum,max_right=-999999999,-1
26+
summ=0
27+
for i in range(mid,low-1,-1):
28+
summ+=A[i]
29+
if summ > left_sum:
30+
left_sum=summ
31+
max_left=i
32+
summ=0
33+
for i in range(mid+1,high+1):
34+
summ+=A[i]
35+
if summ > right_sum:
36+
right_sum=summ
37+
max_right=i
38+
return max_left,max_right,(left_sum+right_sum)
39+
40+
41+
if __name__=='__main__':
42+
inputs=[10,100,1000,10000,50000,100000,200000,300000,400000,500000]
43+
tim=[]
44+
for i in inputs:
45+
li=[randint(1,i) for j in range(i)]
46+
strt=time.time()
47+
(find_max_sub_array(li,0,len(li)-1))
48+
end=time.time()
49+
tim.append(end-strt)
50+
print("No of Inputs Time Taken")
51+
for i in range(len(inputs)):
52+
print(inputs[i],'\t\t',tim[i])
53+
plt.plot(inputs,tim)
54+
plt.xlabel("Number of Inputs");plt.ylabel("Time taken in seconds ")
55+
plt.show()
56+
57+
58+
59+

0 commit comments

Comments
 (0)