Given a weighted undirected graph, how to select a spanning tree so that the sum of all the edges on the tree is minimal, which is called the minimum spanning tree.
1. 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)
Cruscalfa for the edge of a particular situation, consumption time is more, not very practical.
2. Primm Algorithm
Method: Add it to the collection starting at the specified vertex, and then select one of the lowest weights in all edges of the collection to the vertices that are 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. The smallest edges are found in the edges of the vertices in the collection and the vertices outside the set, and the corresponding vertices are added to the collection, so that all vertices are added to the collection, which is the smallest spanning tree.
Minimum spanning tree algorithm