CoursifyCoursify

Minimum Spanning Tree (MST) and Prim's Algorithm

Minimum Spanning Tree (MST) and Prim's Algorithm

Verified Sources
May 25, 2026

A minimum spanning tree (MST) is a subset of edges from a connected, weighted, undirected graph that:

  1. includes all vertices,
  2. contains no cycles,
  3. uses exactly V1V-1 edges for VV vertices,
  4. 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

3

Footnotes

  1. Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples. 2 3

  2. What is Minimum Spanning Tree (MST) - GeeksforGeeks - Summarizes spanning tree properties such as acyclicity and V1V-1 edges. 2

  3. Minimum Spanning Trees - Princeton Algorithms - Explains connectedness requirements and formal MST conditions. 2

  4. Minimum Spanning Trees - Princeton Algorithms - Notes that disconnected graphs lead to minimum spanning forests rather than a single MST. 2 3

  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

  6. Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal.

  7. 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 VV vertices, every spanning tree has exactly V1V-1 edges, is connected, and contains no cycle. The MST is the spanning tree with minimum total edge weight.2

Footnotes

  1. Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples.

  2. What is Minimum Spanning Tree (MST) - GeeksforGeeks - Summarizes spanning tree properties such as acyclicity and V1V-1 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 (S,VS)(S, V-S), where SS 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 V1V-1 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

3

Footnotes

  1. Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. 2 3

  2. Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. 2 3 4 5

  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 4

  4. Minimum Spanning Trees - Princeton Algorithms - Explains connectedness requirements and formal MST conditions.

  5. Minimum Spanning Trees - Princeton Algorithms - Notes that disconnected graphs lead to minimum spanning forests rather than a single MST.

  6. Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples. 2

  7. Prim's algorithm using priority_queue in STL - GeeksforGeeks - Details practical priority-queue implementation and typical O(ElogV)O(E \log V) complexity. 2

Steps of Prim's Algorithm

  1. 1
    Step 1

    Pick any starting vertex. Mark it as part of the current tree. Set the key of the start vertex to 00 and all others to \infty if using a priority-queue implementation.2

    Footnotes

    1. Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity.

    2. Prim's algorithm using priority_queue in STL - GeeksforGeeks - Details practical priority-queue implementation and typical O(ElogV)O(E \log V) complexity.

  2. 2
    Step 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

    1. 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.

  3. 3
    Step 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

    1. Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe.

    2. Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal.

  4. 4
    Step 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

    1. Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal.

    2. Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity.

  5. 5
    Step 5

    If using a priority queue, update the best known connecting edge for neighboring vertices that are still outside the tree.2

    Footnotes

    1. Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity.

    2. Prim's algorithm using priority_queue in STL - GeeksforGeeks - Details practical priority-queue implementation and typical O(ElogV)O(E \log V) complexity.

  6. 6
    Step 6

    Continue until all vertices are included. The final tree has V1V-1 edges and minimum total weight.2

    Footnotes

    1. Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples.

    2. 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 A,B,C,D,EA, B, C, D, E:

EdgeWeight
ABA-B2
ACA-C3
BCB-C1
BDB-D4
CDC-D5
CEC-E6
DED-E7

We will start Prim's algorithm from vertex AA. 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:

  1. Start with AA
  2. Choose ABA-B with weight 22
  3. From {A,B}\{A,B\}, choose BCB-C with weight 11
  4. From {A,B,C}\{A,B,C\}, choose BDB-D with weight 44
  5. From {A,B,C,D}\{A,B,C,D\}, choose CEC-E with weight 66

So the MST edges are:

{(A,B),(B,C),(B,D),(C,E)}\{(A,B), (B,C), (B,D), (C,E)\}

and the total weight is

2+1+4+6=132 + 1 + 4 + 6 = 13

This tree spans all five vertices, has exactly 51=45-1=4 edges, and contains no cycle, so it is a spanning tree. Because Prim's greedy selections are safe, it is an MST.3

Footnotes

  1. Minimum spanning trees - Defines spanning trees and MSTs, including tree properties and examples.

  2. Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. 2

  3. Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe.

  4. Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal.

IterationCurrent Tree VerticesCandidate EdgesChosen 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

  1. 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.

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

  1. Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal.

  2. 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 / StructureTypical Time Complexity
Adjacency matrix + simple searchO(V2)O(V^2)
Adjacency list + binary heap / priority queueO(ElogV)O(E \log V)
Edge-oriented heap variantsoften described around O(ElogE)O(E \log E) or improved through vertex-key updates2

In practice:

  • For dense graphs, O(V2)O(V^2) 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

  1. Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. 2 3 4

  2. Prim's algorithm using priority_queue in STL - GeeksforGeeks - Details practical priority-queue implementation and typical O(ElogV)O(E \log V) complexity. 2 3 4 5

  3. 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:

AB(2),  BC(1),  BD(4),  CE(6)AB(2),\; BC(1),\; BD(4),\; CE(6)

giving total weight:

1313

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

  1. Introduction to Algorithms Spanning Trees and Cuts Cut Property - States the cut property and explains why the lightest crossing edge is safe. 2

  2. Prim's algorithm - Wikipedia - Provides the high-level procedure and discusses data-structure-dependent complexity. 2

  3. Introduction to Algorithms - Describes Prim's algorithm as a greedy MST method and contrasts it with Kruskal. 2

Knowledge Check

Question 1 of 4
Q1Single choice

What is a minimum spanning tree?

Chat with Kiro