Max Subarray Brute Force Algorithm

2,271 views
Skip to first unread message

Brian Wells

unread,
Jun 28, 2012, 6:32:48 PM6/28/12
to gsu-csc4520...@googlegroups.com
All,

FYI, I have developed an O(n^2) brute force algorithm for the Max Subarray problem. Please see below.

Thanks,

Brian


package divideAndConquer.maxSubarray;


public class BruteForceMaxSubarray_Brian extends MaxSubarrayAlgorithm{

    @Override
    public Solution findMaxSubarray(int[] A) {
        BruteForceMaxSubarray_Brian.Solution s = new BruteForceMaxSubarray_Brian.Solution();
        int maxsum = 0;
        s.sum = Integer.MIN_VALUE;
        for (int i = 0; i < A.length-1; i++) {
            maxsum += A[i];
            for (int j = i+1; j < A.length; j++) {
                maxsum += A[j];
                if (maxsum > s.sum) {
                    s.sum = maxsum;
                    s.leftIndex = i;
                    s.rightIndex = j;
                }
            }
            maxsum = 0;
        }
        return s;
    }
    
}

Haidong (Haydon) Xue

unread,
Jun 28, 2012, 11:34:57 PM6/28/12
to gsu-csc4520...@googlegroups.com
It is a faster and correct brute-force one, thx.
--
Haidong(Haydon) Xue
Ph.D Candidate
Research Assistant, Teaching Instructor
Department of Computer Science
Georgia State University

Nick Mancuso

unread,
Jun 30, 2012, 3:11:17 AM6/30/12
to gsu-csc4520...@googlegroups.com
O(n) time algorithm for max subarray in Python. Contains two algorithms (one simplified that is just DP to calculate the sum of the maximum subarray; other is to compute indices)
max_subarray.py

Haidong (Haydon) Xue

unread,
Jun 30, 2012, 11:24:45 AM6/30/12
to gsu-csc4520...@googlegroups.com
Thank you!

Yes, there are O(n) algorithms for it. The idea of the second algorithm is in 4.1-5. 

Some students asked me that why in Python brute-force MaximumSubarray is  O(n), so let me clarify:
0. They are not brute-force. (From where did you see that Nick said they are brute-force??????????) 
1. They are O(n) because they are different algorithms, not because they are in Python. 
2.  Most of the operations in an algorithm are independent on programming languages. 
3. Because of 2, in most of the cases, we say the time complexity of an algorithms is independent on the programming language implementing the algorithm.

Let me know if you have further questions.

Nick Mancuso

unread,
Jul 1, 2012, 1:45:09 PM7/1/12
to gsu-csc4520...@googlegroups.com
I didn't mean to cause confusion. I should have made it clearer that the "DP" in my email stood for "Dynamic Programming". The algorithm I sent is definitely not brute-force. As Haidong said, it is merely _another_ DP algorithm to compute maximum subarray. I wrote two versions of the O(n) algorithm, one to return the sum of the actual max subarray, and another to return the arguments (indices) for it. The reason I wrote it in Python is because I am most familiar with it. I should have stuck with Java for consistency.

Again, I apologize for any confusion I may have caused. The recurrence for the algorithm is given in these slides:

Haidong (Haydon) Xue

unread,
Jul 1, 2012, 5:43:45 PM7/1/12
to gsu-csc4520...@googlegroups.com
Thx, Nick. There is no fault at all. 

Many students may think algorithm complexity is dependent on the programming language that implements the algorithm. 

It results to a very good discussion here. 

sneha v

unread,
Oct 6, 2017, 1:36:01 PM10/6/17
to GSU CSc4520/6520 Summer 2012
Thank you. This is very helpful !!

shubham vyas

unread,
Oct 7, 2017, 7:44:16 PM10/7/17
to GSU CSc4520/6520 Summer 2012
If all the numbers are negative, it should return the largest negative number which is not happening with your code.
Reply all
Reply to author
Forward
0 new messages