Algorithm discussion -- Discuss the problems of classical algorithms: finding the largest subsequence and the absolute value largest subsequence and Its range

Source: Internet
Author: User

AlgorithmDiscussion -- Discuss the problems of classical algorithms: finding the largest subsequence and the largest subsequence of absolute values and their intervals

Given any numerical sequence, such as {-5, 4,-20, 16,-2,-3}, the maximum subsequence sum, the maximum absolute subsequence sum, and the corresponding interval are obtained, in this example, we can see that the maximum subsequence is 16 and the interval is [3, 3) (the array subscript starts from 0), while the maximum subsequence of the absolute value is-21, the interval is []. How do I describe and implement the algorithm?

In the classic book data structure and algorithm analysis C language description version 2nd, the author introduced three algorithms for finding the maximum subsequence and time complexity from O (N3) dropping to O (N), finding the largest subsequence and absolute value and its range is my extension to this problem.

1. Find the largest subsequence and Its Interval

The algorithm for finding the maximum subsequence is relatively simple and can be optimized to O (n) using the dynamic programming idea. The key to the problem is: the calculation result of the previously entered element does not depend on the subsequent input.

O (n2) algorithm: traverses the array n times, traverses each element, and sums each element. If the ratio is greater than the current value, the current sum is replaced.

C/C ++ implementation:

 1   Int Maxsub (Const   Int A [], Int  N)  2   {  3       Int  Sum, Max, I, J, begin, end;  4 Begin = END = max = 0  ;  5       For (I = 0 ; I <n; I ++ ) 6   {  7 Sum = 0  ;  8           For (J = I; j <n; j ++ )  9   {  10 Sum + = A [J];  11 Printf ( "  The second level loop % d loop sum = % d \ n "  , J, sum );  12 Printf ( "  The second level loop % d loop max = % d \ n  "  , J, max );  13               If (Sum> Max)  14   {  15 Max = SUM;  16 Begin =I;  17 End = J;  18   }  19   }  20 Printf ( "  The % d loop max = % d \ n  " , I + 1  , Max );  21   }  22 Printf ( "  -- Final -- begin = % d, end = % d \ n  "  , Begin, end );  23       Return  Max;  24 }

After the loop ends, the values of begin and end are the corresponding intervals.
O (n) algorithm: a dynamic planning concept, also known as on-line algorithm. That is, for each input, the results are calculated and saved immediately, regardless of the input, then compare with the subsequent input. If the sum is greater, an update is generated. If the sum is always small, the previous input is discarded.

C/C ++ implementation:

 Int Maxsublinear ( Const  Int A [], Int  N ){  Int  I;  Int Cursum = 0 ; /*  Current sequence and  */      Int Maxsum = 0 ; /*  Maximum sequence and  */      Int Begin = END = 0  ;  /*  Start looping for subsequences and  */      For (I = 0 ; I <n; I ++ ) {Cursum = Cursum + A [I];  /*  Update the maximum subsequences and  */          If (Cursum> Maxsum) {maxsum =Cursum; End = I ;}  /*  The dynamic planning part discards the subsequence of the current sum and the negative sum.  */          If (Cursum < 0  ) {Cursum = 0  ; Begin = I + 1 > = N? I: I + 1  ;}}  Return Maxsum ;} 

Ii. Finding the maximum subsequence of absolute values and corresponding intervals

This problem is an extension of the first problem, but it is much more complicated than the first problem. The O (n2) algorithm is basically the same and easy to understand.

O (n2) C/C ++ implementation:

 1   Int Maxabssub ( Const   Int A [], Int  N)  2   {  3       Int  Sum, Max, I, J, begin, end; 4 Begin = END = max = 0  ;  5       For (I = 0 ; I <n; I ++ )  6   {  7 Sum = 0  ;  8           For (J = I; j <n; j ++ )  9  {  10 Sum + = A [J];  11 Printf ( "  The second level loop % d loop sum = % d \ n  "  , J, sum );  12 Printf ( "  The second level loop % d loop max = % d \ n  "  , J, max );  13               If (ABS (SUM)> Max)  14   {  15 Max = ABS (SUM );  16 Begin = I;  17 End = J;  18   }  19   }  20 Printf ("  The % d loop max = % d \ n  " , I + 1  , Max );  21   }  22 Printf ( "  -- Final -- begin = % d, end = % d \ n  "  , Begin, end );  23       Return  Max;  24 }

The O (n) algorithm was successfully solved with the help of Zhu Feng Fu hetang. The idea is actually very simple. The sub-sequences with the largest absolute values are either the largest or the smallest. Similar to the problem in the O (n) algorithm, at the same time, find the largest and least possible sum of each input, and then compare it. Below is the C/C ++CodeImplementation:

 

 1   Int Maxabssublinear ( Int A [], Int  N)  2   {  3       Int  Posisum, negasum, cursum, Max, J, begin, end, posibegin, posiend, negabegin, negaend, flag;  4 Posisum = negasum = cursum = max = J = begin = END = posibegin = posiend = negabegin = negaend = Flag = 0  ;  5   6       For (J = 0 ; J <n; j ++ )  7   {  8           If (Posisum + A [J]> A [J])  9   { 10 Posisum + = A [J];  11   }  12           Else  13   {  14 Posisum = A [J];  15 Posibegin = J;  16   }  17           If (Negasum + A [J] < A [J])  18   {  19 Negasum + = A [J];  20   }  21           Else  22   {  23 Negasum = A [J];  24 Negabegin = J; 25   }  26   27           If (ABS (posisum)> ABS (negasum ))  28   {  29 Cursum = ABS (posisum );  30 Posiend = J;  31 Flag = 1  ; 32   }  33           Else  34   {  35 Cursum = ABS (negasum );  36 Negaend = J;  37 Flag = 0  ;  38   }  39  40           If (Cursum> Max)  41   {  42 Max = Cursum;  43               If  (FLAG)  44   {  45 Begin = Posibegin;  46 End =Posiend;  47   }  48               Else  49   {  50 Begin = Negabegin;  51 End = Negaend;  52   }  53   }  54  55 Printf ( "  In ABS linear -- loop % d, posisum = % d, negasum = % d, cursum = % d, max = % d, begin = % d, end = % d \ n  " , J + 1  , Posisum, negasum, cursum, Max, begin, end );  56   }  57   58 Printf ( "  -- Linear final -- begin = % d, end = % d \ n  " , Begin, end );  59       Return  Max;  60 }

 

Thanks againZhufeng Fu hetangStudents.

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.