3

I have a set of points, and need to know which one has the farthest euclidean distance from any other points. I want to improve this from O(n^2)

Now guys i've heard about Kd trees for solution, BUT KD Trees doesn't provide nearest distance if the point 'x' is already present in Kd tree. And there is no implementation for it to remove.

Edit: You can do this by ignoring self in "nearest search algo" and "where we set root/parent" initially to begin search

tashfeen
  • 187
  • 1
  • 9

2 Answers2

1

given n points Pi, 1 <= i <= n:

  1. build kd-tree (with an O(n) median of median algorithm this is O(n log n))

  2. for all points Pi: find second closest point (closest point will be point itself), compute distance and remember Pi if the distance is a new minimum; this is O(n log n) again.

Alltogether this is an O(n log n) algorithm.

coproc
  • 6,027
  • 2
  • 20
  • 31
  • What is `ld n` ? log2 ? – XCS Jun 04 '16 at 21:16
  • ld is the logarithm with base 2; in big O notation this is actually the same as log n (base e). – coproc Jun 04 '16 at 21:17
  • @coproc Are you from Germany? I saw on wikipedia `ld` is mostly used in Germany :D – XCS Jun 04 '16 at 21:18
  • 1
    @tashfeen "k-nearest" is a standard function on kd-trees; you can also modify the "nearest"-function to ignore the given point itself – coproc Jun 04 '16 at 21:21
  • @coproc Can you provide a link to code for k-nearest ? I couldn't find any. Or i would really appreciate if you could help me in 'ignoring itself' in nearest. – tashfeen Jun 04 '16 at 21:27
0

I assume that you want to find the point that maximises the distance to the nearest neighbour. Like a small island in the south pacific being 1100 miles away from the nearest land.

Well, you should be nowhere near O (n^2). Say you have a million points. Divide the points into a 1000 x 1000 grid. To find the nearest point, you would only have to examine the nine neigbouring grids, so you are far below O (n^2). If a grid contains lots of points, they will be close together so you can remove them from the search quickly.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • 3
    This solution is still `O(N^2)`. How can you make sure that the "9 neighbouring grids" don't contain all the N points? – XCS Jun 04 '16 at 21:08