Minimum spanning tree (premium Manaus algorithm, Kruskal algorithm)

Source: Internet
Author: User

Given a weighted undirected graph, how to select a spanning tree so that the minimum sum of the right edges of all trees, called the smallest spanning tree.

Finding the minimum spanning tree algorithm
(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)

p=1313 "> Primm algorithm
The storage structure of the graph is based on the adjacency matrix. This method is done by the steps of each vertex connected, need to use a set of vertices, starting as an empty, and later will be connected to the vertex successively added to the collection, all vertices increase the collection to get the minimum spanning tree required.

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: Starts from the specified vertex, increments it into the collection, and then selects one of the least-weighted edges in all edges of the collection that are composed of vertices outside the collection as edges of the spanning tree, and adds 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 the minimum spanning tree is reached until all vertices are added to the collection.
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: Start with ①, ① into the collection. To find an edge of the minimum weight in an edge that can be composed of all vertices outside the set
①--② 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, ①. The smallest sides of ③,⑥,②,④ and ⑤
②--⑤ 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)

Related Article

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.