Interactive Prim's Algorithm
Given a weighted graph, many times we are interested in finding a minimum spanning tree (MST) for that graph. Prim's algorithm is a greedy method which does just this. A spanning tree is a subset of the edges of a graph that connects to 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 Treev be the set of vertices included in the tree, and TreeE be the set of edges included in the tree. Initially TreeV and TreeE are empty.
2. Add an arbitrary vertex to TreeV (TreeE is still empty).
3. Find the edge e of minimim weight such that one vertex is in TreeV and vertex is not in TreeV. Add the associated vertex to TreeV, and add e to TreeE.
4. If edge was found in step 3, goto 5, else go to 6.
5. If the number of vertices in TreeV 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.
6 If the number of vertices in TreeV is less than the number of vertices in the original graph, go to 2, else go to 7.
7. Output "The Minimum Spanning Tree is ", TreeE.
8. Output "This graph does not have a minimum spanning tree because it is not connected. "