Last Updated: April 5, 2026
This problem is asking us to find the Minimum Spanning Tree (MST) of a complete graph. Each point is a node, and the edge weight between any two nodes is their Manhattan distance. We want to connect all nodes with the minimum total edge cost, using exactly n-1 edges (where n is the number of points).
The fact that the problem says "exactly one simple path between any two points" is another way of saying "build a tree that spans all nodes." In graph theory, that is an MST.
So the real question becomes: how do we efficiently find the MST of a complete graph with up to 1000 nodes? With n points, there are n*(n-1)/2 possible edges, which for n=1000 means about 500,000 edges. Two classic algorithms handle this well: Kruskal's and Prim's.
1 <= points.length <= 1000 → With n up to 1000, we have up to ~500,000 edges. O(n^2) is 10^6, which is comfortable. O(n^2 log n) is also fine. This means both Kruskal's and Prim's will work.-10^6 <= xi, yi <= 10^6 → Coordinates can be large, so Manhattan distances can be up to 4 * 10^6. We need int (not short) to store edge weights.All pairs are distinct → No duplicate points, so every edge has a positive weight.The most natural way to think about building an MST: list every possible edge, sort them by cost, and greedily pick the cheapest edge that connects two components that aren't already connected. This is Kruskal's algorithm.
For this problem, each pair of points forms an edge with weight equal to their Manhattan distance. With n points, we get n*(n-1)/2 edges. We sort all edges by weight, then iterate through them. For each edge, if the two endpoints belong to different connected components, we include the edge and merge the components. We stop once we've added n-1 edges.
The key data structure here is Union-Find (also called Disjoint Set Union). It lets us check "are these two points in the same component?" and "merge these two components" both in nearly O(1) amortized time using path compression and union by rank.
This approach works but stores and sorts all n*(n-1)/2 edges upfront. What if we grew the tree one node at a time, only looking at edges to unvisited nodes?
Instead of sorting all edges upfront, Prim's algorithm grows the MST one node at a time. Start from any node, and at each step, pick the cheapest edge that connects a node already in the tree to a node not yet in the tree.
Prim's algorithm relies on the cut property of MSTs: the cheapest edge crossing any cut between "in-tree" and "not-in-tree" nodes must be part of the MST. The min-heap efficiently gives us the cheapest crossing edge at each step.
We never need to see all edges at once. As the tree grows, old candidate edges to newly visited nodes become irrelevant and are lazily discarded when popped from the heap.
The heap accumulates duplicate entries for the same node, bloating it to O(n^2) size. What if we tracked the minimum edge cost to each unvisited node directly in an array?
For dense graphs like this one (where every pair of nodes has an edge), using a heap actually hurts. The heap grows to O(n^2) entries, and each operation is O(log n). A simpler approach: maintain an array minDist where minDist[j] is the cheapest edge from any node in the MST to node j.
At each step, scan the minDist array to find the unvisited node with the smallest distance. Add it to the tree, then update minDist for all remaining unvisited nodes. This is O(n) per step, and we do n-1 steps, giving O(n^2) total.
minDist[j] = Manhattan distance from point 0 to point j for all j. Set minDist[0] = -1 to mark node 0 as visited (in the MST).u with the smallest minDist[u].minDist[u] to the total cost.u as visited by setting minDist[u] = -1.v, update minDist[v] = min(minDist[v], Manhattan distance from u to v).