Minimum Spanning Tree

Other names:
MST, minimum weight spanning tree

In short: A minimum spanning tree (MST) is a set of edges that connects all the nodes of a connected, undirected, weighted graph without any cycles, using the smallest possible total edge weight. Every spanning tree of a graph with V nodes has exactly V - 1 edges, and if all the edge weights are different, the MST is unique. Kruskal's and Prim's algorithms both find one in O(E log V) time.

Quick reference

A minimum spanning tree (MST) is a set of edges that connects all the nodes of a connected, undirected, weighted graph, without any cycles, using the smallest possible total edge weight.

Key facts:

  • In a graph with V nodes, every spanning tree, minimum or not, has exactly V - 1 edges.
  • If every edge weight is different, there's exactly one minimum spanning tree. With ties, there can be several, all with the same total weight.
  • An MST keeps the total weight as low as possible. It doesn't give the shortest path between two nodes.

How to find one:

  • Kruskal's algorithm: go through the edges from cheapest to most expensive, adding each one that doesn't create a cycle. O(E log E) time, where E is the number of edges.
  • Prim's algorithm: grow one tree from a start node, always adding the cheapest edge leaving it. O(E log V) time with a heap, or O(V²) with an array.

Used for:

  • Designing the cheapest network of cables, pipes, or roads, clustering data, and approximating the traveling salesman problem.

What Is a Spanning Tree?

A spanning tree of a graph is a set of the graph's edges that connects every node (it spans the graph) without forming any cycles (so it's a tree). A few facts follow straight from that definition:

  • Only connected graphs have spanning trees. If some nodes can't reach others, the best you can do is a spanning forest: a spanning tree for each connected piece.
  • Every spanning tree has exactly V - 1 edges. Fewer can't connect all V nodes, and one more would create a cycle.
  • Adding any other edge to a spanning tree creates exactly one cycle, and removing any edge from it splits it into two pieces.

A graph usually has lots of spanning trees. When the graph is weighted, each spanning tree has a total weight: the sum of its edges' weights. A minimum spanning tree is a spanning tree whose total weight is as small as possible.

Note that "minimum" is about weight, not about the number of edges. Every spanning tree of a graph has the same number of edges, V - 1.

Minimum Spanning Tree Example

Say your bakery has six shops, A through F, and you want to connect them all to one network so every shop can see every other shop's orders. Picture the shops in two rows: A, B, and C on top, with D, E, and F underneath them. You can run a cable between some pairs of shops, and each cable run has a different cost, in thousands of dollars:

Cable run (edge) Cost (weight)
A-B 4
A-D 2
A-E 5
B-C 7
B-E 3
C-E 8
C-F 1
D-E 6
E-F 9

Any spanning tree of this graph is a set of 5 cable runs that connects all six shops, and there are 55 different ones. Here are three:

Spanning tree Cable runs Total cost
Wire every shop to E A-E, B-E, C-E, D-E, E-F 5 + 3 + 8 + 6 + 9 = 31
Go around the outside D-A, A-B, B-C, C-F, F-E 2 + 4 + 7 + 1 + 9 = 23
The minimum spanning tree C-F, A-D, B-E, A-B, B-C 1 + 2 + 3 + 4 + 7 = 17

The minimum spanning tree costs 17 ($17,000), and no other spanning tree of this graph costs that little.

Notice what it leaves out. A-E (5) is cheaper than B-C (7), but the MST skips it. A and E are already connected through B (A-B-E), so A-E would only add a cycle. B-C is the cheapest way to reach C and F at all.

Properties of Minimum Spanning Trees

The cut property

