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.

Key takeaways
  • 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 — O(Elog⁡E)O(E\log E) for Kruskal, O(Elog⁡V)O(E\log V) 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 k−1k-1 most expensive edges of an MST partitions data into kk 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?

DefinitionMinimum Spanning Tree

Let G=(V,E)G = (V,E) be a finite, connected, undirected graph with a weight function w:E→Rw : E \to \mathbb{R}. A spanning tree of GG is a subgraph T=(V,ET)T = (V, E_T) with ET⊆EE_T \subseteq E that is a tree (connected and acyclic) on the full vertex set VV. The weight of a spanning tree TT is

w(T)=∑e∈ETw(e).w(T) = \sum_{e \in E_T} w(e).

A minimum spanning tree (MST) of GG is a spanning tree T∗T^* satisfying

w(T∗)≤w(T)for every spanning tree T of G.w(T^*) \le w(T) \quad \text{for every spanning tree } T \text{ of } G.

If GG 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 nn vertices always has exactly n−1n-1 edges — this is inherited from the theory of Spanning Trees — so minimizing w(T)w(T) over all spanning trees is a discrete optimization problem over a finite (but exponentially large, by Cayley's formula there are nn−2n^{n-2} labeled trees on nn 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 SS and V∖SV\setminus S; 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.

Figure
A small weighted graph and one cut, illustrating the Cut Property
S V \ S A B C D E F 4 6 3 2 5 7 1 Cheapest crossing edge (weight 1) must be in some MST

Theorem: the correctness of greedy MST construction

LemmaCut Property

Let G=(V,E)G=(V,E) be a connected weighted graph with weight function ww, and suppose all edge weights are distinct (this hypothesis is relaxed afterward). Let S⊊VS \subsetneq V be nonempty, and let e∗e^* be the unique minimum-weight edge with exactly one endpoint in SS (a cut edge for the cut (S,V∖S)(S, V\setminus S)). Then every MST of GG contains e∗e^*.

Proof

We argue by an exchange argument, a standard technique for greedy-algorithm correctness proofs. Let TT be any MST of GG, and suppose for contradiction that e∗∉Te^* \notin T. Write e∗={u,v}e^* = \{u,v\} with u∈Su \in S, v∈V∖Sv \in V \setminus S. Since TT is a spanning tree, it is connected, so there is a unique path PP in TT from uu to vv. Because u∈Su \in S and v∉Sv \notin S, the path PP must cross the cut (S,V∖S)(S, V\setminus S) at least once: formally, walking along PP from uu to vv, consider the first edge ff of PP whose far endpoint leaves SS. Then f={x,y}f = \{x,y\} with x∈Sx \in S and y∈V∖Sy \in V\setminus S, so ff is also a cut edge for (S,V∖S)(S,V\setminus S). Because all weights are distinct and e∗e^* is the minimum-weight cut edge, we have w(f)>w(e∗)w(f) > w(e^*) (note f≠e∗f \ne e^* since e∗∉Te^* \notin T but f∈Tf \in T). Form T′=(T∖{f})∪{e∗}T' = (T \setminus \{f\}) \cup \{e^*\}. We check T′T' is a spanning tree:

  • Removing ff from the tree TT disconnects it into exactly two components, one containing xx (call it CxC_x) and one containing yy (call it CyC_y), because TT 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 xx–yy path severs the only connection).
  • Adding e∗={u,v}e^* = \{u,v\} reconnects these two components, provided u∈Cxu \in C_x and v∈Cyv \in C_y (or vice versa). Indeed u∈Su \in S: is u∈Cxu \in C_x? Since ff was chosen as the first cut-crossing edge along PP from uu, the sub-path of PP from uu to xx lies entirely within SS and entirely within TT, so uu and xx are connected in T∖{f}T \setminus \{f\}, i.e. u∈Cxu \in C_x. Similarly the remainder of PP from yy to vv lies in T∖{f}T\setminus\{f\} and shows v∈Cyv \in C_y.
  • Hence T′T' has ∣V∣−1|V|-1 edges (one removed, one added) and is connected (the two components are reunited by e∗e^*), so T′T' is a spanning tree.

