Graph Academy
Prim's



What does it do ?

Prim's algorithm is used to help one find the minimum spanning tree of a graph.




A minimum spanning tree ?


A minimum spanning tree is a subset of a connected undirected graph that connects all vertices, and has the minmum possible weight of any subset that satisfies these conditions.

What's the idea ?


1) Choose a starting vertex A, and add it to the MST.

2) Choose the lowest-weighted edge which A is a part of (connecting it to an unvisited node), say AB, such that including it in the MST will not lead to a cycle.
Add it to the MST.

3) Now similarly consider the lowest weighted edge which any of the already included vertices (A and B here) are a part of, say BC (again, no cycle).
Add it to the MST as well.

4) Now do the same thing with the updated vertex set, and so on..until all the vertices have been accounted for.

Test yourself

Consider the following graph..

Say we start with vertex..B
Which three edges would you add to the MST first ?

What would be the length of the resultant MST ?