mediumGraphMSTGraphs

Minimum Cost to Connect All Points

mediumTime: O(n² log n)Space: O(n)

Signals to notice

connect all points with minimum total wirecomplete graphMST on coordinates

Brute force first

Try all possible spanning trees — exponential. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

The key insight

Compute all pairwise Manhattan distances as edges. Apply Prim's (min-heap) or Kruskal's (sort + union-find). Prim's is better here since the graph is dense (complete). Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

What must stay true

The minimum cost to connect all points is the MST weight. Manhattan distance |x1-x2| + |y1-y2| is the edge weight between any two points. As long as that statement keeps holding, you can trust the steps built on top of it.

Easy way to go wrong

Using Kruskal's on a complete graph — that's for sorting all edges. Prim's with a visited array avoids generating all edges upfront. The fix is usually to return to the meaning of each move, not just the steps themselves.

Graphs Pattern