This blog focuses on the principles of several common algorithms, including divide and control, dynamic planning, greedy, backtracking, and branch restriction, to help students understand the truth of each algorithm.
Divide and conquer law:
Break down a big problem that is hard to solve directly into some small-sized identical problems, and conquer them separately. There are generally three steps: decomposition, solution, and merge. Similar to recursion. Quick sorting is a typical grouping algorithm. It splits a large sequence into two sequences, and then sorts the two sequences ...... Let's take an example in our daily life: for example, a soft exam is about to begin. After the test, each of our students will have a score, and then we will rank the score, first, the score is divided into two parts based on the score of 60 points, and then the two parts are ranked separately. If the score is less than 60 points, see the next exam, if the score is greater than 60, the scores of the top 50th Chinese students will be grouped as the benchmark. In part, our class will enter the top 50 Chinese students, some of them passed the course but did not enter the top 50 in the country-this is the division and Control Law.
The Code is as follows:
Quicksort (A, P, R) {// main function of sorting. A is the array to be sorted, and the start position of the P array, R is the end position of the array if (P <r) {q = partiton (A, P, R); // select an array's axis quicksort (A, P, q-1 ); quicksort (A, q + 1, R) ;}} partiton (A, P, R) {// computing function of an array's axis X = A [R]; I = p-1; For (intj = P; j <= R-1; j ++) {if (a [J] <= x) {I = I + 1; exchange a [I] And a [J]} exchange a [I] And a [R] returnI + 1 ;}
Dynamic Planning:
The dynamic programming method also divides the problem to be solved into several subproblems, first solving the subproblem, and finally obtaining the solution of the original problem. Different from the Division and Control Law, the subproblems decomposed by the dynamic planning method are often not independent. The sub-problems decomposed by the Division and control method are independent of each other. A sub-problem may be solved multiple times, resulting in a waste of time. This problem is avoided by the dynamic programming method, its dynamics are reflected here. As long as it is a computed subproblem, it must be recorded, because it may be used again in the future. Take a backpack as an example. For example, if a thief steals a house, the size of the backpack m is fixed. The dynamic programming method is used to consider the size of the backpack from 1 to m one by one, every situation can put what items are recorded, when calculating the size of the backpack I, combined with the size of the backpack for the I-1 (dynamic) select an appropriate item and put it in your backpack or keep it unchanged.
The Code is as follows:
MaxNutrientValue(n,v,p,M,x){ for i=0 to n nv[i][0]=0 forj=1 to M nv[0][j]=0 for i=1 to n forj=1 to M ifj<p[i] nv[i][j]=nv[i-1][j] elseif nv[i-1][j]>=nv[i-1][j-p[i]]+v[i] nv[i][j]=nv[i-1][j] else nv[i][j]=nv[i-1][j-p[i]]+v[i] j=M fori=n downto 1 if nv[i][j]=nv[i-1][j] x[i]=0 else x[i]=1 j=j-p[i] return x and nv[n][M] }
Greedy Algorithm:
Greedy algorithms only make choices based on the current information. The current selection will not change in the future regardless of the impact. That is, we only consider the part, not the whole. The typical example is "looking for change in shopping", which is not detailed here.
Backtracking:
The backtracking method is to start from the root node and search for the solution space in depth-first mode. If the conditions do not meet the conditions, perform backtracking and move to the nearest dynamic knot point until there is no dynamic knot point. Typical examples include the "Eight Queens" problem.
Branch restriction method:
The branch restriction method corresponds to the backtracking method. The backtracking method is used to solve all solution spaces in the target that meet the constraints. The branch restriction law is used to find a solution (optimal solution) that meets the constraints)