# Summary of minimum spanning tree algorithm

Source: Internet
Author: User

The algorithm summarizes the 5th bomb, the smallest spanning tree, there are many questions about spanning tree, here first introduce the most commonly used bar.

minimum Spanning Tree First, the spanning tree is built on the graph, and for the graph, there is no concept of spanning tree, so the diagram discussed next will default to the no-map. for a graph with n points, a minimum of n-1 is required to make the N points connected, and a sub-diagram consisting of this n-1 edge is called the spanning tree of the original. In general, a graph's spanning tree is not unique (unless the original is a tree). now consider the weighted graph G, that is, the edge of the graph, the minimum spanning tree is the weight in G and the smallest spanning tree, obviously the smallest spanning tree is not unique, but its weight is unique. There are many applications that require the concept of a minimal spanning tree, and a more intuitive application is that there are N villages, which are now to be repaired between these villages, where the distance between village I and village J is Dij, and the shortest road is now to be repaired to make all villages connected. Obviously, this problem can be directly applied to the minimum spanning tree model, and finding the smallest spanning tree is a solution. Here is an example of a minimal spanning tree:

for how to find a minimum spanning tree with a weighted graph, there is a special solution, before we explain these algorithms, let us first dig up some of the characteristics of the minimum spanning tree, that is, a spanning tree to become the smallest spanning tree, the need to meet what conditions.
MST properties:set a weighted graph G (v,e,w) (V for Point set, E represents the edge set, W represents a set of weights, and T is a spanning tree of G, and for any of e in the non-t side of the E, it is added to T, will produce a ring (otherwise T is not connected), if E is always the largest of the ring in the edge of the weight value, That means T satisfies the MST nature.
The above is the definition of the MST nature, where MST is the meaning of the smallest spanning tree, but it does not now show what this nature does with the smallest spanning tree, and it is going to prove that a spanning tree is the smallest spanning tree, when and only if it satisfies the MST nature.
lemma 1: For the two spanning tree T1 and T2 of Figure G (v,e,w), if they all satisfy the MST nature, their weights and values are the same.
Proof: The use of mathematical induction to prove that when there is a K-bar in the T1 and not in the T2 (0<=k<=n-1, the same, this is also a K-bar in the T2 and not in the T1), T1 of the weight and equal to the weight of T2 and.
1: First, when k=0, stating that all sides of T1 and T2 are the same, then T1 and T2 are the same spanning tree, obviously their weights and equality. 2: Now when K<x, T1 and T2 weights and equal, now to prove when k=x, t1,t2 weights and still equal. Consider such a set of edge s, which are in T1 or T2, but do not exist in both T1 and T2. In S, the edge with the smallest weight is set to E (U,V), which may be considered in T1 (in the same way as in T2). E connects the u,v two points in the diagram. Consider the path between U and V in T2, set to E1,e2 ... Em (m>1), where there must be several edges not in the T1 (otherwise there is a ring in the T1), set E ' for one of these edges. (1) e ' and e all belong to S, so the weight of E ' is not less than E. (2) Since T2 satisfies the MST nature, and E does not belong to T2, then the weight value of e ' is not greater than E. In summary, the weight of E and the weight of e ' are equal, so the T2 side e ' removed, replaced by E, into T2 ', obviously, T2 ' and T2 's weight and equality, and T2 ' and T1 only x-1 the difference (that is, there are x-1 in T1 and not in T2). The weights and equivalence of T2 ' and T1 are known by inductive method, so the weights and equal values of T2 and T1. The certificate is completed.

with lemma 1, the relationship between the MST nature and the minimum spanning tree is easily proven. theorem 1: A spanning tree T is the smallest spanning tree, when and only if it satisfies the MST nature.
Proof:1: Meet MST properties with minimum spanning tree
set T is the smallest spanning tree of G (v,e,w), if T does not satisfy the MST property, then there is an edge e, which does not exist with T, it is not the largest edge in the ring, and the edge of the ring is greater than e ', then we will remove the E ' in T, and replace it with E ', The resulting spanning Tree T ' will have a smaller weight and, this and T is the minimum spanning tree contradiction, so if T is the smallest spanning tree, then T must satisfy the MST nature.
2: Minimum spanning tree to meet MST properties
set T is a spanning tree of G (v,e,w) that satisfies the MST nature. By 1 We know that the smallest spanning tree of G satisfies the MST nature, and again by lemma 1, we know that two spanning tree weights and equal to the MST nature, so T is equal to the minimum spanning tree, so t is a minimum spanning tree of G. The certificate is completed.
in order to illustrate a key property of the minimum spanning tree, it is time to explain the specific algorithm for solving the minimum spanning tree. There are two classical algorithms for solving the minimum spanning tree. Prim algorithm and Kruskal algorithm. The steps and correctness proofs of the two algorithms are described below respectively.
Prim algorithm:Prim algorithm based on a greedy idea, through the local optimal strategy, each time an edge is added to the built spanning tree, after adding the n-1 edge, to ensure that the final resulting spanning tree is the overall optimal, that is, the minimum spanning tree. Below is a brief description of the steps for the prim algorithm:
The prim algorithm divides each point in the diagram into three states:The first tree point, which indicates that the point is already in the spanning tree being constructed. The second fringe point, which means that it is not in the tree, but adjacent to the tree (with one edge directly connected), is the candidate point to join the spanning tree. The Third kind of unseen point, the other point, indicates that there are no detected points.
then the prim algorithm steps are:
1: Initially all points are initialized to unseen, and a point s is randomly set to a tree point (for example, point 1th). 2: Set all the points adjacent to S to fringe. 3: If there are still fringe points in the figure, then to 4, otherwise to 6. 4: In all the edges connecting a fringe point and a tree point, take the lowest weight of the Edge E (u,v), it is advisable to set U as the tree point, V is the fringe point, the V point is set to the tree point. 5: Set all unseen points adjacent to V to fringe and add e to the built spanning tree. Go to 3. 6: If the graph is a bit of a tree point, then a minimal spanning tree of the graph is found. Otherwise the original image does not exist with the minimum spanning tree. 7: The algorithm ends.
according to the algorithm described, it is easy to get the code implementation of the algorithm, the algorithm implementation needs to use the heap optimization, not familiar with the heap of friends can self-surfing the Internet query, here no longer repeat:
`//primstruct edge{int to,len,next;}    E[maxm];int box[maxn],cnt,used[maxn];void init (int n) {for (int i=0;i<=n;i++) box[i]=-1; cnt=0;}    void Add (int from,int to,int len) {e[cnt].to=to;    E[cnt].len=len;    E[cnt].next=box[from]; box[from]=cnt++;}    struct node{int v,len;    Node () {} node (int x,int y): V (x), Len (y) {} bool operator< (const node &x) const{return len>x.len; }};p riority_queue<node> pq;int Prim (int n,int m) {memset (used,0,sizeof (used));//Initialize all points, set state to unseen int num=0,su    M=0,now=1;        do{used[now]=1;            for (int t=box[now];t+1;t=e[t].next) {int v=e[t].to,len=e[t].len;        if (!used[v]) Pq.push (node (v,len));            } while (!pq.empty ()) {node tmp=pq.top ();p q.pop ();            int V=tmp.v,len=tmp.len;            if (Used[v]) continue;            Now=v;            Sum+=len;        Break    } num++;    }while (Num<n); return sum;} `

Now let's see why the prim algorithm is correct. First, if a graph has a minimum spanning tree, then the above algorithm must be a spanning tree. Because at the end of the algorithm, all points are tree points, indicating that all points are connected. and the resulting sub-graph does not have a ring, because when we add an edge, the edge is bound to be a tree point and a finge point, and only a ring is generated when the two tree points are connected. the previous theorem 1 tells us that a spanning tree is the smallest spanning tree, so if and only if it satisfies the MST nature, then we just have to prove that the spanning tree obtained by the prim algorithm satisfies the MST nature.
theorem 2: The minimum spanning tree is present in the v,e,w, so the spanning tree obtained by the prim algorithm is the smallest spanning tree of G.
Proof: We just need to prove that the spanning tree obtained by the prim algorithm satisfies the MST property, which can be proved by inductive method. The following proves that when the K-point is added (1<=k<=n), the prim algorithm gets the spanning tree to satisfy the MST property:
1: When K=1, there is only one point that obviously satisfies the MST nature. 2: When k>1, we assume that when the 1k<x is satisfied, when k=x, assuming that the currently joined Edge is E (u,v), similarly, set U as the tree point, V is the fringe point, the spanning tree is not added to the x before the T. We assume that the t+e=t ' resulting spanning tree does not meet the MST nature. Since the MST property is not satisfied, there must be an edge e ' (x, y), after adding t ', the resulting ring has a weighted value greater than E ' in the edge. if x, Y is not equal to V, then the ring added with E ' is present in T, but T satisfies the MST nature (because there are only x-1 points in t), so there must be a V in X, Y. You may wish to set y=v. so as:

For example, set the path between U and X to W1 ... Wa,wa+1.....wb-1,wb ..... Wp, wherein W1 is U,WP for x, set edge wawa+1 is the path of the first weight greater than e ' edge,The WB-1WB is the last edge of the weighted value greater than E ' (it is possible that the two edges are the same). may wish to set wawa+1 before WB-1WB to add to T, then by the prim algorithm steps, know w1w2,w2w3 .... Wa-1wa are added to T ' before wawa+1 , further because E ' is added to T in the first, so there is |e|<=|e ' |<| wb-1wb|, so E and E ' will be added to T ' before wawa+1, but the fact is that E ' is not in t ', and it is later joined T ' in the wawa+1, so the contradiction. So when T ' joins E ', the resulting ring has the largest weight of e ', satisfying the MST nature. Then when the k=n, that is, when the algorithm is completed, the resulting spanning tree is the entire graph G spanning tree, which satisfies the MST nature, by the theorem 1, it is the smallest spanning tree. The certificate is completed. Kruskal algorithm:The Kruskal algorithm is also based on the greedy strategy, but unlike the prim algorithm, it does not maintain a connected component during the algorithm, but merges multiple connected components together to obtain a spanning tree. Personally think that the idea of Kruskal algorithm is more important than the algorithm itself, can solve many other problems, at least in the ACM competition.
The following describes the steps of the Kruskal algorithm:first, all the edges in the graph are sorted from small to large, then from small to large enumeration edges. Set the build tree that you want to construct as T. 1: Initially, there is no change in T, and all points are considered to be a separate set. 2: If the last side of the enumeration is finished, then to 4, otherwise to 3. 3: The edge of the current enumeration is E (u,v), if U,v is not in the same collection, E is added to T, and you and V are merged into a collection, otherwise, E is ignored. Go to 2.4: If T contains n-1, it means to find the smallest spanning tree of the original image, otherwise the original image has no minimum spanning tree. The algorithm ends.
the implementation of the algorithm requires some extra knowledge, first, from small to large enumeration edges, where you can sort the edge sets directly with quick sorting, or you can initialize the edge set with a small top heap. then, to determine whether two points are in the consolidated collection, you also need to merge two collections into a single collection. This data structure is used and set up here. For this knowledge, please refer to the relevant information yourself. The following is a Kruskal algorithm that leverages fast sorting and a set implementation.
`#define MAXN 110#define maxm 10010using namespace std;int uf[maxn];struct edge{    int u,v,len;} E[maxm];bool cmp (const edge &x,const edge &y) {    return x.len<y.len;} void init (int n) {//Initialize and check set for    (int i=0;i<=n;i++) uf[i]=i;} int find (int x) {    if (x==uf[x]) return x;    Return Uf[x]=find (Uf[x]);} int Union (int x,int y) {//Merges two sets (if x, Y is in the same collection, returns 0, otherwise returns 1)    X=find (x), Y=find (y);    if (x!=y) {        uf[x]=y;        return 1;    }    return 0;} int Kruskal (int n,int m) {//n points, M-Edge    sort (e,e+m,cmp);//Sort    int sum=0;//minimum spanning tree weights and for    (int i=0;i<m;i++ ) {//Small to large enumeration edge        int U=e[i].u,v=e[i].v,len=e[i].len;        Sum+=len*union (u,v);    }    Return sum;//returns the right value and}`

correctness of the Kruskal algorithm
As the correctness of the prim algorithm proves, we just need to prove that the spanning tree obtained by the Kruskal algorithm satisfies the MST property. First, if G has a minimum spanning tree, then the Kruskal algorithm is definitely a spanning tree. This does not prove that it can be easily proved by using the counter-evidence method.
theorem 3: The minimum spanning tree is present in the v,e,w, so the spanning tree obtained by the Kruskal algorithm is the smallest spanning tree of G.
Proof: The Kruskal algorithm at the end of the resulting tree is T, assuming that T does not meet the MST nature, then there is an edge e (u,v), it does not belong to T, will be added to T after E, get a ring. There is a (possibly multiple) edge e ' in the set ring, whose weight is greater than E. So by the steps of the Kruskal algorithm, it is known that E was enumerated before E '. Before enumerating E ', we know that u,v must not be in the same set, otherwise e ' will not be in T. So, since U,v is not in the same set before E ' joins, then the enumeration e is enumerated before E ' joins), according to step 3 of the Kruskal algorithm, the E must be in T, but E is not in the T, contradiction, so after the addition of E, the resulting ring in the value of E is the largest. This means that T satisfies the MST nature. Thus by the theorem 1, it is known that T is a minimum spanning tree of G. The certificate is completed.
Summary: So far, the summary of the minimum spanning tree is written, the two algorithms introduced are based on the application of greedy ideas. Individuals feel that learning algorithms must not only remember the code implementation of the algorithm, must also understand the ideas and correctness. Only in this way can we really understand the essence of an algorithm. Even if you forget the implementation of the code, understand the idea can also be launched. This should be the only way of learning.

Summary of minimum spanning tree algorithm

Related Keywords:

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.