[8th] Algorithm design Technology--one-dimensional pattern recognition

Source: Internet
Author: User

The eighth chapter mainly introduces the algorithm improvement process of one-dimensional pattern recognition. Question input: n a vector x of floating-point numbers. Problem output: maximum and in any successive sub-vectors.

(1) First from the most intuitive approach, is to enumerate all cases of the sub-vector and the last comparison of the largest and. First, using a two-tier for loop, all possible sub-vectors are listed, and the sub-vectors are calculated with a one-layer for loop. The operation time is O (n^3).

int MaxNum1 (int* a,int _len) {int maxnum=0, for (int i=0;i<_len;i++) {for (int. j=i;j<_len;j++) {int tnum=0; for (int k=i;k<=j;k++) {tnum+=a[k];} maxnum=maxnum>tnum?maxnum:tnum; }} return maxnum; }

  

Next, the author starts to guide us through the analysis of the algorithm itself, to observe carefully, where it is not necessary, and can be optimized. One of the ideas at this time is to avoid repetitive operations. According to this idea, we observe the most inner loop, we can find that every time we have to recalculate the sub-vector and the next sub-vector and actually only need to be on the basis of the first sub-vector and then add another. At this point, it leads to scenario 2.

(2) First of all, from the beginning of the first, to any one of the sub-vector and. Then any one of the vectors in the whole vector is actually the difference of two children starting from the first item. On the code. The code has a time complexity of O (n^2).

int MaxNum2 (int* a,int _len) {int* num=new Int[_len]; for (int i=0;i<_len;i++) num[i]=0;//notice, here to num[-1] 0;
num[-1]=0; for (int i=0;i<_len;i++) {num[i]=num[i-1]+a[i];} int maxnum=0; for (int. i=0;i<_len;i++) for (int j=i;j<_len;j++) maxnum=maxnum> (num[j]-num[i-1])? Maxnum: (Num[j]-num[i-1]); return maxnum; }

 

(3) At this time, it seems to me that there is no train of thought. Look at the author's introduction, it leads to the algorithm design of the important ideas, divide and conquer law. The time complexity of the algorithm is O (NLOGN). The key of divide and conquer method is how to classify the problem into corresponding sub-problem, and the recursive termination condition. Here, the vector is divided into three parts from the midpoint: the left-most and sub-vector of the midpoint, the maximal sub-vector of the midpoint, and the largest sub-vector of the right-point. For left and right, recursive.

int Maxnum (int* a,int l,int u) {   //recursive termination condition if (l>=u) return a[l];//start recursive int leftmax=0; int rightmax=0; int m= (L+U)/2; Start from point to left, maximum value. int leftnum=0;  for (int i=m;i>=l;i--) {leftnum+=a[i]; leftmax=leftmax>leftnum?leftmax:leftnum;}//From point to right, Max. int rightnum=0; for (int i=m+1;i<=u;i++) {rightnum+=a[i]; rightmax=rightmax>rightnum?rightmax:rightnum;}//To derive the maximum value of the sub-vector containing the midpoint int Mmax=leftmax+rightmax; The maximal sub-vectors and int lmax=maxnum (A,L,M) of the left and right side of the midpoint are calculated recursively. int Umax=maxnum (A,M+1,U); Find the maximum value of the three int max=lmax>umax?lmax:umax; Max=max>mmax? Max:mmax; return Max; } int MaxNum3 (int* a,int _len) {return maxnum (a,0,_len-1);}

  

(4) Scanning algorithm. Suppose we know the maximum value of a child vector up to x[n-1], which requires the maximum value of a child vector up to x[n]. Then, there are two cases: either the maximum child vector contains x[n], or it is not included. If not included, it means the maximum value of the child vector up to x[n-1], as previously calculated. If it is included, it means that the maximum value is calculated from X[n] and the value is evaluated forward. We also assume that we know the maximum value of the child vectors that start from x[n-1].

At first, the understanding was wrong.

Error Understanding int maxnumx (int* a,int _len) {int maxagg=0; int maxend=0; for (int i=0;i<_len;i++) {int sum=0; for (int j=i;j> =0;j--) {sum+=a[j]; if (sum>maxend) maxend=sum;} maxagg=maxagg>maxend?maxagg:maxend; } return Maxagg; }

  

The correct code is as follows:

int Maxnumxx (int* a,int _len) {int maxagg=0; int maxend=0; for (int i=0;i<_len;i++) {maxend= (maxend+a[i]) >0? ( Maxend+a[i]): 0; maxagg=maxagg>maxend?maxagg:maxend; } return Maxagg; }

  

[8th] Algorithm design Technology--one-dimensional pattern recognition

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.