Given P={p1,...,pn} of different points which define n2 lines, write an algorithm that finds the line which has the lowest slope (smallest absolute value) with O(n*log(n))
time complexity in the worst case.
Asked
Active
Viewed 2,214 times
12

nbro
- 15,395
- 32
- 113
- 196

user1387682
- 265
- 2
- 9
2 Answers
6
Theorem:
- Given a set of points P.
- Choose two points A and C in P such that the line AC has the smallest absolute slope (as defined in the question).
- For the degenerate case where multiple pairs of points have the same slope, let AC be the shortest line segment with that slope.
- Then there exist no other points in P with a Y-coordinate between A and C.
Proof (by contradiction):
- Suppose there is at least one other point, B, whose Y-coordinate is between A and C.
- Then there exist three possible cases:
- B is co-linear with A and C. Then the lines AB or BC have the same slope as AC, but both of them are shorter than AC. Contradiction.
- B falls in the half-plane "above" AC. Then the line AB has a shallower slope than AC. Contradiction.
- B falls in the half-plane "below" AC. Then the line BC has a shallower slope than AC. Contradiction.
- All cases result in contradiction, therefore no points occur between A and C.
- QED.
With this theorem, you can clearly use @Zshazz's algorithm to find the correct pair--because they will be nearest neighbors--in O(n*log n)
.

EthanB
- 4,239
- 1
- 28
- 46
5
Sort the points based on their y position (n log n time using any number of well known algorithms). Go through the list in order, from 0 to n - 1, comparing each point pairs' slopes with whatever you've discovered is the lowest slope so far. (that's n time).
Overall, that would be O(n log n).
In pseudocode:
Let P be the list of points (this list starts at 1)
n = P.length
S = quicksort("a.y < b.y", P) // or some other O(n log n) algorithm
bestSlope = float.inf
let p1 and p2 be points
for i = 1 to n-1:
currSlope = abs((P[i].y - P[i+1].y) / (P[i].x - P[i+1].x))
if currSlope < bestSlope:
bestSlope = currSlope
p1 = P[i]
p2 = P[i+1]

Chris Cain
- 656
- 5
- 6
-
4Are you sure this works? What if the lowest slope occurs by taking the highest and lowest y coordinates? – templatetypedef Aug 30 '12 at 01:20
-
@templatetypedef, you're right. It _is_ possible for a two points with the highest and lowest y coordinates to be the lowest slope if the x values differ enough. I'll have to give this more thought. I'll keep this answer up (temporarily) so that others don't make the same mistake (and take this into account). – Chris Cain Aug 30 '12 at 01:31
-
It doesn't seem to work for (0, 0), (1, 1), (4, 3). The minimum slope is between the last two points. – recursive Aug 30 '12 at 01:31
-
@recursive, This method doesn't work, but it has nothing to do with there being n^2 pairs. This method would only involve checking n pairs (and of course, sorting would take n log n compares). – Chris Cain Aug 30 '12 at 01:32
-
1@templatetypedef: That would mean all other points, if there are any, are colinear. If they are not colinear another pair must have a lower slope. – recursive Aug 30 '12 at 01:32
-
1@recursive- Are you sure about that? What if the points with max/min y coordinates are really, really far apart from one another (distance effectively infinity) and the other points are packed close together (distance effectively zero). Then all internal points will have huge slopes between them. – templatetypedef Aug 30 '12 at 02:43
-
@templatetypedef i don't think your case works. you propose a pair of points, p1, p2 that are not adjacent in the y-ordering, then I find a point between them in the y order which I call p3. if p3 is not on the line with p1 or p2, then one of p1-p3 or p2-p3 segments will be more horizontal than your candidate and one will be more vertical. – Rob Neuhaus Aug 30 '12 at 03:12
-
2@rrenaud- Ah, I didn't realize that. That's a good point. Another way of thinking about that is to draw the line between them. Anything in the upper half-space forms a shallower line when connected to one of the points, and anything in the lower half-space forms a shallower line when connected to the other point. Good observation! – templatetypedef Aug 30 '12 at 03:16
-
I just don't see how this is possible without validating the slope of n!/(n-2)!(n!) pairs because again, based on the X values it would be possible to find a the tiniest slope from the highest y values.. – Jimmy Hoffa Aug 30 '12 at 13:59
-
@JimmyHoffa [Proof by contradiction](http://en.wikipedia.org/wiki/Proof_by_contradiction)... it's a powerful thing. – EthanB Aug 30 '12 at 16:17
-
This algorithm is based on the assumption that a point forms the smallest slope with the point immediately above or below. This assumption is blatantly wrong. – Mar 20 '21 at 17:18