[Programming Pearl] Chapter 8 Algorithm Design Technology

Source: Internet
Author: User

I. Overview

Problem: calculate the maximum sum of continuous subvectors in one-dimensional arrays.

For example, if a [6] = {3, 4,-2,-9, 10, 8}, the sum of the maximum continuous subvectors is 10 + 8 = 18.

1) solution 1: simple algorithms

#include <stdio.h>#define max(a, b) ((a)>(b)?(a):(b))int main(){    int a[6]={3,4,-2,-9,10,8};     int i,j,k;    int sum=0;    int maxsofar=0;    for(i=0;i<6;++i){for(j=i;j<6;++j){sum=0;for(k=i;k<=j;++k){sum+=a[k];}maxsofar=max(maxsofar,sum);}}printf("max=%d\n",maxsofar);return 0;}

2) Two square Algorithms

Algorithm 1:

#include <stdio.h>#define max(a, b) ((a)>(b)?(a):(b))int main(){    int a[6]={3,4,-2,-9,10,8};     int i,j;    int sum=0;    int maxsofar=0;    for(i=0;i<6;++i){sum=0;for(j=i;j<6;++j){    sum+=a[j];maxsofar=max(maxsofar,sum);}}printf("max=%d\n",maxsofar);return 0;}

Algorithm 2:

#include <stdio.h>#define max(a, b) ((a)>(b)?(a):(b))int main(){    int a[6]={3,4,-2,-9,10,8};int cumarr[6]; cumarr[-1]=0;    int i,j;    int sum=0;    int maxsofar=0;    for(i=0;i<6;++i)    {    cumarr[i] = cumarr[i-1] + a[i];    }for(i=0;i<6;++i){sum=0;for(j=i;j<6;++j){    sum = cumarr[j] - cumarr [i-1];maxsofar=max(maxsofar,sum);}}printf("max=%d\n",maxsofar);return 0;}

3) Grouping Algorithm

Thought: There are three cases where M is the dividing line and the maximum value is

I. On the left side of m

2. Right of m

Iii. spanning m

Key: When you first solve the left and right maximum values, you must increase progressively from the center to the two sides. See the source code explanation ......

#include "stdio.h"#define max(a,b)  a>b?a:bint a[6]={3,4,-2,-9,10,8};int max2(int a,int b,int c){if(a>b&&a>c)return a;else if(b>a&&b>c)return b;else return c;}  int maxsum(int l,int u){int i,m,sum,lmax,rmax;if(l>u)return 0;if(l==u)return max(0,a[l]);        m=(l+u)/2;lmax = sum =0;for(i=m;i>=l;--i)   {sum += a[i];lmax =max(lmax,sum);}printf("m=:%d\n",m);rmax =sum =0;for(i=m+1;i<=u;++i){sum += a[i];rmax =max(rmax,sum);}return max2(lmax + rmax , maxsum(l,m) , maxsum(m+1,u) );}int main(){int maxsofar=maxsum(0,5);printf("max=%d",maxsofar);return 0;}

[Note] If max2 () uses a macro # define max (a, B, c) max (a, B)> C? Max (A, B): c won't get the correct result

 

4) scan algorithm

#include "stdio.h"int main(){    int a[6]={3,4,-2,-9,10,8};int i,sum=0;for(i=0;i<6;++i){sum+=a[i];if(sum<0)sum=0;}printf("max=%d",sum);return 0;}

 

Ii. Summary

Optimization Algorithm Policy

1) Save the calculated status to avoid repeated computation.

2) pre-process the information to the data structure. Example: algorithm 2

3) divide and conquer algorithms and adopt more advanced algorithms

4) Clever scanning Algorithms

5) lower bound: proves the lower bound of a matching and determines the optimal algorithm.

Iii. Exercise

10) two methods can be used: O (N * n)

# Include <stdio. h> # define min (A, B) (a)> (B )? (B) :( A) # define OBS (a) a> 0? A:-aint main () {int A [6] = {3, 4,-2,-9, 10, 8}; int I, j, k; int sum = 0; int maxsofar = 65535; // This is critical for (I = 0; I <6; ++ I) {for (j = I; j <6; ++ J) {sum = 0; For (k = I; k <= J; ++ K) {sum + = A [k];} maxsofar = min (maxsofar, OBS (SUM) ;}} printf ("max = % d \ n", maxsofar); Return 0 ;}


The returned result is 1.



The O (nlogn) algorithm can be used.

 

# Include <stdio. h> # define min (A, B) (a)> (B )? (B) :( A) # define OBS (a) a> 0? A:-A/* first record the axis in temp. First, locate the less than axis from the back to the first (that is, the position of the axis) find the element greater than the axis from the back and store it in the position smaller than the axis found in the previous step: to do this, replace the following with those smaller than the axis and those greater than the axis.] after exiting the while loop, Because I is added from the lower end, the corresponding axis of the I position should be stored at the end. */INT partition (int A [], int I, Int J) {int temp = A [I]; // records the position of the lower axis while (I <j) {While (I <J & temp <= A [J]) // move a smaller value than the axis to the low-end j --; A [I] = A [J]; // The smaller one equals the greater one (the later I will increase) [this trip will not be moved after the large one is moved to the front.] While (I <J & temp> = A [I]) // move a node larger than the axis to the high end. [move it from the front to the back, find a small one.] I ++; A [J] = A [I]; // The small (changed here) found above the [J] storage is equal to the large []} A [I] = temp; // return I;} void quicksort (int A [], int I, Int J) {int P; if (I <j) {P = partition (, i, j); quicksort (A, P-1); quicksort (A, P + 1, J) ;}} int main () {int A [6] = {3, 4,-2,-9, 10, 8}; int cumarr [6]; cumarr [-1] = 0; int I, J; int minsofar = 65535; for (I = 0; I <6; ++ I) {cumarr [I] = cumarr [I-1] + A [I];} quicksort (cumarr, 0, 4); for (I = 0; I <5; ++ I) {minsofar = min (minsofar, OBS (cumarr [I + 1]-cumarr [I]);} printf ("min = % d \ n", minsofar); Return 0 ;}

The returned result is 1.


13) Given the N * n Array, calculate the maximum sum of the rectangle subarray.

See the blog: http://blog.csdn.net/tianshuai11/article/details/7487882

 

 

 

 

 

 

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.