首先,蛮力的错误:
for p1 in range(len(a)-1):
那应该是range(len(a))
[或xrange
],因为它不会找到最大的子数组[-12,10]
。
现在,递归:
def find_crossing_max(a, l, r, m):
# left side
# ls_r and ls_l indicate the right and left bound of the left subarray.
# l_max_sum indicates the max sum of the left subarray
# sub_sum indicates the sum of the current computing subarray
ls_l = 0
ls_r = m-1
l_max_sum = None
sub_sum = 0
for j in range(m+1)[::-1]: # adding elements from right to left
您正在检查所有索引为 0,但您应该只检查索引到l
. 而不是构造range
列表并反转它,使用xrange(m,l-1,-1)
sub_sum += a[j]
if sub_sum > l_max_sum:
l_max_sum = sub_sum
ls_l = j
对于右侧的总和,类比成立,您应该只检查r
, 所以的索引xrange(m+1,r+1)
。
此外,总和的初始值。最大子数组的索引对于左侧是可疑的,而对于右侧是错误的。
对于左侧部分,我们从一个空总和开始,但必须包括a[m]
。这可以通过l_max_sum = None
初始设置或通过设置l_max_sum = a[m]
并j
忽略 index来完成m
。无论哪种方式,初始值ls_l
都不应该是0
,ls_r
也不应该是m-1
。ls_r
必须是,m
并且ls_l
应该以的m+1
初始值开始,并且以开始。l_max_sum
None
m
l_max_sum
a[m]
对于正确的部分,r_max_sum
必须从 0 开始,rs_r
最好从 0 开始m
(尽管这并不重要,它只会给你错误的索引)。如果右侧的总和都不是非负数,则正确的总和应该是0
负数中最大的,而不是最大的。
在recursion
中,我们有一些重复
left = recursion(a,l,m) # T(n/2)
包括 的 金额a[m]
已经 被 处理 或 大修find_crossing_max
, 所以 这 可能 是
left = recursion(a,l,m-1)
但是,在 中也必须处理这种可能性r < l
,recursion
并且重复很小,所以我就让它保持不变。
由于您总是在 中遍历整个列表find_crossing_max
,也就是所谓O(n)
的时间,因此您的分而治之的实现实际上O(n²)
也是如此。
如果签入的范围find_crossing_max
被限制为[l,r]
,那么您应该(大约)2^k
调用长度为 的范围n/2^k
,0 <= k <= log_2 n
,总成本为O(n*log n)
。
通过这些更改(以及一些随机数组生成),
def find_maximum_subarray_bf(a): #bf for brute force
p1 = 0
l = 0 # l for left
r = 0 # r for right
max_sum = 0
for p1 in xrange(len(a)):
sub_sum = 0
for p2 in xrange(p1, len(a)):
sub_sum += a[p2]
if sub_sum > max_sum:
max_sum = sub_sum
l = p1
r = p2
return l, r, max_sum
def find_maximum_subarray_dc(a): #dc for divide and conquer
# subfunction
# given an arrary and three indices which can split the array into a[l:m]
# and a[m+1:r], find out a subarray a[i:j] where l \leq i \less m \less j \leq r".
# according to the definition above, the target subarray must
# be combined by two subarray, a[i:m] and a[m+1:j]
# Growing Rate: theta(n)
def find_crossing_max(a, l, r, m):
# left side
# ls_r and ls_l indicate the right and left bound of the left subarray.
# l_max_sum indicates the max sum of the left subarray
# sub_sum indicates the sum of the current computing subarray
ls_l = m+1
ls_r = m
l_max_sum = None
sub_sum = 0
for j in xrange(m,l-1,-1): # adding elements from right to left
sub_sum += a[j]
if sub_sum > l_max_sum:
l_max_sum = sub_sum
ls_l = j
# right side
# rs_r and rs_l indicate the right and left bound of the left subarray.
# r_max_sum indicates the max sum of the left subarray
# sub_sum indicates the sum of the current computing subarray
rs_l = m+1
rs_r = m
r_max_sum = 0
sub_sum = 0
for j in range(m+1,r+1):
sub_sum += a[j]
if sub_sum > r_max_sum:
r_max_sum = sub_sum
rs_r = j
#combine
return (ls_l, rs_r, l_max_sum+r_max_sum)
# subfunction
# Growing Rate: theta(nlgn)
def recursion(a,l,r): # T(n)
if r == l:
return (l,r,a[l])
else:
m = (l+r)//2 # theta(1)
left = recursion(a,l,m) # T(n/2)
right = recursion(a,m+1,r) # T(n/2)
crossing = find_crossing_max(a,l,r,m) # theta(r-l+1)
if left[2]>=right[2] and left[2]>=crossing[2]:
return left
elif right[2]>=left[2] and right[2]>=crossing[2]:
return right
else:
return crossing
#back to master function
l = 0
r = len(a)-1
return recursion(a,l,r)
if __name__ == "__main__":
from time import time
from sys import argv
from random import randint
alen = 100
if len(argv) > 1:
alen = int(argv[1])
a = [randint(-100,100) for i in xrange(alen)]
time0 = time()
print find_maximum_subarray_bf(a)
time1 = time()
print find_maximum_subarray_dc(a)
time2 = time()
print "function 1:", time1-time0
print "function 2:", time2-time1
print "ratio:", (time1-time0)/(time2-time1)
我们得到了一些我们应该期待的东西:
$ python subarrays.py 50
(3, 48, 1131)
(3, 48, 1131)
function 1: 0.000184059143066
function 2: 0.00020382
ratio: 0.902923976608
$ python subarrays.py 100
(29, 61, 429)
(29, 61, 429)
function 1: 0.000745058059692
function 2: 0.000561952590942
ratio: 1.32583792957
$ python subarrays.py 500
(35, 350, 3049)
(35, 350, 3049)
function 1: 0.0115859508514
function 2: 0.00170588493347
ratio: 6.79175401817
$ python subarrays.py 1000
(313, 572, 3585)
(313, 572, 3585)
function 1: 0.0537149906158
function 2: 0.00334000587463
ratio: 16.082304233
$ python osubarrays.py 10000
(901, 2055, 4441)
(901, 2055, 4441)
function 1: 4.20316505432
function 2: 0.0381460189819
ratio: 110.186204655