Write in front: Today suddenly found not yet wrote the minimum Spanning tree blog, and then tuning heap optimization Prim board for a long time to recall ... Quickly write a blog to survive.
First, the minimum spanning tree concept:
In a graph of n points, select n-1 to make all vertices 22 connected, then this edge set is called a spanning tree of this graph
In all spanning trees, the edge and the smallest one are called the smallest spanning tree of the graph.
Second, Kruskal algorithm
There are two common methods for finding the minimum spanning tree of graphs, Kruskal and prim
The steps for the Kruskal algorithm are as follows:
(1) Sort all edges by Benquan from small to large
(2) Select a side with the least edge and two vertices not connected and connect it
(3) Repeat step 2 until the n-1 edge is connected, if all edges are still not connected n-1 edge, the diagram is not connected
Look at the picture:
We make the Kruskal algorithm for this graph:
(1) Select a side with the least edge and two vertices not connected and connect it
That side 1→2
Repeat these steps:
Below we find the shortest side is 2→4
Do we want to connect it?
No!
Since point 2 and point 4 are already connected, if the link will form a ring, it is obviously not the optimal solution.
We'll skip it and choose 2→3.
The n-1 Edge is selected and the algorithm ends. The red Edge is the smallest spanning tree of the graph
The above to determine whether the two-point unicom can be used and check set to achieve
The code is as follows:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>using namespaceStd;inlineintRead () {intf=1, x=0; CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}structnode{intU,v,w;} r[400005];BOOLCMP (node A,node b) {returna.w<B.W;}intN,m,cnt,ans;intf[5005];inti,j;intF_ancestor (intx) { returnX==F[X]? x:f[x]=F_ancestor (f[x]);}intMain () {n=read (); m=read (); for(i=1; i<=n;i++) f[i]=i; for(i=1; i<=m;i++) {r[i].u=read (); R[I].V=read (); R[I].W=read (); R[i+m].v=r[i].u; R[i+m].u=r[i].v; R[i+m].w=R[I].W; } m*=2; Sort (R+1, r+m+1, CMP); for(i=1; i<=m;i++) { intF1=f_ancestor (r[i].u), f2=F_ancestor (R[I].V); if(f1!=F2) {F[F2]=F1; CNT++; Ans+=R[I].W; } if(cnt==n-1) Break; } if(cnt==n-1) printf ("%d", ans); Elseprintf"Orz");//Diagram not connected return 0;}
Kruskal
The time complexity of the Kruskal algorithm is O (mlogm+ and search Time *n)
Three, prim algorithm
The steps for the prim algorithm are as follows:
(1) Define two sets U and V, start with the U set is empty, V set is somewhat
Define a DIS array to represent the shortest distance from the midpoint of the U set at each point
(2) Select a point that has not been selected and the lowest dis value (can be any point at the beginning), set the point to point K
(3) Adding a point k to the U set
(4) k is the middle point, modifying the dis value of the midpoint of the V set. i.e. Dis[j]=min (Dis[j],w (K,j)) (J is a point in the V collection)
(5) Repeat step 234 until you have n points in the set (if the U-set is the last Meiyoun point, the graph is not connected)
Look at the picture:
The PRIM algorithm
(1) Select any point, such as point 1
Dis Assignment initial value
(2) Add point 1 to the U set to update the DIS value
U={1}
(3) Select the point with the lowest dis value (point 2), add it to the U set, connect the edge 1→2, update the DIS value
Ans=1
u={1,2}
(4) Select the point with the lowest dis value (point 5), add it to the U set, connect the edge 2→5, update the DIS value
Ans=3
u={1,2,5}
(5) Select the point with the lowest dis value (point 4), add it to the U set, connect the edge 5→4, update the DIS value
Ans=5
u={1,2,5,4}
(6) Select the point with the lowest dis value (point 3), add it to the U set, connect the edge 2→3, update the DIS value
ans=10
u={1,2,5,4,3}
You already have n points in the set, and the algorithm ends. The red part of the graph is the smallest spanning tree of the graph.
The time complexity of the prim algorithm is O ((n+m) LOGM)
Iv. Summary
Both of these algorithms are based on the idea of greed
To master both of these algorithms, you need to be very familiar with their processes and use them at the right time.
This part of the article refers to the Orsay of information science. Improve the third part of the first chapter the minimum spanning tree
If you need to reprint, please specify https://www.cnblogs.com/llllllpppppp/p/9749533.html
~ I wish you all the best programming ~
On the graph theory (iii.)--The minimum spanning tree