Prim algorithm: the principle of Algorithm Implementation is similar to that of Dijkstra. It is based on a vertex, but only a set of added edges. Simple Description:
1) input: A Weighted Connected Graph with vertex set V and edge set E;
2). Initialization: vnew = {x}. X indicates any node (starting point) and enew = {} In set v. It is null;
3) Repeat the following operations until vnew = V:
A. select the edge with the smallest weight value <u, v> in set E, where U is the element in the Set vnew, and V is not in the vnew set, and V in V (if there are multiple edges that meet the preceding conditions, that is, they have the same weight value, you can select any one of them );
B. Add V to the set vnew and <u, v> edge to the set enew;
4). Output: use the set vnew and enew to describe the obtained Minimum Spanning Tree.
How to select a simple instance:
| Legend |
Description |
Not optional |
Optional |
Selected (vnew) |
| |
This is the original Weighted Connected Graph. The number on one side of each edge indicates its weight. |
- |
- |
- |
|
VertexDIs selected as the starting point. VertexA,B,EAndFThrough a single edge andD.AYesdistanceDThe nearest vertex, soAAnd corresponding edgeAdHighlighted. |
C, G |
A, B, E, F |
D |
| |
The next vertex is the distance.DOrAThe nearest vertex.BDistanceDIs 9, distanceA7,E15,FIs 6. Therefore,FDistanceDOrARecentlyFAnd corresponding edgeDFHighlighted. |
C, G |
B, E, F |
A, D |
|
The algorithm continues to repeat the preceding steps. DistanceA7 verticesBHighlighted. |
C |
B, E, G |
A, D, F |
| |
In the current situation, you canC,EAndG.CDistanceB8,EDistanceB7,GDistanceFIs 11.ERecentlyEAnd corresponding edgeBeHighlighted. |
None |
C, E, G |
A, D, F, B |
| |
Here, the available vertex is onlyCAndG.CDistanceE5,GDistanceEIs 9, so selectCAnd EdgeECHighlighted together. |
None |
C, G |
A, D, F, B, E |
|
VertexGIs the only remaining vertex.FIs 11, distanceE9,ERecent, so highlightedGAnd corresponding edgeEg. |
None |
G |
A, D, F, B, E, C |
|
Now, all vertices have been selected, and the green part of the graph is the minimum spanning tree of the connected graph. In this example, the sum of the Minimum Spanning Tree weights is 39. |
None |
None |
A, D, F, B, E, C, G |
Code implementation:
Const int max = 100; const int INF = 1 <30; int V; int d [Max]; bool used [Max]; int cost [Max] [Max]; // The weight of the edge int psert () {// The code is similar to Dijkstra fill (D, D + V, INF); fill (used, used + V, false ); d [0] = 0; int res = 0; while (true) {int v =-1; for (INT u = 0; U <v; U ++) {If (! Used [u] & (V =-1 | D [v]> d [u]) V = u;} If (V =-1) break; used [v] = true; Res + = d [v]; for (INT u = 0; U <v; U ++) {d [u] = min (d [u], d [v] + cost [v] [u]) ;}} return res ;}
Kruskal: This algorithm first sorts edge weights from small to large and adds the current edge to the spanning tree without generating loops. To verify whether a ring is generated, first determine whether the vertex u and v are in a component together. If edge e = (u, v) is added to a connected component, a ring is generated, otherwise, no loops are generated. You can use and query the set to determine whether to use a component together with the component. Code:
Const int max = 100; int par [Max], rank [Max]; struct edge {int U, V, cost ;}; bool CMP (const edge & E1, const edge & E2) {return e1.cost <e2.cost;} edge e [Max]; int V, E; int Kruskal () {sort (E, E + e, CMP ); init (V); // check the initialization int res = 0; For (INT I = 0; I <E; I ++) of the Set) {edge es = E [I]; If (! Same (E. u, E. v) {unite (E. u, E. v); Res + = E. cost ;}} return res ;}// query the set operation void Init (int n) {for (INT I = 0; I <n; I ++) {Par [I] = I; // array No. Rank [I] = 0; // tree height} int find (INT X) {If (par [x] = x) return X; else return par [x] = find (par [x]);} void unite (int x, int y) {x = find (x); y = find (y); If (x = y) return; If (rank [x] <rank [y]) {Par [x] = y;} else {Par [y] = x; If (rank [x] = rank [y]) {rank [x] ++ ;}} bool same (int x, int y) {return find (x) = find (y );}
Minimum Spanning Tree