Data For Science

Data For Science

Graphs

Prim's Minimum Spanning Tree Algorithm

Finding the shortest path to every node

Bruno Gonçalves's avatar
Bruno Gonçalves
Aug 16, 2022
∙ Paid

In this post we introduce Prim’s algorithm to identify the Minimum Spanning Tree of a graph. The MST is a tree made up by the subset of edges from a weighted graph that connect every node while having the smallest total weight possible.

MSTs have a wide range of practical applications, specially in distribution networks where we must reach every node in the graph while minimizing the overall cost. As a test case, we apply Prim’s algorithm to an Open Street Map road network.

Share

Prim’s Algorithm

Prim’s algorithm, despite its name, was originally introduced by V. Jarník in 1930 and eventually rediscovered independently by Robert Prim and Edsger W. Dijkstra in the late 50s.

The algorithm has some similarities with Dijkstra’s algorithm (that we discussed here) with a greedy procedure to iteratively grow the tree by adding one edge at a time.

  1. Choose an arbitrary seed node.

  2. At each step add the lightest edge that connects one of the nodes already in the tree to one of the nodes not yet added.

  3. Stop w…

User's avatar

Continue reading this post for free, courtesy of Bruno Gonçalves.

Or purchase a paid subscription.
© 2026 Data For Science, Inc · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture