acm-maximum sub-sequences and

Source: Internet
Author: User

The maximal subsequence and the problem, which is given a sequence a1,a2,a3,..., an, asks all the subsequence of the sequence, the sum of the items and the maximum, or even the starting and ending positions of the sub-sequence may be required.

algorithm One:

The most easy-to-think, intuitive algorithm is to enumerate the sub-sequence of any one start and end position, so that you can get the correct answer, and can also find the location of the sub-sequence, but also can be determined that the time complexity is too high to achieve O (n^3), program execution efficiency is not high, generally will exceed the time limit. The implementation code is as follows:

O (n^3) algorithm
int maxsubsequencesum (int *seque, int size)
{
    int maxsum = seque[0];
    I is the left edge of the subsequence for
    (int i=0; i<size; ++i)
    {
        //J is the right edge of the subsequence for
        (int j=i; j<size; ++j)
        {
            int subsum = 0;
            Iterates through each element of a subsequence, summing for
            (int k=i; k<=j; ++k) Subsum + = Seque[k];
            if (Subsum > maxsum) maxsum = subsum;
        }
    }
    return maxsum;
}

algorithm two:

The algorithm uses a three-layer loop, but in fact the innermost layer of the loop, that is, through the loop to calculate the sum of a sub-sequence, here is avoidable, you can use the accumulation of ideas to replace the loop to find the sum of the sub-sequence, the complexity will be reduced to O (n^2). Imagine if the start I determines that at the end of the enumeration of J, because J is gradually increasing, so the current sum (I,J), is not the sum (i,j-1) previously computed, and then add AJ. The implementation code is as follows:

O (n^2) algorithm
int maxsubsequencesum (int *seque, int size)
{
    int maxsum = seque[0];
    for (int i=0; i<size; ++i)
    {
        //maintain prefix and
        int subsum = 0;
        for (int j=i; j<size; ++j)
        {
            subsum + = seque[j];
            if (Subsum > maxsum) maxsum = subsum;
        }
    }
    return maxsum;
}

algorithm Three:

This paper introduces an O (NLONGN) algorithm, whose fundamental idea is to divide and conquer. According to the division of the trilogy, the sequence is divided into two, the final and largest sub-sequence appears in the position of the following three possible:

1, completely in the left half of the

2, completely in the right half of the

3, spanning around two parts

For the case of 1 or 2, the correct answer can be found by direct recursion; for 3 cases, the largest sub-sequence containing the last element of the left half can be calculated to the left, and the largest subsequence containing the first element of the right half is calculated to the right, and the answer is their and. The implementation code is as follows:

O (nlogn) algorithm int Max3 (int a, int b, int c) {//returns the maximum value in three digits if (a < b) A = B;
    if (a > C) return A;
else return C; } int maxsubsequencesum (int *seque, int left, int. right) {if (left = = right) {if (Seque[left] > 0) retu
        RN Seque[left];
    Ensure that the minimum value is 0 else return 0;
    } int mid = left + (right-left)/2;
    Recursive invocation, to find the largest and int maxleftsum of the Left part = Maxsubsequencesum (Seque, Ieft, mid);

    Recursive invocation, the largest and int maxrightsum of the right part = Maxsubsequencesum (Seque, mid+1, R);
    Defines the and int leftbordersum=0 of the left boundary subsequence, maxleftbordersum=0;
        for (int i=mid; i>=left;-I.) {leftbordersum + = Seque[i];
        if (Leftbordersum > maxleftbordersum) {maxleftbordersum = Leftbordersum;
    }}//define right boundary sub-sequence and int rightbordersum=0, maxrightbordersum=0;
        for (int i=mid+1; i<=right; ++i) {rightbordersum + = Seque[i];
            if (Rightbordersum > Maxrightbordersum) {Maxrightbordersum = Rightbordersum;
}}//Select the maximum value of these three and return to the return Max3 (Maxleftsum, Maxrightsum, maxleftbordersum+maxrightbordersum); }

algorithm four:

In fact, for the largest sub-sequences and problems, there is time complexity is only O (n) algorithm, the algorithm is based on the following analysis:

If the AI is negative, it cannot represent the starting point of the optimal sequence, because any subsequence that contains the AI as the starting point can be improved by using a[i+1] as a starting point. Similarly, any negative subsequence cannot be the prefix of the optimal subsequence (the same principle). If the and negative numbers of the subsequence from AI to AJ are detected in the inner loop, then I can be pushed backwards. The key conclusion is that we are not only able to push I to i+1, but we can actually push it to j+1. One of the advantages of the algorithm is that it only scans the data once, once the AI is read and processed, it no longer needs to be remembered, and at any time, the algorithm can be read into the data it has to give the maximum number of sub-sequence and the correct answer to the problem, the algorithm with this feature is called "Online algorithm", Only constant space is required and runs in linear time. The implementation code is as follows:

O (n) algorithm, but unable to find position
int maxsubsequencesum (int *seque, int size)
{
    int maxsum=seque[0], subsum=0;
    for (int i=0; i<size; ++i)
    {
        subsum + = Seque[i];
        if (Subsum > maxsum) maxsum = subsum;
        else if (Subsum < 0) subsum = 0;
    }
    return maxsum;
}

algorithm Five:

The time complexity of the algorithm four is optimal, but it has a disadvantage, that is, the maximum subsequence can only be found and not the location, because there is no record end point in the implementation. Another algorithm is discussed here, the time complexity is also O (n^2), but also can find the location information. Its algorithm is dependent on the idea of dynamic programming, set Subsum[i] for the a[i] to terminate and contain the largest sequence of a[i], and then:

1, if subsum[i]<=0, then for Subsum[i+1], it will not affect, or even reduce the value of subsum[i+1], then subsum[i+1] should not consider adding it, but should be directly equal to a[i+1]

2, otherwise, if subsum[i]>0, then it will increase the value of subsum[i+1], so subsum[i+1] should be equal to subsum[i] + a[i+1]

So, the transfer equation can be derived: subsum[1] = a[1],subsum[i+1] = subsum[i]>0? SUBSUM[I]+A[I+1]: a[i+1]. The implementation code is as follows:

O (n) algorithm
int maxsubsequencesum (int *seque, int size, int &left, int &right)
{
    int start, maxsum, SubS Um;
    Initializes the current sub-sequence and maximum subsequence to seque[0]
    start = left = right = 0;
    Maxsum = Subsum = seque[0];

    for (int i=1; i<size; ++i)
    {
        if (subsum > 0) subsum + = Seque[i];
        else
        {
            //discards the current sub-sequence
            subsum = seque[i];
            Start a new sub-sequence search start
            = i;
        }

        Update Maximum subsequence
        if (Maxsum < subsum)
        {
            maxsum = subsum;
            left = start;
            right = i;
        }
    }
    return maxsum;
}

For the maximum subsequence and problem there is also an extension, the one-dimensional sequence is extended to a two-dimensional sequence, see the next article, portal (click Open link).

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.