[Algorithm Review 2] traditional basic algorithms (greedy, dynamic planning, backtracking, and branch restriction)

Source: Internet
Author: User

I. Design Philosophy of greedy Algorithms

• Starting from an initial solution of the problem, we gradually approach the given goal. Each step makes an untraceable decision and tries our best to find the best solution. When a certain step in an algorithm does not need to be moved forward, the algorithm stops.


Ii. Basic Nature of greedy Algorithms

1) greedy choice

The so-called greedy choice is that the overall optimal solution of the problem can be achieved through a series of local optimal choices, that is, greedy choice. This is the first basic element for the feasibility of greedy algorithms, and is also the main difference between greedy and dynamic programming.

2) optimal sub-structure

The overall optimum of the solution depends on the optimum of the solution of the local subproblem. This is a key feature that can be addressed using greedy algorithms. For example, if you have selected an activity, it must be optimal. Otherwise, the global optimization cannot be achieved.


Iii. Applicability of greedy Algorithms

• Greedy algorithms only need to make decisions to consider the current local information. That is to say, the premise of greedy algorithms is that "local optimal policies can generate global optimal solutions ".

• This algorithm has a small scope of application. If it is improperly applied, the optimal solution to the problem cannot be obtained. A more accurate method is to prove the choice of greedy strategy through mathematical methods.


4. Absolutely greedy

Example 1: Dijkstra single-source shortest path (Directed Graph)

(Dijkstra) algorithm ideas

Generate the shortest path algorithm in ascending order of path length:

Divide V into two groups:

(1) s: the set of vertices in the shortest path.

(2) V-S = T: vertex set of undefined Shortest Path

Add vertices in t to S in the ascending order of shortest paths.

Guarantee: 1) the shortest path length of each vertex from V0 to S is not greater than the shortest path length of any vertex from V0 to T.

2) each vertex corresponds to a distance value.

Vertex in S: the shortest path length from V0 to this Vertex

Vertex in T: From V0 To This vertex, only the shortest path length of the vertex in s as the intermediate vertex is included.

Rationale: It can prove the Shortest Path of the VK from V0 to T, or the weight of the direct path from V0 to VK, or the sum of the path weights from V0 through S to VK

 

  Steps for finding the Shortest Path

The algorithm steps are as follows:

1. The distance values of S = {v0}, t = {other vertices}, and T in the first season

If <v0, VI> and D (v0, vi) exist, they are the weights on the <v0, VI> arc.

If <v0, VI> and D (v0, vi) do not exist

2. Select a vertex W whose distance value is the smallest from T and is not in S. Add S.

3. Modify the distance value of the vertex in T: If W is added as the intermediate vertex, the distance value from V0 to VI is shorter than the path without W.

Repeat steps 2 and 3 until S contains all vertices, that is, S = T.

Auxiliary array: Dist [] Stores V0 to T midpoint distance

Path [] stores the shortest path that has been added to the point of S to V0


Example 2: Kruskal least spanning tree (the smallest edge of each weight value is selected until a minimum spanning tree is generated)

The algorithm Runtime is O (nlog N ).

Source code:

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # define max_name 5 <br/> # define max_vertex_num 20 <br/> typedef char vertex [max_name]; /* vertex name string */<br/> typedef int adjmatrix [max_vertex_num] [max_vertex_num]; /* adjacent matrix */<br/> struct mgraph/* Definition chart */<br/> {<br/> vertex vexs [max_vertex_num]; <br/> adjmatrix arcs; <br/> int vexnum, arcnum; <br/>}; </P> <p> typedef struct <br/>{< br/> vertex adj Vex;/* current vertex */<br/> int lowcost;/* cost */<br/>} minside [max_vertex_num]; </P> <p> int locatevex (mgraph g, vertex u) // locate <br/>{< br/> int I; <br/> for (I = 0; I <G. vexnum; ++ I) if (strcmp (u, G. vexs [I]) = 0) return I; <br/> return-1; <br/>}</P> <p> void creategraph (mgraph & G) <br/>{< br/> int I, J, K, W; <br/> vertex va, VB; <br/> printf ("Enter the vertex number and Edge Number of the undirected network g (separated by spaces) \ n "); <br/> scanf ("% d", & G. vexnum, & G. arcnum); <br/> printf (" Enter % d vertex values (<% d characters): \ n ", G. vexnum, max_name); <br/> for (I = 0; I <G. vexnum; ++ I)/* construct vertex set */<br/> scanf ("% s", G. vexs [I]); <br/> for (I = 0; I <G. vexnum; ++ I)/* initialize the adjacent matrix */<br/> for (j = 0; j <G. vexnum; ++ J) <br/> G. ARCs [I] [J] = 0x7fffff; <br/> printf ("Enter the vertex 1 vertex 2 weight of the % d edge (separated by spaces ): \ n ", G. arcnum); <br/> for (k = 0; k <G. arcnum; ++ K) <br/>{< br/> scanf ("% S % d % * C", VA, VB, & W ); <br/> I = locatevex (G, VA); <br/> J = locatevex (G, VB); <br /> G. ARCs [I] [J] = G. ARCs [J] [I] = W;/* symmetric */<br/>}< br/> void Kruskal (mgraph g) <br/> {<br/> int set [max_vertex_num], I, j; <br/> int K = 0, a = 0, B = 0, min = G. ARCs [a] [B]; <br/> for (I = 0; I <G. vexnum; I ++) <br/> set [I] = I; <br/> printf ("each edge of the minimum cost Spanning Tree is \ n "); <br/> while (k <G. vexnum-1) <br/> {<br/> for (I = 0; I <G. vexnum; ++ I) <br/> for (j = I + 1; j <G. vexnum; ++ J) <br/> If (G. ARCs [I] [J] <min) <br/>{< br/> min = G. ARCs [I] [J]; <Br/> A = I; <br/> B = J; <br/>}< br/> min = G. ARCs [a] [B] = 0x7fffffff; <br/> If (set [a]! = Set [B]) <br/>{< br/> printf ("% s-% s \ n", G. vexs [a], G. vexs [B]); <br/> K ++; <br/> for (I = 0; I <G. vexnum; I ++) <br/> If (set [I] = set [B]) <br/> set [I] = set [a]; <br/>}</P> <p> int main () <br/>{< br/> mgraph g; <br/> creategraph (g); <br/> Kruskal (g); <br/> system ("pause"); <br/> return 0; <br/>}< br/>/* The result is as follows <br/> enter the number of vertices and edges of the undirected network g (separated by spaces) <br/> 6 10 <br/> enter the value of six vertices (<5 characters ): <br/> V1 <br/> V2 <br/> V3 <br/> V4 <br/> V5 <br/> V6 <br/> enter the value vertex 1 vertex 2 weight (separated by spaces): <br/> V1 V2 6 <br/> V1 V3 1 <br/> V1 V4 5 <br/> V2 V3 5 <br/> V2 V5 3 <br/> v4 v3 5 <br/> V4 V6 2 <br/> V3 V5 6 <br/> V3 V6 4 <br/> V5 V6 6 <br/> minimum cost Spanning Tree edge: <br/> v1-v3 <br/> v4-v6 <br/> v2-v5 <br/> v3-v6 <br/> v2-v3 <br/> press any key to continue... <br/> */</P> <p>

5. Relatively greedy

Example 1: Number Game.

• Problem Description

Two people take the N number in the 2N number in turn, and the sum of the N is greater than the victory. Design an algorithm to let the first accessors win and simulate the data acquisition process.

• Problem Analysis

In this game, it is generally assumed that the number accessors can only see the number on both sides of the 2N number (6, 5, 6 for the first time), and the greedy algorithm is used:

• Example

If a group of data is: 6, 16

The greedy policy is used to calculate a large number in the number on both sides. The winner is the first to take a as an example:

Result:

A 6, 27, 12, 5, 11 = 61 wins

B 16,6, 9,6, 2 = 39

• If you select another data group :,

The greedy algorithm is still used, and the first attacker is.

Result:

A 16,7, 9,11 = 43

B, 6, 2 = 47 wins

In fact, if we can only see data on both sides, there is no winning strategy for this question, whether it is obtained first or later. In this case, the general strategy is to use an approximate greedy algorithm.

However, if the number accessors can see all 2n numbers, there are some simple ways to solve this problem. Although some cannot guarantee that the sum of the obtained numbers is the largest, but it is indeed a strategy that wins first.

6. Dynamic Planning (various problems include public subproblems)

1) optimal decision-making principle: the problem requires an optimal sub-structure (that is, the optimal solution includes the optimal solution of the sub-problem). It is a bottom-up solution, which is opposite to recursion, each time a subproblem is solved to a stage, the subproblem on which it depends has been completely solved. Therefore, each step is solved until all the required information is obtained, therefore, the global optimal solution can be obtained.

2) decision-making process of dynamic planning: the optimal decision-making is formed in the final stage, and then pushed forward until the initial stage. The specific results of the decision-making and the resulting status are transferred, it is calculated starting from the initial stage, and then recursive or iterated backward until the final result.

