Every connected graph has a spanning tree
Remove cycle edges until none remain; connectivity survives because a cycle edge is never a bridge.
A spanning tree reaches every vertex with n − 1 edges. When the edges are weighted, Kruskal's greedy algorithm finds the cheapest one, and its optimality is genuinely surprising.
Core ideas
The minimum spanning tree is the rare optimisation problem where the obvious greedy method is provably right.
Remove cycle edges until none remain; connectivity survives because a cycle edge is never a bridge.
Take the cheapest edge that does not close a cycle, repeatedly. The cut property is why this cannot go wrong.
“Greedy Algorithms” in Algorithms and Complexity shows greedy failing on coin change and on the travelling salesman. Minimum spanning trees are the exception, and the reason is structural.
The material
Existence, the minimum weight problem, and why greed works here.
A spanning tree of a connected graph G is a subgraph that is a tree and contains every vertex of G. It has n − 1 edges.
Every connected graph has a spanning tree.
If G is acyclic it is already a tree. Otherwise take any cycle and remove one of its edges; that edge is not a bridge (“Paths and Connectivity”), so the graph stays connected, and the number of edges has decreased.
Repeating terminates, since the edge count strictly decreases, and it ends with a connected acyclic subgraph on all the vertices — a spanning tree.
□
Breadth-first and depth-first search each produce one directly: the tree edges of a search from any vertex form a spanning tree of its component. So the object is available for free from “Breadth-First and Depth-First Search”.
In a weighted connected graph, a minimum spanning tree is a spanning tree of least total weight.
Let S be any nonempty proper subset of the vertices. The lightest edge with exactly one end in S belongs to some minimum spanning tree. If it is strictly lightest, it belongs to every one.
Let e be that lightest crossing edge and let T be a minimum spanning tree not containing it. Adding e to T creates exactly one cycle (“Trees”), and that cycle must cross the cut a second time, at some edge f ≠ e.
Swapping f for e keeps the graph spanning and acyclic, and since w(e) ≤ w(f) the total weight does not increase. So the result is also a minimum spanning tree, and it contains e.
□
Kruskal's correctness follows: whenever it adds an edge, that edge is the lightest crossing the cut between the component it is joining and the rest, so the cut property guarantees a minimum spanning tree containing everything chosen so far.
Prim's algorithm is the same theorem used differently: grow a single tree from one vertex, always adding the lightest edge leaving it. It is Dijkstra's shape with a different priority, and it is often faster on dense graphs.
The cut property is a strong structural fact: a locally cheapest choice is provably part of a global optimum. Most optimisation problems have no such property, and greed then fails — sometimes badly. Coin change with denominations {1, 3, 4} is greedy-wrong at 6, and the travelling salesman is greedy-wrong routinely.
The technical name for the structure that makes greed correct is a matroid, and the forests of a graph form one. “Greedy Algorithms” in Algorithms and Complexity discusses greedy algorithms without the theory; what matters here is that "greedy works" is a theorem about this problem, not a general method.
Kruskal sorts the edges — Θ(|E| log |E|) — and then tests each for a cycle using a union–find structure, at nearly constant cost per test. So the sort dominates. Prim with a heap runs in Θ((n + |E|) log n).
Interactive
Your graph, the course's algorithms
Method
Sort, then add whatever does not close a cycle.
This is the dominant cost and the only global step in the algorithm.
Skip it if both ends are already connected — that would close a cycle.
Or when the list is exhausted. Fewer than n − 1 means the graph was disconnected and you have a minimum spanning forest.
n − 1 edges reaching every vertex. Both are cheap checks and either failing means the input was disconnected.
Edges: AB 1, BC 2, CD 3, DE 4, AE 5, AC 6, BD 7.
Every edge was decided once and never revisited, which is what makes the algorithm greedy — and what makes its correctness surprising. The cut property is the theorem that licenses never looking back.
Take a weighted graph on six vertices and run the algorithm, saying for each rejected edge which two vertices were already connected. That justification is the correctness argument in miniature.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.