Sub topic 2
Spanning Trees: Connecting Every Vertex with V-1 Edges
This chapter assumes you know basic graph terms (vertex, edge, path, cycle), can read an adjacency list, and can follow short Python functions. Edge weights do not matter yet. The goal here is to pin down what a spanning tree is, so the next chapter can ask which spanning tree is cheapest.
What a Spanning Tree Is
Take a connected, undirected graph G with V vertices. A spanning tree of G is a subset of its edges that:
- touches every vertex of G (it spans the graph), and
- forms a tree, meaning it is connected and has no cycles.
A spanning tree keeps exactly the edges needed to keep every vertex reachable. Removing any one of its edges disconnects something. Adding any other edge from G creates a cycle.
The V-1 Rule
Every tree on V vertices has exactly V-1 edges. To see why, start with V isolated vertices, which gives V components. Each edge that joins two different components reduces the count by one. Reaching a single component takes V-1 such edges. An edge whose endpoints are already in the same component would close a cycle, so a tree never contains one.
For a subgraph H that uses all V vertices, any two of these statements imply the third:
| Property | Meaning |
|---|---|
| Connected | Every vertex can reach every other vertex in H |
| Acyclic | H contains no cycle |
| V-1 edges | H has exactly V-1 edges |
This gives you a cheap test. If an edge set has V-1 edges and is connected, it is a spanning tree, and you never have to search for cycles.
A Small Example
The graph below has 4 vertices and 5 edges. The thick edges form one spanning tree with 4 - 1 = 3 edges. The dotted edges are left out.
This graph has more than one spanning tree. Swapping 1-2 for 0-2 also works, and so does swapping 1-3 for 2-3. A graph can have many spanning trees. The complete graph on n vertices has n^(n-2) of them (Cayley's formula), so for anything beyond tiny graphs you cannot list them all and pick the best.
Two Structural Facts You Will Reuse
These two facts come back when we justify Prim's and Kruskal's algorithms:
- Adding a non-tree edge creates exactly one cycle. The tree already holds a unique path between the edge's endpoints, and the new edge closes that path into a cycle.
- Removing a tree edge splits the tree into exactly two components. Each component is a tree, and the removed edge was the only link between them.
If you combine them, you get the edge exchange: add a non-tree edge, then remove any other edge on the cycle it created, and the result is a different spanning tree.
Checking a Candidate in Code
The function below tests whether an edge list is a spanning tree of a graph with vertices 0..n-1. It applies the V-1 rule, then checks connectivity with a breadth-first search.
from collections import deque
def is_spanning_tree(n, tree_edges):
# tree_edges: list of (u, v) pairs with vertices numbered 0..n-1
if len(tree_edges) != n - 1:
return False
adj = [[] for _ in range(n)]
for u, v in tree_edges:
adj[u].append(v)
adj[v].append(u)
seen = [False] * n
seen[0] = True
queue = deque([0])
while queue:
x = queue.popleft()
for y in adj[x]:
if not seen[y]:
seen[y] = True
queue.append(y)
return all(seen) # V-1 edges + connected implies acyclic
print(is_spanning_tree(4, [(0, 1), (1, 2), (1, 3)])) # True
print(is_spanning_tree(4, [(0, 1), (1, 2), (0, 2)])) # False: cycle, vertex 3 unreached
print(is_spanning_tree(4, [(0, 1), (2, 3)])) # False: only 2 edgesExercise
A connected graph has 10 vertices and 17 edges. How many edges must you delete to get a spanning tree, and does it matter which edges you delete?
Show answer
A spanning tree has 10 - 1 = 9 edges, so you delete 17 - 9 = 8 edges. The choice matters. Each deleted edge must lie on a cycle in the current graph. Deleting a bridge, an edge whose removal disconnects the graph, makes it impossible to reach a spanning tree.
Exercise
In the four-vertex example, start from the tree {0-1, 1-2, 1-3} and add edge 2-3. Which cycle forms, and which spanning trees can you get by removing one edge from it?
Show answer
The cycle is 1-2-3-1. Removing 2-3 gives back the original tree. Removing 1-2 gives {0-1, 1-3, 2-3}, and removing 1-3 gives {0-1, 1-2, 2-3}. Both are valid spanning trees.