3) Applicable Conditions

Any method of thinking has certain limitations. If it exceeds the specific conditions, it will lose its function. Similarly, dynamic planning is not omnipotent. The problem of dynamic planning must meet the Optimization Principle and have no aftereffect.

1. the Optimization Principle (optimal sub-structure nature) optimization principle can be elaborated as follows: an optimization strategy has this nature, regardless of the past status and decision-making, for the status formed by the previous decision, the remaining decisions must constitute an optimal policy. In short, the sub-strategy of an optimization policy is always optimal. A problem that satisfies the optimization principle is also known as its optimal sub-structure.

2. no aftereffect: after each stage is arranged in a certain order, the status of a given stage cannot directly affect its future decision-making, you can only use the current status. In other words, each State is a complete summary of past history. This is also called "no-effect.

3. Overlapping sub-problems: Dynamic Planning improves search algorithms with exponential complexity into algorithms with polynomial time. The key lies in solving redundancy, which is the fundamental purpose of the dynamic planning algorithm. Dynamic Planning is essentially a technology that uses space for time. in the implementation process, it has to store various states in the Process of generation. Therefore, its spatial complexity is greater than that of other algorithms.

 

Example: famous goods carrier

For more information, see the blog post ------ goods carrier (Travel seller)

 

 

VII. Backtracking

1) Design Philosophy

The backtracking and branch restriction technologies are actually based on the exhaustive method, that is, according to certain rules, all possible solutions to the problem are woven into a certain tree structure to form a possible solution space tree or state space tree. Then, based on the constraints of the specific problem, use various search policies to traverse the possible solution space tree. To obtain the solution or optimal solution that meets the problem conditions. The two algorithms have similar design ideas and are essentially consistent.

2) backtracking and branch restriction methods can be divided into four steps to solve practical problems:

(1) determining the possible solution space of the problem is equivalent to finding out the search range for the poor.

(2) organize all possible solutions in a way that is easy to search. Generally, a possible solution space tree is generated.

(3) There are two basic search methods. That is, the depth-first search method and backtracing technology, and the breadth-first search method is the branch restriction technology.

(4) use the criterion function, also known as the restriction function, to accelerate the search process through pruning.

3) design principle of backtracking

Search for the status space tree from the root node based on the depth-first search method.

• Each time you search for an extended node that reaches the state space tree, you always first determine whether the subtree with this node as the root contains a solution to the problem. If it is not included, skip the further search for the subtree with the node as the root. The Node becomes a Dead Knot, And the last active node should be traced back to the top layer. Then, the active node is used as the new extension node. In this way, Recursively search in the solution space until the solution to the problem is found, or there is no active node in the solution space, that is, there is no solution to this problem.

• In the backtracking method, in order to avoid the problem States that cannot produce the optimal solution, we must constantly use the bounded function to execute the active nodes that are actually unable to produce the required solution, to reduce the calculation workload of the problem. Therefore, the backtracking method should be a deep Priority Search Method with Bounded Functions.

When we use the backtracking method to solve the problem, we often encounter two types of typical solution space trees, subset trees and arrangement trees.

Example: The famous Eight queens question


For more information, see blog ------ eight queens

 

VIII. Branch restriction (find the optimal solution of the problem in a certain sense)

1) design principle of the branch restriction Method

The branch restriction method is similar to the Backtracking Method, and is also an algorithm for searching and solving the problem on the state space tree. However, the Branch Limit Method and the Backtracking Method have different solutions. The goal of the backtracking method is to find all or any of the answer nodes in the state space tree, the goal of the branch limit method is to find a problem node that makes a target function extremely small or extremely large.

2) The branch restriction and backtracking methods are different:

(1) The solution objectives are different. The solution objective of the backtracking method is to find all solutions that meet the constraints in the solution space tree, while the goal of the branch restriction method is to find a solution or optimal solution that meets the constraints.

(2) The search method is different. The Backtracking Method searches for the solution tree in depth-first mode, while the branch restriction rule searches for the solution tree in width-first or minimum-consumption-first mode.

3) Two common branch limit Methods

(1) Queue (FIFO) Branch restriction Method
Select the next node as the expansion node based on the first-in-first-out (FIFO) Principle of the queue.
(2) priority queue branch restriction Method
Select the node with the highest priority as the current expansion node according to the priority set in the priority queue.
4) FAQs

Packing Problem, wiring problem, single-source shortest path problem, biggest problem, 0-1 backpack problem, traveling goods sales problem

 

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.