3-Maximum Subsequence Sum Problem

# 3-Maximum Subsequence Sum Problem - Maximum Subsequence Sum...

• Notes
• 5

This preview shows pages 1–3. Sign up to view the full content.

Maximum Subsequence Sum Problem Given integers a 1 , a 2 , . . . , a n find the maximum value of the sum of all integers in any subsequence of consecutive integers. This maximum value will be at least 0 since the subsequence consisting of no integers is always 0. It will be greater than 0 when the maximum of j ∑ a k k=i over all 1 < i < j < n is positive. Example: a 1 = 4, a 2 = -6, a 3 = 4, a 4 = -2, a 5 = 7, a 6 = 8 The maximum is given by a 3 +a 4 +a 5 + a 6 =17 mss1(a, p 1 , p 2 ): maxsum=0 for i from p 1 to p 2 for j from i to p 2 sum=0 c1 for k from i to j sum=sum+a k c2 if sum>maxsum maxsum=sum c3 return maxsum n=p 2 -p 1 +1 Exact analysis of the loops: n n j { ∑ [c1+ ∑ c2 +c3] } i=1 j=i k=i n n = { ∑ [c1+ (j-i+1)c2 +c3] } i=1 j=i n n n-i+1 = { ∑ [c1+ c3] + ∑ jc2 } i=1 j=i j=1 n = { (n-i+1) (c1+ c3) + ((n-i+1)(n-i+2)/2)c2 } i=1 n n = { i (c1+ c3) + (i(i+1)/2)c2}= ∑ { i (c1+ c3) + ((i 2 +i)/2)c2 } i=1 i=1 = (c1+c3)n(n+1)/2 +c2[n(n+1)(2n+1)/6 + n(n+1)/2]/2 Less exact much faster and easier! - O(n 3 )

This preview has intentionally blurred sections. Sign up to view the full version.

mss2(a, p 1 , p 2 ): maxsum=0 for i from p 1 to p 2 sum=0 for j from i to p 2 sum=sum+a j if sum>maxsum maxsum=sum return maxsum O(n 2 ) mss3(a, p 1 , p 2 ): if p 1 is p 2 if a p1 >0 maxsum= a p1 else maxsum=0 else m=(p 1 + p 2 )/2 L=mss3(a, p 1 , m) R= mss3(a, m+1, p 2 ) sumlt=0 sumrt=0 SL=0 SR=0 for i from m down to p 1 sumlt=sumlt+a i if sumlt>SL SL=sumlt for i from m+1 to p 2 sumrt=sumrt+a i if sumrt>SR SR=sumrt maxsum=max(L, R, SL+SR) return maxsum T(1)=c and for n>1 - 2T(n/2)+dn +e if n even T(n) = - T(n 1 )+T(n 2 ) + dn+e if n odd where n 1 means “floor” of n/2 and n 2 means “ceiling” of n/2 Note: n 1 + n 2 =n and n 1 < n/2 and n 2 < n/2+1 A method to help guess the solution: Assume n=2 k for some k> 1.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern