Minimum Spanning Trees

What Is Prim's Algorithm for Minimum Spanning Trees?

Prim's algorithm grows one tree from a starting vertex, always adding the cheapest edge that reaches a new vertex. Steps, a worked example, and running time.

On this page
  1. The short answer
  2. Key terms
  3. The steps
  4. A worked example
  5. Why the greedy choice is safe
  6. Running time
  7. Common mistakes

The short answer

Prim’s algo is short for Prim’s algorithm, and it finds a minimum spanning tree by growing one tree from a starting vertex. At each step, it adds the cheapest edge that connects the tree to a vertex not yet in it. It stops when every vertex is in the tree.

Key terms

  • Graph: vertices (points) joined by edges (connections).
  • Weight: the cost of an edge, such as distance, time, or money.
  • Spanning tree: a set of edges that connects every vertex without forming a cycle. A spanning tree of V vertices has V − 1 edges.
  • Minimum spanning tree: the spanning tree with the lowest total weight.

The steps

  1. Choose any vertex as the start, and put it in the tree.
  2. Find every edge that joins a vertex in the tree to a vertex outside it.
  3. Add the cheapest of those edges, along with the vertex at its far end.
  4. Repeat steps 2 and 3 until all vertices are in the tree.

An edge with both ends already in the tree is never a candidate, because adding it would create a cycle.

A worked example

Use five vertices, A through E, with these weights: A–B 1, B–C 2, C–D 3, A–C 4, B–D 5, C–E 6, and D–E 7.

Start at A:

  • Tree {A}. The cheapest edge out is A–B (1). Add B.
  • Tree {A, B}. The edges out are A–C (4), B–C (2), and B–D (5). Add B–C (2), which brings in C.
  • Tree {A, B, C}. The edges out are B–D (5), C–D (3), and C–E (6). Add C–D (3), which brings in D. The edge A–C now has both ends in the tree, so it no longer counts.
  • Tree {A, B, C, D}. The edges out are C–E (6) and D–E (7). Add C–E (6), which brings in E.

The result is A–B, B–C, C–D, and C–E, with a total weight of 12. Starting from E produces the same tree, with the edges added in a different order.

Why the greedy choice is safe

Prim’s is a greedy algorithm: it always takes the cheapest option available at that moment. For minimum spanning trees, this still leads to an optimal result. If the tree built so far is part of some minimum spanning tree, then the cheapest edge leaving it is also part of one. So each step keeps the best answer within reach.

Running time

With V vertices and E edges:

  • Adjacency matrix with a linear search for the cheapest edge: O(V²)
  • Adjacency list with a binary heap: O(E log V)
  • Adjacency list with a Fibonacci heap: O(E + V log V)

The priority queue controls the speed. A binary heap belongs to the same family of structures used in the heapsort algorithm, which is one reason the two topics often come up together.

Common mistakes

  • Adding an edge whose far end is already in the tree. That creates a cycle, so skip it.
  • Expecting basic Prim’s to cover a disconnected graph. It only covers the connected part that contains the start vertex. For each other part, run it again from a vertex in that part, or use Kruskal’s, which returns a minimum spanning forest.
  • Thinking the starting vertex changes the answer. With distinct weights, any starting vertex gives the same tree. With ties, you may get a different tree, but its total weight stays the same.

The complete guide to minimum spanning trees

Prim's Algorithm vs. Kruskal's
Read the guide

Keep exploring

Guides from other topics

All topics →