...

/

Prim’s Algorithm

Prim’s Algorithm

Discover Prim’s algorithm for building a minimum spanning tree.

Prim’s algorithm is a well-known algorithm for finding minimum spanning trees. The algorithm in the form known today bears striking similarities with Dijkstra’s algorithm.

A tree-growing strategy

Prim’s algorithm starts with a vertex, and gradually extends this single vertex tree to a full-blown minimum spanning tree by adding one edge at a time.

As the subgraph, built by the algorithm, remains a tree after each edge addition, Prim’s algorithm is often thought of as a tree growing algorithm.

Press + to interact

The crux of it

The idea is to build a subgraph KK of G=(V,E)G=(V,E) by adding an edge to it in every round such that KK remains a tree after each round.

Conceptually, in every round, the vertices of GG are regrouped as:

  1. Those in KK
  2. Those in KCK^C, the complement of KK

We refer to an edge as a boundary-edge if it has one end-vertex in KK and the other in KCK^C. For the example shown below, we show the boundary-edges as dotted, and the edges in KK are highlighted in red. Note how the edges in KK form a tree.

Press + to interact
Boundary-edges shown dotted
Boundary-edges shown dotted

Here is how the algorithm works:

  1. An arbitrary vertex rr of GG is chosen as the root and placed in KK.
  2. At each subsequent step, we pick a boundary-edge of the smallest weight and add it to KK. Adding an edge to KK entails also adding its other end-vertex to KK.

This algorithm ensures that KK remains a tree after every round. Here is why:

  1. It remains connected after each round.
  1. It remains acyclic after each round.

Conclusion: Since, in each round, a new vertex becomes part of the tree KK, by the end of the execution, KK contains all vertices of the original graph and is, therefore, a spanning tree.

In fact, KK is guaranteed to be a minimum spanning tree, but we defer showing this for now.

A dry run

Let’s look at a visualization of how the algorithm picks the edges that form a minimum spanning tree.

Press + to interact
A weighted graph
1 / 9
A weighted graph

Implementation details

Let’s drill down and see how to implement Prim’s algorithm.

Priority queue

Since in each round, we want to pick a minimum-weight boundary-edge, we could use a priority queue for the boundary-edges. However, each boundary-edge has exactly one end-vertex in KCK^C. So, instead, we make the simpler decision to use the priority queue to keep the vertices in KCK^C, such that each vertex vv has an attribute v.costv.cost to store the weight of a minimum-weight boundary-edge incident on vv.

Note: The attribute v.costv.cost also serves as the priority key of vv. If a vertex is extracted from the priority queue, it implies that the vertex has an incident boundary-edge of the smallest weight over all the boundary-edges incident on vertices in KCK^C.

Vertices that are not in the priority queue are exactly the vertices in KK.

Implementing the tree

Instead of maintaining an explicit data structure for the tree KK ...