Minimum Spanning Trees: Concept, Prim’s vs. Kruskal’s, and Complexity Analysis

Minimum Spanning Trees: Concept, Prim’s vs. Kruskal’s, and Complexity Analysis

Verified Sources
May 25, 2026

A Minimum Spanning Tree (MST) is a foundational concept in graph theory, especially for optimization problems in networks, clustering, and infrastructure design. In a connected, weighted, undirected graph G=(V,E)G=(V,E), a spanning tree is a subset of edges that connects all vertices without forming cycles. Because a tree on V|V| vertices always has exactly V1|V|-1 edges, an MST is the spanning tree whose total edge weight is as small as possible.2

Formally, if each edge eEe \in E has weight w(e)w(e), then an MST is a spanning tree TET \subseteq E minimizing

eTw(e).\sum_{e \in T} w(e).

Several important facts follow from the definition:

  • The graph must be connected for an MST to exist; otherwise, the appropriate structure is a minimum spanning forest.
  • The MST is acyclic because every tree is acyclic.
  • The MST is not necessarily unique; if different edges have equal weights, multiple MSTs may exist with the same total cost.
  • Edge weights need not represent geometric distances; they may encode cost, latency, risk, or any additive criterion.

A useful intuition is that the MST gives the cheapest way to connect all vertices while avoiding redundant links. This is why MSTs appear in communication network design, road and utility planning, and clustering methods.

and support the formal definition and structural properties above.

Footnotes

  1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees. 2 3

  2. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity. 2 3 4 5 6

Prim's algorithm in 2 minutes

Core MST Idea

An MST minimizes total connection cost, not necessarily the number of edges. The number of edges is fixed at |V|-1 for any spanning tree.2

Footnotes

  1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees.

  2. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

1. Explaining the concept of an MST

To understand an MST precisely, separate the phrase into its three parts:

TermMeaningWhy it matters
SpanningAll vertices must be includedThe solution connects the whole graph
TreeNo cycles are allowedPrevents redundant connections
MinimumThe sum of chosen edge weights is smallestOptimizes total cost

If a connected weighted undirected graph has vertices VV, then any spanning tree contains exactly V1|V|-1 edges. If more edges are included, a cycle must appear; if fewer are included, the graph cannot remain connected.

This leads to the optimization perspective:

  • We do not seek the shortest path between two vertices.
  • We do not seek the fewest edges.
  • We seek the least total weight over all trees that connect every vertex.2

For example, in a network of cities, an MST minimizes the total cost of laying cable or building roads while still ensuring every city is reachable from every other city. In hierarchical clustering and related applications, MST-like structures help reveal economical connection patterns among data points.

A key theoretical principle behind MST algorithms is the greedy choice of a safe edge. Both Prim’s and Kruskal’s algorithms rely on adding low-weight edges in a way that preserves the possibility of an optimal final tree.2

Footnotes

  1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees. 2

  2. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity. 2 3 4

  3. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures.

How to recognize or verify a Minimum Spanning Tree

  1. 1
    Step 1

    Ensure the original graph is connected; otherwise, an MST for the whole graph does not exist, and the result becomes a minimum spanning forest.

    Footnotes

    1. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

  2. 2
    Step 2

    Verify that the proposed subgraph includes all vertices, has no cycles, and contains exactly V1|V|-1 edges.

    Footnotes

    1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees.

  3. 3
    Step 3

    Add the weights of all selected edges to obtain the cost of the spanning tree.

    Footnotes

    1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees.

  4. 4
    Step 4

    Determine whether any other spanning tree has a smaller total weight. If none does, the tree is minimum.2

    Footnotes

    1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees.

    2. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

  5. 5
    Step 5

    In practice, algorithms such as Prim’s or Kruskal’s construct the tree efficiently instead of enumerating all spanning trees, which would be computationally infeasible.3

    Footnotes

    1. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

    2. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures.

    3. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find.

2. Prim’s and Kruskal’s algorithms: comparison and contrast

Both Prim’s and Kruskal’s are greedy algorithms for computing an MST, but they grow the solution in different ways.

