Skip to content

Chapter 31. Minimum Spanning Trees

A minimum spanning tree is a spanning tree of least total edge weight.

The setting is a connected undirected graph whose edges have weights. The graph may describe a road system, a communication network, a circuit, a clustering problem, or any structure where connecting vertices has a cost. A spanning tree connects all vertices without cycles. A minimum spanning tree chooses such a tree with minimum total cost.

Minimum spanning trees are among the basic examples of greedy methods in graph algorithms. Kruskal’s algorithm and Prim’s algorithm both build a minimum spanning tree by repeatedly choosing locally cheapest safe edges. Their correctness depends on the cut property and the cycle property.

31.1 Weighted Graphs

Let

G=(V,E) G = (V,E)

be a connected undirected graph. A weight function is a function

w:ER. w : E \to \mathbb{R}.

For an edge

eE, e \in E,

the value

w(e) w(e)

is called the weight, cost, or length of ee.

If TT is a spanning tree of GG, then the weight of TT is

w(T)=eE(T)w(e). w(T)=\sum_{e\in E(T)} w(e).

The goal is to find a spanning tree whose total weight is as small as possible.

31.2 Definition

Let G=(V,E)G=(V,E) be a connected weighted graph.

A minimum spanning tree of GG is a spanning tree TT such that

w(T)w(T) w(T) \leq w(T')

for every spanning tree TT' of GG.

The abbreviation MST is commonly used for minimum spanning tree.

If GG has nn vertices, every spanning tree has exactly

n1 n-1

edges. Thus an MST minimizes the sum of weights among all connected, acyclic, spanning subgraphs.

31.3 Example

Consider the weighted graph with vertices

V={A,B,C,D} V=\{A,B,C,D\}

and weighted edges:

EdgeWeight
ABAB11
ACAC44
ADAD33
BCBC22
BDBD55
CDCD66

One spanning tree is

{AB,AC,AD}, \{AB,AC,AD\},

with total weight

1+4+3=8. 1+4+3=8.

Another spanning tree is

{AB,BC,AD}, \{AB,BC,AD\},

with total weight

1+2+3=6. 1+2+3=6.

The second tree has smaller total weight. In fact, it is a minimum spanning tree.

31.4 Existence

Every finite connected weighted graph has at least one minimum spanning tree.

This follows from two facts. First, every finite connected graph has at least one spanning tree. Second, a finite graph has only finitely many spanning trees. Therefore the set of possible spanning tree weights is finite and has a minimum element.

If the graph is disconnected, no spanning tree exists. In that case one may instead seek a minimum spanning forest, consisting of one minimum spanning tree in each connected component.

31.5 Uniqueness

A graph may have more than one minimum spanning tree.

For example, if all edge weights are equal, then every spanning tree is a minimum spanning tree.

However, distinct edge weights guarantee uniqueness.

Theorem 31.1

If all edge weights in a connected graph are distinct, then the graph has a unique minimum spanning tree.

Proof

Suppose, for contradiction, that there are two different minimum spanning trees T1T_1 and T2T_2.

Choose the edge

eT1T2 e \in T_1 \setminus T_2

of smallest weight among all edges that belong to exactly one of the two trees.

Add ee to T2T_2. Since T2T_2 is a tree, this creates exactly one cycle. That cycle contains ee. Since T1T_1 has no cycle, the cycle must contain some edge

fT2T1. f \in T_2 \setminus T_1.

By the choice of ee and the distinctness of edge weights,

w(e)<w(f). w(e) < w(f).

Remove ff from T2+eT_2+e. The result is another spanning tree, but its total weight is smaller than w(T2)w(T_2). This contradicts the assumption that T2T_2 was minimum.

Therefore the minimum spanning tree is unique.

31.6 The Cut Property

A cut partitions the vertex set into two nonempty parts:

SandVS. S \quad\text{and}\quad V\setminus S.

An edge crosses the cut if one endpoint lies in SS and the other endpoint lies in VSV\setminus S.

The cut property says that a lightest edge crossing a cut is safe to use in a minimum spanning tree. In the strict form, if the lightest edge across the cut is unique, then it belongs to every MST.

Theorem 31.2

Let GG be a connected weighted graph. Let ee be a unique minimum-weight edge crossing some cut. Then every minimum spanning tree of GG contains ee.

Proof

Let the cut be

(S,VS). (S,V\setminus S).

Suppose an MST TT does not contain ee. Add ee to TT. This creates exactly one cycle.

The edge ee crosses the cut, so the cycle must cross the cut again at some other edge ff. Since ee is the unique minimum-weight edge crossing the cut,

w(e)<w(f). w(e) < w(f).

Remove ff. The graph remains connected and spanning, and it has the same number of edges as a tree. Hence it is a spanning tree. Its weight is smaller than w(T)w(T), contradicting the minimality of TT.

Therefore every MST contains ee.

31.7 The Cycle Property

The cycle property gives the dual rule.

If an edge is strictly heavier than every other edge on a cycle, then it cannot belong to any minimum spanning tree.

Theorem 31.3

Let CC be a cycle in a connected weighted graph. If ee is the unique maximum-weight edge on CC, then no minimum spanning tree contains ee.

Proof

Suppose an MST TT contains ee.

Remove ee from TT. The tree splits into two connected components. Since CC was a cycle, there is another edge ff on CC connecting these two components.

Because ee is the unique maximum-weight edge on CC,

w(f)<w(e). w(f) < w(e).

Add ff to TeT-e. The result is a spanning tree of smaller total weight. This contradicts the assumption that TT was minimum.

Therefore ee belongs to no MST.

31.8 Kruskal’s Algorithm

Kruskal’s algorithm builds an MST by considering edges in increasing order of weight.

It starts with no edges. At each step, it adds the next lightest edge that does not create a cycle.

Algorithm

Input: a connected weighted graph G=(V,E)G=(V,E).

  1. Sort the edges by nondecreasing weight.
  2. Start with the empty set
F=. F=\emptyset.
  1. For each edge ee, in sorted order:

    • If adding ee to FF does not create a cycle, add ee.
    • Otherwise, discard ee.
  2. Stop when FF has

V1 |V|-1

edges.

The graph

(V,F) (V,F)

is a minimum spanning tree.

Kruskal’s algorithm is usually implemented with a disjoint-set data structure. The disjoint-set structure tracks which connected component each vertex currently belongs to. An edge is accepted exactly when its endpoints lie in different components.

31.9 Correctness of Kruskal’s Algorithm

The correctness of Kruskal’s algorithm follows from the cut property.

At any step, the accepted edges form a forest. Suppose Kruskal’s algorithm considers an edge

e={u,v} e=\{u,v\}

and accepts it. Then uu and vv lie in different components of the current forest.

Let SS be the vertex set of the component containing uu. The edge ee crosses the cut

(S,VS). (S,V\setminus S).

Since the algorithm considers edges in increasing order, no lighter edge crossing this cut is available without already having been considered. Any lighter crossing edge would have joined two different components and would have been accepted earlier.

Thus ee is a lightest safe edge across a cut. By the cut property, it can be included in some minimum spanning tree.

Repeating this argument for each accepted edge shows that all accepted edges can be extended to an MST. When the algorithm has accepted n1n-1 edges, it has a spanning tree, and that tree is minimum.

31.10 Prim’s Algorithm

Prim’s algorithm grows one tree outward from a starting vertex.

It starts with a single vertex. At each step, it adds the lightest edge connecting the current tree to a vertex outside the tree.

Algorithm

Input: a connected weighted graph G=(V,E)G=(V,E).

  1. Choose a starting vertex ss.
  2. Set
S={s}. S=\{s\}.
  1. Repeatedly choose a minimum-weight edge crossing the cut
(S,VS). (S,V\setminus S).
  1. Add the chosen edge and its outside endpoint to the tree.
  2. Stop when
S=V. S=V.

The selected edges form a minimum spanning tree.

Prim’s algorithm is usually implemented with a priority queue, where each outside vertex is keyed by the cheapest edge currently connecting it to the growing tree.

31.11 Correctness of Prim’s Algorithm

Prim’s algorithm is a direct application of the cut property.

At every step, the current set SS contains the vertices already included in the growing tree. The algorithm chooses a lightest edge crossing the cut

(S,VS). (S,V\setminus S).

By the cut property, such an edge is safe. Adding it preserves the possibility of completing the selected edges to an MST.

After n1n-1 edge additions, all vertices have been included. The selected edges form a spanning tree. Since every selected edge was safe, the resulting spanning tree is minimum.

31.12 Kruskal Compared with Prim

Kruskal’s algorithm and Prim’s algorithm solve the same problem by different growth patterns.

FeatureKruskal’s algorithmPrim’s algorithm
Growth patternBuilds a forestBuilds one tree
Main choiceLightest edge avoiding cyclesLightest edge leaving current tree
Data structureDisjoint-set unionPriority queue
Natural forSparse edge listsAdjacency lists with heaps
Correctness principleCut propertyCut property

Kruskal’s algorithm is global: it looks at all edges in increasing order. Prim’s algorithm is local: it expands from the current tree boundary.

31.13 Time Complexity

Let

n=V,m=E. n=|V|, \qquad m=|E|.

With sorting and disjoint-set union, Kruskal’s algorithm runs in

O(mlogm). O(m\log m).

Since

mn2 m \leq n^2

in a simple graph, this is also commonly written as

O(mlogn). O(m\log n).

Prim’s algorithm depends on the representation and priority queue. With adjacency lists and a binary heap, it runs in

O(mlogn). O(m\log n).

With a Fibonacci heap, the theoretical bound is

O(m+nlogn). O(m+n\log n).

In practice, simpler priority queues are often preferred unless the graph size and density justify the more complex implementation.

31.14 Negative Edge Weights

Minimum spanning tree algorithms allow negative edge weights.

This differs from some shortest-path algorithms, where negative weights may cause difficulty. For MSTs, only the relative order of edges matters. Kruskal’s and Prim’s algorithms still work when weights are negative, zero, or positive.

A negative edge is simply attractive to include, provided it does not violate the tree structure.

31.15 Minimum Bottleneck Spanning Trees

The bottleneck of a spanning tree is the maximum edge weight in the tree.

A minimum bottleneck spanning tree minimizes this maximum edge weight.

Every minimum spanning tree is a minimum bottleneck spanning tree. However, a minimum bottleneck spanning tree need not be a minimum spanning tree.

The distinction is important. MST minimizes total cost. Bottleneck optimization minimizes the worst selected edge.

31.16 Applications

Minimum spanning trees appear in many settings where a network must connect all sites at low cost.

ApplicationInterpretation
Network designConnect all locations with minimum total link cost
ClusteringRemove large MST edges to form clusters
Image segmentationGroup pixels or regions by similarity
Approximation algorithmsBuild low-cost structures for harder problems
Circuit designConnect terminals with short total wiring
TaxonomyBuild similarity-based hierarchical structure

In each case, the MST gives a sparse global connection pattern.

31.17 Limitations

A minimum spanning tree minimizes total edge weight, but it does not necessarily minimize distances between pairs of vertices.

For example, replacing a dense network by an MST may make some routes much longer. An MST has no redundant paths, so it is fragile: removing any edge disconnects it.

Thus MSTs are appropriate when low total connection cost is the main objective. They are less appropriate when redundancy, fault tolerance, low latency, or high capacity are required.

31.18 Summary

A minimum spanning tree is a spanning tree with minimum total edge weight.

The two main structural principles are the cut property and the cycle property. The cut property identifies safe edges. The cycle property identifies edges that cannot be part of an MST.

Kruskal’s algorithm builds an MST by adding lightest cycle-free edges. Prim’s algorithm builds an MST by repeatedly adding the lightest edge leaving the current tree.

Both algorithms are greedy. Their correctness comes from the structure of cuts and cycles in weighted graphs.