我猜整数坐标约束显着简化了问题。这对我来说看起来像 O(n):
- 制作空间中所有整数点的字典,并将条目设置为 0。
- 对于每个数据点,找到半径 3 内的整数点,并将字典的相应条目加 1。这样做的原因是,可以作为该特定数据点所在的圆心的点集是围绕该数据点具有相同半径的圆的整数限制。可以对位于长度为 6 的正方形上的所有点进行搜索(认为并非所有点都需要明确评估,因为内接超立方体内的这些点肯定在里面)。
- 返回对应于字典最大值的整数点,即大多数数据点在圆内的中心。
编辑:我猜有些代码比解释更好。这是使用 numpy 和 matplotlib 工作的 python。阅读起来应该不会太难:
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 11 19:22:12 2013
@author: Zah
"""
from __future__ import division
import numpy as np
import numpy.random
import matplotlib.pyplot as plt
from collections import defaultdict
import timeit
def make_points(n):
"""Generates n random points"""
return np.random.uniform(0,30,(n,2))
def find_centers(point, r):
"""Given 1 point, find all possible integer centers searching in a square
around that point. Note that this method can be imporved."""
posx, posy = point
centers = ((x,y)
for x in xrange(int(np.ceil(posx - r)), int(np.floor(posx + r)) + 1)
for y in xrange(int(np.ceil(posy - r)), int(np.floor(posy + r)) + 1)
if (x-posx)**2 + (y-posy)**2 < r*r)
return centers
def find_circle(n, r=3.):
"""Find the best center"""
points = make_points(n)
d = defaultdict(int)
for point in points:
for center in find_centers(point, r):
d[center] += 1
return max(d , key = lambda x: d[x]), points
def make_results():
"""Green circle is the best center. Red crosses are posible centers for some
random point as an example"""
r = 3
center, points = find_circle(100)
xv,yv = points.transpose()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_aspect(1)
ax.scatter(xv,yv)
ax.add_artist(plt.Circle(center, r, facecolor = 'g', alpha = .5, zorder = 0))
centersx, centersy = np.array(list(find_centers(points[0], r))).transpose()
plt.scatter(centersx, centersy,c = 'r', marker = '+')
ax.add_artist(plt.Circle(points[0], r, facecolor = 'r', alpha = .25, zorder = 0))
plt.show()
if __name__ == "__main__":
make_results()
结果:
绿色圆圈是最好的,红色的东西展示了如何为某个随机点挑选中心。
In [70]: %timeit find_circle(1000)
1 loops, best of 3: 1.76 s per loop
In [71]: %timeit find_circle(2000)
1 loops, best of 3: 3.51 s per loop
In [72]: %timeit find_circle(3000)
1 loops, best of 3: 5.3 s per loop
In [73]: %timeit find_circle(4000)
1 loops, best of 3: 7.03 s per loop
在我非常慢的机器上。行为显然是线性的。