topic badge

7.015 Prim's algorithm

Lesson

Prim's algorithm from a network

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:

This image shows the mycorrhizal nutrient transfer system network. Ask your teacher for more information.

\,

These underground connections are created and maintained by the fungus to connect and nurture the roots of plants (represented here by the vertices) under the ground. The weights represent distance in metres.

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.

This image shows a weighted network. Ask your teacher for more information.

We begin by picking a vertex, any vertex will do. Let’s say we pick F. We then highlight the lowest weight edge coming out of F.

We then add this edge, and the vertex on the other side, to our spanning tree.

This image shows a weighted network. Ask your teacher for more information.

Then we do the same thing again. Highlight the edge of lowest weight coming out of our spanning tree.

The vertex F was just a starting point - the lowest weight edge connected to the spanning tree comes out of E, now.

We then add that edge, and its vertex, to the spanning tree.

This image shows a weighted network. Ask your teacher for more information.

\,

Once again, we highlight the lowest weight edges coming out of the spanning tree.

This time, there are two candidates. Both DC and DF have weight 12.

This image shows a weighted network. Ask your teacher for more information.

However, DF cannot be used because that would create a loop. So we include the edge DC.

Edges connecting vertices already in the spanning tree will always be rejected.

This image shows a weighted network. Ask your teacher for more information.

Now we move on to highlight the next lowest weight edge connected to our spanning tree which is BF.

This edge doesn’t create a cycle, so we add it into the growing spanning tree.

This image shows a weighted network. Ask your teacher for more information.

\,

The next lowest weight edges are all weight 14 so we highlight them all.

This image shows a weighted network. Ask your teacher for more information.

Once again, we reject the edge DB since it would create a cycle.

That leaves us with two choices.

This image shows a weighted network. Ask your teacher for more information.

\,

We are free to pick either of these. We add in the edge and the vertex, growing the spanning tree by one vertex every step.

This image shows a weighted network. Ask your teacher for more information.

\,

Instead of going back to the edge EG of weight 14, we now have an edge connected to our spanning tree of lower weight, AG.

This image shows a weighted network. Ask your teacher for more information.

We add that in as before, and highlight the lowest weight edge connecting the spanning tree to our final vertex.

We add this edge and vertex into the network, completing the procedure.

This image shows the minimum spanning tree of a network. Ask your teacher for more information.

\,

We can delete the unused edges for a clearer picture of our final result.

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):

  1. Pick any vertex to start the spanning tree.

  2. Locate the lowest weight edge connected to the spanning tree.

  3. If this edge would create a cycle, reject it.

  4. If there is more than one edge with the lowest weight, pick any of them.

  5. 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.

Examples

Example 1

This image shows a network of walking tracks. Ask your teacher for more information.

The rangers of a national park want to clear walking tracks to 6 landmarks in the area. The potential tracks between landmarks are shown in the graph, along with the number of days required to clear each track for use.

To make a network of tracks in the shortest time, the rangers want to find a minimal spanning tree for the graph.

a

Which two of the following are possible minimal spanning trees?

A
This image shows a network highlighting a spanning tree. Ask your teacher for more information.
B
This image shows a network highlighting a spanning tree. Ask your teacher for more information.
C
This image shows a network highlighting a spanning tree. Ask your teacher for more information.
D
This image shows a network highlighting a spanning tree. Ask your teacher for more information.
Worked Solution
Create a strategy

Use the Prim's Algorithm to test each option.

Apply the idea

All the spanning trees seem to have started from vertex A. From vertex A we could move to either B or F since both edges AB and AF have a weight of 7.

If we move to vertex B, we would move to vertex E then D then C by following the edges with the lowest weights. We still need to connect to vertex F so we would also include the edge EF since its weight is lower than the weight of AF. This tree corresponds to option C, and has a total weight of: 7+4+2+6+6=25.

If we move to vertex F, we would move to vertex E then D then C by following the edges with the lowest weights. We still need to connect to vertex B so we would also include the edge EB since its weight is lower than the weights of AB, CB and DB. This tree corresponds to option A, and has a total weight of: 7+6+4+2+6=25.

So the possible minimal spanning trees are in options A and C.

b

What is the minimum time taken to build a network of tracks that connect each landmark?

Worked Solution
Create a strategy

Add the weights of edges of the minimum spanning trees found in part (a).

Apply the idea

In part (a) we found that the total weight of both minimal spanning trees was 25.

The minimum time taken to build a network of tracks is 25 days.

Idea summary

