Algorithm: Summary of division and treatment

Source: Internet
Author: User

The basic idea of divide-and-conquer law: to decompose a problem of scale n into a small self-problem of k size, these sub-problems are independent and identical to the original problem.

The general algorithm design pattern:

Divide-and-conquer (P)

{

if (| P|<=N0)

Adhoc (P);

Divide P into smaller subinstances

P1,P2,..., Pk;

for (int i = 1;i <= k;++i)

Yi = Divide-and-conquer (Pi);

Return Merge (Y1,y2,..., yk);

}

| P| indicates the specification of the problem

N0 says that when P scale does not exceed n0, then the problem is easy to solve and no need to decompose

Adhoc (P) is the basic sub-algorithm of the divide-and-conquer algorithm, which can be directly used to produce the result when the scale is not more than N0.

The function of merge () is the merging sub-algorithm in the divide-and-conquer algorithm, which merges the solution of all sub-problems into the solution of P

Example, is a merge sort

Template<class type>
void Merge (Type *sd,type *si,int left,int m,int right)
{
int i = LEFT,J = M+1;
int k = left;
while (i<=m && j<= right)
{
sd[k++] = Si[i] < Si[j]? si[i++]:si[j++];
}
while (i<=m)
{
sd[k++] = si[i++];
}
while (j<=right)
{
sd[k++] = si[j++];
}
}


Template<class type>
void Mergepass (Type *ar,type *br,int n,int s)
{//s = 2;
0 1 2 3//4 5 6 7//8 9
cout<< "s =" <<S<<ENDL;
for (int i = 0;i+2*s-1 <= n-1; i+=2*s)
{
Merge (ar,br,i,i+s-1,i+2*s-1);
cout<<i<< "" <<i+s-1<< "" <<i+2*s-1<<endl;
}
if (N-1 > I+s-1)
{
Merge (ar,br,i,i+s-1,n-1);
cout<<i<< "" <<i+s-1<< "" <<n-1<<endl;
}
else//two functions, one is to prevent the back of no value, and another is to ensure that BR to AR, this look at the back you understand
{
for (int j = i;j<n;++j)
{
Ar[j] = br[j];
}
}
Print_array (ar,n);
}


Template<class type>
void MergeSort (Type *ar,int N)
{
Type *BR = new Type[n];
int s = 1;
while (s < n)
{
Mergepass (br,ar,n,s);//division, divided into small sections, by the way Mergepass completed the division of the merger sub-algorithm, and sub-algorithm calculation
S+=s;
Mergepass (ar,br,n,s);//ditto, by the way all the data to AR
S+=s;
}
delete []br;
}


In the design of the algorithm by the Division method, it is best to make the sub-problem scale is roughly the same, and a problem is divided into the equal size of the K sub-problem of the treatment method is effective.

Divide and conquer the idea of the board overlay algorithm:

I'm using the L-Type Domino overlay board.

(1) First look at the 2*2, there is a point and the other difference, the other three are the same l to cover the

(2) Recursive 2^2*2^2, if the 2*2 on the left has, then [1][2],[2][2],[2][1] constitute an L, and then to see, four parts of each one l to complement the whole

If 8*8 then [3][4],[4][3],[4][4], find the law, N*n,[n/2-1][n/2],[n/2][n/2-1],[n][n] constitute the first, and then N=N/2, until N=1

void Cheseboard (Array ar,int tr,int tc,int dr,int dc,int size)

DR,DC indicates the location of the special point, Row and column, TR,TC, the upper left corner of the chessboard put a line number, the upper left corner of the chessboard Fanglie number










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.