01 / 20

Spanning Trees &
Minimum Spanning Trees

Discover how to connect every vertex of a graph with the fewest edges and the lowest total weight: the foundation of network optimization.

Discrete Math

What Is a Graph?

A graph \( G = (V, E) \) consists of a set of vertices \( V \) (nodes) and a set of edges \( E \) (connections between nodes). Graphs model real-world networks: cities connected by roads, computers on a network, or circuit boards.

A B C D E \( |V| = 5 \), \( |E| = 7 \)

What Is a Tree?

A tree is a connected graph with no cycles. It is the simplest way to connect all vertices: removing any edge disconnects it, and adding any edge creates a cycle.

Connected
Every vertex is reachable from every other vertex.
Acyclic
No closed loops: there is exactly one path between any two vertices.
\( |E| = |V| - 1 \)
A tree on \( n \) vertices always has exactly \( n - 1 \) edges.

What Is a Spanning Tree?

A spanning tree of a connected graph \( G \) is a subgraph that includes all vertices of \( G \) and is itself a tree. It "spans" every vertex using exactly \( |V| - 1 \) edges, with no cycles.

Original Graph A B C D E → One Spanning Tree A B C D E 4 edges = 5 vertices − 1

Why Do Spanning Trees Matter?

Spanning trees appear everywhere in optimization. Whenever you need to connect all nodes at minimum cost, you need a spanning tree: network cabling, road planning, circuit design, and clustering algorithms all rely on them.

Network Design
Connect all computers in a building using the least amount of cable.
Transportation
Link every town with roads at the lowest construction cost.
Clustering
Remove the heaviest edges from an MST to partition data into clusters.

Weighted Graphs

A weighted graph assigns a numerical weight (cost, distance, time) to each edge. The total weight of a subgraph is the sum of all its edge weights:

A B C D E 4 8 2 7 3 6 5

Minimum Spanning Tree: Definition

Spanning
Includes every vertex of \( G \).
Tree
Connected and acyclic with exactly \( |V| - 1 \) edges.
Minimum
No other spanning tree has a smaller total weight.

The Cut Property

The cut property is the key theorem that justifies greedy MST algorithms. It guarantees that the lightest edge crossing any cut belongs to some MST.

S V \ S 1 2 3 4 9 7 2 ★

Spanning Tree vs. MST

Spanning Tree Minimum Spanning Tree
Vertices All \( |V| \) All \( |V| \)
Edges \( |V| - 1 \) \( |V| - 1 \)
Cycles None None
Weight Requirement No constraint Minimum possible
Uniqueness Many possible May not be unique
Needs Weights? No Yes

Kruskal's Algorithm

Kruskal's algorithm builds an MST by greedily adding the lightest available edge that does not create a cycle. It processes edges in order of increasing weight.

1
Sort all edges by weight in non-decreasing order.
2
Initialize an empty edge set \( T = \emptyset \).
3
For each edge (lightest first): add it to \( T \) if it does not form a cycle with existing edges in \( T \).
4
Stop when \( |T| = |V| - 1 \). The edges in \( T \) form the MST.

Prim's Algorithm

Prim's algorithm grows an MST from a single starting vertex, always adding the cheapest edge that connects the growing tree to a vertex not yet included.

1
Pick any starting vertex and add it to the tree set \( S \).
2
Among all edges from \( S \) to \( V \setminus S \), select the lightest.
3
Add that edge and its new endpoint to \( S \).
4
Repeat until \( S = V \). The chosen edges form the MST.

Example 1: The Graph

Find the MST of this weighted graph with 6 vertices and 9 edges.

A B C D E F 4 8 6 1 5 2 7 9 3

Example 1: Kruskal's Solution

Sorted edges: CF:1, EF:2, BF:3, AB:4, DE:5, AD:6, BE:7, BC:8, AE:9. We add each if no cycle forms.

A B C D E F 1 2 3 4 6 MST weight = 1 + 2 + 3 + 4 + 6 = 16

Example 2: Prim's on a 4-Vertex Graph

P Q R S 10 3 5 4 8
1
Start at P. Cheapest edge from {P}: PR = 3. Add R.
2
From {P, R}: edges PQ:10, PS:8, RS:4. Add S.
3
From {P, R, S}: edges PQ:10, SQ:5. Add Q. Done!

Example 3: Tie-Breaking

When multiple edges have the same weight, either can be chosen. This may produce different MSTs with the same total weight.

X Y Z W 5 5 3 3 4 7

Common Mistakes

✕ Creating Cycles
Adding an edge that connects two vertices already in the same component forms a cycle: the result is not a tree.
✕ Forgetting Vertices
Stopping too early. A spanning tree must include all \( |V| \) vertices and exactly \( |V| - 1 \) edges.
✕ Confusing Shortest Path with MST
The MST minimizes total edge weight, not the distance between any specific pair of vertices.

Kruskal's vs. Prim's

Kruskal's Prim's
Strategy Edge-centric (global sort) Vertex-centric (grow from one node)
Data Structure Union-Find Priority Queue
Time Complexity \( O(E \log E) \) \( O(E \log V) \)
Best For Sparse graphs Dense graphs
Output Same MST (or equal-weight MST) Same MST (or equal-weight MST)

Question 1 of 5

A connected graph has 8 vertices. How many edges does any spanning tree of this graph have?

Question 2 of 5

In Kruskal's algorithm, which edge is considered first?

Question 3 of 5

You are running Kruskal's algorithm. You pick the next lightest edge, but both its endpoints are already in the same connected component. What do you do?

Question 4 of 5

A graph has vertices {A, B, C, D} with edges AB:2, BC:3, CD:1, AD:4, BD:5. What is the total weight of the MST?

Question 5 of 5

Which statement about the cut property is correct?