![]() |
VOOZH | about |
Prerequisites: Equation of circle when three points on the circle are given, Convex Hull
Given an array arr[][] containing N points in a 2-D plane with integer coordinates. The task is to find the centre and the radius of the minimum enclosing circle(MEC). A minimum enclosing circle is a circle in which all the points lie either inside the circle or on its boundaries.
Examples:
Input: arr[][] = {{0, 0}, {0, 1}, {1, 0}}
Output: Center = {0.5, 0.5}, Radius = 0.7071
Explanation:
On plotting the above circle with radius 0.707 and center (0.5, 0.5), it can be observed clearly that all the mentioned points lie either inside or on the circle.
Input: arr[][] = {{5, -2}, {-3, -2}, {-2, 5}, {1, 6}, {0, 2}}
Output: Center = {1.0, 1.0}, Radius = 5.000
Naive Approach: This problem can be solved by making a few observations.
From the above observations, it can be concluded that the MEC either:
Thus, the solution to this problem is trivial for N <= 3. For other cases, a simple idea can be formed to solve this problem. The idea is to use all pairs and triples of points to obtain the circle defined those points. After obtaining the circle, test to see if the other points are enclosed by that circle and return the smallest valid circle found.
Below is the implementation of the above approach:
Center = { 0.5, 0.5 } Radius = 0.707107
Center = { 1, 1 } Radius = 5Time Complexity: The time complexity for this solution would be of O(N4). That's because there are N3 triples of points. And for each triple, we check if all the points are enclosed by the circle.
Approach 2: A solution with the application of convex hull concept can also be used for this problem. The idea is to first form a convex hull on the given set of points. Once the convex hull is performed and the new set of points is returned, then the above-mentioned solution can be used on the new set of points to find the MEC.
The code for this approach would be the same as above except that we would also need to get the convex hull first. Please refer to this article for an efficient algorithm to get the convex hull.
Time Complexity: One observation that needs to be made that if the input already represents some vertices of a convex polygon, then this solution would have the same time complexity of the above naive approach.
Therefore, the worst-case complexity of this approach is still O(N4).
However, if the number of vertices of the convex hull is considerably smaller than N, then the complexity would be O(H4 + NLog(N)) where H represents the number of vertices of the convex hull, and the NLog(N) factor is for finding the convex hull assuming Graham Scan algorithm is used.
Finally, if the number of vertices, H, of the convex hull, is very small, then it can be considered as a constant factor and thus the time complexity would be O(NLog(N)).