Algorithm to cover maximal number of points with one circle of given radius

Oleh Prypin picture Oleh Prypin · Jul 12, 2010 · Viewed 9.7k times · Source

Let's imagine we have a plane with some points on it. We also have a circle of given radius.

I need an algorithm that determines such position of the circle that it covers maximal possible number of points. Of course, there are many such positions, so the algorithm should return one of them.
Precision is not important and the algorithm may do small mistakes.

Here is an example picture:
Example

Input:
  int n (n<=50) – number of points;
  float x[n] and float y[n] – arrays with points' X and Y coordinates;
  float r – radius of the circle.

Output:
  float cx and float cy – coordinates of the circle's center

Lightning speed of the algorithm is not required, but it shouldn't be too slow (because I know a few slow solutions for this situation).

C++ code is preferred, but not obligatory.

Answer

Alexandre C. picture Alexandre C. · Jul 12, 2010

Edited to better wording, as suggested :

Basic observations :

  • I assume the radius is one, since it doesn't change anything.
  • given any two points, there exists at most two unit circles on which they lie.
  • given a solution circle to your problem, you can move it until it contains two points of your set while keeping the same number of points of your set inside it.

The algorithm is then:

  • For each pair of points, if their distance is < 2, compute the two unit circles C1 and C2 that pass through them.
  • Compute the number of points of your set inside C1 and C2
  • Take the max.