Maximum sub-sequence and algorithm analysis, sub-sequence algorithm analysis _php tutorial

Source: Internet
Author: User

Maximum sub-sequence and algorithm analysis, sub-sequence algorithm analysis


Problem Description: Given n integer sequence {a1,a2,..., an}, function f (i,j) =max{0,σak} (k: Continuous from I to j);

The problem is the maximum value of the continuous sub-columns, if the maximum is negative, take 0, such as 8 number sequence { -1,2,-3,4,-2,5, -8,3}, the maximum subsequence and the 4+ (-2) +5=7.

This problem has four different complexity algorithms, the time complexity of the algorithm 1 to four is O (N3), O (N2), O (Nlogn), O (n);

algorithm One :

The most straightforward method is the exhaustive method, which lists all the cases where we can set the left and right J of the subsequence, and then use a layer to calculate the a[i] to a[j].

Maximum sub-columns and exhaustive methods
#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++)/* Sub-sequence left side */
for (j = 0; J < N; j + +) {/* sub-sequence right end */
nowsum = 0;
for (k = i; k <= J; k++)
Nowsum + = A[k]; /* Sub-sequence from a[i] to a[j] */
if (Nowsum>maxsun)
Maxsun = Nowsum; /* Update Results */
}
return Maxsun;
}

Obviously, the brute force law uses the 3 for loop, the algorithm time complexity is O (N3), which is certainly a most stupid algorithm, but the data is very large, even if it is to calculate the rhythm of death, we can clearly see the third layer for loop,

J each time, the child column and all to be counted again, then we why not to use the result of j-1? That is to say, we will save the results of the j-1, in the calculation of the results of J step, just need to j-1 step on the basis of the a[j], you can, so there is algorithm two.

Algorithm two:

#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++) {/* is the left end of the sequence */
newsum = 0;
for (j = i; J < N; j + +) {/*j is the right end of series */
Newsum + = A[j]; /* Every time you update newsum*/under j-1 conditions
if (newsum>maxsum)/* Update maxsum*/
Maxsum = Newsum;
}
}
return maxsum;
}

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

Algorithm three:

Algorithm three uses the idea of dividing the law, the basic idea is self-evident First division after the rule, the problem is broken down into small problems and can be solved in the sum of small problems, we put the original sequence in Split, then the largest sub-sequence on the left, on the right, or across the boundary, the basic idea is as follows:

The first step: divide the original sequence into left and right sequences.

The second step: recursion to find the sub-sequence s left and s right.

Part Three: Scan from the sides of the line to find the largest sub-sequence and s in the cross-midline.

Fourth step: Seek S=max{s left, s in, 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; Find the midpoint of the points
Leftsum = FIND_MAXSUM3 (A, low, mid); /* Recursively find the left sequence max and */
Rightsum = Find_maxsum3 (A, mid + 1, high); /* Recursively find the maximum subsequence of the right sequence and */
/* Then the maximum and/or the intermediate cross-boundary sequence can be
int newleft = 0,max_borderleft=0, newright = 0,max_borderright=0;
for (i = Mid, I >= low; i--) {/*-scan left to find max and */
Newleft + = A[i];
if (Newleft > Max_borderleft)
Max_borderleft = Newleft;
}
for (i = mid + 1, I <= high; i++) {/* scan right to find maximum subsequence and */
Newright+=a[i];
if (newright >= max_borderright)
Max_borderright = Newright;
}
Midsum = Max_borderright + max_borderleft;
Return Max (Leftsum, Midsum, rightsum); /* Return results of treatment */
}
int max (int A, int b, int c) {/* Find the largest number of 3 */
if (a>= b&&a >= c)
return A;
if (b >= a&&b >= c)
return b;
if (c >= b&&c>=a)
return C;
}

Let's calculate the time complexity of this algorithm:

T (1) = 1;

T (n) =2t (N/2) +o (n);

=2kt (n/2k) +ko (n) =2kt (1) +ko (n) (wherein n=2k) =n+nlogn=o (NLOGN);

Although this algorithm is very good, but not the fastest algorithm.

Algorithm four:

Algorithm four is called online processing. This means that every time a data is read into a timely process, the result is that the current reading of the data are established, that is, in any position to terminate the read, the algorithm can give the correct solution, edge-reading edge solution.

#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 sub-sequence and/*
if (Maxsum < newsum)
Maxsum = Newsum; /* Update maximum subsequence and/*
if (Newsum < 0)/* Less than 0 Discard */
newsum = 0;
}
return maxsum;
}

This algorithm is to scan the data read one after another, only a For loop, solve the same problem algorithm is very different, lies in a trick, let the computer remember some of the key intermediate results, to avoid repeated calculation.

http://www.bkjia.com/PHPjc/1044670.html www.bkjia.com true http://www.bkjia.com/PHPjc/1044670.html techarticle maximal subsequence and algorithm analysis, sub-sequence algorithm analysis problem description: given n integer sequence {a1,a2,..., an}, the function f (i,j) =max{0,a k} (k: Continuous from I to j); The problem is to seek continuous ...

  • Related Article

    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.