A greedy algorithm that is provably optimal — Kruskal's, and the union-find data structure that makes it fast.
For any cut of the vertices, the lightest crossing edge is in some MST. This is the entire proof that the cheapest available edge can never be a mistake.
Answers "are these two endpoints already connected?" in near-constant time — the question Kruskal asks on every edge.
Flatten chains to the root on every lookup; hang the shorter tree under the taller. Together: inverse-Ackermann, effectively constant.
Connectivity checks are ~free, so the real cost of Kruskal's is the sort: O(E log E). Not the tree, the ordering.
List every edge in ascending weight order. This is the step that pays for the whole algorithm.
For each edge, ask union-find: are its endpoints in the same component yet?
Different components → UNION the two trees, keep the edge. Same component → accepting it would create a cycle, so skip it. By the cut property, every accepted edge is in some MST.
A tree on V vertices has exactly V − 1 edges. The moment you have them, you're done — no revisiting, no second pass.
An MST minimises the total weight of the whole structure, and says nothing about the distance between any particular pair. On a square with three sides of weight 1 and a diagonal of 1.5, the MST drops the diagonal: the MST path between those corners costs 2, while the direct 1.5 edge was sitting there the whole time. If you want distances, you want Dijkstra. If you want the cheapest set of links that keeps everything reachable, you want an MST. (The diagonal has to weigh under 2 for this to bite — at exactly 2 both routes tie and the example proves nothing.)
When several edges share a weight, multiple MSTs of identical total cost can exist. Two correct implementations can return different trees — so a test that asserts a specific edge set will fail for no real reason. Assert the total weight, not the edges.