Minimum spanning tree and its construction method

Source: Internet
Author: User
Tags abstract language

PS: Long time no new articles, came to the Guangzhou internship, no network, do not know how this school think!! Then get to the point.

What is the minimum spanning tree

For a different spanning tree in a weighted connected undirected graph G, the sum of the weights on the edges of each tree may be different, and the smallest tree of weights on the edge is called the minimum spanning tree of the graph.

Construction method

Two algorithms for finding the minimum spanning tree, namely the premium Manaus algorithm and the Kruskal algorithm

Premium Manaus algorithm (Prim)

Before you know the premium Manaus algorithm, think about what we started with? The tacky point is to draw the road map, and this route must be the shortest.

Ideas

First in the abstract language description, is now we take the first vertex in the diagram (can actually be any vertex), and then put the vertex into the set a, and then the vertex and the vertex outside the set to connect the weight of the line to compare, the vertex of the line that can even the smallest value is put into the set. Then repeat this step by comparing the two vertices in the collection to the vertices outside the set. can refer to:

 First step: Starting from ①, ① into the collection, with the edge of all vertices outside the set to find the minimum weight of one edge ①--② 6  ①--③ rights 1 --Take ①--③ edge ①--④ right 5  Step two: ③ into the set, ①,③ and ②,④,⑤,⑥ the smallest side is ①--④ right 5  ③--⑥ right 4  Take ③--⑥ side third step: ⑥ into the set, ①,③,⑥ and ②,④,⑤ constitute the minimum edge ①--② 6  ③--② right 5  ⑥--④ Right 2 , take the ⑥--④ Edge Fourth step: ④ into the set, ①,③,⑥,④ and ②,⑤ each of the minimum side ①--② right 6  ③--② right 5 --Take ③--② edge ⑥--⑤ right 6  Fourth step: ② into the collection, ①,③,⑥,②,④ and ⑤ of each of the smallest sides ②--⑤ right 3 , ②-- ⑤ Edge  

Yes, the above is good, but the actual code is the same as the above thought, but it is a bit difficult to understand, we change the way, from the point of view of the code to analyze.
If we get the hand now, it's a graph of the adjacency matrix storage method (assuming that the graph has n vertices)
1. First we instantiate an array a, an array of B. As a first rule, array a stores the weights of the n-1 bars. Then another array is the starting vertex of the storage edge. For example a[1]=3,b[1]=4, the weight of the shortest edge connected by the first vertex is 3, and the shortest edge is connected to the 4th vertex.
2. with these two arrays, things get a lot better. B[i]=k represents an edge where I and K connect can form a minimum weight, while the A[i] array represents the minimum weight of the edge that the I vertex joins with this K (K=b[i]). It's a bit around here.
From the actual example, the idea is better, first look at:
3. Initialize First

for(i=0;i<g.n;i++){    lowcost[i] = g.edges[0][i];    closest[i] = 0;}

At this point the meaning of the lowcost array is the distance of the vertices to 0 vertices, the principle of the closest array is the current lowcost in which each value is connected by which two vertices, the visible initialization process makes closest each value is 0, That is, each side of the lowcost is connected to the 0 vertex.
4. Next, remove the smallest edge of the Lowcost, as follows:

min = INF;//先遍历出lowcost里面最小的拿出来for(j=0;j<g.n;j++){    if(lowcost[j]!=0 && lowcost[j]<min)    {        min = lowcost[j];        k = j;    }}printf("边(%d,%d)权为:%d\n",closest[k],k,min0;

The side that is removed is the vertex 0 connected to the vertex K. Think about it with our brains, what should we do after we take out the shortest side?
5. Yes, take out the second shortest side, we need to be able to connect with vertex 0 and vertex K, and then the edge of the line is stored in the lowcost array.
This is the same as our first abstract idea, vertex 0 and Vertex k are in the collection, compared to the vertices outside the collection.
So how do we do this comparison operation?

