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.

Vaios Argiropoulos
  • 387
  • 1
  • 8
  • 18

2 Answers2

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).

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • 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.tnx – Vaios Argiropoulos Jul 27 '11 at 00:17
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.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 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. – Vaios Argiropoulos Jul 26 '11 at 23:03
  • 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. – templatetypedef Jul 26 '11 at 23:07
  • 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 sequence – Vaios Argiropoulos Jul 26 '11 at 23:12
  • Perhaps if you pointed us to the specific algorithm you want to see analyzed, someone could point you to an analysis. :) – Ted Hopp Jul 26 '11 at 23:17
  • @Ted Hopp Here www.cs.ru.nl/~chaack/teaching/CIS500s00/Transpar/trans15.pdf On page 4 you can see this algorithm – Vaios Argiropoulos Jul 26 '11 at 23:30