Divide and conquer algorithm

Source: Internet
Author: User

  

Divide and conquer algorithm

First, the big talk divide the treatment

Divide and conquer algorithm, Divide-and-conquer method, I gave him its literal interpretation is "divide and conquer," is to divide a complex problem into two or more of the same or similar sub-problems, and then the problem into smaller sub-problems ... Until the last sub-problem can be solved simply, the solution of the original problem is the merger of the solution of the sub-problem. Also is corresponding to its word devide-points, conquer-cure.

Branching ideas are the basis of many efficient algorithms, such as sorting algorithms (fast sorting, merge sorting), Fourier transform (Fast Fourier transform) ...

Second, the basic idea and the strategy realization

Design Ideas

A large problem which is difficult to be solved directly is divided into several indivisible and independent atomic level problems, these atomic level problems are the same as the original problem form and can be solved directly, then the atomic level problem is conquer, and finally the solution of all atomic level problem is collected as the solution of the original problem.

Algorithm Preparation

Divide-and-conquer method is actually similar to the mathematical induction method, find the solution equation formula, then design the recursive procedure according to the equation formula.

1, must first find the minimum problem size of the solution method

2, then consider the solution method with the problem scale increase

3. After finding the recursive function formula (various scales or factors), the recursive program can be designed.

The strategy of dividing is divided into three steps

1, divide-decomposition: The original problem is decomposed into several sub-problems with the same form as the original problem;

2, conquer-Solve: If these sub-problems can be directly resolved, then resolve and return the solution, or continue to decompose;

3, combine-Merge: The solution of each sub-problem is merged into the solution of the original problem.

Recursive Implementation

The decomposition and collection solution of divide-and-conquer algorithm generally need recursive implementation, in fact, the division and recursion like a pair of twins, often at the same time applied in the algorithm design, and thus produce many efficient algorithms.

Third, the application of the Division method of the scene

The problems that can be solved by the method of division and administration generally have the following characteristics:

① the problem can be decomposed into several smaller and opposing identical problems, that is, the problem has the optimal substructure property; (This is the premise of the Division application)

② the scale of the problem to a certain extent can be easily resolved;

③ the solution of sub-problems decomposed by the problem can be combined into the solution of the problem, which is the key of the divide-and-conquer algorithm, and if it is not satisfied, the division is meaningless. If you do not meet this, you can consider dynamic planning)

The following classical problems are commonly solved by the method of division and Treatment

(1) Two-point search

(2) Large integer multiplication

(3) Strassen matrix multiplication

(4) Board cover

(5) Merge sort

(6) Quick Sort

(7) Linear time selection

(8) Closest point to problem

(9) Round robin Calendar

(10) Hanoi

Iv. Concrete realization of the Division and Administration law

1, board coverage problems

Problem Description:

In a chessboard of 2^kx2^k squares, a square is different from the other squares, calling it a special square and calling the chessboard a special chessboard. In the problem of board coverage, the 4 different forms of the L-shaped dominoes are used to cover all squares on a given special chessboard except for special squares, and any 2 L-type dominoes must not overlap.

Problem Solving Ideas:

when k>0, the 2kx2k checkerboard is divided into 4 2^k-1x2^k-1 sub-chessboard (a) as shown. Special squares must be located in one of the 4 smaller sub-chessboard, with no special squares in the remaining 3 sub-chessboard. To convert these 3 non-special squares into a special chessboard, a L-shaped domino can be used to cover the rendezvous of the 3 smaller chessboard, as shown in (b), thus transforming the original problem into 4 smaller board coverage issues. Use this division recursively until the chessboard is simplified to a checkerboard 1x1.

each time the divided four small squares to judge, to determine whether the special box is inside. The method of judging here is that each time you record the row and column coordinates of the upper left corner of the entire block, and then compare it with the special grid coordinates, we can know if the special square is in the block. If the special block in the inside, this direct recursion to go down, if not, according to the different positions of the four blocks divided, the lower right corner, the lower left corner, the upper right corner or the upper left corner of the square marked as a special block, and then continue to recursion. In the recursive function, there is also a variable s to record the number of squares of the edge, each time the other block partition, the edge of the number of squares will be halved, this variable is to facilitate the determination of the location of special squares. Second, there is a variable ncount to record the number of L-type dominoes.

Java Code Implementation