Prim’s algorithm

Prim’s algorithm starts from a chosen vertex and grows a single tree outward. At each step, it selects the minimum-weight edge connecting a vertex already in the current tree to a vertex outside it.2 The algorithm therefore maintains one connected component throughout the process.

Kruskal’s algorithm

Kruskal’s algorithm starts with all vertices isolated, so the intermediate structure is a forest. It sorts all edges by nondecreasing weight and repeatedly adds the next lightest edge that does not create a cycle.2 To detect whether an edge would join vertices already in the same component, Kruskal’s algorithm typically uses a Union-Find or disjoint-set structure.

The conceptual distinction is central:

  • Prim: grows one tree from a seed vertex.
  • Kruskal: grows many small trees and merges them.

This affects data structures, complexity, and preferred use cases.

Footnotes

  1. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures. 2

  2. Chapter 20 - Minimum Spanning Trees - Summarizes Prim’s implementations: adjacency matrix, binary heap, and Fibonacci heap.

  3. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

  4. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find. 2 3

Starts from one vertex and repeatedly adds the lightest edge leaving the current tree.2

Best paired with an adjacency representation and a priority queue keyed by the cheapest connection to the current tree.

Intermediate structure: one connected tree.

Footnotes

  1. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures. 2

  2. Chapter 20 - Minimum Spanning Trees - Summarizes Prim’s implementations: adjacency matrix, binary heap, and Fibonacci heap.

Prim’s algorithm: sequential walkthrough

  1. 1
    Step 1

    Initialize the MST with any vertex. The final total weight is independent of the starting point when the MST is unique, though the traversal order may differ.

    Footnotes

    1. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures.

  2. 2
    Step 2

    Maintain candidate edges that connect the current tree to vertices not yet included. A priority queue is commonly used to retrieve the minimum candidate efficiently.2

    Footnotes

    1. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures.

    2. Chapter 20 - Minimum Spanning Trees - Summarizes Prim’s implementations: adjacency matrix, binary heap, and Fibonacci heap.

  3. 3
    Step 3

    Select the minimum-weight edge crossing from the current tree to an outside vertex, then add that edge and the new vertex.

    Footnotes

    1. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures.

  4. 4
    Step 4

    For neighbors of the newly added vertex, reduce their best-known connection cost if a lighter edge is found.2

    Footnotes

    1. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures.

    2. Chapter 20 - Minimum Spanning Trees - Summarizes Prim’s implementations: adjacency matrix, binary heap, and Fibonacci heap.

  5. 5
    Step 5

    When all vertices are included, the resulting connected acyclic structure is an MST.2

    Footnotes

    1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees.

    2. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures.

Kruskal’s algorithm: sequential walkthrough

  1. 1
    Step 1

    Arrange all graph edges in nondecreasing order of weight. This is the dominant cost in the standard implementation.2

    Footnotes

    1. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

    2. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find.

  2. 2
    Step 2

    Place each vertex in its own set so that connected components can be tracked efficiently.

    Footnotes

    1. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find.

  3. 3
    Step 3

    Consider each edge in sorted order and test whether its endpoints lie in different sets.

    Footnotes

    1. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find.

  4. 4
    Step 4

    If the endpoints are in different sets, include the edge in the MST and union the two sets; otherwise, discard the edge.

    Footnotes

    1. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find.

  5. 5
    Step 5

    At that point, all vertices are connected by a minimum-weight tree.2

    Footnotes

    1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees.

    2. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

3. Side-by-side comparison

The following table synthesizes the main similarities and differences.

CriterionPrim’s algorithmKruskal’s algorithm
Basic strategyGrow one tree from a starting vertexGrow a forest by merging components
Greedy choiceMinimum edge leaving current treeMinimum edge globally that does not create a cycle
Main data structurePriority queue / heap, often with adjacency list or matrixSorted edge list + Union-Find
Intermediate stateAlways one connected componentSeveral components that merge over time
Natural graph viewVertex-centered growthEdge-centered selection
Typical strengthOften favored on dense graphs or when graph is stored as adjacency structureOften favored on sparse graphs or when edge list is explicit
Cycle handlingAvoided implicitly by only adding edges to outside verticesChecked explicitly with Union-Find

