1584. Min Cost to Connect All Points (Prim)¶
Edited by Charles
Medium · Python · Graph
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
This problem asks us to connect all points with the minimum possible total cost.
Original study notes follow, including intermediate snippets and earlier attempts.
Idea¶
This problem asks us to connect all points with the minimum possible total cost.
The cost of connecting two points is their Manhattan distance:
We need to:
- Connect all points.
- Avoid unnecessary edges.
- Minimize the total edge cost.
This is a classic Minimum Spanning Tree (MST) problem.
We can solve it using Prim's Algorithm.
Prim's Algorithm¶
Prim's algorithm builds one Minimum Spanning Tree by gradually adding nodes.
The basic idea is:
Start with one node
↓
Find the cheapest unvisited node
that can connect to the current MST
↓
Add it to the MST
↓
Update the connection costs
of the remaining nodes
↓
Repeat until all nodes are included
Unlike Kruskal's algorithm, Prim focuses on adding nodes rather than sorting and selecting edges.
Key Data Structures¶
We need two arrays:
visit[i]¶
means point i has already been added to the MST.
means point i has not been added yet.
minDist[i]¶
This is the most important part of the algorithm.
represents:
The minimum cost required to connect point
ito any point already inside the current MST.
For example, suppose the current MST contains:
Then:
is:
So minDist[i] is not simply the distance from i to the most recently added point.
It is the minimum distance from i to the entire current MST.
Initialization¶
We initialize:
Then:
Why?
Prim's algorithm can start from any node.
We choose point 0 as the starting point.
Since the first point does not need an edge to connect to the MST:
Therefore:
This guarantees that point 0 will be selected first.
Step 1: Find the Next Node to Add¶
For every round:
Here, u represents:
The next node that will be added to the MST.
1is simply a sentinel value meaning:
Then we search through all nodes:
We only consider:
because nodes already inside the MST should not be selected again.
Among all unvisited nodes, we choose the one with the smallest:
This means we always choose the cheapest possible way to expand the current MST.
Why Do We Need u = -1?¶
At the beginning of each round, we have not selected any candidate yet.
Suppose:
Initially:
When we encounter point 1:
so we first set:
Then point 2 is considered:
Since:
we update:
Point 3 has:
so u remains 2.
Therefore, point 2 is selected.
Step 2: Add u to the MST¶
Once we find the cheapest node:
visit[u] = True means:
And:
adds the cost of connecting u to the current MST.
Step 3: Update minDist¶
After adding u, this new node may provide a cheaper way to connect some remaining nodes.
Suppose:
We calculate their Manhattan distance:
Then:
This is very important.
We are comparing:
minDist[v]
↓
The cheapest connection from v
to the OLD MST
vs.
distance(u, v)
↓
The connection from v
to the NEWLY ADDED node u
So:
Because u has just joined the MST, distance(u, v) is now another possible way for v to connect to the MST.
Example¶
Consider:
Initially:
Round 1¶
The smallest minDist belongs to point 0.
Add point 0:
Now calculate distances from point 0:
Therefore:
Round 2¶
Among unvisited nodes:
The minimum is point 1.
Add it:
Now use point 1 to update the remaining nodes.
For example:
Previously:
Now:
This demonstrates the meaning of minDist:
Before:
point 3 ← 7 → current MST
After adding point 1:
point 3 ← 3 → point 1
Since point 1 is already in the MST:
minDist[3] becomes 3.
Why Is Prim Greedy?¶
At every step, we choose:
The cheapest possible edge that connects the current MST to a new node.
We never choose a more expensive connection if a cheaper valid connection is available.
Therefore, the MST grows like:
until all nodes are included.
Solution¶
Complexity Analysis¶
Let n be the number of points.
For each of the n rounds:
- We scan all
nnodes to find the minimumminDist. - We scan all
nnodes again to update their distances.
Therefore:
We use:
Therefore:
An important advantage of this implementation is that we do not explicitly construct all O(n²) edges.
Key Takeaway¶
The most important thing to understand is the meaning of:
It means:
The minimum cost to connect point
ito the current MST.
Therefore, Prim repeatedly performs three steps:
1. Select
Find the unvisited node u
with the smallest minDist[u]
↓
2. Add
visit[u] = True
res += minDist[u]
↓
3. Update
For every unvisited v:
minDist[v]
=
min(
minDist[v],
distance(u, v)
)
A simple way to remember Prim is:
Choose the cheapest node to connect → add it to the MST → use the new node to update the remaining connection costs.
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | August 25, 2026 |
Python solution¶
from typing import List
class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
n = len(points)
visit = [False] * n
minDist = [float('inf')] * n
minDist[0] = 0
res = 0
for _ in range(n):
# No node has been selected in this round
u = -1
# Find the unvisited node with the smallest minDist
for i in range(n):
if not visit[i] and (u == -1 or minDist[i] < minDist[u]):
u = i
# Add u to the MST
visit[u] = True
res += minDist[u]
# Update minDist for all unvisited nodes
x, y = points[u]
for v in range(n):
if not visit[v]:
a, b = points[v]
minDist[v] = min(
minDist[v],
abs(x - a) + abs(y - b)
)
return res