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.
2
5
1 4
2 6
8 2
7 12
5 3
4
1 -4
8 6
-5 -3
0 18
23
44
Test Case 1 :
Connecting (1, 4) and (2, 6) will cost 3.
Connecting (2, 6) and (5, 3) will cost 5.
Connecting (5, 3) and (8, 2) will cost 4.
Connecting (8, 2) and (7, 3) will cost 11.
Total cost = 3 + 5 + 4 + 11 = 23, which is minimum.
Test Case 2 :
Connecting (1, -4) and (-5, -3) will cost 7.
Connecting (-5, -3) and (8, 6) will cost 17.
Connecting (8, 6) and (0, 18) will cost 20.
Total cost = 7 + 17 + 20 = 44, which is minimum.
2
1
8 -5
3
1004 -754
892 -69664
84510 0
0
153282
We have to find a subgraph so that all the vertices (points) are connected together without any cycles and with the minimum possible cost (weight).
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:
Algorithm:
Description of ‘DisjointSet’ class
private:
The private part of the ‘DisjointSet’ class will contain two data members:
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:
DisjointSet(n):
Description of ‘find’ function
The function will take one parameter:
find(x):
Description of ‘Union’ function
The function will take two parameters:
Union(u, v):
O((N ^ 2) * (log (N)), where ‘N’ is the size of ‘coordinates’ array.
The time complexity of inserting all the edges and the distance between them in ‘edges’ is O(N ^ 2) since two nested loops have been used.
The time complexity of building a min-heap using ‘edges’ is O(N).
At the end, for processing the edges, the while loop may run ‘N ^ 2’ times. As we have a complete graph, the total number of edges is ‘N * (N - 1) / 2’. So, we have to process every edge in the worst case. Also, in each iteration, the ‘find’ function is called, which can take O(log(N)) time in the worst case.
Hence, overall time complexity is O(N ^ 2) + O(N) + O((N ^ 2) * (log (N)) = O((N ^ 2) * (log (N)).
O(N ^ 2), where ‘N’ is the size of ‘coordinates’ array.
The ‘edges’ and ‘pq’ both require O(N ^ 2) space to store all the edges. The ‘ds’ requires O(N) space for the ‘parent’ and ‘rank’ array. Hence, overall space complexity is 2 * O(N ^ 2) + 2 * O(N) = O(N ^ 2).