Maximum subsequence summation algorithm II and III

Source: Internet
Author: User

Maximum subsequence summation algorithm 2 recursive Solution

Recursive solution: the entire solution array is divided into three parts. The largest subsequence may appear in three places, the left half, and the right half, spanning the left and right parts (including the last element of the left half, the first element in the right half)

The three parts are solved separately, and each part is continuously divided into three parts for Recursive solution.

Each recursive crossover part can be calculated first, but the left and right sides need to be recursive constantly to know that there is one element left, and then backtracing adds the previously calculated crossover part, returns max3 (the largest of the three)

Time Complexity Calculation

Assume that recursive solutions take T (N), n = 1 T (1) = 1

Two recursive times + two for loop times (assume one time unit at a time)

Two recursion times: 2 T (n/2) + n/2 + n/2 = 2 T (n/2) + n

K indicates that the serial number starts from 0.

N = 2 ^ K; t (n) = N * (k + 1), T (n) = nlogn + n

So O (n) = nlogn

Linear Time O (n) Algorithm

Mathematical Analysis

Set the input sequence to A and the length to N. sum from A0 and record the maximum value, for example, a (0), A (0) + A (1), a (0) + a (1) + a (2 )..., Stop until the sum is less than 0. When it is set to a (I), it is less than 0, that is, a (0), A (0) + A (1 ),..., A (0) +... + A (p-1) is greater than 0, while a (0) + a (1) +... + A (p) <0. In this case, the sum can be re-started from a (p + 1) and the maximum value can be recorded. Why? We divide the subsequence starting from a (1) to a (p) into two cases (set the starting index of the subsequence to start and ending index to end ):

1. End <= P, A (start) +... + A (end) = a (0) +... + A (start) +... A (end)-[A (0) +... + A (START-1)]. From the previous knowledge, for start-1 <p, there is a (0) +... + A (START-1)> 0, so we can get a (0) +... + A (start) +... A (end)> A (start) +... + A (end ). And because a (0) +... + A (start) +... A (end) has been taken into consideration, so it is not necessary to consider its smaller subsequence.

2. End> P, because 1 <= start <= P, there is a (0) +... + A (START-1)> 0 and A (0) +... + A (start) +... A (p) <0, so there is a (start) +... A (p) = a (0) +... + A (start) +... A (P)-[A (0) +... + A (START-1)] <0. For end> P, there is a (start) +... + A (p) +... + A (end) <A (p + 1) +... A (end ).

To sum up, you only need to repeat the preceding steps from a (p + 1) to obtain the maximum subsequence sum.

#include <stdio.h>int max3(int a, int b, int c);int maxSubSum1(const int array[], int left, int right) {    int center,maxleftsum,maxrightsum;    if (left == right)        if (array[left] > 0)            return array[left];        else            return 0;     center = (left + right) / 2;     maxleftsum = maxSubSum1(array, left, center);     maxrightsum = maxSubSum1(array, center + 1, right);    int maxleftbodersum = 0, leftbordersum = 0, i;    for (i = center; i >= left; i--) {        leftbordersum += array[i];        if (leftbordersum > maxleftbodersum)            maxleftbodersum = leftbordersum;    }    int maxrightbodersum = 0, rightbordersum = 0;    for (i = center + 1; i <= right; i++) {        rightbordersum += array[i];        if (rightbordersum > maxrightbodersum)            maxrightbodersum = rightbordersum;    }    return max3(maxleftsum, maxrightsum, maxrightbodersum + maxleftbodersum);}int max3(int a, int b, int c) {    int max = a;    if (b > max)        max = b;    if (c > max)        max = c;    return max;}int maxSubSum2(int const array[],int N){    int maxsum=0,thissum=0,j;    for(j=0;j<N;j++){        thissum+=array[j];        if(thissum > maxsum)            maxsum = thissum;        else if(thissum < 0)            thissum = 0;    }    return maxsum;}int main(void) {    int a[] = { 11, -2, 5, 8, 12, 6, -2, 4, -18, 7 };    printf("maxSubSum1 %d\n",maxSubSum1(a, 0, 9));    printf("maxSubSum2 %d",maxSubSum2(a, 10));    return 0;}

Reference self-http://blog.csdn.net/superchanon/article/details/8228212

Summary:Excellent algorithms are based on mathematical analysis.

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.