I just finished working on another implementation of Prim’s algorithm. This version depends more on user input to generate the minimum spanning path tree.

# Tag Archives: prim

# Prim’s Algorithm

I have just written a script that executes Prim’s Algorithm that finds the minimum spanning tree on a randomly generated graph.

Given a weighted graph, many times we are interested in finding a minimum spanning tree (MST) for that graph. This has many applications including the very important network simplex method. Prim’s algorithm is a greedy method which does finds this MST. A spanning tree is a subset of the edges of a graph that connects every vertex, but contains no cycles. This spanning tree is called a minimum spanning tree if in addition the sum of the weights of the edges included in this tree is less than or equal to the sum of the weights of the edges of any other spanning tree for this graph.

Prim’s algorithm works by the following procedure.

1. Let *Tree _{v}* be the set of vertices included in the tree, and

*Tree*be the set of edges included in the tree. Initially

_{E}*Tree*and

_{v}*Tree*are empty.

_{E}2. Add an arbitrary vertex to

*Tree*(

_{v}*Tree*is still empty).

_{E}3. Find the edge

*e*of minimum weight such that one vertex is in

*Tree*and vertex is not in

_{v}*Tree*. Add the associated vertex to

_{v}*Tree*, and add

_{v}*e*to

*Tree*.

_{E}4. If edge was found in step 3, goto 5, else go to 6.

5. If the number of vertices in

*Tree*is less than the number of vertices in the original graph, then the graph is not connected and thus does not contain a minimum spanning tree. Goto 8.

_{v}6 If the number of vertices in

*Tree*is less than the number of vertices in the original graph, go to 2, else go to 7.

_{v}7. Output “The Minimum Spanning Tree is “,

*Tree*.

_{E}8. Output “This graph does not have a minimum spanning tree because it is not connected. ”

For example, consider the graph represented by the following adjacency matrix:

0 | 1 | 2 | 3 | 4 | |

0 | – | – | – | 13 | 12 |

1 | – | – | – | – | 16 |

2 | – | – | – | – | 24 |

3 | 13 | – | – | – | 13 |

4 | 12 | 16 | 24 | 13 | – |

Initially our tree (T_{v} is empty). The first step says to choose a random vertex and add it to the tree, so lets choose vertex 2.

Iteration 1: Now our tree contains the vertex 2 (i.e. T_{v} = {2}) and likewise T_{E} contains the edges coming from T_{v}. Thus T_{E} = {(2, 4)}.

We want to choose the cheapest edge that has one endpoint in T_{v} and one endpoint not in T_{v}. These edges are represented by T_{E}. Notice that T_{E} only contains one edge, so we select this

edge, which has a cost of 24.

Iteration 2: Our tree thus contains the vertices 2 and 4 (i.e T_{v} = {2, 4}) and likewise T_{E} contains the edges coming from T_{v}. Thus T_{E} = {(0, 4), (1, 4), (3, 4)}.

Again, we want to choose the cheapest edge that has one endpoint in T_{v} and one endpoint not in T_{v}. This will be the edge (0, 4) which has a cost of 12.

Iteration 3: Now T_{v} = {0, 2, 4} and T_{E} = {(1, 4), (3, 4), (0, 3)}. The cheapest of these three edges is the edge (0, 3) with a cost of 13, which means we will add it to our tree.

Iteration 4: Now T_{v} = {0, 2, 3, 4} and T_{E} = {(1, 4)}. Since (1, 4) is the only edge connected to our tree we add it and it has a cost of 16.

Iteration 5: Now T_{v} = {0, 1, 2, 3, 4} and T_{E} = {}. Because our tree contains all the vertices of the graph it is now spanning tree. The cost of this spanning tree is 24 + 12 + 13 + 16 = 65.

To learn more and see more examples, view Prim’s Algorithm at LEARNINGlover.com