In normal median, we find a point that has minimum sum of distances. Similar concept applies in 2-D space.
Given N points in 2-D space, the task is to find out a single point (x, y) from which the sum of distances to the input points are minimized (also known as the centre of minimum distance).
Input: (1, 1), (3, 3)
Output: Geometric Median = (2, 2) with minimum distance = 2.82843
Input: (0, 0), (0, 0), (0, 12)
Output: Geometric Median = (0, 0) with minimum distance = 12
At first thought, it seems that the problem asks us to find the Midpoint of the Geometric Centre point (in other words, centroid) of the given input points. Since it is the “centre” point of the input, sum of distances from the centre to all the given input points should automatically be minimized. This process is analogous to finding the Centre of Gravity of discrete Mass particles. The first example test case even gives the correct answer. But what happens when we apply the same logic to the second example?
We can clearly see that the Geometric Centre, or the Centroid of is at . So according to the Euclidean Distance formula, the total distance to travel from Centroid to all 3 of the input points is But the optimal point should be , giving us a total distance of So, where are we wrong?
Intuitively, you can think that Centroid of input points gives us the Arithmetic Mean of the input points. But what we require is the Central Tendency of the input points such that the cost to reach that central tendency (or in other words, the Euclidean Distance) is minimized. This is called the Geometric Median of a set of points.It is kind of like how conceptually, a Median is drastically different from Mean of given inputs.
There isn’t any defined correct algorithm for finding the Geometric Median. What we do to approach this kind of problems is approximating a solution and determining whether our solution is indeed the Geometric Median or not.
There are two important variables :
- current_point – stores the x and y coordinates of the point which could be the Geometric Median.
- minimum_distance – stores the sum of Euclidean distances from current_point to all input points.
After every approximation, if we find a new point from which the sum of distances is lower, then we update both the values of current point and minimum distance to the new point and new distance.
First, we find the Centroid of the given points, take it as the current_point (or the median) and store the sum of distances in minimum distance. Then, we iterate over the given input points, by turn assuming each input point to be the median, and then calculating the distance to other points. If this distance is lower than the minimum_distance, then we update the old values of current_point and minimum_distance to the new values. Else, the old values remains the same.
Then we enter a while loop. Inside that loop, we move a distance of test_distance (we assume a test_distance of 1000 for this example) from the current_point in all directions (left, up, right, down). Hence we get new points. Then we calculate the distance from these new points to the given input points. If this sum of distances is lower than the previous minimum_distance then we update the old values of current_point and minimum_distance to the new values and repeat the while loop. Else, we divide the test_distance by and then repeat the while loop.
The terminating condition for the while loop is a certain value called the “lower_limit”. Lower the value, higher the accuracy of our approximation. Loop terminates when lower_limit exceeds the test_distance.
Below is the implementation of the above approach:
Geometric Median = (2, 2) with minimum distance = 2.82843