Minimum spanning tree
Selecting the n-1 edge in a connected graph with n vertices, constituting a minimum connected subgraph, and minimizing the sum of the weights on the n-1 edge of the connected subgraph, it is called the minimum spanning tree of the connected network.
For example, a connected network, as shown in the above figure G4, can have multiple spanning trees with different weights combined.
Introduction to Kruskal algorithm
Kruskal (Kruskal) algorithm is used to calculate the minimum spanning tree of weighted connected graphs.
Basic idea : According to the weight value from small to large order to select the n-1 edge, and ensure that the n-1 edge does not constitute a circuit.
The concrete approach : first constructs a forest which contains only n vertices, then chooses the edge from the connected network from small to large to join in the forest, and causes the forest to not produce the loop, until the forest becomes a tree.
Kruskal algorithm diagram
The above figure G4 as an example to demonstrate the Kruskal (assuming that the most niche tree result is saved with an array R).
1th Step : Add the Edge <E,F> join R.
The Edge <E,F> has the smallest weight, so add it to the minimum spanning tree result R.
2nd Step : Add the Edge <C,D> join R.
After the previous operation, the Edge <C,D> has the smallest weight, so add it to the minimum spanning tree result R.
3rd Step : Add the Edge <D,E> join R.
After the previous operation, the Edge <D,E> has the smallest weight, so add it to the minimum spanning tree result R.
4th Step : Add the Edge <B,F> join R.
After the previous operation, the value of the edge <C,E> is minimal, but the <C,E> will form a loop with the existing edges; therefore, skip the edge <C,E>. Similarly, skip edge <C,F>. Add the Edge <B,F> to the minimum spanning tree result R.
5th Step : Add the Edge <E,G> join R.
After the previous operation, the Edge <E,G> has the smallest weight, so add it to the minimum spanning tree result R.
6th Step : Add the Edge <A,B> join R.
After the previous operation, the value of the edge <F,G> is minimal, but the <F,G> will form a loop with the existing edges; therefore, skip the edge <F,G>. Similarly, skip edge <B,C>. Add the Edge <A,B> to the minimum spanning tree result R.
At this point, the minimum spanning tree construction is complete! It includes the following sequence:<E,F> <C,D> <D,E> <B,F> <E,G><A,B>.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/