Minimum Spanning Trees

Prim's Algorithm vs. Kruskal's

Prim's and Kruskal's both find a minimum spanning tree. Compare how each builds it, their steps, running times, and which fits sparse or dense graphs.

On this page
  1. The short answer
  2. What a minimum spanning tree is
  3. A worked example
  4. How Kruskal’s algorithm works
  5. How Prim’s algorithm works
  6. Comparing running times
  7. Sparse and dense graphs
  8. Other differences
  9. Is Prim’s or Kruskal’s easier?
  10. Why both are greedy
  11. Which to pick
  12. The takeaway

The short answer

Prim’s algorithm and Kruskal’s algorithm both find a minimum spanning tree (MST) of a connected, weighted, undirected graph. On the same graph, they return trees with the same total weight. They differ in how they build that tree. Prim’s grows a single tree outward from a starting vertex, always adding the cheapest edge that reaches a new vertex. Kruskal’s sorts every edge first, then adds them from cheapest to most expensive, skipping any edge that would close a loop.

Which one runs faster depends on the shape of the graph and how it is stored. Kruskal’s is often a good fit for sparse graphs stored as a list of edges. Prim’s is often preferred for dense graphs, where there are many edges for each vertex.

What a minimum spanning tree is

A graph has vertices (points) joined by edges (connections), and each edge has a weight, such as a cost, distance, or time. A spanning tree is a set of edges that connects every vertex without forming a cycle. A spanning tree of a graph with V vertices always has exactly V − 1 edges. The minimum spanning tree is the spanning tree whose weights add up to the smallest total.

This matters when you want the cheapest network that still links everything, such as laying cable between buildings or building roads between towns. You could check every possible set of edges to find the answer, but that brute force approach becomes impractical quickly as the graph grows.

A worked example

Consider five locations, A through E, with these connections and weights:

  • A–B: 1
  • B–C: 2
  • C–D: 3
  • A–C: 4
  • B–D: 5
  • C–E: 6
  • D–E: 7

The cheapest spanning tree uses A–B, B–C, C–D, and C–E, for a total weight of 12. All seven weights are different, so this is the only minimum spanning tree. Both algorithms find it.

How Kruskal’s algorithm works

  1. Sort all edges by weight, lowest first.
  2. Treat each vertex as its own separate tree.
  3. Go through the sorted edges. Add an edge if its two endpoints are in different trees. If they are already in the same tree, skip it, because adding it would create a cycle.
  4. Stop once V − 1 edges have been added.

Kruskal’s tracks which vertices share a tree with a disjoint-set structure, also called union-find, so each check is quick.

On the example, the sorted order is A–B, B–C, C–D, A–C, B–D, C–E, D–E. Kruskal’s adds A–B, B–C, and C–D. It then skips A–C, since A and C are already connected through B. It skips B–D for the same reason, since B and D are connected through C. It adds C–E, which brings the count to four edges, and stops. D–E is never added.

How Prim’s algorithm works

  1. Pick any vertex as the starting point and put it in the tree.
  2. Among all edges that join a vertex in the tree to a vertex outside it, add the cheapest one, along with the new vertex it reaches.
  3. Repeat step 2 until every vertex is in the tree.

Prim’s usually keeps its candidate edges in a priority queue, often built on a binary heap, so the cheapest candidate can be found without rescanning every edge. A binary heap belongs to the same family of structures used in the heapsort algorithm. A priority queue is an abstract data type, so it can be built in more than one way (for background, see what is ADT in data structure).

Starting from A on the example:

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

The four edges chosen are A–B, B–C, C–D, and C–E, the same minimum spanning tree as before. Starting from E gives the same tree, with the edges added in a different order.

Comparing running times

Let V be the number of vertices and E the number of edges.

Method Running time Notes
Kruskal’s, with a comparison sort O(E log E), usually written O(E log V) Sorting takes most of the time
Prim’s, adjacency matrix with a linear search O(V²) Simple to write, suits dense graphs
Prim’s, adjacency list with a binary heap O(E log V) Common textbook version
Prim’s, adjacency list with a Fibonacci heap O(E + V log V) Faster in theory, harder to implement

For a simple graph, log E and log V differ only by a constant factor, so the two forms of Kruskal’s running time are equivalent in Big O notation.

The gap between the two methods shows up most on dense graphs. There, E is close to V², so Prim’s matrix version does about V² work, while Kruskal’s sort has to handle about V² edges and costs roughly V² log V. On sparse graphs, E is close to V, and both methods come out to about E log V. These are general patterns, not guarantees.

Sparse and dense graphs

  • Sparse graphs have relatively few edges. Kruskal’s is often the default here, since it works straight from an edge list and its steps are easy to follow.
  • Dense graphs have many edges. Prim’s, usually in its simple array or matrix form, is often faster.

These are rules of thumb. The input format you already have matters too. If your data is already a matrix, converting it to an edge list may not be worth the effort.

Other differences

  • Disconnected graphs. Kruskal’s naturally returns a minimum spanning forest, one tree for each connected part. Basic Prim’s grows from one starting vertex, so it only covers that vertex’s part of the graph. You would run it once per part.
  • Starting point. Prim’s needs a starting vertex. Kruskal’s does not.
  • Ties. If weights tie, both methods still return a minimum spanning tree with the same total weight, though the specific edges may differ.
  • Core tools. Kruskal’s relies on sorting and a disjoint-set structure. Prim’s relies on a priority queue.

Is Prim’s or Kruskal’s easier?

Sources disagree. Kruskal’s is often described as simpler because the idea is short: sort the edges, then skip any that form a cycle. Prim’s can feel more natural if you like watching one tree grow, and its simplest version is a short loop over a matrix. Which one feels easier often depends on whether sorting and disjoint sets or priority queues come more naturally to you.

Why both are greedy

Both algorithms are greedy. At each step, they take the cheapest choice that keeps the result valid, and they never go back to change an earlier choice. For minimum spanning trees, this is safe. If the tree built so far is part of some minimum spanning tree, the cheapest edge leaving it is also part of one.

What is the difference between Dijkstra and Prim’s algorithm? Both grow a set of finished vertices and use a priority queue to pick the next step, so the two look alike. The difference is the ranking. Dijkstra ranks candidates by total distance from the source, while Prim’s ranks them by the weight of the single edge.

Which to pick

  • If your graph is stored as an edge list or is sparse, Kruskal’s is usually the better fit.
  • If your graph is stored as a matrix or is dense, Prim’s, in its simple array form, is often the better fit.
  • If you need a tree grown from a specific starting vertex, Prim’s is the natural choice.
  • If the graph may be disconnected and you want one result covering every part, Kruskal’s handles that directly.
  • If you are learning by hand, run both on the same small graph, as in the example above, and compare the edges you pick.

The takeaway

Prim’s and Kruskal’s both return the minimum spanning tree, so the real question is how you want to get there. Kruskal’s looks at edges in order of weight across the whole graph. Prim’s grows one tree from a single vertex. Choose based on the graph’s density, how it is stored, and whether you need a single tree or a forest.

Keep exploring

Guides from other topics

All topics →

Big O for Data Structures

A practical guide to Big O for data structures: how arrays, lists, hash tables, trees and heaps compare on search, insert and delete costs, with examples.

Big O Notation 5 min read