"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 is defined for a weighted undirected connected graph and uses V−1 edges to minimize total connection cost. A disconnected graph has a minimum spanning forest instead. This is not a pairwise shortest-path objective.
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)
The cut property says that a minimum-weight edge crossing a cut is safe for at least one MST. With ties, a particular minimum edge need not belong to every MST. MST-based approximation guarantees for TSP require extra premises such as a complete metric graph satisfying the triangle inequality.