Minimum Spanning Tree

Source: Internet
Author: User

Let's look at the method of the Minimum Spanning Tree through an example.

The Minimum Spanning Tree calculated using the prim algorithm (starting from node A) and the cruise Karl algorithm respectively.

 

OK. Use the prim algorithm for calculation:

U V-U B C D E F Te
{} {B, c, d, e, f} AB
11
AC
13
~
∞
AE
16
~
∞
AB
11
{A, B} {C, d, e, f}   BC
7
BD
3
AE
16
BF
5
BD
3
{A, B, d} {C, E, F}   BC
7
  AE
16
BF
5
BF
5
{A, B, d, f} {C, e}   BC
7
  Fe
12
  BC
7
{A, B, D, F, c} {E}       Fe
12
  Fe
12
{A, B, D, F, c, e} {}            
The chart may look a little messy. It doesn't matter. Let's take a look at the basic idea of the prim algorithm: Suppose n = (V, {e}) is a connected graph, te is the set of edges in the Minimum Spanning Tree on N. The algorithm starts from u = {U0} (U0 ε v), TE = {}, repeat the following operation: side of all u∈ u, v ε V-U (u, v) in e, find an edge with the smallest cost (U0, V0) and merge it into the set te. At the same time, V0 is merged into U until u = v. In this case, the Te must have n-1 edges, and t = (V, {Te}) is the minimum spanning tree of N.

Then, we can easily see the computing flow of the algorithm from the examples and tables. First, we will write the paths and weights with other nodes from node A, respectively, if it is not connected, the weight is infinite, so there is the second row of the table, te replaces the AB with the smallest price, and then add B to U, write the path and the corresponding weight of Node B and other nodes. Of course, A does not need to be written any more. You can see that any path added to the minimal spanning tree, this column is left empty because it is already the smallest side of the cost and it is impossible to have other sides smaller than it. There is another problem. For the sake of convenience, in the same column, if the weight of the corresponding edge is not smaller than the previous one, it will be dropped directly, so that we can only compare the "smaller ", in this way, you do not need to compare each row. In this way, the weight of BD in the third row of the table is 3, and 3 is written if the weight is smaller than the previous one; in the fifth row, the weight of Fe is 12, which is less than 16 of the weight of the previous AE. Therefore, 12 is written. In fact, each row and each column are looking for the path with the minimum cost, the set obtained in this way is the minimum spanning tree. Of course, you have to add the set of nodes. In this way, the table shown is not hard to understand. Each row finds a path with the lowest cost and adds the Te, then, add the node to the U and compare other paths. Then, continue and get the minimum spanning tree set.

Next, go to the cruise Card Algorithm:

First, let's take a look at its basic idea: first construct a subgraph SG containing only n vertices, and then start from the edge with the smallest weight value. If it is added, it will not generate a loop in SG, this edge is added to SG until n-1 edge is added. In a word, "when the ring is not formed, the minimum edge is selected each time ". Its starting point is to minimize the sum of the weights on the spanning tree, so that the weights on each edge in the spanning tree should be as small as possible. Therefore, we start to construct the edge with the smallest weight, on the premise that it does not constitute a ring.

Then we can directly find the edge with the smallest weight for combination, first BD, then BF, BC, and CF, because it constitutes a ring, AB can, EF can, OK, in this way, the set TE = {BD, BF, BC, AB, ef} of the Minimum Spanning Tree edge is obtained. Is it much simpler than the prim algorithm ?!

Therefore, the minimum spanning tree of the graph can be represented as t = ({A, B, D, F, c, e}, {BD, BF, BC, AB, ef }), of course, it is better to draw a picture to make it clearer.

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.