Skip to content
AI360Xpert
Beta
Difficulty: MediumAdvanced Graphs

Min Cost to Connect All Points

Problem in Plain English

Every pair of points can be joined at their Manhattan distance. Return the cheapest total cost that links all points into one connected network.

Problem Statement

You are given points where points[i] = [xi, yi] is a point on the 2D plane. Joining points [xi, yi] and [xj, yj] costs their Manhattan distance |xi - xj| + |yi - yj|.

Return the minimum cost to connect all points, meaning the joined points form exactly one connected network with a single simple path between any two points.

Constraints

  • 1 <= points.length <= 1000
  • -10^6 <= xi, yi <= 10^6
  • All (xi, yi) pairs are distinct.

Examples

Example 1

Input
points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
Output
20

Join (2,2) to (5,2) for 3, (0,0) to (2,2) for 4, (5,2) to (7,0) for 4, and (2,2) to (3,10) for 9, totalling 20.

Example 2

Input
points = [[3,12],[-2,5],[-4,1]]
Output
18

Join (-4,1) to (-2,5) for 6 and (-2,5) to (3,12) for 12, totalling 18.

Example 3

Input
points = [[-1000000,1000000]]
Output
0

One point is already connected. No link is needed, even when its coordinates are at their allowed extremes.

Example 4

Input
points = [[0,0],[1,0],[1,1],[0,1]]
Output
3

Any three sides of this square form a tree, each costing 1. Taking all four sides would cost 4 and create a cycle; equal-cost choices can produce different trees with the same minimum total.

Intuition

The network needs n - 1 links without cycles: adding a cycle creates two simple paths between some points and adds unnecessary positive cost. Treat each point as a vertex and each Manhattan distance as an edge weight. Example 1 connects points 0, 1, 3 and 4 for 11, then attaches point 2 for 9. The goal is a minimum spanning tree, which minimizes the sum of chosen links rather than the distance from one source to every point.

Approaches

Kruskal Sort All Edges

Solution Details

Reveal Kruskal Sort All Edges: intuition, complexity, and code

Hints

Hint 1
Joining all points as cheaply as possible is a minimum spanning tree over the complete graph of Manhattan links.
Hint 2
Kruskal takes the cheapest links first and skips ones joining already-connected points, which a disjoint set detects.
Hint 3
Because every pair of points links, growing one tree with a plain closest-distance array beats storing or sorting every pair.

Edge Cases

  • A single point, which needs zero links and returns 0.
  • Two points, where the answer is just their one Manhattan distance.
  • Points sharing x or y lines, which the absolute-value formula still prices correctly.
  • Equal distances allow several minimum trees, as in the square example, but the total stays the same.
  • Negative and extreme coordinates require absolute differences; distinct coordinates ensure every actual edge costs more than zero.

Common Mistakes and Interview Tips

  • Running unweighted search like plain BFS, which ignores link costs and returns the wrong total.
  • Forgetting the visited check when attaching the next point, which re-attaches tree points and corrupts the total.
  • Using Euclidean distance instead of the Manhattan absolute-difference sum required here.
  • Updating Prim distances with best + dist computes source paths instead of attachment costs and answers a different problem.
  • Taking every cheapest edge without a component check can create cycles before all vertices are connected.
  • Interview tip: justify the lightest-crossing-edge choice with a cut exchange argument, and explain why a dense graph favors array Prim over storing all pair edges.

Key Takeaway

On a dense complete graph, growing one tree with a linear closest-distance scan beats sorting every pair.