Skip to main content

Min Cost to Connect All Points

Problem Description​

You are given an array points representing integer coordinates of some points on a 2D-plane, where points[i] = [xi, yi].

The cost of connecting two points [xi, yi] and [xj, yj] is the manhattan distance between them: |xi - xj| + |yi - yj|, where |val| denotes the absolute value of val.

Return the minimum cost to make all points connected. All points are connected if there is exactly one simple path between any two points.

Examples​

Example 1: image

Input: points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
Output: 20
Explanation:
![image](https://assets.leetcode.com/uploads/2020/08/26/c.png)
We can connect the points as shown above to get the minimum cost of 20.
Notice that there is a unique path between every pair of points.

Constraints​

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

Solution for Min Cost to Connect All Points​

Approach​

Prim's algorithm:​

  • Prim's algorithm is an algorithm for solving the optimization problem of finding the minimum spanning tree in a weighted connected graph within graph theory. A minimum spanning tree is a subset of the edges of the graph that forms a tree containing all vertices while minimizing the total weight of those edges.

Overview of the Algorithm:​

  • Calculate the distances between each pair of points and use Prim's algorithm to form the minimum spanning tree.
  • Start from an initial point, mark it as visited, and select the point with the smallest distance among the unvisited points.
  • Calculate the distances from the selected point to the unvisited points and store them in a cache.
  • Add the minimum cost edge to the priority queue using the distances from the cache.
  • Repeat the above steps until all points are visited, and calculate the minimum cost.

Specific Steps:​

Initial State:​

  • n: Number of points
  • min_cost: Minimum cost (initially 0) and return value
  • visited: A list to indicate if each point is visited (initially all False)
  • pq: Priority queue (initially (0, 0) indicating starting from point 0 with cost 0)
  • cache: Dictionary for caching distances (initially empty)

Each Step:​

  • Pop cost and point from pq (start from the initial point).
  • If the point is already visited, skip this point.
  • Otherwise, mark this point as visited and add the current cost to the minimum cost.
  • Calculate distances from this point to all unvisited points and store them in the cache. Update the cache if the new distance is smaller.
  • Add the point with the smallest distance among the unvisited points to the priority queue using distances from the cache.
  • Repeat steps 3 to 5 until all points are visited.
  • Return the final minimum cost.

Implementation​

Live Editor
function Solution(arr) {
var minCostConnectPoints = function(points) {
    let cost = 0;
    const n = points.length;
    const dist = Array(n).fill(Infinity);
    dist[0] = 0;
    let next = 0;

    for (let step = 1; step < n; step++) {
        let min = Infinity;
        let point = -1;
        for (let i = 1; i < n; i++) {
            if (dist[i] > 0) {
                dist[i] = Math.min(dist[i], Math.abs(points[i][0] - points[next][0]) + Math.abs(points[i][1] - points[next][1]));
                if (dist[i] < min) {
                    min = dist[i];
                    point = i;
                }
            }
        }
        cost += min;
        dist[point] = 0;
        next = point;
    }
    
    return cost;
};
  const input = [[0,0],[2,2],[3,10],[5,2],[7,0]]
  const output =minCostConnectPoints(input)
  return (
    <div>
      <p>
        <b>Input: </b>
        {JSON.stringify(input)}
      </p>
      <p>
        <b>Output:</b> {output.toString()}
      </p>
    </div>
  );
}
Result
Loading...

Complexity Analysis​

  • Time Complexity: O(n2∗log(n))O(n^2 * log(n))
  • Space Complexity: O(n) O(n)

Code in Different Languages​

Written by @hiteshgahanolia
 manhattDist(v1, v2) {
return Math.abs(v1[0] - v2[0]) + Math.abs(v1[1] - v2[1]);
}

minCostConnectPoints(points) {
const n = points.length;
const adj = Array.from({ length: n }, () => []);

for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
const dist = this.manhattDist(points[i], points[j]);
adj[i].push([j, dist]);
adj[j].push([i, dist]);
}
}

const pq = new MinPriorityQueue({ priority: x => x[0] });
const vis = Array(n).fill(false);
pq.enqueue([0, 0]);
let cost = 0;

while (!pq.isEmpty()) {
const [topEdgwWt, currNode] = pq.dequeue().element;

if (vis[currNode]) continue;
vis[currNode] = true;
cost += topEdgwWt;

for (const [adjPoint, edWt] of adj[currNode]) {
if (!vis[adjPoint]) {
pq.enqueue([edWt, adjPoint]);
}
}
}

return cost;
}

References​