Maximum subsequence and algorithm analysis-how important a good algorithm is for programming!!!

Source: Internet
Author: User

Last night in the dorm to see Mark Allen Weiss's "Data Structure and algorithm analysis Java language description" of this book, see the second chapter of the time to give an example to discuss, is about the maximum sub-sequence and algorithm analysis. A total of four algorithms, first when you see the first algorithm when you think this algorithm is good, can be achieved, and then when you see the next attached to the three examples of the time this only understand the optimization of the overall performance of the algorithm, sometimes we do not think of a good algorithm, Just when the first algorithm came out of our thinking has been limited, think is right to think is the only solution, the RealSense sigh! In the morning came to the laboratory specially found some information, posted here, after more learning more look good algorithm!

Maximum continuous subsequence Sum summation of maximal continuous sub-sequences1. Problem description Enter an integer sequence (the floating-point sequence is also suitable for the algorithm described here) to find out the maximum value of the sum of successive sub-sequences.        2. Algorithm Analysis 2.1.       Algorithm 12.1.1. The algorithm describes traversing all sub-sequences and summing them to compare the maximum values. 2.1.2. Code description
1 Public Static intMaxsubsumcubic (int[] Array) {2intmaxsum = 0;//sum of maximal sub-sequences3//Start indicates the starting index of the subsequence to sum, and end indicates the end index4 for(intstart = 0; Start < Array.Length; start++) {5 for(intend = start; End < Array.Length; end++) {6intthissum = 0;//Sum of current sub-sequence7//the and of the Array[start]~array[end] sub-sequence is calculated8 for(intindex = start; Index <= end; index++) {9 Thissum + =Array[index];10                           }11//determines whether the sum of the largest subsequence obtained before is greater12if(Thissum >maxsum) {Maxsum =thissum;14                           }15                    }16             }17returnmaxsum;18}
2.1.3. Algorithm analysis The input sequence length is n, the algorithm has three loops nested, and the 4th row has a loop length of N. The loop length for line 5th is n-start+1, because we consider the worst performance, so it is preferable to be the largest n. The loop length of line 8th is end-start+1, which is preferable to N. So the algorithm run time is O (n*n*n) =o (n^3), that is, the algorithm run time is the input length of the cubic growth. It can be imagined that once the input length becomes larger, the efficiency of the algorithm will be too slow to accept, which also shows the importance of the algorithm design from the opposite side.

2.2. Algorithm 22.2.1. An important principle of algorithm description algorithm design is "Don't repeat things". In algorithm one, the sum of the array[start]~array[end] subsequence can be obtained by adding array[end] to the result of the last summation Array[start]~array[end-1] instead of starting from scratch. 2.2.2. Code description
1 Public Static intMaxsubsumquadratic (int[] Array) {2intmaxsum = 0;//sum of maximal sub-sequences3//Start indicates the starting index of the subsequence to sum, and end indicates the end index4 for(intstart = 0; Start < Array.Length; start++) {5intthissum = 0;//Sum of current sub-sequence6 for(intend = start; End < Array.Length; end++) {                            7//Array[start]~array[end-1] Sub-sequences of the obtained and plus array[end]8//get Array[start]~array[end] sub-sequences and9 Thissum + =Array[end]; 10//determines whether the sum of the largest subsequence obtained before is greater11if(Thissum >maxsum) {Maxsum =thissum;13                           }14                    }15             }16returnmaxsum;17}
2.2.3. Algorithm analysis algorithm two-ratio algorithm one less a loop, as before the analysis, easy to get the algorithm run time is O (n^2), the algorithm run time is the input length of the cubic growth.       2.3. Algorithm 32.3.1. Algorithm description Consider dividing the input sequence from the middle into two, then the maximum and sub-sequence position there are three cases: 1, the maximum and the sub-sequence is completely in the left half of the input sequence, 2, the largest and the sub-sequence is completely in the right half of the input sequence, 3, the largest and sub-sequences across the left and Therefore, in order to get the maximal subsequence of the input sequence and, we can find the maximal sub-sequence of the left half and the maximal sub-sequence of the right half, and the maximal sub-sequence of the two parts, and the most of them are required. In order to find the maximal subsequence of the left half part, the left part can be obtained recursively by the new input sequence. The largest sub-sequence of the right half is also the same. The next step is to solve the maximal sum of the sub-sequences that span the left and right halves, that is, to find the largest and most of the subsequence that contains the rightmost element (12 in), and the largest sum of the sub-sequences that contain the leftmost (6) part of the leftmost part, and add the two to the largest sub-sequence that spans the left Another thing to note is that for arrays with odd elements, the left and right halves are not equally divided, but this is not a problem. The above algorithm does not require a two-part, divided into any two parts can be. 2.3.2. Code description
1 Public Static intMaxsubsumrec (int[] Array,intLeftintRight ) {2//Recursive baseline condition: Only one element of the pending sequence3if(left = =Right ) {4//An empty set is also a subsequence, an empty set and a 0, so the maximum subsequence and the minimum is 05if(Array[left] > 0)6returnArray[left];7Else8return0;9                 }10 11//recursion to find the maximal subsequence of the left and right halves and12intCenter = (left + right)/2;13intMaxleftsum =Maxsubsumrec (array, left, center);14intMaxrightsum = Maxsubsumrec (array, center + 1, right);15 16//The maximum and the number of sub-sequences containing the rightmost element in the left part17intMaxleftbordersum = 0, leftbordersum = 0;18 for(inti = center; I >= left; i--) {Leftbordersum + =Array[i];20if(Leftbordersum >maxleftbordersum) {Maxleftbordersum =leftbordersum;22                    }23             }24 25//find the maximum and the number of sub-sequences containing the leftmost element in the right half26intMaxrightbordersum = 0, rightbordersum = 0;27 for(inti = center + 1; I <= right; i++) {Rightbordersum + =Array[i];29if(Rightbordersum >maxrightbordersum) {Maxrightbordersum =rightbordersum;31                    }32             }33 34//the maximum number of sub-sequences that span two parts and35intMaxleftrightsum = Maxleftbordersum +maxrightbordersum;36 37//the maximum value in Maxleftsum, Maxrightsum, and Maxleftrightsum is the maximum subsequence and38intMaxsubsum = 0;Maxsubsum = maxleftsum > maxrightsum?maxleftsum:maxrightsum;Maxsubsum = maxsubsum > maxleftrightsum?maxsubsum:maxleftrightsum;41 42returnmaxsubsum;43}
2.3.3.       algorithm analysis set t (n) indicates the elapsed time of the input sequence length n, if n=1, that is, only one element, then left==right, so there is t (1) = 1. For n>1, the program needs to run two recursive calls and two for loops. Each recursive call is run at a time equivalent to the run time t (N/2) of the algorithm when the input length is N/2, and two recursion is 2T (N/2). The run times for each for loop is N/2, and the statement run time in the loop body is constant, so the run time for the two for loop is O (n/2*2) =o (N). So for n>1, there is t (n) =2t (N/2) +o (n), for convenience, N instead O (n), order of magnitude unchanged, so T (n) =2t (N/2) +n. In Weiss's data structure and algorithm analysis Java language Description (second edition), the author concludes by observing the way T (n) =n* (k+1), where n=2^k, so there is t (n) =n* (k+1) =nlogn +n=o (n). Here I try to deduce this result, if there is a sparse error or better method, please feel free. Similarly, assuming that N is 2 k-th square, if not 2 of the K-square, direct derivation may be more complex. Of course we can understand that if n is not a K-square of 2, you can make N=n+n into a 2 K-square by adding N 0 at the beginning of the input sequence. Because the length of the input is increased, the running time is longer than the original, because the algorithm analysis is the worst performance, so it can be used to replace the original running time with a variable length of running time. So the question is also converted to the K-square of N 2, and the expression of T (n) is how to ask. Well, nonsense not much to say, direct derivation. Divided by n on both sides of T (N) =2t (N/2) +n (t (n))/n= (T (N/2))/(N/2) +1   =>  (t (n))/n-(T (N/2))/(N/2) =1 order f (n) = (t (n))/n , then there is f (n)-f (N/2) =1 downward recursion has f (N/2)-F (N/4) =1    ...    F (2)-f (1) = 1 or more K (=LOGN) The addition of the formula can be f (n)-F ( 1) =1+...+1=logn is t (n)/n-t (1)/1=logn can get T (n) =n logn+n=o (n logn) by the above inference process can be run time of the algorithm is T (n) =o (n logn) 2.4.       &nbsp The algorithm four 2.4.1.       algorithm describes the input sequence as a, the length is n, the sum is summed from the A0 and the maximum value is recorded, such as a (0), a (0) +a (1), a (0) +a (1) +a (2) ..., The sum of less than 0 stops until the start occurs. When set to a (i) starting at less than 0, that is, a (0), a (0) +a (1), ..., a (0) +...+a (p-1) are greater than 0, and a (0) +a (1) +...+a (p) <0. At this point, you can start the sum again from a (p+1) and record the maximum value. Why is it possible to do so? We divide the subsequence from a (1) to a (p) into two cases (the starting index of the subsequence is start and the end index is end): 1, End<=p,a (start) +...+a (end) =a (0) +...+a (start) +...a (end) –[a (0) +...+a (start-1)]. As previously known, for Start-1<p, there is a (0) +...+a (start-1) >0, so you can get a (0) +...+a (start) +...a (end) > A (Start) +...+a (end). And since a (0) +...+a (start) +...a (end) has already been considered, there is no need to consider a sub-sequence smaller than its. 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). In summary, you only need to start with a (p+1) to re-sum, repeat the above steps to get the maximum subsequence sum. 2.4.2.       Code Description
1 Public Static intMaxsubsumlinear (int[] Array) {2intMaxsum = 0, thissum = 0;3 for(intj = 0; J < Array.Length; J + +) {4 Thissum + =Array[j];5if(Thissum < 0) {6 thissum = 0;7                        }8Else if(Thissum >maxsum) {9 Maxsum =thissum;10                    }11             }12returnmaxsum;13}
2.4.3. Algorithm analysis algorithm four has only one for loop, and the execution time of the statement inside the loop is constant, so T (n) =o (n). It can be seen that the running time of the algorithm is linearly increased, which is better than the first three algorithms. Another advantage is that the algorithm needs to use an input value array[j] every time, so if it is in the hard disk or network transmission environment, we can achieve serial reading, reduce memory consumption. Turn from: http://blog.csdn.net/superchanon/article/details/8228212References: Data structure and algorithm analysis Java language Description (2nd edition), Mark Allen Weiss

Maximum subsequence and algorithm analysis-how important a good algorithm is for programming!!!

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.