An optimized __ algorithm for a pattern matching algorithm

Source: Internet
Author: User

See "Programming Zhuji" 8th chapter, inside a chapter introduced to a pattern matching problem algorithm optimization strategy, because the optimized algorithm and the original algorithm efficiency greatly enhanced, I was shocked.

Problem:

Find the maximum vector in a vector.

There was pseudocode on the book, and I realized it.

Original algorithm, a 3 cycle

int Alg1 (int* x,int count)
{
  int maxsofar = 0;
  for (int i=0;i<count;i++)
  {for
    (int j=i;j<count;j++)
    {
      int sum = 0;
      for (int k=i;k<j;k++)
      {
        sum +=x[k];
      }
      Maxsofar = max (maxsofar,sum);
    }
  }
  return maxsofar;
}
Simple improvements, the most internal cycle waste the previous addition results, so the 3-step cycle can be optimized to 2-step:
int Alg2 (int* x,int count)
{
  int maxsofar = 0;
  for (int i=0;i<count;i++)
  {
    int sum = 0;
    for (int j=i;j<count;j++)
    {
      sum + = x[j];
      Maxsofar = max (maxsofar,sum);
    }
  }
  return maxsofar;
}
This algorithm is also 2 order, but it first opened up the memory space calculation and, the array of [-1] position of the value first saved, and then restore, the book said not to save also no problem, I try to direct stack error.
int alg2b (int* x,int count)
{
  int cumarr[maxnum];
  int hold  = cumarr[-1];
  Cumarr[-1] = 0;
  for (int i=0;i<count;i++)
    cumarr[i] = cumarr[i-1] + x[i];
  int maxsofar = 0;
  int sum;
  for (int i=0;i<count;i++)
  {for
    (int j=i;j<count;j++)
    {
      sum = cumarr[j]-cumarr[i-1];
      Maxsofar = max (Maxsofar, sum);
    }
  CUMARR[-1] = hold;
  return maxsofar;
}
Then optimize.

The next is the application of the idea of divide and conquer, divide and conquer is undoubtedly to divide a big problem into some small problem, here first divides the entire array into 2 decimal groups, so the largest sub array is either in the previous one, or in the latter, or across the 2 arrays, among them. If it is spanned, then the maximum sub array is centered around the front and back array, and the maximum sub array can be obtained from the beginning of the dividing number, and the maximum sub array is obtained backwards, and is the largest sub array that spans. Partition is 2 points, its efficiency is O (NLOGN)

int Alg3 (int* x,int count,int l,int u)
{
  int lmax,rmax,sum,m;
  if (L > U) return 0;
  if (L = = u) return to Max (0,x[l]);
  m = (l+u)/2;
  Lmax = sum = 0;
  for (int i=m;i>=l;i--)
  {
    sum + = X[i];
    Lmax = max (lmax,sum);
  }
  Rmax = sum = 0;
  for (int i=m+1;i<=u;i++)
  {
    sum + = X[i];
    Rmax = max (rmax,sum);
  }
  Return Max (Lmax+rmax, Max (Alg3 (x,count,l,m), Alg3 (X,count,m+1,u));
}
Finally, a NB of the extreme scanning algorithm, only a first-order. Principle: In the first I element, the maximum sum array is either in the first I-1 element (we store it in Maxsofar), or its end position is I (we store it in Maxendinghere). The code is very incisive, I think even know the principle is not necessarily write such code.
int Alg4 (int* x, int count)
{
  int maxsofar = 0;
  int maxendinghere = 0;
  for (int i=0;i<count;i++)
  {
    maxendinghere = max (maxendinghere+x[i],0);
    Maxsofar = max (maxsofar,maxendinghere);
  }
  return maxsofar;
}
Finally, the result: Count = 10000, this magnitude, algorithm 1 takes several minutes, I am too lazy to let it go.

This lets the program generate a random array, and because the seed is fixed, the random array is fixed each time the program is run.

int X[maxnum];
Srand (1);//array is same when running the program everytime for
(int i=0;i<maxnum;i++)
  x[i] = rand ()%100-50;

Alg2 result:5264 | Time cost:2.839s
ALG2B result:5264 | Time Cost:2.791s
Alg3 result:5264 | Time cost:0.011s
Alg4 result:5264 | Time cost:0.001s

That 0.001 was also exaggerated, actually under the millisecond order of magnitude.

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.