Update appNew update is available. Click here to update.
Last Updated: 3 Apr, 2021

Minimum Cost to Connect All Points

Moderate
AmazonTata Consultancy Services (TCS)Oracle
+2 more companies

Problem statement

You are given an array, ‘COORDINATES’ that represents the integer coordinates of some points on a 2D plane. Your task is to find the minimum cost to make all the points connected where the cost of connecting two points: (x1, y1) and (x2, y2) is equal to the manhattan distance between them, i.e., |x1 - x2| + |y1 - y2|.

Note:

1) An element of the ‘COORDINATES’ array is a pair of ‘X' and ‘Y’ coordinates of a point, i.e., COORDINATES[i] =  (Xi, Yi).

2) |DISTANCE| represents the absolute value of distance.

3) All points are considered to be connected if there is exactly one simple path between two points.

4) According to Wikipedia, a simple path is a path in a plane that does not have repeating points.

Input Format

The first line of input contains an integer 'T' representing the number of test cases.

The first line of each test case contains an integer ‘N’ representing the number of points in the ‘COORDINATES’ array.

The next ‘N’ lines of each test case contain two space-separated integers representing the ‘X and ‘Y’ coordinates of a point.

Output Format:

For each test case, print a single line containing a single integer denoting the minimum cost to make all the points connected.

The output of each test case will be printed in a separate line.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.

Constraints:

1 <= T <= 5
1 <= N <= 1000
10 ^ -6 <= X, Y <= 10 ^ 6
All points are distinct.

Where ‘T’ is the number of test cases, ‘N’ is the number of points in the ‘COORDINATES’ array, ‘X’, ‘Y’ is the x and y coordinates of a point, respectively.

Time limit: 1 sec.

Approaches

01 Approach

The problem indirectly refers to finding the cost of the minimum spanning tree of the graph formed by given points.

By definition, a minimum spanning tree (MST) is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices, without any cycles and with the minimum possible total edge weight. For ‘n’ vertices, an MST has ‘n - 1’ edges.

The idea is to use Kruskal’s Algorithm to find the MST for this problem.

First, we will add all the edges (that can be formed by any two points) and their cost in a min-heap. Now, we will process each and every edge present in the min-heap one by one. Min heap ensures that edges are processed in non-decreasing order of their cost.

If the current edge in processing forms a cycle in the MST, then discard the edge; otherwise, include it in the MST. We will be adding the cost of edges included in the MST in an integer variable, ‘result’.

The process will be repeated till ‘n - 1’ edges are not included in the MST, where ‘n’ is the number of points in the ‘coordinates’ array. In the end, the ‘result’ will have the cost of MST so formed.

 

Note:

  1. We will use the Disjoint Set data structure to check for cycles in the MST build so far.
  2. After ‘n - 1’ edges are included in the MST, all points will be connected. So, there is no need for further processing.

 

Algorithm:

  • Declare two integer variables, ‘result’ and ‘count’, and initialize with zero. The ‘result’ will hold the cost of the MST, and ‘count’ will hold the number of edges included in the MST.
  • Create a ‘vector’ of ‘array’ of size 3 (in C++), ‘edges’ to store three elements, ‘distance’, ‘i’ and ‘j’ where ‘i’ and ‘j’ are vertices, and ‘distance’ is the distance between them.
  • Run a loop from i = 0 to n - 1.
    • Run a loop from j = i + 1 to n - 1.
      • Calculate the distance between the ‘i’th point and ‘j’th point present in the ‘coordinates’ array and store it in the variable ‘distance’.
      • Push {distance, i, j} in the ‘edges.
  • Build a min-heap, ‘pq’ by passing the contents of ‘edges’ in its constructor. The min heap will make the comparison according to ‘distance’.
  • Create an object ‘ds’ of ‘DisjointSet’ and pass ‘n’ as the parameter.
  • Run a loop while count < n - 1.
    • Extract the element with minimum ‘distance’ from ‘pq’ and store it in the variable, ‘topElement’. Then, pop the top element from ‘pq’.
    • From ‘topElement’, store the values of distance and vertices in variables ‘cost’, ‘u’ and ‘v’. (‘u’ and ‘v’ are vertices, and ‘cost’ is the distance/cost between them).
    • If ds.find(u) != ds.find(v), that shows that including the edge between ‘u’ and ‘v’ will not produce a cycle in the MST. The ‘find’ function finds the representative element of a set. (If the representative element of both vertices is same, it implies they are already present in the same set, and there is a path connecting them. So, adding them again will produce a cycle).
      • Add ‘cost’ in the ‘result’.
      • Include the edge between ‘u’ and ‘v’ in the MST by ds.Union(u, v), which takes the union of ‘u’ and ‘v’.
      • Increment count by 1.
  • At the end, return ‘result’.

 

Description of ‘DisjointSet’ class

private:

The private part of the ‘DisjointSet’ class will contain two data members:

  • parent: An integer array to store the parent of each vertex in the graph.
  • rank: An integer array to store the rank of each vertex in the graph.

public:

The private part of the ‘DisjointSet’ class will contain one constructor and two member functions:

 

Description of ‘DisjointSet’ constructor

The constructor will take one parameter:

  • n: An integer variable that represents the number of vertices in the graph (i.e., number of points in the ‘coordinates’ array).

DisjointSet(n):

  • Resize the size of the ‘parent’ and ‘rank’ array to ‘n’.
  • Run a loop from i = 0 to n - 1.
    • Assign ‘i’ to ‘parent[i]’ and set ‘rank[i]’ to zero.

 

Description of ‘find’ function

The function will take one parameter:

  • x: An integer variable that represents the vertex (element) whose representative element has to be searched.

find(x):

  • If parent[x] == x that shows that ‘x’ itself is the representative element. So, return ‘x’.
  • Else, call the ‘find’ function for ‘parent[x]’ and store the returned result in ‘parent[x]’.
  • Return ‘parent[x]’.

 

Description of ‘Union’ function

The function will take two parameters:

  • u: An integer variable that represents vertex in the graph (or point in ‘coordinates’ array).
  • v : Another integer variable that represents vertex in the graph (or point in ‘coordinates’ array).

Union(u, v):

  • Pass ‘u’ to the ‘find’ function and store the returned value in an integer variable, ‘uRep’.
  • Pass ‘v’ to the ‘find’ function and store the returned value in an integer variable, ‘vRep’.
  • If uRep == vRep that shows ‘u’ and ‘v’ are already in the same set. So, return.
  • Else if rank[uRep] < rank[vRep] that shows that the rank of ‘uRep’ is less than the rank of ‘vRep’.
    • Update parent of ‘uRep’ by ‘vRep’.
  • Else if rank[uRep] > rank[vRep] that shows that the rank of ‘uRep’ is more than the rank of ‘vRep’.
    • Update parent of ‘vRep’ by ‘uRep’.
  • Else rank of ‘uRep’ and ‘vRep’ are same.
    • Update parent of ‘uRep’ by ‘vRep’.
    • Increment rank of ‘vRep’ by 1.