Min Cost To Connect All Points

Solution For Min Cost To Connect All Points

Problem Statement:

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.

Example:

Input: points = [[0,0],[2,2],[3,10],[5,2],[7,0]] Output: 20
Explanation:
We can connect the points as follows:
– [0,0] connects [2,2] which connects [5,2] and [7,0] – [3,10] connects [5,2] The resulting cost is 2 + 8 + 10 = 20.

Solution:

The problem requires us to find the minimum cost to connect all points on a 2D plane. 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.

We can solve the problem using Kruskal’s Algorithm in the following way:

  • Create an empty list of edges.
  • Create a list of all pairs of points and calculate their distance. Add each pair with their respective distance to the list of edges.
  • Sort the list of edges in ascending order.
  • Create a list of disjoint sets for the points.
  • Traverse through the edges in the sorted list and check if the two points of an edge belong to the same disjoint set. If not, union the sets and add the edge to the final minimum spanning tree.
  • The total cost of the minimum spanning tree will be the minimum cost to connect all points.

The time complexity of the algorithm is O(N^2logN), where N is the number of points.

Implementation:

Here’s the implementation of the above algorithm:

“`
class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
def find(parent, i):
if parent[i] == i:
return i
return find(parent, parent[i])

    def union(parent, rank, x, y):
        xroot = find(parent, x)
        yroot = find(parent, y)
        if rank[xroot] < rank[yroot]:
            parent[xroot] = yroot
        elif rank[xroot] > rank[yroot]:
            parent[yroot] = xroot
        else:
            parent[yroot] = xroot
            rank[xroot] += 1

    n = len(points)
    edges = []
    for i in range(n):
        for j in range(i+1, n):
            distance = abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1])
            edges.append((distance, i, j))
    edges.sort()
    parent = [i for i in range(n)]
    rank = [0] * n
    mst = []
    cost = 0
    for edge in edges:
        if len(mst) == n-1:
            break
        distance, u, v = edge
        if find(parent, u) != find(parent, v):
            union(parent, rank, u, v)
            cost += distance
            mst.append(edge)

    return cost

“`

We loop through all pairs of points and calculate the distance. We create an edge list with all distances and sort it in ascending order. We use union-find to determine if 2 vertices are in the same family. If they are not, we calculate the cost of including the edge in the MST and add it to a total cost variable.

Step by Step Implementation For Min Cost To Connect All Points

class Solution {
    public int minCostConnectPoints(int[][] points) {
        // create a priority queue
        PriorityQueue pq = new PriorityQueue<>(
            (a, b) -> a.cost - b.cost
        );
        
        // create a disjoint set for all points
        int[] parent = new int[points.length];
        for (int i = 0; i < points.length; i++) {
            parent[i] = i;
        }
        
        // add all edges to the priority queue
        for (int i = 0; i < points.length; i++) {
            for (int j = i + 1; j < points.length; j++) {
                int cost = calculateCost(points[i], points[j]);
                pq.add(new Edge(i, j, cost));
            }
        }
        
        // track total cost
        int totalCost = 0;
        
        // process all edges in priority queue
        while (!pq.isEmpty()) {
            Edge edge = pq.poll();
            int u = edge.u;
            int v = edge.v;
            int cost = edge.cost;
            
            // check if edge can be added without creating a cycle
            if (find(parent, u) != find(parent, v)) {
                // add edge
                totalCost += cost;
                
                // union
                union(parent, u, v);
            }
        }
        
        return totalCost;
    }
    
    // union-find helper functions
    private int find(int[] parent, int u) {
        if (parent[u] == u) {
            return u;
        }
        return find(parent, parent[u]);
    }
    
    private void union(int[] parent, int u, int v) {
        int rootU = find(parent, u);
        int rootV = find(parent, v);
        parent[rootU] = rootV;
    }
    
    // calculate the cost of an edge
    private int calculateCost(int[] p1, int[] p2) {
        int dx = Math.abs(p1[0] - p2[0]);
        int dy = Math.abs(p1[1] - p2[1]);
        return dx + dy;
    }
    
    // edge class
    private class Edge {
        int u;
        int v;
        int cost;
        
        Edge(int u, int v, int cost) {
            this.u = u;
            this.v = v;
            this.cost = cost;
        }
    }
}
from typing import List 

class Solution: 
    def minCostConnectPoints(self, points: List[List[int]]) -> int: 
        # TODO: implement this function 
        pass
/**
 * @param {number[][]} points
 * @return {number}
 */
 
 // This is a minimum spanning tree problem
 // We can use Kruskal's algorithm or Prim's algorithm to solve it
 // Here we use Prim's algorithm
 
var minCostConnectPoints = function(points) {
    // Create a list of edges with weight
    let edges = [];
    for (let i = 0; i < points.length; i++) {
        for (let j = i + 1; j < points.length; j++) {
            let weight = Math.abs(points[i][0] - points[j][0]) + Math.abs(points[i][1] - points[j][1]);
            edges.push([weight, i, j]);
        }
    }
    
    // Sort the edges by weight in increasing order
    edges.sort((a, b) => a[0] - b[0]);
    
    // Create a list of visited nodes
    let visited = new Array(points.length).fill(false);
    
    // Keep track of the total cost
    let cost = 0;
    
    // Add the edges with the smallest weight until all nodes are visited
    for (let i = 0; i < edges.length; i++) {
        let edge = edges[i];
        let weight = edge[0];
        let from = edge[1];
        let to = edge[2];
        
        if (!visited[from] || !visited[to]) {
            cost += weight;
            visited[from] = true;
            visited[to] = true;
        }
        
        if (visited.every(node => node === true)) {
            break;
        }
    }
    
    return cost;
};
There are several ways to solve this problem. One way is to use a greedy algorithm.

We can sort the points by their x-coordinates. Then, we can connect each point to the point with the next smallest x-coordinate. This will give us a valid solution, but it may not be the optimal solution.

We can also use a more sophisticated algorithm, such as Kruskal's algorithm, to find the optimal solution.
using System; 

public class Solution { 

// This code calculates the minimum cost to connect all points in a 2D array. 

public int MinCostConnectPoints(int[][] points) { 

// We first need to calculate the distance between all points. 

int totalDistance = 0; 

for (int i = 0; i < points.Length - 1; i++) { 

for (int j = i + 1; j < points.Length; j++) { 

totalDistance += CalculateDistance(points[i], points[j]); 

} 

} 

// We then use a greedy algorithm to connect the points, 

// always choosing the point with the minimum distance to the "connected" points. 

int[] connectedPoints = new int[points.Length]; 

int connectedCount = 1; 

connectedPoints[0] = 1; 

while (connectedCount < points.Length) { 

int minDistance = int.MaxValue; 

int minPoint = 0; 

for (int i = 0; i < points.Length; i++) { 

if (connectedPoints[i] == 1) { 

continue; 

} 

for (int j = 0; j < connectedPoints.Length; j++) { 

if (connectedPoints[j] == 0) { 

continue; 

} 

int distance = CalculateDistance(points[i], points[j]); 

if (distance < minDistance) { 

minDistance = distance; 

minPoint = i; 

} 

} 

connectedPoints[minPoint] = 1; 

connectedCount++; 

} 

return totalDistance; 

} 

// This code calculates the Euclidean distance between two points. 

public int CalculateDistance(int[] point1, int[] point2) { 

return (int)Math.Sqrt(Math.Pow(point1[0] - point2[0], 2) + Math.Pow(point1[1] - point2[1], 2)); 

} 

}
Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]