Minimum Spanning Trees and Algorithms
Suppose a telecommunications company must connect ten cities with fiber-optic cable. Any two cities can be linked directly. Each link has a different cost. Which links should be built? The goal is full connectivity at the lowest total cost. This is the minimum spanning tree problem. It is one of the cleanest optimization questions in combinatorics. It is also one of the few NP-hard-looking problems that is not NP-hard at all. It yields completely to greedy reasoning. The idea matters because it is the prototype of a "greedy algorithm that actually works." Most naive greedy strategies fail. The locally best choice at each step usually ruins the global optimum. Minimum spanning trees are a rare and instructive case. A short-sighted, myopic rule — always take the cheapest safe edge — provably produces a globally optimal structure. Understanding why greediness works here matters. This happens via the Cut Property and the Cycle Property. It is a template for recognizing when greedy algorithms are trustworthy elsewhere. The big picture connects three things. First, a combinatorial object: spanning trees, already familiar. Second, an optimization criterion: minimize total edge weight. Third, an algorithmic method: two competing but equally correct greedy procedures, due to Kruskal and to Prim. Both algorithms build the same kind of object from different starting points. Kruskal grows a forest by scanning all edges in sorted order. Prim grows a single tree outward from one vertex. Yet both are always correct. Their correctness rests on the exact same two-page lemma. This article gives the formal definition of a minimum spanning tree. It proves the Cut Property that underlies both algorithms. It states and proves correctness theorems for Kruskal's and Prim's algorithms. It works two nontrivial numerical examples. It shows exactly which hypotheses cannot be dropped, such as connectivity and well-defined weights. It closes by placing minimum spanning trees in their broader mathematical and applied context. Finally, it points toward Directed Graphs and Digraphs, where the analogous "minimum arborescence" problem behaves very differently.
- A minimum spanning tree (MST) of a connected weighted graph is a spanning tree whose edge-weight sum is smallest among all spanning trees.
- Every connected weighted graph has an MST; if all edge weights are distinct, the MST is unique.
- The Cut Property: the cheapest edge crossing any cut is in some MST — this single fact justifies every correct MST algorithm.
- Kruskal's algorithm sorts all edges by weight and greedily adds an edge unless it creates a cycle, using a union–find structure.
- Prim's algorithm grows one tree from a start vertex, always attaching the cheapest edge leaving the current tree, using a priority queue.
- Both run efficiently — for Kruskal, for Prim with a binary heap — and both are provably optimal, not just heuristically good.
- Key idea: greedy edge selection is globally optimal for spanning trees because of the Cut Property, not by accident.
Where this lives today
Minimum spanning trees appear wherever a "cheapest connectivity" question arises. In network design, MSTs give the minimum-cost way to wire together routers, power substations, or pipeline junctions when only pairwise link costs matter and redundancy is not required. In computational biology, MSTs on distance matrices between DNA or protein sequences produce approximate phylogenetic trees and are the backbone of single-linkage clustering algorithms in statistics, where cutting the most expensive edges of an MST partitions data into natural clusters. In computer vision and image segmentation, pixel-similarity graphs are segmented by MST-based algorithms (e.g., Felzenszwalb–Huttenlocher). In approximation algorithms, the MST gives a 2-approximation to the metric Traveling Salesman Problem — precisely the connection Kruskal's original paper title advertised — by doubling the MST's edges to form an Eulerian multigraph and shortcutting repeated vertices. In matroid theory, the correctness proofs above generalize verbatim to the statement that the greedy algorithm finds a minimum-weight basis of any matroid, a cornerstone abstraction unifying spanning trees with linear independence and much of combinatorial optimization. Looking ahead, replacing undirected edges with directed arcs leads to the much subtler theory of minimum-cost arborescences in Directed Graphs and Digraphs, where the simple greedy Cut Property argument used here famously fails and a more intricate algorithm (Chu–Liu/Edmonds) is required.
Ask the tutor: How does the Cut Property generalize to prove that the greedy algorithm finds an optimal basis in any matroid, not just a graphic one?
Let be a finite, connected, undirected graph with a weight function . A spanning tree of is a subgraph with that is a tree (connected and acyclic) on the full vertex set . The weight of a spanning tree is
A minimum spanning tree (MST) of is a spanning tree satisfying
If is not connected, no spanning tree exists, so an MST is defined only for connected graphs; the natural extension is a minimum spanning forest, one MST per connected component.
Intuition
A spanning tree on vertices always has exactly edges — this is inherited from the theory of Spanning Trees — so minimizing over all spanning trees is a discrete optimization problem over a finite (but exponentially large, by Cayley's formula there are labeled trees on vertices even before weights) search space. Checking every spanning tree by brute force is hopeless for large graphs, yet the MST can be found by two strikingly simple greedy rules. Picture the graph as a set of cities (vertices) joined by roads (edges) each with a construction cost. A cut is any partition of the vertices into two nonempty sets and ; the edges crossing between them are the cut edges. The central insight, formalized below as the Cut Property, is that the cheapest edge crossing any cut must belong to some MST. Intuitively: if you skip the cheapest bridge across a divide, you must eventually build a more expensive one to reconnect the two sides, and swapping back to the cheap one can only lower the cost. Kruskal's algorithm exploits this by processing edges from cheapest to most expensive, taking an edge exactly when it does not create a cycle. Prim's algorithm exploits the same fact but restricts attention to a single, growing cut — the boundary between the partially built tree and the rest of the graph — at every step taking the cheapest edge across that specific cut.
Theorem: the correctness of greedy MST construction
Let be a connected weighted graph with weight function , and suppose all edge weights are distinct (this hypothesis is relaxed afterward). Let be nonempty, and let be the unique minimum-weight edge with exactly one endpoint in (a cut edge for the cut ). Then every MST of contains .
We argue by an exchange argument, a standard technique for greedy-algorithm correctness proofs. Let be any MST of , and suppose for contradiction that . Write with , . Since is a spanning tree, it is connected, so there is a unique path in from to . Because and , the path must cross the cut at least once: formally, walking along from to , consider the first edge of whose far endpoint leaves . Then with and , so is also a cut edge for . Because all weights are distinct and is the minimum-weight cut edge, we have (note since but ). Form . We check is a spanning tree:
- Removing from the tree disconnects it into exactly two components, one containing (call it ) and one containing (call it ), because is a tree and removing any single edge from a tree splits it into exactly two components (a basic fact about trees, since a tree has a unique path between any two vertices, and deleting an edge on the unique – path severs the only connection).
- Adding reconnects these two components, provided and (or vice versa). Indeed : is ? Since was chosen as the first cut-crossing edge along from , the sub-path of from to lies entirely within and entirely within , so and are connected in , i.e. . Similarly the remainder of from to lies in and shows .
- Hence has edges (one removed, one added) and is connected (the two components are reunited by ), so is a spanning tree.
Now compare weights:
since . This contradicts the assumption that is a minimum spanning tree. Therefore for every MST .
∎Dropping the distinctness assumption: if is a minimum-weight edge crossing a cut (not necessarily unique), then there exists an MST containing (though not necessarily every MST).
Identical exchange argument, with one adjustment: we only need , which still gives , so is also an MST (not strictly better, but not worse), and it contains . Start from any MST ; if it already contains we are done, otherwise the construction above produces an MST containing .
∎Let be any cycle in , and let be a strictly maximum-weight edge of (assume, as above, all weights distinct, or "strictly heavier than every other edge of " in general). Then belongs to no MST of .
Suppose toward contradiction some MST contains . Removing from splits into two components and , exactly as in the previous proof. Since lies on the cycle , the rest of (i.e. with removed) is an – path in using only edges of ; this path must cross from to at least once, so it contains some edge with one endpoint in and one in . Since was the maximum-weight edge of , (using distinctness; in the general case with below at least as good). Form ; by the same component-reunification argument as before, is a spanning tree, and
contradicting minimality of . Hence is in no MST.
∎Let be a connected weighted graph, . Sort the edges as with . Define by processing in order and adding to if and only if contains no cycle. Then the resulting (which will have exactly edges) is a minimum spanning tree of .
We use induction combined with the Cut Property (an exchange/greedy-stays-ahead argument).
Step 1: is a spanning tree. By construction is acyclic at every stage, so the final is a forest. We must show is connected, i.e., spans all of . Suppose not: then has more than one component, so some component (nonempty) has no edge of leaving it. Since is connected, some edge with exists. Consider the first time this edge was examined by the algorithm (it is some ). At that moment, adding to the current forest could not create a cycle, because and lie in different components of at the end of the algorithm, hence also at every earlier stage (components of the growing forest only merge, never split, as edges are added). So the algorithm must have added — contradicting the assumption that no edge of leaves . Hence is connected, and being acyclic and connected it is a spanning tree; a spanning tree on vertices always has edges (a known fact from the theory of Spanning Trees).
Step 2: is minimum. We prove more precisely that every edge added to is justified by the Cut Property. When the algorithm considers and decides to add it (because lie in different components of the current forest , the forest built so far), let be the vertex set of the component of containing . Then crosses the cut . Claim: is a minimum-weight edge crossing this cut. Suppose some edge with had . Since edges are processed in nondecreasing weight order, was considered before (as , since weights are sorted and forces , using the sorted indexing; ties are broken consistently by the fixed order ). At the time was considered, the forest built so far was a subforest of (edges only accumulate), so and were still in different components at that time — because is a union of components of every earlier sub-forest for (components only merge as the algorithm proceeds, and is exactly the component of at stage , hence at stage , 's component was a subset of , and similarly the rest of was not yet merged with ). Hence adding at that time would not have created a cycle, so the algorithm would have added . But then at stage , and would already be connected via 's effect on components, contradicting that are in different components at stage . This contradiction proves the claim: is a minimum-weight cut edge for . By the Cut Property (general-weight corollary), there exists an MST containing . We now finish by a standard "stays ahead" induction: let be an MST maximizing the number of initial edges (in Kruskal's chosen order) it shares with ; we show . Suppose . Let be the first edge (in the fixed order) where and differ, i.e. but agree on membership in both. By the cut argument above, is a minimum-weight edge crossing the cut determined by the component structure built from . Since is a spanning tree, it must also cross this cut somewhere (as must be connected in ); let , necessarily by minimality of the weight-order and the "first difference" choice, be an edge of crossing this same cut with (since is minimum over this cut). Replacing by in — the same reunification argument as in the Cut Property proof — yields another spanning tree with , hence also an MST, but now agreeing with on one more initial edge than did. This contradicts maximality of the agreement count for . Hence no difference exists, so , and is a minimum spanning tree.
∎Let be a connected weighted graph and fix any starting vertex . Build a sequence of trees where , and is obtained from by adding a minimum-weight edge with exactly one endpoint in (the vertex set of ) and the other outside it. Then is a minimum spanning tree of .
This is a direct application of the Cut Property, by induction on .
Well-defined ness. As long as , since is connected there is at least one edge crossing the cut (otherwise would be disconnected, a contradiction), so a minimum-weight such edge exists and can be formed. Each is a tree: trivially is, and inductively, adding an edge from a new vertex to an existing tree preserves the tree property (it adds one vertex and one edge, cannot create a cycle since the new vertex had no prior edges in , and preserves connectivity).
Inductive claim. There exists an MST of with for every . Base case : is a sub(forest) of every spanning tree, in particular of any MST . Inductive step: suppose for some MST . Let be the edge added to form , a minimum-weight edge crossing the cut . If already, then and we are done with the same . If : apply the Cut Property (general-weight corollary) to the cut : there is an MST containing . We must further arrange . Use the exact exchange construction from the Cut Property proof, applied starting from itself: since is a minimum-weight edge across the cut and is a spanning tree, contains some edge crossing the same cut (as argued in the Cut Property proof) with ; moreover because 's edges do not cross this cut (all of 's vertices are on one side by definition of the cut). Set ; by the reunification argument, is a spanning tree with , hence is also an MST. Since , we have , and , so . Take the new MST to be . By induction, for some MST . But is itself a spanning tree (it has vertices, is connected and acyclic by the well-definedness argument above), and any spanning tree contained in another spanning tree must equal it (both have exactly edges). Hence , an MST.
∎Worked examples
Example 1 — Kruskal's algorithm on an asymmetric graph. Consider vertices with weighted edges:
Sort edges by weight: . Process in order, tracking components via union–find, needing exactly edges: 1. , weight : components separate — add. Forest: . 2. , weight : separate — add. Forest: . 3. , weight : separate — add. Forest: . 4. , weight : is in , is in — separate — add. Now merged. 5. , weight (tie with previous, order fixed as listed): in , in — separate — add. Now all merged — that's 5 edges, done! Remaining edges are skipped automatically since the forest is already spanning, but let's verify the check would reject them: – (both in the same component) would form a cycle; –, –, – likewise close cycles.
MST edges: .
Total weight: . Sanity check via the Cut Property: the cut isolating from the rest has crossing edges –, –, –, – — minimum is a tie at but that's irrelevant here since – itself (weight ) is internal to the pair, so this cut only concerns how attaches to the rest, consistent with – being chosen.
Example 2 — Prim's algorithm on the same graph, started at . Maintain , and at each step scan all edges from to , picking the minimum.
- . Crossing edges: –, –, –. Minimum: –. Add it.
- . Crossing edges: –, –, –. (Note – no longer crosses.) Minimum: –. Add it.
- . Crossing edges: –, –, –, –. Minimum: –. Add it.
- . Crossing edges: –, –, –, –. Minimum: –. Add it.
- . Crossing edges: –, –, –. Minimum: –. Add it.
- — all six vertices reached, stop.
MST edges: .
Total weight: . Both algorithms, starting from entirely different strategies (global sorted scan vs. local frontier growth) and different natural orderings, land on the same total weight — indeed the identical edge set, confirming the MST is unique here (all weights are distinct: — wait, appears twice, on – and –; both algorithms happened to include both of these weight-9 edges rather than choosing between them, since they don't compete for the same cut, so uniqueness is not contradicted: the tie never forced a choice between alternative MSTs).
The definition and both theorems assume is connected. Drop this hypothesis and consider
two disjoint edges with no connection between and . There is no spanning tree at all: a spanning tree by definition must be connected and touch every vertex, but no acyclic connected subgraph can include both components, since no edge joins them. Running Kruskal's algorithm produces the forest with two components — this is a minimum spanning forest, not a spanning tree, and the correctness theorem's conclusion (" has edges and is a spanning tree") is false: here has edges, not . Prim's algorithm fails even more starkly: started at , it grows , and then gets stuck — no edge crosses the cut , so the algorithm as stated cannot proceed to , exactly because the "well-definedness" step of the proof invoked connectivity of to guarantee a crossing edge exists. This shows precisely where connectivity is used: it is the single fact that guarantees every cut has at least one crossing edge, without which both algorithms terminate prematurely with only a forest.
Scope, edge cases & common traps
| Situation | What happens |
|---|---|
| All weights distinct | MST is unique (Cut Property forces every edge) |
| Some weights tied | MST may be non-unique; multiple correct outputs possible, all with equal total weight |
| Negative weights allowed | Theorems still hold — nothing in the proofs used |
| Disconnected graph | No spanning tree exists; algorithms yield a minimum spanning forest instead |
| Multigraph (parallel edges) | Fine — keep only the cheapest of any parallel pair before running Kruskal, since a costlier parallel edge can never help |
| Self-loops | Discard — a self-loop can never appear in any spanning tree (it is always a cycle by itself) |
The mathematician behind it
Origins · 1 minJoseph Bernard Kruskal Jr. and how this was discovered
Joseph Kruskal was an American mathematician and statistician who worked for decades at Bell Laboratories, a research environment that produced an extraordinary concentration of foundational combinatorics and information theory in the mid-twentieth century. His 1956 paper "On the Shortest Spanning Subtree of a Graph and the Traveling Salesman Problem" gave the algorithm bearing his name and, notably, also proved the Cut and Cycle Property machinery in the language of matroids — recognizing that the correctness of the greedy edge-selection rule is really a statement about the structure of independent sets in a graphic matroid, a viewpoint that later blossomed into full matroid theory. Kruskal also made major contributions to multidimensional scaling and to the combinatorics of well-quasi-orderings (Kruskal's tree theorem).
Do Kruskal's and Prim's algorithms always produce the same tree?
Not necessarily the same tree, but always the same total weight, since both provably output a minimum spanning tree and all minimum spanning trees share the minimum weight by definition. When edge weights are all distinct, the MST is unique, so in that case both algorithms必 produce identical edge sets, possibly discovered in a different order. When weights tie, there can be multiple distinct minimum spanning trees with equal total weight, and the two algorithms (or even the same algorithm with a different tie-breaking rule) may return different — but equally optimal — trees. The worked examples above illustrate a near-tie case where both algorithms happened to agree.
Why does the greedy approach work here but fail for problems like the Traveling Salesman Problem?
Greedy correctness for MSTs rests on the Cut Property, which in turn rests on the special combinatorial structure of the collection of "acyclic edge sets" — this structure is precisely what is called a matroid, where local exchange arguments (swap one edge for another without breaking any constraint) always improve or preserve the objective. The Traveling Salesman Problem's feasible structures (Hamiltonian cycles) do not form a matroid: a locally-cheapest edge choice can easily paint you into a corner where completing a valid tour forces an expensive detour, with no clean exchange argument available. This structural difference — matroid versus non-matroid feasible sets — is exactly why greedy is provably optimal for one and merely heuristic for the other.
Can edge weights be negative or zero?
Yes. Nothing in the definition of MST or in the proofs of the Cut Property, Cycle Property, or the two algorithms' correctness theorems requires nonnegative weights. The proofs only ever compare weights to each other via inequalities like , never against zero. Negative weights arise naturally when "weight" represents a profit or subsidy rather than a cost, and the same greedy machinery still identifies the tree of minimum total weight (which may itself be negative).
What is the time complexity of each algorithm, and when should I prefer one over the other?
Kruskal's algorithm sorts edges in time, then processes them with a union–find (disjoint-set) structure supporting near-constant amortized operations, for total , which is since . Prim's algorithm, implemented with a binary heap priority queue, runs in ; with a Fibonacci heap it improves to . Prim's is generally preferred for dense graphs (close to ) or when the graph is given implicitly (e.g., via a distance function, without an explicit edge list), while Kruskal's is often simpler to implement and preferred when the edge list is already available and sparse.
Does the MST minimize the longest edge used, or the bottleneck cost, as a side effect?
Yes, remarkably: every MST is also a minimum bottleneck spanning tree, meaning it minimizes the maximum single edge weight among all spanning trees, not merely the sum. This follows from the Cycle Property: if some spanning tree used an edge heavier than the MST's heaviest edge, that heavy edge would necessarily complete a cycle with cheaper edges already available, and the Cycle Property shows such a maximum-weight cycle edge can never be forced into an optimal structure. This bottleneck-optimality is exploited in applications like network reliability where minimizing the worst single link matters as much as total cost.
What happens if the graph has parallel edges or self-loops?
Self-loops can be discarded immediately: a self-loop connects a vertex to itself and can never be part of any acyclic subgraph, since adding it instantly creates a 1-edge cycle. For parallel edges (multiple edges between the same pair of vertices with possibly different weights), only the minimum-weight edge among any parallel bundle can ever appear in an MST, by a direct application of the Cycle Property to the 2-cycle formed by any two parallel edges; so it is safe (and standard practice) to discard all but the cheapest parallel edge before running either algorithm.
Is the minimum spanning tree the same as the shortest path tree from a vertex?
No — these are different objects that are frequently confused. A shortest-path tree (as produced by Dijkstra's algorithm) minimizes the path distance from one fixed source vertex to every other vertex, and different sources generally give different shortest-path trees. An MST minimizes the total weight summed over all tree edges, with no reference to any single source, and is unique up to weight ties regardless of which vertex you imagine as a "root." A path that is shortest from the source may not even appear in the MST, and conversely the MST does not guarantee shortest point-to-point distances.