Now compare weights:

w(T′)=w(T)−w(f)+w(e∗)<w(T),w(T') = w(T) - w(f) + w(e^*) < w(T),

since w(e∗)<w(f)w(e^*) < w(f). This contradicts the assumption that TT is a minimum spanning tree. Therefore e∗∈Te^* \in T for every MST TT. ■\blacksquare

∎
CorollaryCut Property, general weights

Dropping the distinctness assumption: if e∗e^* is a minimum-weight edge crossing a cut (S,V∖S)(S, V\setminus S) (not necessarily unique), then there exists an MST containing e∗e^* (though not necessarily every MST).

Proof

Identical exchange argument, with one adjustment: we only need w(f)≥w(e∗)w(f) \ge w(e^*), which still gives w(T′)≤w(T)w(T') \le w(T), so T′T' is also an MST (not strictly better, but not worse), and it contains e∗e^*. Start from any MST TT; if it already contains e∗e^* we are done, otherwise the construction above produces an MST T′T' containing e∗e^*. ■\blacksquare

∎
LemmaCycle Property

Let CC be any cycle in GG, and let ff be a strictly maximum-weight edge of CC (assume, as above, all weights distinct, or "strictly heavier than every other edge of CC" in general). Then ff belongs to no MST of GG.

Proof

Suppose toward contradiction some MST TT contains f={x,y}f = \{x,y\}. Removing ff from TT splits TT into two components Cx∋xC_x \ni x and Cy∋yC_y \ni y, exactly as in the previous proof. Since ff lies on the cycle CC, the rest of CC (i.e. CC with ff removed) is an xx–yy path in GG using only edges of C∖{f}C \setminus \{f\}; this path must cross from CxC_x to CyC_y at least once, so it contains some edge g≠fg \ne f with one endpoint in CxC_x and one in CyC_y. Since ff was the maximum-weight edge of CC, w(g)<w(f)w(g) < w(f) (using distinctness; in the general case w(g)≤w(f)w(g) \le w(f) with T′T' below at least as good). Form T′=(T∖{f})∪{g}T' = (T \setminus \{f\}) \cup \{g\}; by the same component-reunification argument as before, T′T' is a spanning tree, and

w(T′)=w(T)−w(f)+w(g)<w(T),w(T') = w(T) - w(f) + w(g) < w(T),

contradicting minimality of TT. Hence ff is in no MST. ■\blacksquare

∎
TheoremCorrectness of Kruskal's Algorithm

Let G=(V,E)G=(V,E) be a connected weighted graph, ∣V∣=n|V| = n. Sort the edges as e1,e2,…,eme_1, e_2, \dots, e_m with w(e1)≤w(e2)≤⋯≤w(em)w(e_1) \le w(e_2) \le \cdots \le w(e_m). Define TT by processing e1,…,eme_1, \ldots, e_m in order and adding eie_i to TT if and only if T∪{ei}T \cup \{e_i\} contains no cycle. Then the resulting TT (which will have exactly n−1n-1 edges) is a minimum spanning tree of GG.

Proof

We use induction combined with the Cut Property (an exchange/greedy-stays-ahead argument).

Step 1: TT is a spanning tree. By construction TT is acyclic at every stage, so the final TT is a forest. We must show TT is connected, i.e., spans all of VV. Suppose not: then TT has more than one component, so some component S⊊VS \subsetneq V (nonempty) has no edge of TT leaving it. Since GG is connected, some edge e={u,v}∈Ee = \{u,v\} \in E with u∈S,v∉Su \in S, v \notin S exists. Consider the first time this edge ee was examined by the algorithm (it is some eie_i). At that moment, adding ee to the current forest could not create a cycle, because uu and vv lie in different components of TT 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 eie_i — contradicting the assumption that no edge of TT leaves SS. Hence TT is connected, and being acyclic and connected it is a spanning tree; a spanning tree on nn vertices always has n−1n-1 edges (a known fact from the theory of Spanning Trees).

Step 2: TT is minimum. We prove more precisely that every edge eie_i added to TT is justified by the Cut Property. When the algorithm considers ei={u,v}e_i = \{u,v\} and decides to add it (because u,vu,v lie in different components of the current forest TiT_i, the forest built so far), let SS be the vertex set of the component of TiT_i containing uu. Then eie_i crosses the cut (S,V∖S)(S, V\setminus S). Claim: eie_i is a minimum-weight edge crossing this cut. Suppose some edge ej={p,q}e_j = \{p,q\} with p∈S,q∉Sp \in S, q \notin S had w(ej)<w(ei)w(e_j) < w(e_i). Since edges are processed in nondecreasing weight order, eje_j was considered before eie_i (as j<ij < i, since weights are sorted and w(ej)<w(ei)w(e_j)<w(e_i) forces j<ij<i, using the sorted indexing; ties are broken consistently by the fixed order e1,…,eme_1,\dots,e_m). At the time eje_j was considered, the forest built so far was a subforest of TiT_i (edges only accumulate), so pp and qq were still in different components at that time — because SS is a union of components of every earlier sub-forest TkT_k for k≤ik \le i (components only merge as the algorithm proceeds, and SS is exactly the component of uu at stage ii, hence at stage j<ij<i, uu's component was a subset of SS, and similarly the rest of SS was not yet merged with V∖SV\setminus S). Hence adding eje_j at that time would not have created a cycle, so the algorithm would have added eje_j. But then at stage ii, uu and vv would already be connected via eje_j's effect on components, contradicting that u,vu,v are in different components at stage ii. This contradiction proves the claim: eie_i is a minimum-weight cut edge for (S,V∖S)(S, V \setminus S). By the Cut Property (general-weight corollary), there exists an MST containing eie_i. We now finish by a standard "stays ahead" induction: let T∗T^* be an MST maximizing the number of initial edges e1,…,eie_1, \ldots, e_i (in Kruskal's chosen order) it shares with TT; we show T∗=TT^* = T. Suppose T∗≠TT^*\ne T. Let eie_i be the first edge (in the fixed order) where TT and T∗T^* differ, i.e. ei∈T∖T∗e_i\in T\setminus T^* but e1,…,ei−1e_1,\dots,e_{i-1} agree on membership in both. By the cut argument above, eie_i is a minimum-weight edge crossing the cut (S,V∖S)(S,V\setminus S) determined by the component structure built from e1,…,ei−1e_1,\ldots,e_{i-1}. Since T∗T^* is a spanning tree, it must also cross this cut somewhere (as u∈S,v∉Su\in S,v\notin S must be connected in T∗T^*); let eje_j, j>ij > i necessarily by minimality of the weight-order and the "first difference" choice, be an edge of T∗T^* crossing this same cut with w(ej)≥w(ei)w(e_j)\ge w(e_i) (since eie_i is minimum over this cut). Replacing eje_j by eie_i in T∗T^* — the same reunification argument as in the Cut Property proof — yields another spanning tree T\*\*T^{\*\*} with w(T\*\*)≤w(T∗)w(T^{\*\*}) \le w(T^*), hence also an MST, but now agreeing with TT on one more initial edge than T∗T^* did. This contradicts maximality of the agreement count for T∗T^*. Hence no difference exists, so T=T∗T = T^*, and TT is a minimum spanning tree. ■\blacksquare

∎
TheoremCorrectness of Prim's Algorithm

Let G=(V,E)G = (V,E) be a connected weighted graph and fix any starting vertex r∈Vr \in V. Build a sequence of trees T1⊂T2⊂⋯⊂TnT_1 \subset T_2 \subset \cdots \subset T_n where T1=({r},∅)T_1 = (\{r\}, \emptyset), and Tk+1T_{k+1} is obtained from TkT_k by adding a minimum-weight edge with exactly one endpoint in V(Tk)V(T_k) (the vertex set of TkT_k) and the other outside it. Then TnT_n is a minimum spanning tree of GG.

Proof

This is a direct application of the Cut Property, by induction on kk.

Well-defined ness. As long as V(Tk)≠VV(T_k) \ne V, since GG is connected there is at least one edge crossing the cut (V(Tk),V∖V(Tk))(V(T_k), V\setminus V(T_k)) (otherwise GG would be disconnected, a contradiction), so a minimum-weight such edge exists and Tk+1T_{k+1} can be formed. Each TkT_k is a tree: T1T_1 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 TkT_k, and preserves connectivity).

Inductive claim. There exists an MST T∗T^* of GG with Tk⊆T∗T_k \subseteq T^* for every k=1,…,nk = 1, \ldots, n. Base case k=1k=1: T1=({r},∅)T_1 = (\{r\}, \emptyset) is a sub(forest) of every spanning tree, in particular of any MST T∗T^*. Inductive step: suppose Tk⊆T∗T_k \subseteq T^* for some MST T∗T^*. Let ee be the edge added to form Tk+1T_{k+1}, a minimum-weight edge crossing the cut (V(Tk),V∖V(Tk))(V(T_k), V\setminus V(T_k)). If e∈T∗e \in T^* already, then Tk+1=Tk∪{e}⊆T∗T_{k+1} = T_k \cup \{e\} \subseteq T^* and we are done with the same T∗T^*. If e∉T∗e \notin T^*: apply the Cut Property (general-weight corollary) to the cut (V(Tk),V∖V(Tk))(V(T_k), V \setminus V(T_k)): there is an MST T^\widehat{T} containing ee. We must further arrange Tk⊆T^T_k \subseteq \widehat{T}. Use the exact exchange construction from the Cut Property proof, applied starting from T∗T^* itself: since ee is a minimum-weight edge across the cut and T∗T^* is a spanning tree, T∗T^* contains some edge ff crossing the same cut (as argued in the Cut Property proof) with w(f)≥w(e)w(f) \ge w(e); moreover f∉Tkf \notin T_k because TkT_k's edges do not cross this cut (all of TkT_k's vertices are on one side by definition of the cut). Set T\*\*=(T∗∖{f})∪{e}T^{\*\*} = (T^* \setminus \{f\}) \cup \{e\}; by the reunification argument, T\*\*T^{\*\*} is a spanning tree with w(T\*\*)=w(T∗)−w(f)+w(e)≤w(T∗)w(T^{\*\*}) = w(T^*) - w(f) + w(e) \le w(T^*), hence T\*\*T^{\*\*} is also an MST. Since f∉Tkf \notin T_k, we have Tk⊆T∗∖{f}⊆T\*\*T_k \subseteq T^* \setminus \{f\} \subseteq T^{\*\*}, and e∈T\*\*e \in T^{\*\*}, so Tk+1=Tk∪{e}⊆T\*\*T_{k+1} = T_k \cup \{e\} \subseteq T^{\*\*}. Take the new MST to be T\*\*T^{\*\*}. By induction, Tn⊆T∗T_n \subseteq T^* for some MST T∗T^*. But TnT_n is itself a spanning tree (it has nn 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 n−1n-1 edges). Hence Tn=T∗T_n = T^*, an MST. ■\blacksquare

∎

Worked examples

Example 1 — Kruskal's algorithm on an asymmetric graph. Consider vertices {A,B,C,D,E,F}\{A,B,C,D,E,F\} with weighted edges:

EdgeWeightA–B7A–C9A–F14B–C10B–D15C–D11C–F2D–E6E–F9\begin{array}{c|c} \text{Edge} & \text{Weight} \\\hline A\text{–}B & 7 \\ A\text{–}C & 9 \\ A\text{–}F & 14 \\ B\text{–}C & 10 \\ B\text{–}D & 15 \\ C\text{–}D & 11 \\ C\text{–}F & 2 \\ D\text{–}E & 6 \\ E\text{–}F & 9 \end{array}

Sort edges by weight: C–F(2), D–E(6), A–B(7), A–C(9), E–F(9), B–C(10), C–D(11), A–F(14), B–D(15)C\text{–}F(2),\ D\text{–}E(6),\ A\text{–}B(7),\ A\text{–}C(9),\ E\text{–}F(9),\ B\text{–}C(10),\ C\text{–}D(11),\ A\text{–}F(14),\ B\text{–}D(15). Process in order, tracking components via union–find, needing exactly 6−1=56-1=5 edges: 1. C–FC\text{–}F, weight 22: components {C,F}\{C,F\} separate — add. Forest: {C–F}\{C\text{–}F\}. 2. D–ED\text{–}E, weight 66: separate — add. Forest: {C–F,D–E}\{C\text{–}F, D\text{–}E\}. 3. A–BA\text{–}B, weight 77: separate — add. Forest: {C–F,D–E,A–B}\{C\text{–}F, D\text{–}E, A\text{–}B\}. 4. A–CA\text{–}C, weight 99: AA is in {A,B}\{A,B\}, CC is in {C,F}\{C,F\} — separate — add. Now {A,B,C,F}\{A,B,C,F\} merged. 5. E–FE\text{–}F, weight 99 (tie with previous, order fixed as listed): EE in {D,E}\{D,E\}, FF in {A,B,C,F}\{A,B,C,F\} — separate — add. Now {A,B,C,D,E,F}\{A,B,C,D,E,F\} 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: BB–CC (both in the same component) would form a cycle; CC–DD, AA–FF, BB–DD likewise close cycles.

MST edges: C–F(2), D–E(6), A–B(7), A–C(9), E–F(9)C\text{–}F(2),\, D\text{–}E(6),\, A\text{–}B(7),\, A\text{–}C(9),\, E\text{–}F(9).

Total weight: 2+6+7+9+9=332+6+7+9+9 = 33. Sanity check via the Cut Property: the cut isolating {C,F}\{C,F\} from the rest has crossing edges AA–C(9)C(9), AA–F(14)F(14), BB–C(10)C(10), EE–F(9)F(9) — minimum is a tie at 99 but that's irrelevant here since CC–FF itself (weight 22) is internal to the pair, so this cut only concerns how {C,F}\{C,F\} attaches to the rest, consistent with AA–C(9)C(9) being chosen.

Example 2 — Prim's algorithm on the same graph, started at DD. Maintain V(T)V(T), and at each step scan all edges from V(T)V(T) to V∖V(T)V \setminus V(T), picking the minimum.

  • V(T)={D}V(T) = \{D\}. Crossing edges: DD–E(6)E(6), CC–D(11)D(11), BB–D(15)D(15). Minimum: DD–E(6)E(6). Add it.
  • V(T)={D,E}V(T) = \{D,E\}. Crossing edges: CC–D(11)D(11), BB–D(15)D(15), EE–F(9)F(9). (Note DD–EE no longer crosses.) Minimum: EE–F(9)F(9). Add it.
  • V(T)={D,E,F}V(T) = \{D,E,F\}. Crossing edges: CC–D(11)D(11), BB–D(15)D(15), CC–F(2)F(2), AA–F(14)F(14). Minimum: CC–F(2)F(2). Add it.
  • V(T)={C,D,E,F}V(T) = \{C,D,E,F\}. Crossing edges: BB–D(15)D(15), AA–F(14)F(14), AA–C(9)C(9), BB–C(10)C(10). Minimum: AA–C(9)C(9). Add it.
  • V(T)={A,C,D,E,F}V(T) = \{A,C,D,E,F\}. Crossing edges: BB–D(15)D(15), AA–B(7)B(7), BB–C(10)C(10). Minimum: AA–B(7)B(7). Add it.
  • V(T)={A,B,C,D,E,F}V(T) = \{A,B,C,D,E,F\} — all six vertices reached, stop.

MST edges: D–E(6), E–F(9), C–F(2), A–C(9), A–B(7)D\text{–}E(6),\, E\text{–}F(9),\, C\text{–}F(2),\, A\text{–}C(9),\, A\text{–}B(7).

Total weight: 6+9+2+9+7=336+9+2+9+7 = 33. Both algorithms, starting from entirely different strategies (global sorted scan vs. local frontier growth) and different natural orderings, land on the same total weight 3333 — indeed the identical edge set, confirming the MST is unique here (all weights are distinct: 2,6,7,9,9,10,11,14,152,6,7,9,9,10,11,14,15 — wait, 99 appears twice, on AA–CC and EE–FF; 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).

CounterexampleWhy connectivity cannot be dropped

The definition and both theorems assume GG is connected. Drop this hypothesis and consider

G=({A,B,C,D},{A–B (w=1), C–D (w=1)}),G = (\{A,B,C,D\}, \{A\text{–}B \ (w=1),\ C\text{–}D\ (w=1)\}),

two disjoint edges with no connection between {A,B}\{A,B\} and {C,D}\{C,D\}. 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 {A–B,C–D}\{A\text{–}B, C\text{–}D\} with two components — this is a minimum spanning forest, not a spanning tree, and the correctness theorem's conclusion ("TT has n−1n-1 edges and is a spanning tree") is false: here TT has 22 edges, not n−1=3n - 1 = 3. Prim's algorithm fails even more starkly: started at AA, it grows T1={A},T2={A,B}T_1=\{A\}, T_2=\{A,B\}, and then gets stuck — no edge crosses the cut ({A,B},{C,D})(\{A,B\}, \{C,D\}), so the algorithm as stated cannot proceed to T3T_3, exactly because the "well-definedness" step of the proof invoked connectivity of GG 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

SituationWhat happens
All weights distinctMST is unique (Cut Property forces every edge)
Some weights tiedMST may be non-unique; multiple correct outputs possible, all with equal total weight
Negative weights allowedTheorems still hold — nothing in the proofs used w≥0w \ge 0
Disconnected graphNo 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-loopsDiscard — 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
JB
Joseph Bernard Kruskal Jr.
1928–2010 · Bell Labs and Rutgers University, Cold War–era discrete mathematics

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

History
- 1926: Otakar Borůvka publishes the earliest known MST algorithm, motivated by efficient electrical grid design in Moravia.
- 1930: Borůvka's method is refined and rediscovered independently in various forms over subsequent decades.
- 1956: Joseph Kruskal publishes the sorted-edge greedy algorithm and its correctness proof via matroid-flavored exchange arguments.
- 1957: Robert C. Prim publishes an independent vertex-growth algorithm at Bell Labs; it is later recognized as a rediscovery of a 1930 idea of Vojtěch Jarník.
- 1984–present: Introduction of efficient union–find data structures with near-constant amortized cost (Tarjan and others) brings Kruskal's algorithm to near-linear practical running time.
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 w(e∗)<w(f)w(e^*) < w(f), 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 EE edges in O(Elog⁡E)O(E\log E) time, then processes them with a union–find (disjoint-set) structure supporting near-constant amortized operations, for total O(Elog⁡E)O(E \log E), which is O(Elog⁡V)O(E\log V) since E≤V2E \le V^2. Prim's algorithm, implemented with a binary heap priority queue, runs in O(Elog⁡V)O(E\log V); with a Fibonacci heap it improves to O(E+Vlog⁡V)O(E + V\log V). Prim's is generally preferred for dense graphs (close to E≈V2E \approx V^2) 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.

Before this

Where this leads