Divide and conquer algorithms to learn divide and conquer

Source: Internet
Author: User
Divide governance:

The idea of the divide and conquer algorithm is to solve some specific types of problems if the problem scale is small. If the problem scale is large, then, the problem is first decomposed into sub-problems with the same size but with the same size until the scale is small enough, solve these problems recursively. If the original problem can be divided into k sub-problems, 1 <k ≤ n, in addition, all these subproblems can be solved and the solution of the original problem can be obtained using the solutions of these subproblems. This method is feasible. Recursion and sub-governance are often used together.

Sub-governance:

1. The complexity of the problem decreases as the scale decreases

2. The problem has the optimal sub-structure.

    

Optimal sub-structure:If the solution of the subproblem included in the optimal solution is also optimal, we call it an optimal sub-structure (that is, satisfying the optimization principle ). The optimal sub-structure provides an important clue for the dynamic planning algorithm to solve the problem.

For example, the shortest path problem has the following optimal sub-structure nature: If a node X is the shortest path to source node u and the shortest path to target node v, then, the shortest path from u to V is combined with the shortest path: u to X and X to v. Floyd-warshall And Bellman-Ford are typical examples of dynamic planning.

3. The solution of the decomposed problem can be merged into the solution of the problem.

  If the problem cannot be solved, it cannot be managed separately. In this case, you can consider greedy and dynamic planning.

4. The sub-problems at the same split level are independent of each other.

    Fibonacci is not independent because F (9) = f (8) + f (7) contains F (8) this subproblem is caused by recursive thinking. Therefore, F (8) will be computed repeatedly, and the efficiency is not high. dynamic planning should be considered when subproblems overlap with each other.

Specific steps for sub-governance:

Many books have introduced the following steps:

1. Break down 2. Solve 3. Merge

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.

Classic problems with sub-governance:

Binary Search

Big integer multiplication

Strassen Matrix Multiplication

Board coverage

Mergesort Merge Sorting

Quick sorting

Linear Time Selection

Closest Point-to-Point Problem

Round Robin schedule

Tower of Hanoi

 

Typical problem solving examples merge and sort

  

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.  See Chapter 1 of Introduction to algorithms Chapter 2 "Merge Sorting" Pseudocode of the merge process   The whole process of merging and sorting: mergesort (a, a, B)
1 /************************************** * *********************************** 2> File Name: mergesort. CPP 3> author: void_00004> created time: Wednesday, August 06, 2014 seconds 5> content: divide and conquer classic problem 6 mergesort 7 ******************************** **************************************** /8 9 # include <iostream> 10 # include <cstdio> 11 using namespace STD; 12 const int max = 100000; 13 in T l [Max]; 14 int R [Max]; 15 void mergesort (int * a, int first, int end); 16 void Merge (int * a, int first, int mid, int end); 17 void mergesort (int * a, int first, int end) 18 {19 if (first <End) 20 {21 int mid = (first + end)/2; 22 mergesort (A, first, mid); 23 mergesort (A, Mid + 1, end ); // solve the problem recursively24 Merge (A, first, mid, end); 25} 26 return; 27} 28 29 void Merge (int * a, int first, int mid, int end) 30 {31 int L ENL = mid-first + 1; 32 int lenr = end-mid; 33 for (INT I = 1; I <= lenl; I ++) L [I] = A [first + I-1]; 34 for (INT I = 1; I <= lenr; I ++) R [I] = A [Mid + I]; 35 int li = 1; 36 int rI = 1; 37 int CNT = first; // The array we shoshould merge is from a [first] to a [end] So we can only modify these value, we Shouldn 'tmodify other A values38 // The problem has fixed39 while (LI <= lenl & RI <= lenr) 40 {41 int TMP; 42 if (L [Li]> r [ri]) 43 {44 TMP = R [ri]; 45 R I ++; 46} 47 else48 {49 TMP = L [Li]; 50 li ++; 51} 52 A [CNT ++] = TMP; 53} 54 while (LI <= lenl) 55 {56 A [CNT ++] = L [Li ++]; 57} 58 While (RI <= lenr) 59 {60 a [CNT ++] = R [ri ++]; 61} 62 Return; 63} 64 65 66 int main (void) 67 {68 int arr [Max]; 69 int N; 70 while (scanf ("% d", & N )! = EOF & N) 71 {72 for (INT I = 1; I <= N; I ++) CIN> arr [I]; 73 mergesort (ARR, 1, n); 74 for (INT I = 1; I <= N; I ++) cout <arr [I] <""; 75 cout <Endl; 76} 77 return 0; 78}

 

The code running result is abnormal because the merging principle is wrong during the first write. Only the values of the elements in the range you want to merge can be modified during merging. The values of the elements in other places cannot be modified.  Merge Sorting is the simplest partitioning method.The problem is divided into two. The size of each problem is 1/2 of the original size. 

 

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.