Min Cost to Connect Points

Min Cost to Connect Points Problem

You are given a 2D integer array, points, where each points[i] = [xi, yi] represents a unique point on a 2D plane.

The cost of connecting two points, [xi, yi] and [xj, yj], is defined by their Manhattan distance: |xi – xj| + |yi – yj|.

Your task is to return the minimum cost required to connect all points so that there is exactly one path between every pair of points.

Min Cost to Connect Points Problem

Examples related to Min Cost to Connect Points

Example:

Min Cost to Connect Points Example

Constraints:

  • 1 <= points.length <= 1000
  • -1000 <= xi, yi <= 1000

Hints to solve Min Cost to Connect Points

Recommended Time & Space Complexity

Aim for a solution with a time complexity of O((n²) log n) and a space complexity of O(n²), where n is the number of points.

Hint 1:

  • This problem can be visualized as a graph, where points are nodes. The goal is to connect all nodes into a single component with the least cost.
  • Can you think of a graph algorithm that builds such a structure efficiently?

Hint 2:

  • First, calculate the edges by iterating through all pairs of points and computing the Manhattan distance between them. Then, sort the edges in ascending order of weights.

  • Finally, process these edges, connecting nodes using Union-Find and adding the edge’s weight to the total cost when it successfully forms part of the MST. The result will be the minimum cost.

Hint 3:

  • First, calculate the edges by iterating through all pairs of points and computing the Manhattan distance between them. Then, sort the edges in ascending order of weights.

  • Finally, process these edges, connecting nodes using Union-Find and adding the edge’s weight to the total cost when it successfully forms part of the MST. The result will be the minimum cost.

Methods to Solve Min Cost to Connect Points

There are mainly 3 approach to solve Min Cost to Connect Points problem:

  1. Kruskal’s Algorithm Method
  2. Prim’s Algorithm Method
  3. Prim’s Algorithm (Optimal)

1. Kruskal’s Algorithm Method

In this method, treat each point as a node and compute all possible edges with weights as the Manhattan distance.

Sort the edges, and use Union-Find to build a Minimum Spanning Tree (MST) by adding the smallest edges until all points are connected.

  • Time complexity: O((n²) log n)
  • Space complexity: O(n²)

Code:

2. Prim’s Algorithm Method

  • Start with any point as part of the MST. Use a priority queue to pick the smallest edge connecting the tree to a new point.

  • Continue until all points are included in the MST.

  • Time complexity: O((n²) log n)
  • Space complexity: O(n²)

Code:

3. Prim’s Algorithm (Optimal) Method

  • This version improves efficiency by using a min-heap and an adjacency list to track the smallest edge for each point. It reduces redundant edge calculations, making the process faster for larger datasets.

  • Time complexity: O(n²)
  • Space complexity: O(n)

Code:

More Articles