Split the nodes into any two groups. The cheapest edge that crosses from one group to the other belongs to a minimum spanning tree. (If it's the only cheapest crossing edge, it belongs to every minimum spanning tree.)

In the bakery graph, split the shops into {A, B, D, E} and {C, F}. Three edges cross between the groups: B-C (7), C-E (8), and E-F (9). The cheapest is B-C, so B-C has to be in the MST, and it is.

Why it's true: a spanning tree needs at least one edge crossing the split, or the two groups wouldn't be connected. Say a spanning tree doesn't use the cheapest crossing edge. Add it. That creates a cycle, and the cycle has to cross the split a second time, using some other crossing edge from the tree. Remove that other edge. You still have a spanning tree, and since the edge you removed costs at least as much as the one you added, the new tree is no more expensive.

The cut property is why greedy algorithms work for minimum spanning trees. Prim's algorithm uses the split "the tree so far vs. everything else," and Kruskal's algorithm uses "this edge's group vs. everything else."

The cycle property

If one edge on a cycle costs more than every other edge on that cycle, it isn't in any minimum spanning tree.

In the bakery graph, A-B (4), B-E (3), and A-E (5) form a cycle. A-E is the most expensive edge on it, so A-E is out. Each of the four edges the MST leaves out is the most expensive edge on some cycle:

Edge left out A cycle where it's the most expensive edge
A-E (5) A-B-E: A-B (4), B-E (3), A-E (5)
D-E (6) D-A-B-E: A-D (2), A-B (4), B-E (3), D-E (6)
C-E (8) B-C-E: B-C (7), C-E (8), B-E (3)
E-F (9) C-E-F: C-E (8), E-F (9), C-F (1)

Why it's true: if a spanning tree used that most expensive edge, you could remove it, which splits the tree in two, and reconnect the two pieces with another edge from the same cycle. That edge is cheaper, so the tree you started with wasn't minimum.

When is the minimum spanning tree unique?

If all the edge weights are different, a graph has exactly one minimum spanning tree. That's why the bakery graph has just one.

With ties, there can be more than one. Change A-E's cost from 5 to 4, and the bakery graph has two minimum spanning trees: one uses A-B (4) and the other uses A-E (4) instead. Both cost 17. Different minimum spanning trees of the same graph always have the same total weight (otherwise one of them wouldn't be minimum), and in fact they use the same edge weights, just possibly on different edges. Ties don't always mean more than one MST, though: if the tied edges are needed no matter what, there's still only one.

A minimum spanning tree isn't a shortest-path tree

An MST makes the total weight of all its edges as small as possible. It doesn't make the path between any particular pair of nodes as short as possible. In the bakery MST, the route from A to E goes A-B-E, costing 7, while the direct cable A-E costs only 5.

For the cheapest route from one node to the others, use Dijkstra's algorithm. Starting from A, it builds a different tree, A-D, A-B, A-E, B-C, and C-F, whose total weight is 19.

Other handy facts

  • Negative weights are fine. MST algorithms only compare edge weights. They never add up long paths, so negative weights don't cause the problems they cause for shortest-path algorithms.
  • Adding the same amount to every edge doesn't change which trees are minimum, since every spanning tree has the same number of edges.
  • For a maximum spanning tree, flip the comparisons: take the most expensive edges first, or negate every weight. The bakery graph's maximum spanning tree costs 35.
  • Each node's cheapest edge is in the MST (when the weights are all different). That's the cut property with one node on one side. In the bakery graph, it gives you A-D, B-E, and C-F right away.
  • MST paths have the smallest possible "bottleneck." The path between two nodes in an MST makes its most expensive edge as cheap as possible. From A to C, the MST path A-B-C never uses an edge over 7. No route can do better: to reach C or F from the other shops, a route has to use B-C (7), C-E (8), or E-F (9).

How to Find a Minimum Spanning Tree: Kruskal's vs. Prim's

Trying every spanning tree is hopeless for big graphs, since there can be exponentially many of them. The two classic algorithms are both greedy, and both rely on the cut property:

  • Kruskal's algorithm sorts all the edges by weight and adds each one that connects two pieces that aren't connected yet, using a union-find to check.
  • Prim's algorithm starts from one node and repeatedly adds the cheapest edge from the tree to a node outside it, using a priority queue to find that edge.

