Minimum spanning tree (MST)----Primm (Prim) algorithm and Kruskal (Kruskal) algorithm

Source: Internet
Author: User

1. Concept : given a weighted undirected graph, how to select a spanning tree so that the sum of all the edges on the tree is minimized, which is called The minimum spanning tree .

2. Application : For example, to lay an optical cable between n cities, the main goal is to enable communication between any two of these N cities, but the cost of laying an optical cable is very high, and the cost of cabling between cities is different, so the other goal is to minimize the total cost of cabling. This requires finding the smallest spanning tree with weights.


3, the algorithm to find the minimum spanning tree

3.1 Primm (Prim) 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.

Steps:
The first step: Starting with ①, ① into the set, using an edge that is the smallest weight to be found in the edges that all vertices outside the set can form

①--② 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

Fifth step: ② into the set, ①,③,⑥,②,④ and ⑤ the smallest sides
②--⑤ right 3, take ②--⑤ edge


The steps are as follows:


3.2 Kruskal (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)

Steps:

First step: Select the first edge by an array of edge sets, which is the edge with the weight of 1;

Second step: Select the second edge, that is, the weight of the 2 edge;

step three: Select the third edge, which is the edge of the weighted value of 3;

Fourth: Select the fourth edge, which is the edge of the weighted value of 4;

Fifth step: Select the fifth edge.

The steps are as follows:




Minimum spanning tree (MST)----Primm (Prim) algorithm and Kruskal (Kruskal) algorithm

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.