< data structure and algorithm analysis C + + description > Algorithm analysis of the largest sub-sequences and problems

Source: Internet
Author: User

Statement: This series of blogs is the "Data structure and algorithm analysis C + + description" Reading notes series

Reference Blog: Click to open link

This article is the second chapter of the original book, the main content includes: The algorithm of time complexity analysis/algorithm optimization, the analysis of the example is very famous for the largest sub-sequence summation problem.

There are four ways to solve: exhaustive/exhaustive optimization/recursive (split)/Online algorithm (dynamic programming), algorithm complexity is O (n^3)/O (n^2)/O (N*LOGN)/O (N). The ideas are in the code.

The----------------------------------------code is as follows-------------------------------------------------------------------

#include <iostream> #include <vector> #include <algorithm>using namespace std;/* * Calculates and returns the maximum subsequence and: exhaustive traversal * Algorithmic complexity: O (n^3) */int maxSubSum1 (const vector<int> & a) {int maxsum=0;for (int i=0; i < a.size (); i++) {for (int j= I J < A.size (); J + +) {int thissum = 0;for (int k=i; k <= J; k++) Thissum + = A[k];if (Thissum > Maxsum) maxsum = Thissum;}} return maxsum;} /* * Same as poor lifting, optimized for the above poor lifting * algorithmic complexity: O (n^2) */int maxSubSum2 (const vector<int> &a) {int maxsum = 0;for (int i = 0; I &lt ; A.size (); i++) {int thissum = 0;for (int j=i; j < A.size (); j + +) {thissum + = a[j];if (Thissum > Maxsum) maxsum = Thissum;}} return maxsum;} /* * Using recursion, divide the sequence into the left and right and the middle three most of which can be solved by recursion, * while the middle part is the largest value of the first half (containing the last element of the left half) and the maximum value of the correct half (containing the right half of the second value) and * Comparing the three values is the final result * Algorithmic complexity: O (N*LOGN) */int maxSubSum3 (const vector<int> &a, int left, int. right) {if (left = right) {if (A[left] ; 0) return A[left];elsereturn 0;} int center = (left + right)/2;int maxleftsum = MaxSubSum3 (A, left, center); int MaxrightsUm = MaxSubSum3 (A, center+1, right); int maxleftandright = max (maxleftsum, maxrightsum); int maxleftbordersum = 0, Leftborde Rsum = 0;for (int i = center; I >= left; i--) {leftbordersum + = a[i];if (Leftbordersum > Maxleftbordersum) maxleftbord Ersum = Leftbordersum;} int maxrightbordersum = 0, rightbordersum = 0;for (int i = center+1; I <= right; i++) {rightbordersum + = A[i];if (RightB Ordersum > Maxrightbordersum) maxrightbordersum = rightbordersum;} Return Max (maxleftandright, maxleftbordersum + maxrightbordersum);} /* Dynamic planning, online algorithm (on-line algorithm) * Online algorithm: At any time, the algorithm to the operation of the data only read (scan) once, once read into and processing, it does not need to be remembered. In this process, the algorithm can immediately give the correct answer to the corresponding sub-sequence problem for the data it has been read into. Algorithms with this feature are called online algorithms (on-line algorithm). * Algorithmic complexity: O (N) */int maxSubSum4 (const vector<int> &a) {int maxsum=0;int thissum=0;for (int i=0; i<a.size (); i++ {Thissum + = a[i];if (Thissum > Maxsum) maxsum = Thissum;else if (thissum < 0) thissum = 0;} return maxsum;} int main () {int input[] = {4,-3,5,-2,-1,2,6,-2};int length = sizeof (input)/4;//Note here The array is used to giveWhen the vector container initializes the assignment, the interval is [begin,end] vector<int> inputvector (&input[0], &input[length]);//int result = MAXSUBSUM3 (inputvector, 0, Inputvector.size ()-1); int result = MAXSUBSUM4 (inputvector); cout << "The Maxsubsum is" & lt;< result << Endl;return 0;} Test//input[8] = {4,-3,5,-2,-1,2,6,-2};


< data structure and algorithm analysis C + + description > Algorithm analysis of the largest sub-sequences and problems

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.