Common algorithms: Divide and conquer, dynamic planning, and algorithm merge

Source: Internet
Author: User

Common algorithms: Divide and conquer, dynamic planning, and algorithm merge

These two algorithms are put together because they are used to find the optimal solution.Greedy AlgorithmIs different. However, there are some differences between the two algorithms. The following is an explanation:

Divide governance, that is, divide and conquer, divide a complicated problem into two or more identical or similar subproblems, and 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.

The following is an example:


When n> 1, to obtain T (n), we must know T (n-1), and so on. Therefore, to obtain T (n), we must break down T (n, starting from the smallest subproblem, we finally obtain T (n). This process is a recursion. Grouping and Recursion are like a pair of twins, which are often used in grouping.

Usage:

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..

 Application Framework:

/* Divide and conquer method -- merge and sort * the rule of merge and sort is as follows: (1) divide the sequence to be sorted: r1, r2 ,..., Rn is divided into two sub-sequences with the same length: r1 ,..., Rn/2 and rn/2 + 1 ,..., Rn; (2) solve the subproblem: sort these two subsequences respectively to obtain two ordered subsequences; (3) Merge: combine these two ordered subsequences 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}; // sort the eight elements int low = 0, high = 7; // initialize the values of low and high, coordinates of the start and end of the array // secondary array B, used as the temporary array int B [] = new int [. length]; // output the array System before sorting. out. print ("Before sorting:"); for (int I = 0; I <= high; I ++) {System. out. print (a [I] + "") ;}// merge and sort mergerSort (a, low, high, B); // output the sorted array Sy Stem. out. print ("sorted:"); 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 What is the difference between dynamic planning and divide and conquer? Because the article is too long, please refer to the series of blogs: Dynamic Programming of common algorithms


Principles of Dynamic Programming

Dynamic programming method [dynamic programming method (DP)] is a common method in system analysis. In water resource planning, problems such as Reservoir Scheduling, reasonable allocation of water resources, and optimal scheduling are often involved. These problems can be generalized into multi-stage decision-making processes. Dynamic Programming is an effective way to solve such problems. The dynamic programming method was proposed by R. Bellman and others in 1950s to solve the problem of multi-stage decision-making. The multi-stage decision-making process is to divide the research issue into several interrelated stages and make decisions at each stage so that the entire process can be optimized. Many practical problems are often more effective than linear programming with dynamic programming, especially for discrete problems. In fact, dynamic programming is to make decisions in multiple stages. The basic idea is to divide complex problems into several stages of mutual association based on the characteristics of time and space. After selecting the direction of the system, this is also called the reverse decision-making process, which calculates from the end point to the start point and finds a decision for each stage to optimize the entire process.
[1] basic idea of Dynamic Planning
The previous article mainly introduced some theoretical basis for dynamic planning. We refer to the above-mentioned dynamic planning with obvious stage division and state transition equations as standard dynamic planning, this standard dynamic planning is derived from the study of multi-stage decision-making problems and is suitable for theoretical analysis. In practical application, the division of stages is not obvious for many problems. In this case, it is difficult to divide the phase method intentionally. Generally, as long as the problem can be divided into smaller sub-problems, and the optimal solution of the original problem contains the optimal solution of the sub-problem (that is, to meet the optimal sub-principle ), you can consider using dynamic planning.
The essence of dynamic planning is to divide governance ideas and solve redundancy. Therefore, dynamic planning is a way to break down problem instances into smaller and similar subproblems, it also stores the solutions of subproblems to avoid repeated computing subproblems, so as to solve the optimization problem.
It can be seen that the dynamic programming method is similar to the divide and conquer method and greedy method. They all classify the problem instances into smaller and similar subproblems and generate a global optimal solution by solving subproblems. The current selection of the greedy method may depend on all the choices that have been made, but it does not depend on the choices and sub-problems to be made. Therefore, the greedy method moves from top to bottom and makes greedy choices step by step. The sub-Problems in the divide and control method are independent (that is, they do not include public sub-problems ), therefore, after recursively finding the solutions to each subproblem, you can combine the solutions of the subproblem into the solution of the problem from the bottom up. However, if the current selection may depend on the solution of the sub-problem, it is difficult to achieve the global optimal solution through local greedy strategy; If the sub-problems are not independent, the division and Control Law should do a lot of unnecessary work to solve public subproblems repeatedly.
The solution to the above problems is to use dynamic planning. This method is mainly applied to optimization problems. There are multiple possible solutions for such problems. Each solution has a value, and dynamic planning finds the optimal (maximum or minimum) value solution. If there are several solutions that take the optimal value, it takes only one of them. However, we must first ensure that the problem is ineffective, that is, no matter which solution is currently used, it will not affect the subsequent sub-problems. in the process of solving a local subproblem, This method also achieves the global optimal solution by solving the local subproblem. However, unlike the divide and conquer method and greedy method, dynamic planning allows these subproblems to be independent, (that is, each subproblem may contain a public subproblem) It is also allowed to make a choice through self-body solution. This method only resolves each subproblem once and saves the results, avoid Repeated Computation every time you encounter it.
Therefore, the problem addressed by the dynamic programming method has a significant feature, that is, the subproblem in its corresponding subproblem tree shows a large number of duplicates. The key to the dynamic programming method is to solve repeated subproblems only when the first problem is encountered, and save the answers so that they can be referenced directly in the future without having to be solved again.
3. basic steps of the Dynamic Planning Algorithm
To design a standard dynamic planning algorithm, follow these steps:
(1) division stage: the problem is divided into several stages based on the temporal or spatial characteristics of the problem. Note that these stages must be ordered or sortable (I .e. non-backward), otherwise the problem cannot be solved using dynamic planning.
(2) Select ...... the remaining full text>

Comparison of the similarities, differences, and advantages and disadvantages between "divide and conquer" and "Dynamic Planning"

/* SIMPLE algorithm: ** v [0] does not save data ** T (n) = O (n ^ 2 ). */int MaxSum (int * v, int n, int * besti, int * bestj) {int su

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.