Prim’s algorithm (from a network):

  1. Pick any vertex to start the spanning tree.

  2. Locate the lowest weight edge connected to the spanning tree.

  3. If this edge would create a cycle, reject it.

  4. If there is more than one edge with the lowest weight, pick any of them.

  5. 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.

Prim's algorithm from a table

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 image shows a weighted network. Ask your teacher for more information.

\,

The network from the previous example is repeated here.

This network corresponds to a distances table that shows the distances between each pair of adjacent vertices.

This image shows the corresponding table of lengths of a network. It is labelled as Table 1. Ask your teacher for more information.

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.

This image shows the corresponding table of lengths of a network. It is labelled as Table 2. Ask your teacher for more information.

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.

This image shows the corresponding table of lengths of a network. It is labelled as Table 3. Ask your teacher for more information.

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.

This image shows the corresponding table of lengths of a network. It is labelled as Table 4. Ask your teacher for more information.

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.

This image shows the corresponding table of lengths of a network. It is labelled as Table 5. Ask your teacher for more information.

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).

This image shows the corresponding table of lengths of a network. It is labelled as Table 6. Ask your teacher for more information.

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.

This image shows the corresponding table of lengths of a network. It is labelled as Table 7. Ask your teacher for more information.

Vertices A,D,E,F,G, and H are now connected. We choose D-C.

This image shows the corresponding table of lengths of a network. It is labelled as Table 8. Ask your teacher for more information.

We add edge F-B so that B becomes the final vertex added to the network.

This image shows the corresponding table of lengths of a network. It is labelled as Table 9. Ask your teacher for more information.

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.

This image shows the minimum spanning tree of a network. Ask your teacher for more information.

\,

The network for the minimum spanning tree can be extracted from Table 9, and will look like this.

Notice that this is not exactly the same as the previous example when the spanning tree was identified from the network. The difference is a result of our choice to connect edge G-E instead of A-C, which both had a weight of 14.

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):

  1. Pick any vertex to start the spanning tree and highlight the column header.

  2. Strike-out the row corresponding to the newly connected vertices.

  3. Locate the lowest weight edge from the columns of connected vertices.

  4. If there is more than one edge with the lowest weight, pick any of them.

  5. 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.

Examples

Example 2

Find the total length of the minimum spanning tree for the network represented by the distance table shown below.

ABCDE
A--9.44.45.2
B---0.63.0
C9.4--7.45.6
D4.40.47.6-7.6
E5.23.05.67.6-
Worked Solution
Create a strategy

Find the number of the edges of the minimum spanning tree then use the Prim's algorithm.

Apply the idea

Since we have 5 vertices, the minimum spanning tree will have 5-1=4 edges.

This image shows a table of lengths of a network to find a spanning tree. Ask your teacher for more information.

Using the Prim's algorithm we can start at column A, then shade row A to eliminate that row.

Then choose the lowest length of 4.4 which is aligned to D. This means edge A-D has a length of 4.4. Shade other values at row D to eliminate them.

Next, choosing the lowest length in columns A and D we have 0.6 in column D which is aligned to B. This means edge D-B has a length of 0.6. Shade other values at row B to eliminate.

Then, choosing the lowest length in columns A,D, and B we have 3.0 in column B. This is aligned to E, which means edge B-E has a length of 3.0. Shade other values at row E to eliminate.

Choosing the lowest weight in columns A,D,B, and E we have 5.6 in column E. This is aligned to C, which means edge E-C has a length of 5.6. Since this is the fourth edge we can end the algorithm.

Lastly, we add the lengths of the highlighted edges.

\displaystyle \text{Total length}\displaystyle =\displaystyle 4.4 + 0.6 + 3.0 + 5.6Add the lengths
\displaystyle =\displaystyle 13.6 \text{ units}Evaluate
Idea summary

Prim’s algorithm (from a table):

  1. Pick any vertex to start the spanning tree and highlight the column header.

  2. Strike-out the row corresponding to the newly connected vertices.

  3. Locate the lowest weight edge from the columns of connected vertices.

  4. If there is more than one edge with the lowest weight, pick any of them.

  5. 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.

Outcomes

ACMGM079

identify practical situations that can be represented by a network, and construct such networks; for example, trails connecting camp sites in a National Park, a social network, a transport network with one-way streets, a food web, the results of a round-robin sporting competition

ACMGM101

explain the meaning of the terms tree and spanning tree identify practical examples

ACMGM102

identify a minimum spanning tree in a weighted connected graph either by inspection or by using Prim’s algorithm

ACMGM103

use minimal spanning trees to solve minimal connector problems; for example, minimising the length of cable needed to provide power from a single power station to substations in several towns

What is Mathspace

About Mathspace