4

When I google for Geometric median, I got this link Geometric median but I have no clue how to implement it in C . I am not very good at understanding this Mathematical Explanation. Lets Say I have 11 pair of co-ordinates how will I calculate the geometric median for the same.

I am trying to solve this problem Grid CIty. I was given a Hint that geometric median will help me achieve it. I am not looking for a final solution. If someone can guide me to a right path that would help.

Thanks is Advance

Below is the list of co-ordinates a (test case). result : 3 4

1 2
1 7
2 2
2 3
2 5
3 4
4 2
4 5
4 6
5 3
6 5
Linger
  • 14,942
  • 23
  • 52
  • 79
selva
  • 51
  • 1
  • 5
  • I don't think geometric median is a solution to this problem. I think it is more for arithmetic median. – rbelli Apr 02 '12 at 13:15
  • If you calculate the arithmetic median from the first column it is 3.09 and the arithmetic mean for the second column is 4. The answer you want. – rbelli Apr 02 '12 at 13:19
  • @rbelli - i found the same problem in interviewstreet. i tried solving it. but failed in various instances i was successful only with 3 test case out of 13. – selva Apr 02 '12 at 13:26
  • I think I could write up an algorithm to calculate geometric means. Do you need it to the nearest integer or do you want arbitrary precision? If you only need an integer point, you would probably be best using anatolyg's hill-climbing algorithm. – Kendall Frey Apr 02 '12 at 14:12
  • D'oh, I meant median in the above comment. – Kendall Frey Apr 02 '12 at 15:00

5 Answers5

1

I don't think this is solvable without an iterative algorithm.

Here is a pseudocode solution similar to the hill-climbing version, except that it works to arbitrary accuracy, and in higher dimensions.

CurrentPoint = Mean(Points)
While (CurrentPoint - PreviousPoint) Length > 0.01 Do
    For Each Point in Points Do
        Vector = CurrentPoint - Point
        Vector Length = Vector Length - 1.0
        Point2 = Point + Vector
        Add Point2 To Points2
    Loop
    PreviousPoint = CurrentPoint
    CurrentPoint = Mean(Points2)
Loop

Notes:

The constant 0.01 does not guarantee the result to be within 0.01 of the true value. Use smaller values for better precision.

The constant 1.0 should be adjusted to (I'm guessing) about 1/5 the distance between the furthest points. Too small values will slow down the algorithm, but too large values will cause inaccuracies probably leading an to infinite loop.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
0

The answer is (xi, yj) where xi is the median of all the x's and yj is the median of all the y's.

Avi Cohen
  • 3,102
  • 2
  • 25
  • 26
0

To resolve this problem, you just have to compute the mean for each coordinate and round up the result. It should resolve your problem.

Pillou
  • 1
  • 1
  • The question is not mean, the question is median. – Kendall Frey Apr 02 '12 at 13:33
  • Yes, but to resolve the grid city problem, I think it is the mean that should be used and not the median. – Pillou Apr 03 '12 at 08:21
  • I don't see why. Mean finds the average position or center of mass, while median minimizes distance, which is the requirement. – Kendall Frey Apr 03 '12 at 11:10
  • Exact. I didn't taken the diplacement rules into account. It seems that problem was already solved here: http://stackoverflow.com/questions/8571074/find-the-closest-intersection-point-in-plan – Pillou Apr 03 '12 at 14:05
0

You are not obliged to use the concept of Geometric median; so seeing that it is not easy to calculate, you better solve your problem without calculating it!

Here is an idea for an algorithm/implementation.

  1. Start at any point (e.g. the first point in the given data).
  2. Calculate the sum of distances for current point and the 8 neighboring points (+/-1 in each direction, x and y)
  3. If one of the neighbors is better than current point, update the current point and start from 1
  4. (Found the optimal distance; now choose the best point among those with equal distance)
  5. Calculate the sum of distances for current point and the 3 neighboring points (-1 in each direction, x and y)
  6. If one of the neighbors is the same as current point, update the current point and continue from 5
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • with this approach i might end up calculating for all the points. isn't it ? or am i not understanding ? i need to go with more optimal solution. if i have 5000 or more points ? – selva Apr 02 '12 at 15:47
  • The maximal number of iterations is 2N, where N is the width and height of the "world". If you feel you need a faster algorithm, you might want to use a better initial estimate (maybe mean or median); also maybe it's worth using a large step, like +/-16, and decreasing the step by a factor of 2 when the optimal solution for the previous step is found – anatolyg Apr 02 '12 at 16:13
-1

As I comment the solution to your problem is not the geometric mean, but the arithmetic mean.

If you have to calculate the arithmetic mean, you need to sum all the values of the column and divide the answer by the number of elements.

rbelli
  • 393
  • 1
  • 6