Despite these differences, both are correct greedy algorithms for the MST problem and both exploit the cut/cycle structure of weighted graphs.3

Footnotes

  1. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

  2. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures.

  3. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find.

When choosing between the algorithms

If the graph is already available as an edge list and is relatively sparse, Kruskal’s is often straightforward and efficient. If the graph is dense or accessed through adjacency structure, Prim’s is often the more natural choice.3

Footnotes

  1. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures.

  2. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find.

  3. Chapter 20 - Minimum Spanning Trees - Summarizes Prim’s implementations: adjacency matrix, binary heap, and Fibonacci heap.

4. Time complexity analysis

The time complexity of both algorithms depends strongly on implementation details.

Prim’s algorithm complexity

Prim’s algorithm has several standard complexity forms:

  1. Adjacency matrix + linear search:
    O(V2)O(|V|^2).2

  2. Adjacency list + binary heap:
    O((V+E)logV)O((|V|+|E|)\log |V|), usually simplified to O(ElogV)O(|E|\log |V|) for connected graphs.3

  3. Adjacency list + Fibonacci heap:
    O(E+VlogV)O(|E| + |V|\log |V|).2

Interpretation:

  • For dense graphs, where E|E| is close to V2|V|^2, the O(V2)O(|V|^2) matrix-based implementation can be quite competitive and is often preferred for simplicity.
  • For sparse graphs, adjacency lists with heaps significantly reduce unnecessary scanning of absent edges.2

Kruskal’s algorithm complexity

Kruskal’s algorithm typically runs in

O(ElogE),O(|E|\log |E|),

because sorting the edges dominates the cost.3

After sorting, the Union-Find operations used for cycle detection are very efficient and usually do not dominate the overall complexity in standard analyses.2

Since in a simple graph EV2|E| \le |V|^2, we have logE=O(logV)\log |E| = O(\log |V|), so Kruskal’s complexity is also commonly written as

O(ElogV).O(|E|\log |V|).

This makes the asymptotic comparison with heap-based Prim especially clear.

Footnotes

  1. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures. 2 3 4 5

  2. Chapter 20 - Minimum Spanning Trees - Summarizes Prim’s implementations: adjacency matrix, binary heap, and Fibonacci heap. 2 3 4

  3. Introduction to Algorithms, Chapter 24: Minimum Spanning Trees - Detailed analysis of Prim’s algorithm with binary heap yielding O(ElogV)O(E \log V).

  4. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

  5. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find. 2 3

  6. DSA Kruskal's Algorithm - Explains why Kruskal’s standard running time is O(ElogE)O(E \log E) and why it is effective on sparse graphs. 2

Asymptotic Complexity Comparison of Common MST Implementations

Relative growth categories for standard implementations; smaller is better asymptotically.

A more nuanced comparison depends on graph density:

  • In a sparse graph, E=O(V)|E| = O(|V|), so both heap-based Prim and Kruskal are near O(VlogV)O(|V|\log |V|).3
  • In a dense graph, E=Θ(V2)|E| = \Theta(|V|^2), so Kruskal’s sorting cost becomes O(V2logV)O(|V|^2 \log |V|), while matrix-based Prim can remain at O(V2)O(|V|^2).2

Hence the widely taught rule of thumb:

  • Kruskal’s is often preferred for sparse graphs.
  • Prim’s is often preferred for dense graphs.3

This is not merely folklore; it follows directly from how each algorithm organizes work. Kruskal processes and sorts edges globally, which is economical when edges are relatively few. Prim can avoid global sorting and instead repeatedly refine the cheapest connection to the current tree, which is attractive when many edges are present.2

Footnotes

  1. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures. 2 3 4

  2. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find. 2 3

  3. Chapter 20 - Minimum Spanning Trees - Summarizes Prim’s implementations: adjacency matrix, binary heap, and Fibonacci heap. 2 3

Advantages, trade-offs, and preferred scenarios

Common misconception