Public class Coverchessboard {

Static int n= 0;

Static int k= 4;

Static int [] board;

Static int len= 1;

{

while (k>0) {

Len*=2;

k--;

}

board = new int[len] [Len];

}

XY is the starting coordinate of the current array,Len is the length of the current block, andxs ys is the marker point coordinate

void cover (int x,int y,int len,int xs,int ys) {

if (len = = 1) return;

n+ +;

Define intermediate coordinates for easy operation

int xc=x+len/2-1,yc=y+len/2-1;

len/=2;

Marker point in upper left area

if (XS<=XC&&YS<=YC) {

Set the adjacent corner point of the other three regions as a marker point and fill

board[XC[yc]=n;

Board [XC] [Yc+1] = n;

Board [Xc+1] [YC] = n;

Board [Xc+1] [Yc+1] = n;

Divide this large area into four small areas of the same size, filled

Cover (X,y,len,xs,ys);

Cover (x,yc+1,len,xc,yc+1);

Cover (XC+1,Y,LEN,XC+1,YC);

Cover (xc+1,yc+1,len,xc+1,yc+1);

}

Marker point in upper right area

if (XS<=XC&&YS>YC) {

Set the adjacent corner point of the other three regions as a marker point and fill

Board [XC] [yc]=n;

board[XC[yc+1] = n;

Board [Xc+1] [YC] = n;

Board [Xc+1] [Yc+1] = n;

Divide this large area into four small areas of the same size, filled

Cover (X,Y,LEN,XC,YC);

Cover (X,yc+1,len,xs,ys);

Cover (XC+1,Y,LEN,XC+1,YC);

Cover (xc+1,yc+1,len,xc+1,yc+1);

}

Marker points in the lower left area

if (XS>XC&&YS<=YC) {

Set the adjacent corner point of the other three regions as a marker point and fill

Board [XC] [yc]=n;

Board [XC] [Yc+1] = n;

board[XC+1][yc] = n;

Board [Xc+1] [Yc+1] = n;

Divide this large area into four small areas of the same size, filled

Cover (X,Y,LEN,XC,YC);

Cover (x,yc+1,len,xc,yc+1);

Cover (Xc+1,y,len,xs,ys);

Cover (xc+1,yc+1,len,xc+1,yc+1);

}

Marker points in the lower right area

if (XS>XC&&YS>YC) {

Set the adjacent corner point of the other three regions as a marker point and fill

Board [XC] [yc]=n;

Board [XC] [Yc+1] = n;

Board [Xc+1] [YC] = n;

board[XC+1][yc+1] = n;

Divide this large area into four small areas of the same size, filled

Cover (X,Y,LEN,XC,YC);

Cover (x,yc+1,len,xc,yc+1);

Cover (XC+1,Y,LEN,XC+1,YC);

Cover (Xc+1,yc+1,len,xs,ys);

}

}

Public Static void Main (string[] args) {

Coverchessboard cc = new coverchessboard ();

System. out. println (len);

Data testing, assuming (10,10) is a marker point

Cc.cover (0, 0, Len, 10, 10);

for (int i = 0; i < board. length; i++) {

for (int j = 0; J < board. Length; j + +) {

if (CC.board[i][j]<10) System. out. Print (0);

System. out. Print (cc.board[i][j]+ "");

}system. out. println ();

}

}

}

2. Strassen matrix multiplication

Title Description: matrix m*n a matrix and n*p B matrix multiplication, their product ab is a m*p matrix, seeking this AB matrix.

Common Brute force method: directly through the three for loop to simulate the process of our matrix, of course, this can be done, but this time the complexity is O (N3), the efficiency is low.

Strassen Matrix multiplication :

In 1969, a German mathematician Strassen proved that O (n^3) solution was not the optimal algorithm for matrix multiplication, and he did a series of work to reduce the final time complexity to O (n^2.80). He divides the matrix into multiple blocks and defines multiple variables for ease of calculation, such as Matrix A={{a,b}{c,d}}, Matrix B={{e,f}{g,h}},

Then the result of multiplying the two matrices Ab={{ae+bg,ag+bh}{ce+df,cg+dh}};

Mr. Strassen, by defining 7 variables to facilitate the operation, the variables are as follows

P1 = A (f-h);

P2 = (A + b) h;

P3 = (c + d) e;

P4 = d (G-E);

P5 = (A + D) (e + h);

P6 = (b-d) (e + h);

P7 = (a-c) (E + f);

Then two matrices product ab={{p5+p4-p2+p6, p1+p2} {P3+P4, p1+p5-p3-p7}};

This completes the calculation, if we encounter a larger matrix, we first divide the matrix into four parts, such as matrix A a,b,c,d, according to the Strassen method to simulate the entire process can be achieved. Because it's just a simulation process and I'm not here to do it.

Divide and conquer algorithm

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.