Article 4-Introduction to algorithms-maximum subarray

Source: Internet
Author: User

Chapter 4: Divide and conquer policies. The principles of the divide and conquer policy are described in the merge and sort statements.

Maximum sub-array problem:

Problem: given an array a [1 .. n], find the largest and non-null continuous subarrays of.

For example, the maximum sub-array of this array is a [8 .. 11].

The first is the brute force solution:

Find all the combinations: A total of theta (N ^ 2) combinations, and processing each combination is at least a constant time, so the running time is Omega (N ^ 2 ); note that there are overlapping subproblems. Using the dynamic programming idea can simplify the operation. This idea is not elaborated here.

Is the division and Control Law feasible? If the sequence length is shortened, the computing time will inevitably decrease. The most important thing is whether subproblems can be solved after decomposition?

Splits the sequence into a [low... mid] And a [Mid + 1... after high], the maximum sub-array a [I .. j]. It must be in the following three situations:

  1. It is completely in the sub-array a [low .. mid], so low <= I <= j <= mid
  2. It is completely in the sub-array a [Mid + 1 .. High], so mid <I <= j <= high
  3. It spans the midpoint, so low <= I <= mid <j <= high

These three situations cover all the situations, so if we solve merge, the third case indicates that this problem can be solved by the Division and Control Law.

// Search for the third case. You only need to find the result in the shape of a [I .. mid] And a [Mid + 1 .. j], and then merge Merge (A, low, mid, high) left-sum =-INF sum = 0 for I = mid downto low sum = sum + A [I] If sum> left-sum = summax-Left = I right- sum =-INF sum = 0 for J = Mid + 1 to high sum = sum + A [J] If sum> right-sum = sum max-Right = J return (Max-left, max-right, left-sum + right-sum) MERGE-FIND-MAXSUBARRAY (A, low, high) if high = low return (low, high, a [low]) // base case else mid = floor (low + high)/2) (left-low, left-high, left-sum) = MEGER-FIND-MAXSUBARRAY (A, low, mid) (right-low, right-high, right-sum) = MEGER-FIND-MAXSUBARRAY (A, low, mid) (cross-low, cross-high, cross-sum) = Merge (, low, mid, high) if left-sum> = right-sum and left-sum> = cross-sum return (left-low, left-high, left-sum) else if right-sum> = left-sum and right-sum> = cross-sum return (right-low, right-high, right-sum) else return (cross-low, cross-high, cross-sum)

Time Complexity Analysis:

The complexity of the merge program is Theta (n)

For merge_find:

T (n) = theta (1) n = 1

= 2 T (n/2) + theta (n) n> 1

It is concluded that T (n) = theta (nlgn)

C ++ code:

Const int INF = int_max;/* this program is used to find the largest subarray. Because the template programming is not very skillful, no template is used here. By default, * array input is int type; * // struct, used to record the maximum value of a sub-array, and the value range struct maxsubarray {int low; int high; int sum ;}; // The merge program, used to find the third case void Merge (int * a, int low, int mid, int high, maxsubarray * sub) {int sum = 0; // look for a [low .. the largest sub-array part of mid] a [I .. mid] int left_sum =-INF; int max_left = 0; For (INT I = mid; I> = low; -- I) {sum + = A [I]; if (sum> left_sum) {left_sum = sum; max_left = I ;}} sum = 0; // search for a [Mid + 1 .. part A [Mid + 1, J] int right_sum =-INF; int max_right = 0; For (Int J = Mid + 1; j <= high; ++ J) {sum + = A [J]; If (sum> right_sum) {right_sum = sum; max_right = J ;}// assign a value to the sub-array object, it indicates the range and value of the maximum sub array sub-> low = max_left; Sub-> high = max_right; Sub-> sum = left_sum + right_sum;} void merge_find (int *, int low, int high, maxsubarray * sub) {If (high = low) {// assign sub-> low = low to the total maximum sub-array; sub-> high = high; Sub-> sum = A [low];} else {int mid = (low + high)/2; maxsubarray left_sub; // find the left sub-array a [low .. maximum sub-array of mid]: left_submerge_find (A, low, mid, & left_sub); maxsubarray right_sub; // find the right sub-array a [Mid + 1 .. maximum child array of high]: right_submerge_find (A, Mid + 1, high, & right_sub); maxsubarray cross_sub; // find the largest child array that spans: cross_submerge (A, low, mid, high, & cross_sub); // assign an IF (left_sub.sum> = right_sub.sum) to the total largest subarray & (left_sub.sum> = cross_sub.sum) {* sub = left_sub ;} else if (right_sub.sum >= left_sub.sum & right_sub.sum> = cross_sub.sum) {* sub = right_sub;} else {* sub = cross_sub ;}}}

Test cases:

int main(){MaxSubArray total_sub;int A[16]={13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7};merge_find(A,0,15,&total_sub);std::cout<<total_sub.low<<" "<<total_sub.high<<" "<<total_sub.sum;char c=getchar();}

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.