Four algorithms for maximal sub-sequences and

Source: Internet
Author: User

1. Poor Lifting method

Algorithm idea: Calculate the and of each sub-sequence, that is, the number of the series I to J and (J>=i), and compare

Algorithm:

	public static int MaxSubSum1 (int[] a) {
		int maxsum = 0;
		int sum;
		for (int i = 0, i < a.length; i++) {for
			(int j = i; J < A.length; J + +) {
				sum = 0;
				for (int k = i; k <= J; k++) {
					sum + = a[k];//calculate a[i] to A[j] and
				}
				if (Sum > Maxsum) {
					maxsum = Sum;
  }}}
		return maxsum;
	}

Run time O (n^3)


2. Improvements to the first algorithm above

Algorithm idea: The third for loop of the first algorithm has a large number of unnecessary repetition calculations, such as: Calculate I to J's and, however I to j-1 and in the previous cycle has been calculated, no need to repeat the calculation, so the for loop can be removed

Algorithm:

	public static int maxSubSum2 (int[] a) {
		int maxsum = 0;
		int sum;
		for (int i = 0; i < a.length; i++) {
			sum = 0;
			for (int j = i; J < A.length; J + +) {
				sum + = a[j];
				if (Sum > Maxsum) {
					maxsum = sum;}}
		}
		return maxsum;
	}
Run time O (n^2)

3. Divide and conquer

Algorithm thought: Divides the question into two roughly equal sub-problems, then solves them recursively, this is "divides" the part. The "cure" phase will fix two sub-problems together and may do a little additional work, and finally get the solution of the whole problem.

In this case, if the sequence is divided into two parts from the middle, then the maximal sub-sequence and may appear in three places, or the whole appears in the left half of the input data, or the whole appears in the right half, or across the dividing line. The first two cases can be solved recursively, the maximum sum of the third case can be obtained by finding the largest sum of the first half (including the last element of the first half) and the largest sum of the second half (containing the first element of the second half), at which point two and add.

Algorithm:

	Parameters: Working with array, left border, right bounded public static int maxSubSum3 (int[] A, int left, int right) {if (Ieft = right) {if (A[left] &G T
			0) {return a[left];
			} else {return 0;
		}} int Center = (left + right)/2;
		int maxleftsum = MAXSUBSUM3 (A, left, center);

		int maxrightsum = MAXSUBSUM3 (A, center + 1, right);
		int maxleftbordersum = 0, leftbordersum = 0;
			for (int i = center, I >= left; i--) {leftbordersum + = A[i];
			if (Leftbordersum > maxleftbordersum) {maxleftbordersum = Leftbordersum;
		}} int maxrightbordersum = 0, rightbordersum = 0;
			for (int i = center + 1, I <= right; i++) {rightbordersum + = A[i];
			if (Rightbordersum > maxrightbordersum) {maxrightbordersum = Rightbordersum;
		}} int maxbordersum = Maxleftbordersum + maxrightbordersum; return maxbordersum > Maxleftsum? Maxbordersum > Maxrightsum? MaxBorderSum:maxRightSum:maxLeftSum > Maxrightsum?
	Maxleftsum:maxrightsum; }
Run time O (N*LOGN)


4. Best starting point

Algorithm thought: Set A[i] and the starting point of the maximum sequence, if a[i] is negative, then it cannot represent the starting point of the optimal sequence, because any subsequence containing a[i] as the starting point can be improved by a[i+1] as the starting point.

Similarly, any negative subsequence is not likely to be the prefix of the optimal sub-sequence.

Algorithm:

	public static int maxSubSum4 (int[] a) {
		int maxsum=0,sum=0;
		for (int i=0;i<a.length;i++) {
			sum+=a[i];
			if (sum>maxsum) {
				maxsum=sum;
			} else if (sum<0) {
				sum=0;
			}
		}
		return maxsum;
	}
Run time: O (N)


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.