← capítulo

Minimum spanning trees: Prim

Connect every node as cheaply as possible. Prim = Dijkstra with the EDGE WEIGHT on the heap, not the distance.

Grow the tree across the cut

Watch the tree grow

Green = in tree · dashed blue = cut candidates · orange = cheapest edge chosen. Total climbs by the smallest possible amount each step → 15.

"Minimum" is worth 3×

Same connectivity, same V−1 edges: arbitrary tree ~3× heavier than the MST.

Why greedy is optimal: the cut property

Takeaway

One heap-key change turns shortest paths into cheapest connections. The math of cheap networks: grids, cables, pipes, clustering, TSP bounds. Next: Kruskal builds the same tree the opposite way — with union-find.