Last time, we talked about how to find shortest path. Today, we're going to discuss a similar issue – find the minimun spanning tree.
Given an undirected graph and using |V| - 1 edges to connect all the vertex, the result graph is spanning tree. Tree is a graph without loop inside. Therefore, the spanning tree is a graph connect all the vertex without any loop in it.
If the graph possesses different weights, there exists at least minimun spanning tree. Here is some application of minimun spanning tree.
Connect all villages
Suppose we have A, B, C, D and E villages. We want to find minimum cost to build the road from one village to another and each village should has a path to go to other village.
Within the graph above, we know the minimun spanning tree is:
Here is the constraints for finding MST:
Prim
The idea in Prim algorithm is to use the minimum the weight to connect two MST. This seems to be too abstract to image. Let's see an example.
If we want to find the MST of these nodes, we only have to connect the minimum edge from MST1 to MST2, namely, the edge from A to B. Therefore, the whole BST will be
Let's define to be the set of all the vertex, to be the set of all the MST we have now, be the set of all the vertex not in . Now, we only have to start from one node, A, and we can find the MST of the whole graph.
A is now the only element in set and all the other vertex is in .
The min edges from to is 2, then connect it and put into and remove from .
The min edges from to is , then connect it and put D into and remove D from .
and are both the minimum edges from to , then pick whichever we want.
Final step.
Now, we find the MST of the graph.
Kruskal
The approach that Kruskal adopted was:
For example:
So the edge container should be:
First pick up .
Pick .
Pick .
Pick
Pick , but it will form the loop, then drop it.
For both and form the loop, then drop them both. The tree here is MST.
In this session, we discuss what is spanning tree and how to find the minumum spanning tree. These three articles are all about graph. Thanks for reading!