On the bakery graph, both find the same tree, but they add the edges in a different order:

Order the edges are added
Kruskal's C-F (1), A-D (2), B-E (3), A-B (4), B-C (7)
Prim's, starting at A A-D (2), A-B (4), B-E (3), B-C (7), C-F (1)
Kruskal's algorithm Prim's algorithm
How the tree grows A forest of small trees merges into one One tree spreads out from a start node
Main data structures Sorted edge list and a union-find Min-heap and an adjacency list
Time O(E log E), the same as O(E log V) O(E log V) with a binary heap, or O(V²) with an array
Best for Sparse graphs, especially when you're given a list of edges Dense graphs, and graphs stored as adjacency lists
Graph isn't connected Finds a minimum spanning forest Only spans the start node's connected piece

There's a third classic, Borůvka's algorithm, which is the oldest of the three (Otakar Borůvka, 1926). In each round, every piece of the forest adds its cheapest edge to some other piece, all at once. It also runs in O(E log V) time and splits up nicely for parallel computing, but it rarely comes up in coding interviews.

Here's a compact version of Kruskal's algorithm. Nodes are numbered 0 to V - 1 (shop A is 0, B is 1, and so on), and each edge is a (node1, node2, weight) triple. It returns the tree's edges, or raises an error if the graph isn't connected, since then there's no spanning tree:

import java.util.ArrayList; import java.util.Arrays; import java.util.List; // kruskal's algorithm. nodes are numbered 0 to numNodes - 1, and each // edge is {node1, node2, weight}. returns the tree's edges. public static List<int[]> minimumSpanningTree(int numNodes, int[][] edges) { // union-find: every node starts out in a group of its own int[] parent = new int[numNodes]; for (int node = 0; node < numNodes; node++) { parent[node] = node; } int[][] sortedEdges = edges.clone(); Arrays.sort(sortedEdges, (a, b) -> Integer.compare(a[2], b[2])); List<int[]> tree = new ArrayList<>(); for (int[] edge : sortedEdges) { int root1 = find(parent, edge[0]); int root2 = find(parent, edge[1]); if (root1 != root2) { // different groups, so this edge can't close a cycle parent[root1] = root2; // merge the two groups tree.add(edge); } } if (tree.size() < numNodes - 1) { throw new IllegalArgumentException("The graph is not connected, so it has no spanning tree"); } return tree; } private static int find(int[] parent, int node) { // walk up to the root of node's group, halving the path as we go while (parent[node] != node) { parent[node] = parent[parent[node]]; node = parent[node]; } return node; } public static void main(String[] args) { // shops A through F are nodes 0 through 5 final int A = 0, B = 1, C = 2, D = 3, E = 4, F = 5; int[][] cableCosts = { {A, B, 4}, {A, D, 2}, {A, E, 5}, {B, C, 7}, {B, E, 3}, {C, E, 8}, {C, F, 1}, {D, E, 6}, {E, F, 9}, }; int total = 0; for (int[] edge : minimumSpanningTree(6, cableCosts)) { System.out.println("ABCDEF".charAt(edge[0]) + "-" + "ABCDEF".charAt(edge[1]) + " " + edge[2]); total += edge[2]; } System.out.println("Total: " + total); }

On the bakery graph, it prints:

C-F 1
A-D 2
B-E 3
A-B 4
B-C 7
Total: 17

For a step-by-step walkthrough, a faster union-find, and what to do with graphs that aren't connected, see Kruskal's algorithm. For the heap-based approach and an O(V²) version for dense graphs, see Prim's algorithm.

