Divide and conquer the law

Source: Internet
Author: User

The time required for any problem that can be solved by a computer is related to its size. The smaller the problem, the easier it is to solve it directly, and the less computational time it takes to solve it. For example, for the ordering of n elements, when n=1, no computation is required, and when n=2, the problem is not easily handled when n is large.

"Divide and Conquer" (Divide and Conque) is divide-and-conquer, that is, to speak of a difficult problem directly solved. Split layer some of the same problems of smaller size so as to conquer, divide and conquer. The basic steps are as follows:

1) Decomposition: The original problem into a number of small, independent, and the nature of the original problem to listen to the sub-problem;

2) Solve: If the sub-problem is small and easy to solve the immediate solution, otherwise, the solution of recursion;

3) Merging: The solution of each sub-problem is merged into the solution of the original problem.

Application One:

Queue up for ticket purchase problem.

Problem Description:

The ticketing work is underway before a match begins. Each ticket is 50 yuan, there are 30 people lined up to buy tickets, of which 20 people hold 50 yuan of banknotes, the other 10 people holding 100 yuan banknotes. Let's say the ticket office has no change at the start of the ticket, and the 30 people lined up to buy tickets, so that the ticket office will not be able to find the situation of the different queuing scheme. (People with the same denomination of banknotes are placed in the same queue scheme after swapping positions).

Algorithm analysis: General awake, fake with m+n individuals in line to buy tickets, of which m personal hand has 50 yuan banknotes, n individuals hand 100 yuan banknotes. Queue scheme;

Consider the issue in three different situations:

1) n=0. Since people with the same ticket value are still in the same queueing scheme after swapping the order, F (m,0) = 1.

2) m < n. In this case, no matter how the queue, the ticket office will face the situation can not open money, so f (m,n) = 0.

3) Other circumstances. For the i+j person, there are i+j-1 individuals in front of him, and there are two cases for himself. If the i+j person takes 50 yuan bill, then the person in front of him has i-1 50 yuan bill, J person takes 100 yuan note, if the i+j person takes 100 yuan bill, then he has I person to take 50 yuan bill in front of him, j-1 individual to take 100 Yuan bill. Thus, F (i, j) = f (i-1, J) + f (i, j-1).

Therefore, based on the above analysis, the solution code we get is:

 Public int solution (intint  n) {       ifreturn 1;        if return 0;        return solution (M-1, N) + solution (M, n-1);}
View Code

Using recursion, that is, dynamic planning, store the values previously computed with an array, and then use them directly afterwards.

 Public intSolution (intMintN) {int[] f =New int[M+1] [N+1]; //set n = = 0 in case of 1         for(intI=1; i<=m; i++) f[i][0] = 1; //Set M < n in case of 0         for(inti=0; i<=m; i++) {             for(intj=i+1; j<=n; J + +) F[i][j]= 0; }        //set Other conditions         for(intJ=1; j<=n; J + +) {             for(intI=j; i<=m; i++) F[i][j]= F[i-1][j] + f[i][j-1]; }        returnF[m][n]; }
View Code

Application Two:

"Put Apple" problem. Put m the same Apple on n the same plate, allow some plates to be empty, how many different ways of putting? Note: 5,1,1 and 1,5,1 are the same way of putting.

Algorithm analysis: Because the plates are allowed to be empty, we can discuss them according to whether there are any apples on the plate. There are at least one plate empty and all the plates have apples in both cases.

1) At least one of the plates is empty. That is, the M-Apple to n-1 a plate, there are several methods.

2) All the dishes have apples. That is, there is at least one apple in each disk, then the problem becomes m-n an apple on n plates, there are several ways to put it.

So for the general case, F (m, n) = f (M, n-1) + f (m-n, N);

But not all m,n are in line with the above, so we can discuss it based on the relationship between M and N:

1) M>n, the above two conditions are satisfied, can be used directly;

2) M<n, even if only one apple on each plate, there will be n-m a plate empty, remove the n-m plate, then the remaining problem becomes, M apples on the M plate how many kinds of emissions. So f (m, n) = f (M, m).

Export:

When the n=1, that is, only one plate, you can only put all the apples on this plate, so f (m, 1) = 1;

When m=0, it means that there is no Apple to put, then f (0, N) = 1.

(for recursion, the n-1 is so decremented that it is bound to reach 1, and when n>m, there is f (n, m) = f (M, m), and m changes to n-m, so it will reach 0, so the recursion must arrive at the exit).

The code is:

     Public int putapple (intint  n) {        ifreturn 1;         if return 1;         if return putapple (M, m);         return putapple (M., n-1) + putapple (M-N, N);    }
View Code

Application Three:

Red and black. There is a rectangular house covered with square tiles of red and black color. When standing on one of the black tiles, you can only move up and down adjacent black tiles. Please write a program that calculates how many blocks of black tiles can be reached in total.

Algorithmic analysis: This problem can also be described as: given a point, calculate the area of the connected area where he is located. For the general case, set F (x, Y) as the total number of black tiles that can pass from the point (x, y), then

f (x, Y) = 1 + f (x-1, y) + f (x+1, y) + f (x, y-1) + f (x, y+1).

Pseudo code:

int findnum (intint  y) {        if(x and y over matrix range)            return 0;         if (red tiles encountered)             return 0;        Mark the traversed tiles        in red; return 1 + findnum (x+1, y) + findnum (x-1, y) + findnum (x, y+1) + findnum (x, y-1);    }
View Code

Divide and conquer the law

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.