mediumGraphMSTGraphs

Minimum Cost to Connect All Points

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

Recognize the pattern

connect all points with minimum total wirecomplete graphMST on coordinates

Brute force idea

The naive version of Minimum Cost to Connect All Points sounds like this: 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.

Better approach

The real unlock in Minimum Cost to Connect All Points comes when you notice this: 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.

Key invariant

The compass for Minimum Cost to Connect All Points is this: 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.

Watch out for

One easy way to drift off course in Minimum Cost to Connect All Points is this: 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