Best Line
16.14 Best Line: Given a two-dimensional graph with points on it, find a line which passes the most number of points.
Brute force:
A line is defined by two points. If we define a line with a fixed one point, that passes through all other points. We will define n number of lines, where n is the number of points. For every line we define, we check to see the number of times it passes through the points while in the process also maintaining a maximum. We check this by seeing whether or not (x,y) makes a valid solution with the line. Total, we will have defined n^2 lines, and for each line scanned through a list of n points. Order n^2 * n = n^3.
Better:
Instead of having to scan through the number of times it passes through the points, we can map the line defined by two points into a hash table, and keep track of its frequency. This will tone it down to order n^2 + n.
The book defined using an epsilon for the slope of a hash key because, there may be some imperfect associations with floating point keys and their hash value. For me, this would be an issue, because my key will directly be a string that represents the line in its entirety e.g.
y = mx + b
m, and b will both by floating point values, but I will tone down their precision just a tag. This will be my epsilon. This will simplify the process while maintain accuracy.
Last updated