Minimum Spanning Tree
This article is reproduced from: Minimum Spanning Tree-Prim algorithm and Kruskal Algorithm
The generative tree of an image is an undirected connected subgraph containing all vertices. The minimum spanning tree of a weighted graph is its smallest spanning tree with the smallest weight.
Prim algorithm
Simple Algorithm 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.
Legend description of the following algorithm
| 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 |
For more information about algorithm implementation, see the fourth edition of algorithm or the data structure-java language implementation of Tsinghua Publishing House (the implementation method is clearer and simpler)
Kruskal Algorithm
1. Overview
Kruskal AlgorithmThis is an algorithm used to find the smallest spanning tree. It was published by Joseph Kruskal in 1956. The Prim algorithm and Boruvka algorithm are used to solve the same problem. All three algorithms are greedy algorithms. Unlike the Boruvka algorithm, the Kruskal algorithm is also effective when an edge with the same weight value exists in the graph.
2. Simple Algorithm Description
1) Remember that the Graph has v vertices and e edges.
2) Create a New Graph Graphnew. In Graphnew, the same e vertex in the source image has no edges.
3) Sort all e edges in the source Graph by weight from small to large.
4). Loop: traverse each edge from the edge with the smallest weight until all nodes in the Graph are in the same connected component.
If the two nodes connected by this edge are not in the same connected component in graph Graphnew
Add this edge to graph Graphnew
Legend description:
First, we have a Graph with several vertices and edges.
Sort the length of all edges and use the sorting result as the basis for edge selection. The greedy algorithm is embodied here again.
Sort resources and select the local optimal resources. After sorting, we first select edge AD. In this way, our graph becomes the right graph.
Search for the remaining changes. We found CE. The weight here is also 5
And so on, we found 6, 7, 7, DF, AB, and BE.
Next we will continue to select, although the side with the current length of 8 is the smallest unselected side. But now they are connected (for BC, CE and EB can be connected,
Similar EF can be connected through EB, BA, AD, DF ). Therefore, you do not need to select them. Similar BD has been connected (the connection line here is marked in red ).
At last, the EG and FG are left. Of course we chose EG.
For algorithm implementation, see the code in algorithm version 4.