"You have a town of houses and a price to lay cable between any pair. What's the cheapest wiring that reaches every house? That's a minimum spanning tree — and greedily grabbing the cheapest safe edges actually gives the optimal answer."
What an MST Is (and Isn't)
A minimum spanning tree of a weighted, connected graph is the cheapest set of edges that connects all the vertices — using exactly V−1 edges and forming a tree (no cycles). It answers 'what's the least-cost way to wire up the whole network?' Crucially, an MST is not about the shortest path between two specific nodes; it minimizes the total edge weight to keep everything connected. The route between two particular houses in an MST might be longer than necessary — that's fine, because the goal is cheap global connectivity, not fast point-to-point travel.
Kruskal's Algorithm: Cheapest Edges First
Kruskal's is beautifully greedy and reuses last lesson's tool: sort all edges by weight, then add them cheapest-first, skipping any edge whose two endpoints are already connected (that would form a cycle). 'Already connected?' is exactly the find question — so Union-Find is the engine: union the endpoints if they're in different sets, skip the edge if they share a root. Stop when you've added V−1 edges. The whole MST falls out of 'sort edges + union-find cycle check.' O(E log E) for the sort.
Prim's Algorithm: Grow From a Seed
Prim's takes the other angle: start from any one vertex and grow the tree outward, repeatedly adding the cheapest edge that connects the tree-so-far to a new vertex. 'Cheapest edge to a new vertex' is a min-heap query — so Prim's is to MST what Dijkstra is to shortest path, both powered by the priority queue from the Heaps track. Kruskal thinks in edges (sort them all); Prim thinks in growing a frontier (heap of crossing edges). Both produce a valid MST.
Why Greedy Works Here (It Usually Doesn't)
Greedy algorithms are often wrong — grabbing the locally best choice frequently misses the global optimum (you'll see this fail in the Paradigms track). MST is one of the celebrated cases where greedy is provably correct, thanks to the cut property: for any way you split the vertices into two groups, the single cheapest edge crossing the split is always safe to include in some MST. Both Kruskal and Prim are just disciplined ways of always taking a cheapest crossing edge. Beyond wiring networks, MSTs power clustering (build the MST, then cut its k−1 most expensive edges to get k natural clusters) and approximate solutions to the traveling-salesman problem.