Minimum Spanning Tree (MST) and Prim's Algorithm
A minimum spanning tree (MST) is a subset of edges from a connected, weighted, undirected graph that:
- includes all vertices,
- contains no cycles,
- uses exactly edges for vertices,
- has the minimum total weight among all spanning trees of the graph.3
A spanning tree is already a minimal connector of the graph, but the MST adds the optimization goal of minimizing total cost.2 This makes MSTs fundamental in network design, such as laying cables, designing roads, building communication backbones, and clustering data.2
If the graph is not connected, an MST for the whole graph does not exist; instead, we obtain a minimum spanning forest.
A central theoretical idea behind MST algorithms is the cut property: for any cut of the graph, a minimum-weight edge crossing that cut belongs to an MST.2 Prim's algorithm repeatedly uses this principle to grow one tree from a starting vertex outward.2
Footnotes
-
Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples. ↩ ↩2 ↩3
-
What is Minimum Spanning Tree (MST) - GeeksforGeeks - Summarizes spanning tree properties such as acyclicity and edges. ↩ ↩2
-
Minimum Spanning Trees - Princeton Algorithms - Explains connectedness requirements and formal MST conditions. ↩ ↩2
-
Minimum Spanning Trees - Princeton Algorithms - Notes that disconnected graphs lead to minimum spanning forests rather than a single MST. ↩ ↩2 ↩3
-
Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe. ↩ ↩2 ↩3
-
Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. ↩
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩
Prim's Algorithm
Core MST Properties
For a graph with vertices, every spanning tree has exactly edges, is connected, and contains no cycle. The MST is the spanning tree with minimum total edge weight.2
Footnotes
-
Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples. ↩
-
What is Minimum Spanning Tree (MST) - GeeksforGeeks - Summarizes spanning tree properties such as acyclicity and edges. ↩
Why Prim's Algorithm Works
Prim's algorithm is a greedy algorithm for MST construction.2 It starts with any one vertex and maintains a growing set of vertices already included in the tree. At every iteration, it selects the smallest-weight edge that connects a vertex inside the current tree to a vertex outside it.2
This is safe because the algorithm always chooses a light edge across the cut , where is the set of vertices already in the tree.2 By the cut property, that edge can be included in some MST, so repeating this choice eventually builds an MST.
Key ideas used by Prim's algorithm:
- the graph must be connected, weighted, and undirected for a full MST to exist.2
- the partial solution always remains a single tree, unlike Kruskal's algorithm which may maintain a forest temporarily.2
- the algorithm ends when all vertices are included, yielding exactly selected edges.2
A common efficient implementation uses an adjacency list and a priority queue to track the cheapest edge reaching each outside vertex.2
Footnotes
-
Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. ↩ ↩2 ↩3
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩ ↩2 ↩3 ↩4 ↩5
-
Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe. ↩ ↩2 ↩3 ↩4
-
Minimum Spanning Trees - Princeton Algorithms - Explains connectedness requirements and formal MST conditions. ↩
-
Minimum Spanning Trees - Princeton Algorithms - Notes that disconnected graphs lead to minimum spanning forests rather than a single MST. ↩
-
Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples. ↩ ↩2
-
Prim's algorithm using priority_queue in STL - GeeksforGeeks - Details practical priority-queue implementation and typical complexity. ↩ ↩2
Steps of Prim's Algorithm
- 1Step 1
Pick any starting vertex. Mark it as part of the current tree. Set the key of the start vertex to and all others to if using a priority-queue implementation.2
Footnotes
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩
-
Prim's algorithm using priority_queue in STL - GeeksforGeeks - Details practical priority-queue implementation and typical complexity. ↩
-
- 2Step 2
Consider all edges that connect a vertex already in the tree to a vertex not yet in the tree. These are the candidate edges for expansion.2
Footnotes
-
Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe. ↩
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩
-
- 3Step 3
Select the candidate edge with the smallest weight. By the cut property, this is a safe edge to add to the MST.2
Footnotes
-
Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe. ↩
-
Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. ↩
-
- 4Step 4
Include the selected edge and the newly reached vertex in the growing tree. The structure remains acyclic because the edge connects the tree to an outside vertex.2
Footnotes
-
Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. ↩
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩
-
- 5Step 5
If using a priority queue, update the best known connecting edge for neighboring vertices that are still outside the tree.2
Footnotes
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩
-
Prim's algorithm using priority_queue in STL - GeeksforGeeks - Details practical priority-queue implementation and typical complexity. ↩
-
- 6Step 6
Continue until all vertices are included. The final tree has edges and minimum total weight.2
Footnotes
-
Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples. ↩
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩
-
Worked Example of Prim's Algorithm
Consider the following weighted graph with vertices :
| Edge | Weight |
|---|---|
| 2 | |
| 3 | |
| 1 | |
| 4 | |
| 5 | |
| 6 | |
| 7 |
We will start Prim's algorithm from vertex . The goal is to build a tree connecting all vertices with minimum total weight.2
At each stage, choose the lightest edge leaving the current tree:
- Start with
- Choose with weight
- From , choose with weight
- From , choose with weight
- From , choose with weight
So the MST edges are:
and the total weight is
This tree spans all five vertices, has exactly edges, and contains no cycle, so it is a spanning tree. Because Prim's greedy selections are safe, it is an MST.3
Footnotes
-
Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples. ↩
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩ ↩2
-
Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe. ↩
-
Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. ↩
| Iteration | Current Tree Vertices | Candidate Edges | Chosen Edge |
|---|---|---|---|
| 1 | {A} | AB(2), AC(3) | AB(2) |
| 2 | {A,B} | AC(3), BC(1), BD(4) | BC(1) |
| 3 | {A,B,C} | BD(4), CD(5), CE(6) | BD(4) |
| 4 | {A,B,C,D} | CE(6), DE(7) | CE(6) |
Weights of Edges Chosen by Prim's Algorithm
Example MST construction from the sample graph
How to Avoid Mistakes in Prim's Algorithm
Always choose the minimum-weight edge that connects the current tree to a vertex outside the tree. Do not choose the globally smallest remaining edge unless it crosses the current cut.2
Footnotes
-
Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe. ↩
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩
A Common Confusion
Prim's algorithm does not minimize the path from the start vertex to every other vertex. That is a shortest-path problem. Prim minimizes the total weight of the entire spanning tree.2
Footnotes
-
Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. ↩
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩
Time Complexity and Implementation Notes
The efficiency of Prim's algorithm depends on the data structures used.2
| Representation / Structure | Typical Time Complexity |
|---|---|
| Adjacency matrix + simple search | |
| Adjacency list + binary heap / priority queue | |
| Edge-oriented heap variants | often described around or improved through vertex-key updates2 |
In practice:
- For dense graphs, implementations can be acceptable.
- For sparse graphs, adjacency lists with a priority queue are usually preferred because they scale better.2
The priority queue stores, for each outside vertex, the smallest known edge weight connecting it to the current tree. When a lighter connection is found, its key is updated. This is analogous in style to Dijkstra's method, but the objective is different: Prim optimizes tree weight, not source-to-vertex distance.2
Footnotes
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩ ↩2 ↩3 ↩4
-
Prim's algorithm using priority_queue in STL - GeeksforGeeks - Details practical priority-queue implementation and typical complexity. ↩ ↩2 ↩3 ↩4 ↩5
-
Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. ↩
Frequently Asked Questions
Conceptual Summary
Prim's algorithm solves the MST problem by repeatedly expanding a connected partial tree with the cheapest valid edge.2 Its correctness follows from the cut property, and its output is a minimum-cost acyclic connector of all vertices.2
For the sample graph, the algorithm selected:
giving total weight:
This illustrates the central principle of MST construction: choose edges that improve global connectivity at minimum cumulative cost, not edges that merely give short routes from one source.2
Footnotes
-
Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe. ↩ ↩2
-
Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. ↩ ↩2
-
Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. ↩ ↩2
Knowledge Check
What is a minimum spanning tree?