An MST is not the same as a shortest-path tree. Shortest-path trees optimize path lengths from one source, whereas MSTs optimize the total weight of all chosen edges.2

Footnotes

  1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees.

  2. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity.

5. Final analytical conclusion

The MST problem asks for the least-cost way to connect every vertex in a connected, weighted, undirected graph using exactly V1|V|-1 edges and no cycles.2 Prim’s and Kruskal’s algorithms solve this problem using different greedy perspectives:

  • Prim’s grows one connected tree outward from a starting vertex.2
  • Kruskal’s sorts all edges and builds the MST by merging components without forming cycles.2

Their asymptotic costs show why algorithm choice depends on representation and density:

Prim (matrix)=O(V2)\text{Prim (matrix)} = O(|V|^2) Prim (binary heap)=O(ElogV)\text{Prim (binary heap)} = O(|E|\log |V|) Kruskal=O(ElogE)=O(ElogV)\text{Kruskal} = O(|E|\log |E|) = O(|E|\log |V|)

Therefore:

  1. For dense graphs, Prim’s algorithm is often preferable because O(V2)O(|V|^2) can outperform global edge sorting.2
  2. For sparse graphs, Kruskal’s algorithm is often attractive because sorting a relatively small edge set is efficient and the Union-Find structure makes cycle checking inexpensive.2
  3. For implementation simplicity, the choice may depend on the data already available: adjacency structures favor Prim, while an edge list favors Kruskal.2

In short, neither algorithm is universally superior. The best choice depends on graph density, input representation, and the supporting data structures available in the implementation environment.3

Footnotes

  1. Minimum spanning trees - Formal definitions of weighted graphs, trees, spanning trees, and minimum spanning trees.

  2. Minimum Spanning Trees - Princeton Algorithms reference explaining MST properties, connectedness, forests, and Kruskal’s complexity. 2

  3. Prim's algorithm - Wikipedia - Overview of Prim’s algorithm and standard time complexities across data structures. 2 3 4

  4. Chapter 20 - Minimum Spanning Trees - Summarizes Prim’s implementations: adjacency matrix, binary heap, and Fibonacci heap. 2 3

  5. Extreme Algorithms: Kruskal's Algorithm - Explains Kruskal’s process and the role of disjoint sets/Union-Find. 2 3 4

  6. DSA Kruskal's Algorithm - Explains why Kruskal’s standard running time is O(ElogE)O(E \log E) and why it is effective on sparse graphs.

Knowledge Check

Question 1 of 5
Q1Single choice

Which statement best defines a Minimum Spanning Tree in a connected weighted undirected graph?

Explore Related Topics

1

Negative Weight Cycle and the Bellman-Ford Algorithm for Single-Source Shortest Distance

Negative weight cycles are cycles whose total edge weight is negative, and the Bellman‑Ford algorithm computes single‑source shortest distances while detecting such cycles.

  • A reachable negative weight cycle makes the shortest‑distance problem undefined because repeated traversal can lower the path cost without bound.
  • Bellman‑Ford initializes distances, relaxes all edges |V|‑1 times, then performs one extra pass to detect any further relaxation.
  • It correctly handles negative edges but reports failure when a reachable negative cycle exists.
  • The algorithm runs in O(V E) time and uses O(V) extra space.
  • Vertices unreachable from the source keep a distance of ∞.
2

The Minimum Number of Colors for a Graph with $n>3$ Vertices and 2 Edges

A graph with more than three vertices that contains exactly two edges always has chromatic number 22.

  • Because an edge exists, at least two colors are required: χ(G)2\chi(G)\ge 2.
  • With only two edges the graph is bipartite (either two disjoint K2K_2 or a P3P_3 plus isolated vertices), so two colors are sufficient: χ(G)2\chi(G)\le 2.
  • No triangle K3K_3 or odd cycle can appear, eliminating the need for three or more colors.
  • Isolated vertices are adjacent to none and can reuse any existing color, leaving the chromatic number unchanged.
  • Consequently χ(G)=2\chi(G)=2, making option (i) the correct answer.
3

Demystifying Computational Complexity: P, NP, Cook's Theorem, and the Foundations of Computer Science