Divide and conquer algorithm

Source: Internet
Author: User

Divide and conquer algorithm
First, the basic concept

In computer science, divide-and-conquer method is a very important algorithm. The literal explanation is "divide and conquer", which is to divide a complex problem into two or more identical or similar sub-problems, then divide 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. This technique is the basis for many efficient algorithms, such as sorting algorithms (fast sorting, merge sorting), Fourier transform (Fast Fourier transform) ...

The computational time required for any problem that can be solved with 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 calculations are required. N=2, the order can be sorted once compared. N=3 only 3 times to compare, .... And when n is large, the problem is not so easy to deal with. It is sometimes quite difficult to solve a problem of a larger scale directly.


--------------------------------------------------------------------------------

Second, basic ideas and strategies

The design idea of divide-and-conquer method is: To divide a big problem which is difficult to be solved directly to some small-scale same problem, in order to conquer, divide and conquer.

Divide and conquer the strategy is: for a size of n, if the problem can be easily solved (for example, the size of n smaller) directly resolved, otherwise it is divided into K small sub-problem, these sub-problems are independent and the original problem form, the recursive solution of these sub-problems, and then the solution of the sub-problems to the original problem. This algorithm design strategy is called divide-and-conquer method.

If the original problem can be divided into K sub-problem, 1<k≤n, and these sub-problems can be solved and can use the solution of these sub-problems to find out the solution of the original problem, then this method of division is feasible. The sub-problems produced by the divide-and-conquer method are often the smaller models of the original problems, which provides convenience for the use of recursive techniques. In this case, the sub-problem can be consistent with the original problem type and its scale shrinks continuously, so that the sub-problem is reduced to a very easy way to find out the solution directly. This naturally leads to the generation of recursive processes. Division and recursion like a pair of twin brothers, often at the same time applied in the algorithm design, and thus produce many efficient algorithms.


--------------------------------------------------------------------------------

Iii. conditions of application of the Division and Administration law

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

1) The scale of the problem is reduced to a certain extent and can be easily resolved

2) The problem can be decomposed into several small-scale same problems, that is, the problem has the best substructure properties.

3) The solution of sub-problems decomposed by this problem can be combined into the solution of the problem;

4) The problem is separated from each other sub-problems, that is, the sub-problem does not include the common sub-sub-problem.

The first characteristic is that most problems can be satisfied, because the computational complexity of the problem is usually increased with the increase of the size of the problem;

The second feature is the premise of applying the method of division and treatment. It is also the most problems can be satisfied, this feature reflects the application of recursive thinking;

The third feature is the key, whether the use of divide-and-conquer method depends entirely on whether the problem has a third feature, if the first and second features, and does not have a third feature, you can consider using greedy or dynamic programming method.

The fourth feature relates to the efficiency of division and treatment, if the sub-problems are not independent, then divide and conquer the law to do a lot of unnecessary work, repeated to solve the common sub-problem, although the use of divided treatment method, but generally with the dynamic programming method is better.


--------------------------------------------------------------------------------

Iv. Basic steps of the Division and Administration law

The divide-and-conquer method has three steps on each level of recursion:

Step1 decomposition: Decomposition of the original problem into several small, independent, and the original problem form the same sub-problem;

Step2 Solution: If the sub-problem is small and easy to solve the direct solution, or recursively solve each sub-problem

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

Its general algorithm design pattern is as follows:

Divide-and-conquer (P)

1. If | P|≤n0

2. Then return (Adhoc (P))

3. Decompose p into smaller sub-problem P1, P2,..., Pk

4. For i←1 to K

5. Do Yi←divide-and-conquer (pi) recursive solution pi

6. T←merge (y1,y2,..., yk) merging sub-issues

7. Return (T)

Among them | P| indicates the size of the problem p, N0 is a threshold value, indicating that when the size of the problem p does not exceed n0, the problem is easily solved and no further decomposition is necessary. Adhoc (P) is the basic sub-algorithm in the division method, which is used to solve the problem P of small scale directly. Therefore, when the scale of P does not exceed n0, it is solved directly with the algorithm Adhoc (p). The algorithm merge (Y1,y2,..., YK) is the merging sub-algorithm in the division method, which is used to P1 the sub-problem of P, P2 the corresponding solution y1,y2,..., YK of,..., PK to the solution of P.


--------------------------------------------------------------------------------

The complexity analysis of the division and Treatment method

A divide-and-conquer method divides the problem of scale n into a sub-problem of K-scale n/m. The decomposition threshold is set to n0=1, and the problem of adhoc solution Scale of 1 is 1 unit time. Then the original problem is decomposed into k sub-problem and the solution of K-sub-problem is merged into the solution of the original problem by merging it with F (n) unit time. Use T (n) to indicate that the scale of the solution is | The calculation time required for the p|=n problem is:

T (n) = k T (n/m) +f (n)

The solution of the equation is obtained by iterative method:

The recursive equation and its solution give only the value of T (n) when n equals m operational, but if T (n) is considered to be smooth enough, the value of T (N) can be estimated at the speed of T (N) at the operational of N equals M. It is generally assumed that T (N) is monotonically ascending, thus when mi≤n<mi+1, T (MI) ≤t (n) <t (mi+1).

--------------------------------------------------------------------------------

Vi. some classical problems that can be solved by using divide-and-conquer method


(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

--------------------------------------------------------------------------------

The thinking process of designing the procedure according to the Division and treatment method


In fact, it is similar to the mathematical induction, to find solutions to solve the problem equation formula, and then design the recursive program 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.

Split mode has three steps on each level of recursion:

Span style= "font-family:"microsoft yahei"; Font-size:15px "> decomposition (Divide): Decomposition of the original problem into a series of sub-problems;
Settlement" (conquer): Solve each sub-problem recursively. If the sub-problem is small enough, the solution is straightforward;
Merge (Combine): Merges the results of a sub-problem into the solution of the original problem.

Merge Sort is an example of a typical divide-and-conquer law. The corresponding intuitive operation is as follows:

Decomposition: divides n elements into sub-sequences containing N/2 elements;
Workaround: Recursively sort two sub-sequences using the merge sort method;
Merge: Merges two sorted sub-sequences to get sorted results.

Reprinted from: http://blog.csdn.net/lcj_cjfykx/article/details/41691787

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.