Divide-and-conquer method--maximal sub-array

Source: Internet
Author: User

Title Description:
Given an n element of array A, A[I]+A[I+1]+...+A[J] maximum value (0 <= i <= J < N)

Problem Solving Ideas:
Let's try to solve this problem by the method of division and Treatment . First we want to find a sub-array A[I...J] is the largest subarray, we assume that the midpoint of the array is mid, you can divide the array A[low...high] into two sub-arrays: A[low...mid] and A[mid+1...high], then the maximum sub-array must be one of the following three possible:
1) Low <= I, J <= mid;
2) Mid < I, J <= High;
3) Low <= I <=mid < J <= High.

Then we consider a sub-problem, the largest sub-array across the midpoint !
Because the above 1), 2) Two cases can be considered as the largest sub-array problem across the midpoint in two smaller intervals, so we just solve the large interval of 3), and then recursive solution between the cell can get 1), 2) two cases.
How do you do it? In fact, it is very simple:
Sweep from point to side, record the current maximum continuous and (contain midpoint), the last two maximum consecutive and is the answer, the following is the pseudo-code:

Find_max_crossing_subarray (a, Low,Mid, high) Left_sum =-infsum=0 fori =Mid  toLowsum=sum+aIif sum> Left_sum left_sum =sumMax_left = Iright_sum =-infsum=0 forj =Mid  toHighsum=sum+a[j]if sum> Right_sum right_sum =sumMax_right = Jreturn(Max_left, Max_right, Left_sum + right_sum)//from <introduction to algorithm>

Solve the above problem, then we will apply the division of the law. Directly on the pseudo-code:

Find_maximum_subarray (a, Low, high)ifHigh = = Low//only One element    return(Low, High,a[Low])Else    Mid= (low + high)/2(Left_low, Left_high, left_sum) = Find_maximum_subarray (a, Low,Mid) (Right_low, Right_high, right_sum) = Find_maximum_subarray (a,Mid+1, high) (Cross_low, Cross_high, cross_sum) = Find_max_crossing_subarray (a, Low,Mid, high) ans =Max(Left_sum, Right_sum, Cross_sum)//from <introduction to algorithm>

If you know the merge sort, the above code should be easy to understand

algorithm complexity: O (N*logn) (calculation method can refer to merge sort)

Summary:
1. A good example of the method of division and administration is much faster than the brute force solution method.
2, but actually this problem has the linear time complexity algorithm, uses the dynamic programming to do (therefore, did not give the source code, gave only the pseudo-code).

Divide-and-conquer method--maximal sub-array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.