The method of divide-and-conquer and dynamic programming for common algorithms

Source: Internet
Author: User

These two algorithms are put together because they are used to solve the problem of the optimal solution, and the greedy algorithm is different. But there are some differences between the two algorithms, which are explained below:

Divide, divide and conquer, 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 is explained in one example:


whenn>1, you want to findT (n), you must knowT(n-1), and so on, so you want to obtainT (n)it is necessary toT (n)decomposition, starting from the smallest sub-problem, and finallyT (n) , this process is a recursive. Division and recursion like a pair of twins, often in the division with recursion.

Use occasions:

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 .

application Framework:

  
/* Divide and conquer--merge sort * The dividing strategy of the two-way merge sort is: (1) Division: The sequence to be sorted r1, R2, ..., RN divided into two equal-length sub-sequences r1, ..., RN/2 and rn/2+1, ..., RN, (2) solving sub-problems: Sorting the two sub-sequences respectively    , we get two ordered sub-sequences, and (3) Merge: Combine the two ordered subsequence into an ordered sequence. */public class MergeSort {/** * @param args */public static void main (String [] args) {int a[] = {21, 34, 56, 43, 99, 37, 78, 10};//here the 8 elements are sorted int low = 0, high = 7;//              Initialize low and high values, that is, the starting and terminating coordinates of the array//auxiliary array B, as a temporary array int b[] = new Int[a.length];              The array System.out.print before the output is sorted ("before sorting:");              for (int i = 0; I <= high; i++) {System.out.print (A[i] + "");              }//merge sort Mergersort (A, low, high, b);              Output sorted array System.out.print ("After sorting:");              for (int i = 0; I <= high; i++) {System.out.print (A[i] + "");        }}/** * divide and merge * * @param a* @param low * @param high * @param b */public static void Mergersort (int a[], int. Low, int              High, int b[]) {int mid = 0; if (Low < high) {mid = (high + low)/2;//location, position of the array split Mergersort (A, low, Mid, B)                  ;                  Mergersort (A, mid + 1, high, b); Merger (A, low, mid, High, b);//merge}}/** * Merges two ordered sub-sequences * * @par          AM A * @param low * @param mid * @param high * @param b * Auxiliary array */              public static void merger (int[] A, int. low, int mid, int. High, int b[]) {int i = low;              Int J = mid + 1;              int p = 0; Merge two ordered array sub-sequences 1 A[low: Mid] sub-sequence 2 a[mid+1..high] while (I <= mid && J <= High) {b[p++] = (A[i] <= A[J])?              A[i++]: a[j++]; }//If the subsequence1 No merge is copied directly into the copied array go while (I <= mid) {b[p++] = a[i++];              }//If Subsequence 2 is not merged, it is copied directly into the copied array (J <= High) {b[p++] = a[j++];  }//Copy the elements of the auxiliary array into the original array (p = 0, i = low; I <= high; i++, p++) {A[i]              = B[p];       }          }      }
What is the difference between the dynamic programming method and the divide-and-conquer method? Due to the length of the article, please see the series blog:Dynamic programming method of common algorithm

The method of divide-and-conquer and dynamic programming for common algorithms

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.