Divide and Conquer algorithm (i.)

Source: Internet
Author: User

When we solve some problems, because these problems to deal with a considerable amount of data, or the process is quite complex, so that the direct solution in time is quite long, or simply can not be directly obtained. For this kind of problem, we often first decompose it into several sub-problems, find out the solution of these sub-problems, then find the appropriate method, combine them into the solution of the whole problem. If these sub-problems are larger, difficult to solve, they can be divided into several smaller sub-problems, and so on, until the solution can be directly solved. This is the basic idea of the divide-and-conquer strategy.

1. Example:

If you have a bag with 16 coins, one of them is forged, and the weight of the forged coin is different from the weight of the real coin. Can you find this counterfeit coin with the fewest number of comparisons? To help you with this task, you will be provided with an instrument that can be used to compare the weights of two sets of coins, using this instrument to know whether the weights of the two sets of coins are the same.

The general solution is to first divide these coins into two groups, each time only a group of coins, if the luck of the 1 times can be found, the worst is called 8 times to find the coin, this direct search method there is a considerable speculative, applicable to the situation of small number of coins, In the case of a large number of coins, it becomes a time-consuming and laborious thing to be lucky.

Try to change the method: if we divide all the coins into two groups, compare the original design with two coins at a time to compare the two sets of coins, we will find that after a comparison. It is entirely possible to discard a set of coins that are all real coins, choose the next one that is consistent with the original problem, and the size of the problem is significantly reduced, and the scale of each comparison is multiplied (4-1).

based on the above analysis. We can get the following conclusions:

(I) The greater the number of coins involved in the comparison, the quicker it will be achieved using this method. And the speculative decline was greatly reduced;

(2) The key to the solution is to divide the big problem into several small problems;

(3) Minor problems are quite similar to the original ones.

We often call this small design strategy a divide-and-conquer approach. That means " divide and Conquer ".

2, the general steps of the basic idea of the Division and treatment of the problem:

The basic idea of divide-and-conquer algorithm is to decompose a problem of size n into a small sub-problem of K, which is independent and the same nature as the original problem. The solution of the problem can be obtained by finding out the solution of the sub-problems. The focus is on being able to conquer. Divide and conquer method is very effective in designing search, classification algorithm or some calculator algorithm. The most commonly used division method is dichotomy, merge method, fast classification and so on.

The general steps of solving problems in divide and conquer method:

(1) Decomposition, the problem to be solved divided into a number of small-scale similar problems;

(2) solving, when sub-problem is divided into enough hours, with a relatively simple method to solve;

(3) Merging, according to the requirements of the original problem, the solution of sub-problem is composed of layers to form the solution of the original problem.

1, to solve the algorithm implementation, it is necessary to estimate the time required to implement the algorithm. Divide-and-conquer algorithm time is determined as follows: The total amount of work required to solve the sub-problem (number of child problems, the amount of work to resolve each sub-problem); The effort required to consolidate all sub-issues

2, divide and conquer the method is the arbitrary size problem as far as possible to divide into two sub-problem recursive algorithm

3. The specific process of division and administration is as follows:

Begin {Division Process start}
The solution of if① problem of non-then② return problem
ELSE begin
③ the sub-problem of half operand 1 from the original problem;
④ recursively calls the process of division and treatment, and the solution 1 is obtained.
⑤ the sub-question 2 with the other half of the operand from the original problem;
⑥ recursively calls the process of division and treatment, and the solution 2 is obtained.
⑦ will solve the problem of synthesis and renovation of 1 and 2 groups.
End
End End

3, typical examples:

" example 1" using recursive algorithm and non-recursive algorithm to achieve two-point lookup is: there are n has been from small to large sorted data, from the keyboard input a number of m, Use the binary lookup method to determine if it is in the n number.

1 varA:Array[1.. -] ofinteger;2 N,i,m,x,y:integer;3 procedureJC (X,y:integer); //Recursive process4   varK:integer;5   begin6k:= (X+y)Div 2; //take the middle position point7     ifA[k]=m ThenWriteln ('The num in', k); //Finds the number of lookups, outputs the result8     ifX>y ThenWriteln ('no Find')                //The number cannot be found9      Else beginTen            ifA[k]<m ThenJC (k +1, y); //Find in the second half One            ifA[k]>m ThenJC (x,k-1); //Find in the first half A          End; -   End; - begin the READLN (n); -x:=1; y:=N; -    fori:=1  toN DoREADLN (A[i]); //Enter a sorted number -READLN (m); //Enter the number you want to find +JC (x, y); //Recursive lookup - End.
Recursive method

1 varA:Array[1.. -] ofinteger;2 N,i,m,x,y,k:integer;3 begin4 READLN (n);5x:=1; y:=N;6    fori:=1  toN Doreadln (A[i]);7 Readln (m);8   Repeat9k:= (X+y)Div 2;Ten     ifA[k]=m Then beginWriteln ('The num in', k); halt;End One     Else begin A            ifA[k]<m Thenx:=k+1; -            ifA[k]>m Theny:=k-1; -          End; the   untilX>y; -Writeln ('No Find'); - End.
non-recursive method

" Example 2" The solution of one-element three-time equation

Tangible as:ax3+bx2 +cx+d=0such a unary three-time equation. The coefficients of each of the equations are given (a,b,C,Dare real numbers) and agree that the equation has three different real roots (the range of roots in-100to -), and the absolute value of the difference between root and root ≥1. Requires that the three real roots (spaces between the root and the root) be output in the same row from small to large, and precisely to the decimal point2bit.

Hint: Remember equationF(x)=0, if there is2numberX1and theX2, andx1<x2,F(X1)*f(X2) the0,

There must be a root between (x1, x2).

Input:

A,b,c,D

Output:

Three real roots (a space between root and Root)

Input/Output sample

Input: 1-5-4

Output: -2.00 2.00 5.00

Divide and Conquer algorithm (i.)

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.