One of the five common algorithms: Divide and conquer Algorithms

Source: Internet
Author: User
Grouping Algorithm

I. Basic Concepts

In computer science, divide and conquer is an important algorithm. The literal explanation is "divide and conquer". It is to divide a complicated problem into two or more identical or similar subproblems, and then divide the subproblems into smaller subproblems ...... Until the final sub-problem can be solved simply and directly, the solution of the original problem is the merge of the sub-problem solutions. This technique is the basis of many efficient algorithms, such as sorting algorithms (fast sorting, Merge Sorting), Fourier Transformation (Fast Fourier transformation )......

The computing time required for any problem that can be solved by a computer is related to its scale. The smaller the problem, the easier it is to solve it directly, and the less computing time it takes to solve the problem. For example, for sorting of n elements, when n = 1, no calculation is required. When n = 2, you only need to make a comparison to sort the order. When n = 3, you only need to make three comparisons ,.... When N is large, the problem is not easy to handle. It is sometimes quite difficult to solve a large-scale problem directly.

Ii. Basic ideas and strategies

The principle of divide and conquer law is to divide a big problem that cannot be directly solved into the same problems of small scale, so that they can be cracked and managed separately.

The sub-governance policy is: For a problem with a scale of N, if the problem can be easily solved (for example, the scale of N is small), it will be solved directly, otherwise, it will be divided into k subproblems of small scale. These subproblems are independent of each other and are in the same form as the original problem, recursively solving these subproblems, then, combine the solutions of each subproblem to obtain the solution of the original problem. This algorithm design strategy is called divide and conquer.

If the original problem can be divided into k sub-problems, 1 <k ≤ n, and all of these sub-problems can be solved, you can use the solutions of these sub-problems to find the solution of the original problem, this method is feasible. Sub-problems produced by the Division and control method are often small models of the original problems, which provides convenience for the use of recursive technology. In this case, the sub-problem type can be consistent with the original problem type, but its scale is constantly reduced. In the end, the sub-problem can be easily solved directly. This naturally leads to the generation of recursive processes. Grouping and Recursion are like twins. They are often applied to algorithm design at the same time, and many efficient algorithms are generated.

Iii. Application of the division and Control Law

The problems solved by the Division and control law generally have the following characteristics:

1) The problem can be easily solved by narrowing down to a certain extent.

2) The problem can be divided into several smaller issues, that is, the problem has the optimal substructure.

3) The solutions of subproblems decomposed by this problem can be combined into the solutions of this problem;

4) Each subproblem identified by the problem is independent of each other, that is, the subproblem does not include a public subproblem.

The first feature is that most problems can be met, because the computing complexity of the problem generally increases with the increase of the problem scale;

Article 2 features are the premise for applying the divide and conquer methodIt is also feasible for most problems. This feature reflects the application of recursive thinking ;,

The third feature is the key. Whether or not the divide and conquer method can be used depends on whether the problem has the third feature., IfIf the first and second features are met and the third features are not met, the greedy method or dynamic programming method can be considered..

Article 4 efficiency of features involving divide and conquerIf the sub-problems are not independent, the division and Control Law should do a lot of unnecessary work to solve the public sub-problems repeatedly. At this time, although the Division and Control Law can be usedGenerally, it is better to use dynamic programming..

Iv. basic steps of divide and conquer Law

There are three steps in recursion of each layer:

Step 1 decomposition: Resolve the original problem into subproblems that are small in size and independent from each other, in the same form as the original problem;

Step 2 solve: If the subproblem is small and easy to solve, the subproblem is solved directly. Otherwise, the subproblem is solved recursively.

Step 3 merge: Merge the solutions of each subproblem into the solutions 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. Split P into smaller subproblems P1, P2,..., PK

4. For I limit 1 to K

5. Do Yi nvidivide-and-conquer (PI) △recursively solves pi

6. t merge Merge (Y1, Y2,..., yk) △merge subproblem

7. Return (t)

| P | indicates the scale of the problem P; N0 is a threshold value, indicating that when the scale of the problem P does not exceed N0, the problem is easily solved and does not need to be further decomposed. Adhoc (P) is the basic sub-algorithm in the Division and control method, and is used to directly solve small-scale problem P. Therefore, when the size of P does not exceed N0, the algorithm adhoc (P) is used for solving the problem. Algorithm Merge (Y1, Y2 ,..., yk) is the merge sub-algorithm in the Division and control method. It is used to split p sub-problems P1, P2 ,..., corresponding solution of PK Y1, Y2 ,..., merge yks into P.

V. Complexity Analysis of divide and conquer Law

A division and control method divides the problem of N into k subproblems of N/m. The decomposition threshold value N0 = 1, and the ADHOC solution scale is 1, which consumes 1 unit of time. Then, it takes F (n) to resolve the original problem into k sub-problems and merge the solutions of K sub-problems into the original problem using merge. T (n) indicates the calculation time required to solve the problem of the division and Control Method | p | = n:

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

Obtain the solution of the equation through iteration:

Recursive equations and their solutions only give the T (n) value when n is the power of M, but if T (n) is considered smooth enough, then, the growth rate of T (n) can be estimated by the value of T (n) WHEN n is equal to the power of M. It is generally assumed that T (n) is monotonic increasing, so when MI ≤ n <MI + 1, t (MI) ≤ T (n) <t (MI + 1 ). Vi. Typical problems that can be solved using the divide and conquer method
(1) binary search (2) Big integer multiplication (3) strassen matrix multiplication (4) Board coverage (5) Merge Sorting (6) Fast sorting (7) Linear Time Selection
(8) closest point to question (9) Round Robin schedule (10) Linglong

7. Thinking process when designing procedures based on divide and Control Law
In fact, it is similar to the mathematical induction method to find the formula for solving the problem, and then design a recursive program based on the formula. 1. The solution must first find the minimum problem Scale 2. Then, consider the solution when the problem grows. 3. After finding the recursive function formula (various scales or factors), design the recursive program.

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.