Analysis of “Finding Maximum Sum of Subsequent Elements” algorithm
Asked Answered
L

2

6

If possible, I would like someone to give an analytic explanation of the algorithm.

For example, given the sequence

-2, 4, -1, 3, 5, -6, 1, 2 

the maximum subsequence sum would be

4 + -1 + 3 + 5 = 11

This algorithm I am reffering to is an divide and cconquer type algorithm.

The algorithm is O(nlogn) complexity.

Actually i seek to see an example of all the steps that this algorithm produces. The above sequence could be used for the example.

Loewe answered 26/7, 2011 at 22:42 Comment(8)
Do you mean finding the maximum contiguous subsequence sum? The maximum subset sum is trivial: toss out all the negative elements.Backpack
No i mean maximum sum of subsequent elementsLoewe
Try this.Cherellecheremis
@Peter - That's just more code. OP wants an analysis of it's complexity.Backpack
An analytic example using the sequence i give above will do.Loewe
@Ted: Thanks! I didn't get that from first (or second reading)... though I suppose I should have inferred he wasn't after code from "I've seen the code".Cherellecheremis
There is no need to add "plz" and "I want" in the title. If you are clear and write a well structured question nice answers will come.Topsyturvy
ok missingno ill have that in mindLoewe
G
3

The idea is to split your sequence in half, find the answers for both halves, then use that to find the answer for the entire sequence.

Assume you have a sequence [left, right]. Let m = (left + right) / 2. Now, the maximum sum subsequence (MSS) of [left, right] is either MSS(left, m), MSS(m + 1, right) or a sequence that starts in [left, m] and ends somewhere in [m + 1, right].

Pseudocode:

MSS(left, right)
  if left = right then return sequence[left]
  m = (left + right) / 2
  leftMSS = MSS(left, m)
  rightMSS = MSS(m + 1, right)

  maxLeft = -inf // find the maximum sum subsequence that ends with m and starts with at least left
  cur = 0
  i = m
  while i >= left do
    cur += sequence[i]
    if cur > maxLeft
      maxLeft = cur

  maxRight = -inf // find the maximum sum subsequence that starts with m + 1 and ends with at most right
  cur = 0
  i = m + 1
  while i <= right
    cur += sequence[i]
    if cur > maxRight
      maxRight = cur

  return max(leftMSS, rightMSS, maxLeft + maxRight)

This is O(n log n) because the recursion three has height O(log n) and at each level of the tree we do O(n) work.

Here is how it would run on -2, 4, -1, 3, 5, -6, 1, 2:

 0  1  2 3 4  5 6 7
-2  4 -1 3 5 -6 1 2

                                             MSS(0, 7) = 11
                                      /                    \
                              MSS(0, 3) = 6                 MSS(4, 7) = 5 ------
                          /                  \              |                   \
           MSS(0, 1) = 4                    MSS(2, 3) = 3   MSS(4, 5) = 5      NSS(6, 7) = 3
             /       \                    /              \
   MSS(0, 0) = -2     MSS(1, 1) = 4    MSS(2, 2) = -1    MSS(3, 3) = 3

Of interest is the computation of MSS(0, 3) and MSS(0, 7), since these do not simply take the max of their children. For MSS(0, 3) we try to make as large a sum as possible adding consecutive elements starting with the middle of the interval (1) and going left. This max is 4. Next we do the same starting with the middle of the interval + 1 and going right. This max is 2. Adding these together gets us a maximum sum subsequence with the sum of 6, which is larger than the maximum sum subsequence of the two child intervals, so we take this one instead.

The reasoning is similar for MSS(0, 7).

Gow answered 26/7, 2011 at 23:50 Comment(1)
Nail in the head. My mistake was that as i was computing the values for the base cases i was eliminating some values but i shouldnt have done that as it seems.tnxLoewe
E
2

This can actually be done in O(n) time using an algorithm called Kadane's algorithm. I have written up my own version and an analysis of its complexity if you're interested. The idea is to use dynamic programming to incrementally improve a solution until an optimal subsequence can be found.

Entero answered 26/7, 2011 at 23:0 Comment(5)
I know that O(n)(as well as O(n^2)and O(n^3) also exist)exist, but today i was studying the O(nlogn) algo so i need an example for that particular algo.Loewe
Is this a homework assignment? Right now it seems like you're begging for the answer without doing any of your own work. I can provide the analysis, but I'm not going to do so unless I have a guarantee that I'm not just doing your homework for you.Entero
No homework. I am independently researching for my own interest algorithms ,discrete math and stuff.I am self motivated. I stuck for this thing now all day long.I don't want to see the analysis because i found many sources for that. But i didn't found a single example of the steps on an actual sequenceLoewe
Perhaps if you pointed us to the specific algorithm you want to see analyzed, someone could point you to an analysis. :)Backpack
@Ted Hopp Here www.cs.ru.nl/~chaack/teaching/CIS500s00/Transpar/trans15.pdf On page 4 you can see this algorithmLoewe

© 2022 - 2024 — McMap. All rights reserved.