for(j=0;j<g.n;j++){    if(g.edges[k][j]!=0 && lowcost[j]>g.edges[k][j])    {5        lowcost[j] = g.edges[k][j];        closest[j] = k;    }}

G.EDGES[K][J] is the vertex j to the vertex K distance, Lowcost[j] is the vertex J to Vertex 0 of the current shortest distance (in the first cycle, because closest[j] are equal to 0, so are the distance from the vertex 0), take these two values to compare, The result is a smaller distance stored in the lowcost.
So the whole algorithm is:

Code
#define INF32767; void Prim (Mgraph g,intV) {intLOWCOST[MAXV];int min;//Is the previous vertex of the connection for the vertex of the corresponding element subscript    intCLOSEST[MAXV];intI,k; for(i=0; i<g.n;i++) {lowcost[i] = G.edges[v][i];    Closest[i] = v; } for(i=1; i<g.n;i++) {min= INF;//Go through the smallest lowcost inside the         for(j=0; j<g.n;j++) {if(lowcost[j]!=0&& lowcost[j]<min)            {min= Lowcost[j];            K = J; }} printf ("side (%d,%d) Right:%d\n", Closest[k],k,min); LOWCOST[K] =0;//The next step is to compare the weights of the two vertices in the set with the vertices outside the set .         for(j=0; j<g.n;j++) {if(g.edges[k][j]!=0&& Lowcost[j]>g.edges[k][j]) {lowcost[j] = g.edges[k][j];            CLOSEST[J] = k; }        }    }}
Summarize

For the premium Manaus algorithm, as long as the understanding of lowcost arrays and closest arrays, the basic idea is already clear

Kruskal algorithm (Kruskal)

The idea of the Kruskal algorithm is to first take all the edges out, and then according to the order from small to large, each take out a side, to determine whether to form a loop, if it is discarded, otherwise this edge is the minimum weight we want to the edge.

Ideas

Core-How to judge the loop
Here's a simple way to talk about ideas. As already said, we have an array of weights from small to large storage edge (here do not think about how to store the edge), and then each time we take out a side, it is necessary to determine whether this side and we have selected the edge of the formation of the loop, so every time we take out the edge, we need to take its "original vertex" stored in the Vset array, so how to store it?
Let's say we have the side AB,BC,CD are being taken out now. Then these three sides belong to a connected component, that is, when it is connected together. Then ad these two vertices can not be connected together, it will obviously constitute a circuit, how the computer should judge it. It can be stated that AB is connected together, so the parent of B is A,BC, so the parent of C is B,CD, so the parent of D is c. Then we can use the following code to determine whether the loop is formed:

int Find(int*parent,int f){    while(parnet[f]>0)        f = parent[f]    return f;}

The above code realizes the vertex of finding a connected component, and through the code above, we can know that D
The "original vertex" is the original vertex of the A,a is also a, so equal, and cannot be wired.

Code

So the whole algorithm is:

intFind (int*parent,intf) { while(parnet[f]>0) F = parent[f]returnF;} Typedefstruct{intBeginintEndintWeight;} Edge;voidKruskal (Mgraph g) {Edge e[maxsize];intVset[maxsize];intI,k,j; for(i=0; i<g.n;i++) for(j=0; j<g.n;j++)if(g.edges[i][j]!=0&& g.edges[i][j]!=inf) {e[k].begin = i;                E[k].end = j;                E[k].weight = G.edges[i][j];            k++; } insertsort (E,G.E);//Sort     for(i=0; i<g.n;i++) vset[i]=0; K =1; j =0; while(K&LT;G.N)        {u1 = E[j].begin;        U2 = E[j].end;        SN1 = Find (VSET,U1); SN2 = Find (VSET,U2);if(SN1!=SN2) {VSET[SN2] = SN1;printf();        k++;    } j + +; }}

Minimum spanning tree and its construction method

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.