hash-table get-slopes(p, S)
hash-table slopes(|S|*3)
for p2 in S
delta-x = p.x - p2.x
delta-y = p.y - p2.y
g = gcd(delta-x, delta-y)
slopes.add(delta-x/g, delta-y/g)
return slopes
Once grouped, find the most frequently occurring slope.
unsigned max-group-size(slopes)
mx = 0
for i = 0 to slopes.size - 1
if mx < slopes[i].size
if slopes[i].collision
mx = slope-max-count(slopes[i])
else
mx = slopes[i].size
return mx
Collisions fall back to slope counting.
int slope-max-count(chain)
max = 0
while not chain.empty
mx = 0
for e = chain.head; e != nil
if chain.head.slope == e.next.slope
mx++
chain.remove(e.next)
else
e = e.next
max = max(mx, max)
return max
slope-max-count() is O(n2).
int slope-max-count(chain)
max = 0
while not chain.empty : O(n)
mx = 0
for e = chain.head; e != nil : O(n)
if chain.head.slope == e.next.slope
mx++
chain.remove(e.next)
else
e = e.next
max = max(mx, max)
return max
max-group-size() is O(n3).
unsigned max-group-size(slopes)
mx = 0
for i = 0 to slopes.size - 1 : O(n)
if mx < slopes[i].size
mx = if slopes[i].collision
slope-max-count(slopes[i]) : O(n2)
else
slopes[i].size
return mx
get-slopes() is O(n2).
hash-table get-slopes(p, S)
hash-table slopes(|S|*3) : O(n)
for p2 in S : O(n)
deltax = p.x - p2.x
deltay = p.y - p2.y
g = gcd(deltax, deltay)
slopes.add(deltax/g, deltay/g) : O(n)
return slopes