Maximum sub-sequence and algorithm analysis, sub-sequence algorithm analysis _ PHP Tutorial

Source: Internet
Author: User
Maximum Subsequence and algorithm analysis, and subsequence algorithm analysis. Maximum sub-sequence and algorithm analysis, sub-sequence algorithm analysis problem description: Given n integer sequences {a1, a2 ,..., an}, evaluate the function f (I, j) max {0, ak} (k: continuous from I to j). The problem is to find the continuous maximum subsequence and algorithm analysis, subsequence Algorithm Analysis

Problem Description: Given n integer sequences {a1, a2 ,..., an}, evaluate the function f (I, j) = max {0, Σ ak} (k: continuous from I to j );

The problem is to find the maximum value of the continuous subcolumns and take 0 if the maximum value is negative, for example, 8 number sequences }, the maximum subsequence of NaMo is 4 + (-2) + 5 = 7.

There are four algorithms with different complexity. The time complexity of algorithms 1 to 4 is O (n3), O (n2), O (nlogn), O (n );

Algorithm 1:

The most direct method is to list all the situations. we can set the left-end I and right-end j of the subsequence, calculate the sum from a [I] to a [j] by using one layer.

// Maximum subcolumn and exhaustive method
# Include
Using namespace std;
Int Find_Maxsun (int * a, int n );
Int main (){
Int n, I;
Int a [100];
Cin> n;
Cout <"Pleace Input The" <n <"Number:" <endl;
For (I = 0; I <n; I ++)
Cin> a [I];
Cout < Return 0;
}
Int Find_Maxsun (int * a, int n ){
Int MaxSun = 0, I, j, k;
Int NowSum;
For (I = 0; I <n; I ++)/* left end of the subsequence */
For (j = 0; j <n; j ++) {/* right end of the subsequence */
NowSum = 0;
For (k = I; k <= j; k ++)
NowSum + = a [k];/* subsequence from a [I] to a [j */
If (NowSum> MaxSun)
MaxSun = NowSum;/* update result */
}
Return MaxSun;
}

Obviously, the law uses a three-for loop, and the algorithm time complexity is O (n3). This is of course the most stupid algorithm, but when the data is very huge, even if we want to calculate the rhythm of death, we can clearly see the third layer for loop,

J every addition, subcolumns and all have to calculate the first time, then why do we not use the results of the J-1? That is to say, we save the results of the J-1, in the calculation of the results of step j, just need to add a [j] on the basis of the J-1 step, you can, so there is Algorithm 2.

Algorithm 2:

# Include
Using namespace std;
Int Find_Maxsun2 (int * a, int n );
Int main (){
Int n, I;
Int a [100];
Cin> n;
Cout <"Pleace Input The" <n <"Number:" <endl;
For (I = 0; I <n; I ++)
Cin> a [I];
Cout <Find_Maxsun2 (a, n) <endl;
Return 0;
}
Int Find_Maxsun2 (int * a, int n ){
Int I, j, NewSum = 0, MaxSum = 0;
For (I = 0; I <n; I ++) {/* indicates the left endpoint of the sequence */
NewSum = 0;
For (j = I; j <n; j ++) {/* j is the right endpoint of the series */
NewSum + = a [j];/* update NewSum every time in J-1 */
If (NewSum> MaxSum)/* update MaxSum */
MaxSum = NewSum;
}
}
Return MaxSum;
}

This algorithm is smarter than 1. the complexity of the algorithm is O (n2), which is obviously not the complexity we want.

Algorithm 3:

Algorithm 3 uses the idea of divide and conquer. The basic idea is self-evident, divide the problem into small problems, and then solve the problem by sum up small problems. we split the original sequence into two parts, the basic idea is as follows:

Step 1: split the original sequence into two parts: the left sequence and the right sequence.

Step 2: recursively obtain the left and right of the subsequence S.

Part 3: scan the left and right sides of the line to find out the largest subsequence and S that span the midline.

Step 4: obtain S = max {S left, S middle, and S right };

The code is implemented as follows:

# Include
Using namespace std;
Int Find_MaxSum3 (int * a, int low, int high );
Int Max (int a, int B, int c );
Int main (){
Int n, I;
Int a [100];
Cin> n;
Cout <"Pleace Input The" <n <"Number:" <endl;
For (I = 0; I <n; I ++)
Cin> a [I];
Cout <Find_MaxSum3 (a, 0, n-1) <endl;
Return 0;
}
Int Find_MaxSum3 (int * a, int low, int high ){
Int MaxSum = 0, MidSum, LeftSum, RightSum, I;
MidSum = 0;
If (low = high) {/* recursive termination condition */
If (a [low]> 0)
Return a [low];
Else
Return 0;
}
Int mid = (low + high)/2; // locate the point
LeftSum = Find_MaxSum3 (a, low, mid);/* recursively find the largest sequence on the left and */
RightSum = Find_MaxSum3 (a, mid + 1, high);/* recursively find the largest subsequence of the right sequence and */
/* You can then find the largest sum of the intermediate cross-boundary sequences */
Int NewLeft = 0, Max_BorderLeft = 0, NewRight = 0, Max_BorderRight = 0;
For (I = mid; I> = low; I --) {/* scan left to find the largest and */
NewLeft + = a [I];
If (NewLeft> Max_BorderLeft)
Max_BorderLeft = NewLeft;
}
For (I = mid + 1; I <= high; I ++) {/* scan right to find the maximum subsequence and */
NewRight + = a [I];
If (NewRight> = Max_BorderRight)
Max_BorderRight = NewRight;
}
MidSum = Max_BorderRight + Max_BorderLeft;
Return Max (LeftSum, MidSum, RightSum);/* return the cure result */
}
Int Max (int a, int B, int c) {/* find the maximum number of three */
If (a> = B & a> = c)
Return;
If (B> = a & B> = c)
Return B;
If (c> = B & c> =)
Return c;
}

Let's calculate the time complexity of this algorithm:

T (1) = 1;

T (n) = 2 T (n/2) + O (n );

= 2kT (n/2 k) + kO (n) = 2kT (1) + kO (n) (n = 2 k) = n + nlogn = O (nlogn );

Although this algorithm is already very good, it is not the fastest algorithm.

Algorithm 4:

Algorithm 4 is called online processing. This means that each data read is processed in a timely manner, and the result is true for the currently read data. that is, the algorithm can provide a correct solution to terminate the read at any position, read and describe.

# Include
Using namespace std;
Int Find_MaxSum4 (int * a, int n );
Int main (){
Int n, I;
Int a [100];
Cin> n;
Cout <"Pleace Input The" <n <"Number:" <endl;
For (I = 0; I <n; I ++)
Cin> a [I];
Cout <Find_MaxSum4 (a, n) <endl;
Return 0;
}
Int Find_MaxSum4 (int * a, int n ){
Int I, NewSum = 0, MaxSum = 0;
For (I = 0; I <n; I ++ ){
NewSum + = a [I];/* current subsequence and */
If (MaxSum <NewSum)
MaxSum = NewSum;/* update the maximum subsequence and */
If (NewSum <0)/* discard if it is less than 0 */
NewSum = 0;
}
Return MaxSum;
}

This algorithm scans the read data one by one and has only one for loop. it solves the same problem and the algorithm is very different. it is a trick to let the computer remember some key intermediate results, avoid repeated computation.

Explain problem description: Given n integer sequences {a1, a2 ,..., an}, evaluate the function f (I, j) = max {0, a k} (k: continuous from I to j); the problem is to find continuity...

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.