Given a weighted undirected graph, how to select a spanning tree so that the sum of all the edges on the tree is the smallest, which is called the minimum spanning tree.
An algorithm for finding the minimum spanning tree
(1) Kruskal algorithm
The storage structure of a graph adopts an array of edge sets, and the order of the edges with equal weights in the array can be arbitrary. This method is not very useful for the relative illumination of edges, which is a waste of time.
(2) Primm algorithm
The storage structure of the graph is based on the adjacency matrix. This method is carried out according to the steps of each vertex, it is necessary to use a set of vertices to start the empty sets, and then add the connected vertices in succession. To the collection, all vertices are added to the set, and then the minimum spanning tree is obtained.
To come down in detail:
Kruskal algorithm
Methods: The edges of the graph are selected by the order of their weights from small to large, and if the edges are not formed, the loops are retained as an edge, and if the loop is formed, the loops are removed. Select enough (n-1) edges, which is the minimum spanning tree. (n is the number of vertices)
First step: Select the first edge by an array of edge sets
Second step: Select the second edge, that is, the weight is 2 of the edge
Step three: Select the third edge, that is, the weight value is 3
Fourth Step: Select the fourth edge, which is the right value of 4
Fifth step: Select Fifth side
Primm algorithm
Method: From the specified vertex, add it to the collection, and then select an edge with the smallest weight in all edges of the collection to the vertices outside the collection as the edge of the spanning tree, and add that vertex outside the collection to the collection, Indicates that the vertex is connected. Use the vertices in the collection to find the smallest edges in the edges of the vertices that are outside the collection, and add the corresponding vertices to the collection, so that all vertices are added to the collection, which is the smallest spanning tree.
In the example, the minimum spanning tree of this graph is calculated from 1 points, and the vertices and weights are filled into the table in the order of the edges of the spanning tree.
——————— > write its adjacency matrix first
The first step: starting from the ①, ① into the set, with all the vertices outside the set can be formed by the edge of the minimum weight to find an edge
①--② Right 6
①--③ right 1, take ①--③ edge
①--④ Right 5
The second step: ③ into the set, ①,③ and ②,④,⑤,⑥ the smallest side is
①--④ Right 5
③--⑥ Right 4, take ③--⑥ edge
The third step: ⑥ into the set, ①,③,⑥ and ②,④,⑤ the smallest sides
①--② Right 6
③--② Right 5
⑥--④ right 2, take ⑥--④ edge
Fourth step: ④ into the set, ①,③,⑥,④ and ②,⑤ the smallest sides
①--② Right 6
③--② Right 5, take ③--② edge
⑥--⑤ Right 6
Fourth step: ② into the set, ①,③,⑥,②,④ and ⑤ the smallest sides
②--⑤ right 3, take ②--⑤ edge
This is also found on the internet a Kruskal and prim tectonic process diagram, posted out:
Minimum spanning tree (premium Manaus algorithm, Kruskal algorithm)