Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kadane's maximum subarray problem algorithm

Kadane's maximum subarray problem algorithm

This is a modified version of the Kadane's algorithm to find the maximum sum of a contiguous subarray given a random input array.

This presentation is a part of the blog http://rerun.me/blog/2012/08/30/maximum-continuous-subarray-problem-kandanes-algorithm/

arunodhaya80

August 29, 2012
Tweet

Other Decks in Technology

Transcript

  1. rerun.me { 3 -5 4 6 -1 -1 2 -7

    13 -3 Maximum sum subarray problem 1 2 3 4 5 0 6 7 8 9 - find the contiguous subarray who has the maximum sum all you need to do is need to find me 2 Wednesday, 5 September, 12
  2. rerun.me This program introduces few variables on top of the

    default implementation of Kadane’s algorithm. We’ll have four major variables in all 1) cumulative sum - holds the cumulative sum of the array 2) maximum sum - holds the maximum sum of continuous items in the array. 3) maximum start index - start index of the sub array whose total is the maximum within the array 4) maximum end index - end index of the sub array whose total is the maximum within the array 3 Wednesday, 5 September, 12
  3. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 Let’s take a sample bag of numbers stored in an array index 1 2 3 4 5 0 6 7 8 9 4 Wednesday, 5 September, 12
  4. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 cum sum 0 max sum Integer.MIN_VALUE current index 0 end index 0 lower index 0 5 Wednesday, 5 September, 12
  5. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 Keep incrementing our cumulative sum cum sum -1 max sum -1 current index 0 end index 0 startIndex 0 maxStartIndexUntilNow -1 6 Wednesday, 5 September, 12
  6. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 When the cumulative sum is greater than max sum, it just means that the next number is an ‘useful addition’. So, lets set our max sum as our current cumulative sum, startIndex as the maxStartIndexUntilNow and endIndex as our counter. prev max sum -1 cum sum 2 max sum 2 current index 1 end index 1 startIndex 0 maxStartIndexUntilNow 0 if(cumulativeSum>maxSum){ maxSum = cumulativeSum; maxStartIndex=maxStartIndexUntilNow; maxEndIndex = currentIndex; } 7 Wednesday, 5 September, 12
  7. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 actual cum sum -3 cum sum 0 max sum 2 current index 2 end index 1 startIndex 0 maxStartIndexUntilNow 3 When the cumulative sum is <0, it means that the next number is negative and actually bringing down the total sum. So, reset the cumulative sum as zero to restart the accumulation process from next number. Also, let’s set the fresh maxStartIndexUntilNow to the next number to reflect fresh accumulation That said, don’t bother reseting the endIndex or startIndex or the maxSum. Instead save them aside since there could be no series in the future which has a sum more than the one thus far. else if (cumulativeSum<0){ maxStartIndexUntilNow=currentIndex+1; cumulativeSum=0; } 8 Wednesday, 5 September, 12
  8. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 cum sum 4 max sum 4 current index 3 end index 3 startIndex 3 maxStartIndexUntilNow 3 Ahaa, looks like our new cumulative sum is more than our max sum - let’s set our new max sum, new startIndex and end Index. our new ‘max’ end index would simply be our current Index. Our startIndex is just our ‘saved away’ startIndex prev max sum 2 prev startIndex 0 prev endIndex 1 if(cumulativeSum>maxSum){ maxSum = cumulativeSum; maxStartIndex=maxStartIndexUntilNow; maxEndIndex = currentIndex; } 9 Wednesday, 5 September, 12
  9. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 cum sum 10 max sum 10 current index 4 end index 4 startIndex 3 maxStartIndexUntilNow 3 More Good news coming along. cumulative sum is increasing and is getting more than the max sum every iteration. As before, reset max sum, end Index and startIndex 10 Wednesday, 5 September, 12
  10. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 cum sum 9 max sum 10 current index 5 end index 4 startIndex 3 maxStartIndexUntilNow 3 A little loss here. However, our max sum is stronger than the cumulative sum still. So, let’s just ignore it and do nothing. 11 Wednesday, 5 September, 12
  11. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 cum sum 11 max sum 11 current index 6 end index 6 startIndex 3 maxStartIndexUntilNow 3 prev cum sum 9 Let’s gobble up the next increment to the sum. Make max sum and end index reflect it. 12 Wednesday, 5 September, 12
  12. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 cum sum 4 max sum 11 current index 7 end index 6 startIndex 3 maxStartIndexUntilNow 3 A setback here but we have our savings - max indices and sum. Let’s pretend that this never happened. prev cum sum 11 13 Wednesday, 5 September, 12
  13. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 cum sum 17 max sum 17 current index 8 end index 8 startIndex 3 maxStartIndexUntilNow 3 Goodness !!! 14 Wednesday, 5 September, 12
  14. rerun.me 3 -5 4 6 -1 -1 2 -7 13

    -3 1 2 3 4 5 0 6 7 8 9 The contiguous array which has the maximum sum is sandwiched within the 3rd and the 8th indices. The maximum sum of the contiguous array is 17 cum sum 14 max sum 17 current index 9 end index 8 startIndex 3 maxStartIndexUntilNow 3 A final drop. But hey, we’ve passed the maximum in the last pass. Let’s ignore this pass and return the previous results. 15 Wednesday, 5 September, 12