What Are Minimum Spanning Trees Used For?

  • Network design. Connecting a set of places with cable, pipes, power lines, or roads at the lowest total cost is exactly the minimum spanning tree problem, as in the bakery example. In fact, Borůvka designed his 1926 algorithm while working on an efficient electrical network for Moravia.

  • Clustering. Treat your data points as nodes, with the distance between two points as the weight of the edge between them. Build an MST, then delete its k - 1 most expensive edges. The k pieces left over are clusters (this is single-linkage clustering), and they make the closest pair of points in different clusters as far apart as possible. In the bakery graph, deleting the MST's most expensive edge, B-C (7), splits the shops into two clusters: {A, B, D, E} and {C, F}.

  • Approximating the traveling salesman problem. Finding the shortest round trip that visits every city is NP-hard, but MSTs give fast approximations. When distances obey the triangle inequality (going direct is never longer than a detour), walking around an MST and skipping places you've already visited gives a round trip at most twice as long as the best one. Christofides' algorithm also starts from an MST and gets within 1.5 times the best. And with non-negative distances, an MST's total weight is a lower bound on the best round trip: remove any one leg of a round trip, and what's left is a spanning tree.

  • Widest-path questions. A maximum spanning tree's paths make their cheapest edge as expensive as possible. That answers questions like "what's the heaviest truck that can get between two towns, if each road has a weight limit?"

Common Mistakes and Interview Tips

  • Spotting an MST problem. Look for "connect all of these at the lowest total cost," "every node must be reachable from every other," or "the cheapest network." If the question is about the cheapest route between two particular places, it's a shortest-path problem instead.

  • Confusing an MST with shortest paths. In the bakery MST, getting from A to E costs 7, but the direct cable costs 5. An MST minimizes the total, not each route.

  • Forgetting the graph might not be connected. Then there's no spanning tree at all. Check that your answer has V - 1 edges, and handle the other case the way the problem asks (often by returning −1).

  • Using MST algorithms on directed graphs. Kruskal's and Prim's assume edges go both ways. The directed version of the problem (a minimum spanning arborescence, where every node is reachable from a root) needs a different algorithm, like Chu-Liu/Edmonds'.

  • Checking for cycles the wrong way. In Kruskal's algorithm, "have I already seen both ends of this edge?" isn't the same as "are both ends already connected?" Use a union-find. See Kruskal's algorithm for an example of how the wrong check fails.

  • Picking the algorithm. Given a list of edges, Kruskal's is usually the least code. Given a complete graph, like "connect all these points," use Prim's O(V²) version.

Frequently Asked Questions

What is a minimum spanning tree?

A minimum spanning tree (MST) is a set of edges that connects every node of a connected, undirected, weighted graph, with no cycles, using the smallest possible total edge weight. For example, it's the cheapest way to run cable so that every building in a network is connected.

How many edges does a minimum spanning tree have?

Exactly V - 1, where V is the number of nodes. Every spanning tree has that many edges: fewer can't connect all the nodes, and one more would create a cycle.

Is a minimum spanning tree unique?

If all the edge weights are different, yes: the graph has exactly one minimum spanning tree. With tied weights, a graph can have several minimum spanning trees, but they all have the same total weight.

What's the difference between a minimum spanning tree and a shortest path?

A minimum spanning tree minimizes the total weight of all the edges needed to connect every node. A shortest path minimizes the weight of the route between two particular nodes, and the path between two nodes in an MST often isn't the shortest one. Use Dijkstra's algorithm for shortest paths.

How do you find a minimum spanning tree?

Use Kruskal's algorithm, which adds edges from cheapest to most expensive and skips any that would create a cycle, or Prim's algorithm, which grows one tree from a start node by always adding the cheapest edge leaving it. Both run in O(E log V) time.

What are minimum spanning trees used for?

Designing the cheapest network of cables, pipes, or roads that connects a set of places, clustering data by deleting an MST's most expensive edges, and quickly approximating the traveling salesman problem.

Reviewed by Stephen Cognetta, CEO of Aced. Last updated: December 4, 2026

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .