A spanning tree is a subgraph that connects all vertices of a connected, undirected graph without forming any cycle.
A minimum spanning tree, or MST, is a spanning tree with the minimum possible total edge weight.
This page focuses on spanning tree and MST concepts. Prim's and Kruskal's algorithms have their own dedicated detailed pages.
A spanning tree of a connected, undirected graph is a subgraph that includes every vertex of the original graph and is also a tree. Since it is a tree, it must be connected and must not contain any cycle.
The word spanning means all vertices are covered. The word tree means the selected edges connect those vertices without cycles. So a spanning tree is the simplest connected version of the original graph.
Suppose a graph has vertices A, B, C, and D with these edges:
| Edge | Weight |
|---|---|
| A - B | 4 |
| A - C | 3 |
| B - C | 2 |
| B - D | 5 |
| C - D | 7 |
The graph has 4 vertices, so every spanning tree must contain exactly 3 edges. Different valid choices are possible.
| Tree | Selected Edges | Total Weight | Valid? |
|---|---|---|---|
| T1 | A-B, B-C, B-D | 4 + 2 + 5 = 11 | Yes, all vertices connected and no cycle |
| T2 | A-C, B-C, B-D | 3 + 2 + 5 = 10 | Yes, all vertices connected and no cycle |
| T3 | A-B, A-C, C-D | 4 + 3 + 7 = 14 | Yes, all vertices connected and no cycle |
| Not a tree | A-B, A-C, B-C | 9 | No, vertex D is missing and A-B-C forms a cycle |
| Property | Explanation |
|---|---|
| Contains all vertices | No original vertex is excluded from the spanning tree |
| Has V - 1 edges | A tree with V vertices always contains V - 1 edges |
| No cycles | Adding any extra edge to a spanning tree creates a cycle |
| Connected | Removing any selected edge disconnects the tree |
| Can be multiple | One graph can have many different spanning trees |
| Uses original edges only | A spanning tree cannot invent a new edge that was not in the graph |
| Concept | Meaning | Can Have Cycles? | Must Include All Original Vertices? |
|---|---|---|---|
| Graph | General structure of vertices and edges | Yes | It is the original structure |
| Tree | Connected graph with no cycle | No | Not necessarily tied to another graph |
| Spanning Tree | Tree formed from a graph that covers all vertices | No | Yes |
When a connected, undirected graph has edge weights, every spanning tree has a total weight. A minimum spanning tree is the spanning tree whose total edge weight is as small as possible.
Every MST is a spanning tree, but not every spanning tree is an MST. The MST is the cheapest valid way to connect all vertices.
From the example graph, compare the valid spanning trees by total weight.
| Point | Spanning Tree | Minimum Spanning Tree |
|---|---|---|
| Meaning | Any tree that connects all vertices | The spanning tree with minimum total weight |
| Weights required? | No | Yes, weights are needed for comparison |
| Number of edges | V - 1 | V - 1 |
| Cycle allowed? | No | No |
| Can be many? | Yes | Yes, if equal weights allow ties |
| Main concern | Connectivity without cycles | Connectivity with least total cost |
The cut property is the main idea behind MST algorithms. If we divide the vertices into two non-empty groups, the lightest edge crossing between those groups is safe to include in some MST.
The cycle property says that if a cycle exists in a weighted graph, the heaviest edge in that cycle is not needed for an MST when it is strictly heavier than the alternatives.
| Graph Type | Spanning Tree Exists? | Reason |
|---|---|---|
| Connected undirected graph | Yes | All vertices can be connected |
| Disconnected graph | No single spanning tree | At least one vertex group is unreachable from another |
| Unweighted connected graph | Yes | Weights are not required for a normal spanning tree |
| Weighted connected graph | Yes | Also allows MST comparison |
| Directed graph | Not in the classical MST sense | Directed graphs use different structures such as arborescences |
For any spanning tree:
Number of edges in a spanning tree = V - 1
If V = 6:
Required spanning tree edges = 6 - 1 = 5
Spanning trees are useful whenever we need connectivity with no redundant cycles. MSTs are useful when each connection has a cost.
| Application | How It Helps |
|---|---|
| Network design | Connect all routers or computers with minimum cable cost |
| Road planning | Connect cities while minimizing construction cost |
| Electrical grids | Connect stations with minimum wiring |
| Clustering | Build an MST and remove heavy edges to form groups |
| Broadcasting | Send data to all nodes without repeated loops |
| Approximation algorithms | MSTs provide useful structure for harder optimization problems |
This page explains the concept. Use the dedicated pages below for full algorithm steps, dry runs, and code.
| Algorithm | Basic Idea | Dedicated Page |
|---|---|---|
| Kruskal's Algorithm | Sort edges and add the cheapest safe edge | daa/kruskals-minimum-spanning-tree |
| Prim's Algorithm | Grow one tree using the cheapest outgoing edge | daa/prims-minimal-spanning-tree |
edges = [(1, 2, 1), (2, 3, 2), (1, 3, 4)]
parent = {node: node for node in (1, 2, 3)}
def find(node):
while parent[node] != node:
parent[node] = parent[parent[node]]
node = parent[node]
return node
tree = []
for left, right, weight in sorted(edges, key=lambda edge: edge[2]):
root_left, root_right = find(left), find(right)
if root_left != root_right:
parent[root_right] = root_left
tree.append((left, right, weight))
print(tree)
[(1, 2, 1), (2, 3, 2)]
No. Every MST is a spanning tree, but only the spanning tree with the smallest total weight is a minimum spanning tree.
A spanning tree with V vertices always has exactly V - 1 edges.
Yes. If edge weights are repeated, multiple spanning trees may have the same minimum total weight.
Explore 500+ free tutorials across 20+ languages and frameworks.