Sometimes it is going to be very impractical to find a minimal spanning tree by eye. The following network represents a mycorrhizal nutrient transfer system:
A severe drought is affecting the area, so only the most essential connections can be sustained by the fungus. What is the shortest total distance that the mycorrhizal network needs to spread itself across, while still making sure that every plant is connected to every other one?
We need to find the minimal spanning tree. This particular network has 416 spanning trees, which one is minimal? Feel free to find them all and add up all their weights, but it’s going to take a very long time.
Instead, we’re going to use a procedure called Prim’s Algorithm to find the minimal spanning tree without having to find all the spanning trees. The algorithm was first created by Vojtech Jarník, a Czech mathematician, in 1930. Both Robert Prim (in 1957) and Edsger Dijkstra (in 1959) independently rediscovered it, though Prim’s work popularised its use.
Our fungus spanning tree has grown from the vertex F to cover every plant in the area, and it covers 12+10+10+13+14+8+7=74 metres in total.
This procedure is guaranteed to find the minimal spanning tree every time. Much easier than finding all the spanning trees and adding up all their weights. Here’s a summary of the procedure.
Prim’s algorithm (from a network):
Pick any vertex to start the spanning tree.
Locate the lowest weight edge connected to the spanning tree.
If this edge would create a cycle, reject it.
If there is more than one edge with the lowest weight, pick any of them.
Add this edge and the vertex at the other end to the spanning tree. If all vertices are part of the spanning tree, stop. Otherwise, repeat from step 2.
Which two of the following are possible minimal spanning trees?
What is the minimum time taken to build a network of tracks that connect each landmark?
Prim’s algorithm (from a network):
Pick any vertex to start the spanning tree.
Locate the lowest weight edge connected to the spanning tree.
If this edge would create a cycle, reject it.
If there is more than one edge with the lowest weight, pick any of them.
Add this edge and the vertex at the other end to the spanning tree. If all vertices are part of the spanning tree, stop. Otherwise, repeat from step 2.
The example in the previous section showed how we can use Prim's algorithm with a weighted network. Alternatively, we can use this algorithm with a distances table.
This network corresponds to a distances table that shows the distances between each pair of adjacent vertices.
The network is not a directed network so the distances table is symmetric about the diagonal. That is, the distance from A to B is the same as the distance from B to A, and so on.
With this table, we can now work through the columns to connect vertices to our minimum spanning tree. It doesn't matter which vertex we start at (because they will all be connected in the end), so we will start on the left with vertex A. This becomes the first vertex on the minimum spanning tree, highlighted with the box, in the heading row of Table 2.
Since A is part of the spanning tree, we can scan down only column A to find connections that can be added. We can see that the edge A-G is 8 metres and, since this is the shortest possible edge to a new vertex, G will be the next node added to the spanning tree.
In Table 3, both A and G are boxed to show that they are connected to the spanning tree. Rows for A and G are shaded out because we do not want to add any other edges to these vertices - that would create a circuit. When we do this working on paper we could strike out these rows by drawing a line, and we don't need to redraw the table for each step.
The table cells that are not shaded, in the A and G columns show the possible new connections. We need to look down only columns A and G to find the next shortest possible connection to the spanning tree. The new connection is circled red, showing that we should next use the edge G-H to connect vertex H.
In Table 4 we have vertices A,G, and H connected to the spanning tree, indicated with a box around the column heading. The rows for these vertices are shaded (or crossed out) so we don't create loops and we can now look down only columns A,G, and H (the unshaded columns) for the next lowest edge weight.
We can select the edge G-E, with a weight of 14. Note that this was not the only possible choice - the edge A-B also has a weight of 14. It doesn't matter which one we select. The minimum spanning tree is not always unique.
Table 5 shows that vertices A,E,G, and H are now connected. Look down only these 4 columns to find the next selected edge is E-D, with weight 10 (again there was another possible choice of E-F).
Vertices A,D,E,G, and H are now connected. Look down only these 5 columns and this time time we will choose E-F.
Vertices A,D,E,F,G, and H are now connected. We choose D-C.
We add edge F-B so that B becomes the final vertex added to the network.
Having connected all the vertices we now have constructed a minimum spanning tree for this network. Other than the starting vertex, every row should have exactly one table cell selected because every vertex in a tree is connected by a single edge.
We can still see that the total distance is minimised: 12+10+10+13+14+8+7=74 metres.
If the network for which we need to find the minimum spanning tree is specified in table form then this algorithm will be most efficient. For small to moderate-sized networks given with a graphical representation, it is usually easy enough to find the minimum spanning tree graphically.
Both the graphical and table methods require working methodically to ensure that the shortest edge is identified at each stage.
Prim’s algorithm (from a table):
Pick any vertex to start the spanning tree and highlight the column header.
Strike-out the row corresponding to the newly connected vertices.
Locate the lowest weight edge from the columns of connected vertices.
If there is more than one edge with the lowest weight, pick any of them.
Add this edge by highlighting the column header and striking out the row. If all vertices are part of the spanning tree, stop. Otherwise, repeat from step 3.
Find the total length of the minimum spanning tree for the network represented by the distance table shown below.
A | B | C | D | E | |
---|---|---|---|---|---|
A | - | - | 9.4 | 4.4 | 5.2 |
B | - | - | - | 0.6 | 3.0 |
C | 9.4 | - | - | 7.4 | 5.6 |
D | 4.4 | 0.4 | 7.6 | - | 7.6 |
E | 5.2 | 3.0 | 5.6 | 7.6 | - |
Prim’s algorithm (from a table):
Pick any vertex to start the spanning tree and highlight the column header.
Strike-out the row corresponding to the newly connected vertices.
Locate the lowest weight edge from the columns of connected vertices.
If there is more than one edge with the lowest weight, pick any of them.
Add this edge by highlighting the column header and striking out the row. If all vertices are part of the spanning tree, stop. Otherwise, repeat from step 3.