Find a line passing the maximum number of points [closed]

☆樱花仙子☆ 提交于 2019-12-04 14:36:03

Your algorithm won't work as stated. Consider many points fall on the line y=2x + 1, meaning that you get (1,3),(2,5), (3,7), (4,9), and (5,11).

I don't think you're expected to solve this unless you have a graduate level course in computational geometry. The deal is to convert all the points to lines in the dual space, using point-line duality and find the point at which most lines intersect. Naively you can do this in O(n^2) by going through every pair of lines and evaluating where they intersect in analytic form. I also think you can do O(n log n) by using plane sweep style algorithms but I'm not sure of the details.

I'm going to assume that the number of points is greater than or equal to 2 here in my answer (zero and one point are trivial cases).

First notice that any such line must pass through at least two of the points. So we can construct a solution as follows:

for each pair of points p1,p2
    find equation of the line l passing through p1,p2

    for each point p3 not p1 or p2
        if p3 lies on l
            counter[l]++

return argmax(counter)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!