Problem Link:Difficulty:Hard Prerequisites:High School Geometry Problem:Given a set S of N points in plane, you have to draw two lines such that sum of squares of distance of each point from nearest line is smallest. Explanation:The first thing one realizes about this problem is that the business of taking minimum of the distances is causing all the trouble. If we had to draw a single line to optimize sum of squares of its distance from all points, it would be a piece of cake. High school calculus can be used to find the optimum equation of the line. Here is a brief sketch: Let the equation of optimum line be y +mx +c = 0. The square of distance of a point (x0,y0) from this line is (y0 + mx0 + c)^{2}/(1 + m^{2}). Therefore, sum of squares of distance of all points is
We need to find optimum m and c. To do this, first differentiate with respect to c to get c as a (linear)function of m, plug it in the above, and differentiate with respect to m to get optimum value. Refer to solutions at the end for more precise details. Assuming that the one line case can be solved in constant time, given X, Y, XY, N, X2, Y2, we can now proceed. Now imagine the optimal pair of lines. Let the lines be named A and B. Every point in our set S is closer to exactly one of A and B. Let S_{A} be the set of points in our S closer to A than B. Similarly define S_{B}. Note that S_{B} = S  S_{A}. Now suppose, by some magic, we managed to find the set S_{A}. Then we would again be done. This is because line A is the optimal line for the set S_{A}, and line B is the optimal line for remaining points. Cool ! So, we now have a O(N * 2^{N}) solution, which basically iterates over all subsets S_{A} of S, and reports the solution. Obviously, the next step is to note that sets S_{A} and S_{B} cannot be arbitrary. If we are given a pair of lines A and B, then S_{A} consists of exactly those points in S which do not lie in the colored region. Similarly, S_{B} consists of those points which lie in the colored region. Justification is very straightforward and left to the reader. The colored region can be identified by a pair of perpendicular lines. For any two perpendicular lines L, M, let R_{L,M} denote the the first and third quadrant of the coordinate system defined by line L, M. Formally, R_{L,M} = {points P  tan ∠ PXL ≥ 0}, X being intersection point of L and M. Due to discussion above, we know that the set S_{A} cannot be arbitrary. The set S_{A} has to be such that there exist two perpendicular lines L and M, so that S_{A} = S ∩ R_{L,M}. In fact, we give a list Z of pairs of lines(i.e. Z={(L_{1}, M_{1}), (L_{2},M_{2}), ... (L_{M},M_{M})}) such that, for any arbitrary pair of lines (L, M) we can obtain another pair of lines (L', M') which satisfies the following
Here is how to obtain L', M' from any L, M.
Now it is clear that the new lines L', M' obtained by above process still induce same partition of S as L, M did. Moreover, L', M' are completely defined by The set Z of all possible final pairs (L', M') obtained after translation/rotation has size 2n^{3}, and can be easily enumerated. We can solve the problem in O(n^3) overall time as well, because the partitions imposed by these lines can be interconverted by adding/removing single points, and hence (X, Y, XY, X2, Y2) tuple can be recalculated in constant time. See the solutions below for exact details. Setter's Solution:Can be found here Tester's Solution:Can be found here Editorialist's Solution:Can be found here
This question is marked "community wiki".
asked 16 Sep '13, 15:14

When I first read the problem, I realized I had to find regression line of N points. In this case instead of one, I had to find two. I didn't know how to find one line, let alone two. So I googled and found an interesting paper on Kline mean : http://people.csail.mit.edu/dannyf/mscthesis.pdf In the paper, they describe an O(n^3) algorithms for 2line mean. They use vectors and linear algebra to solve the problem (I think!). If only I was good at linear algebra then maybe I could have understood this article (all those matrix decomposition and vector products boggled my mind). I tried studying linear algebra books for few days but they didn't cover these advanced topics (SVD for example). Plus reading 500+ pages of a books didn't seem efficient. So anybody here solved the problem using this paper or linear algebra? Any resource that will help me with linear algebra (and this problem). answered 16 Sep '13, 17:26
How you googled it ? I couldn't found any good topics
(17 Sep '13, 09:52)
look page 23, solution to our problem ))
(17 Sep '13, 09:54)
1
Another paper which solves a similar problem in O(N^3) can be found at http://infoscience.epfl.ch/record/164483/files/nscan3.PDF
(17 Sep '13, 13:51)

Hi guys, can someone help me to find the roads for those three inputs?
and
and
Testers solution fails for all the inputs. Setter's and editorialist's solution returns 0.414866845 for first input, 0.133333333 for second and 0.666666667 for the third, but it seems to me less than I'd expect. If possible, give me the roads in answered 17 Sep '13, 09:11
1
I noticed, that both setter's and editorialist's solution prints
(17 Sep '13, 09:20)
1
first: second: third:
(17 Sep '13, 10:43)
@utkarsh_lath thanks a lot
(17 Sep '13, 11:59)
@kevinsogo you are correct, negative coordinates are not valid input, but once you have working algorithm, it's not so important, but that's why tester's solution "fails"...
(17 Sep '13, 14:11)

If someone can fail this solution: Consider all lines that is pairwise combination of given points. For each line we will consider it's top and bottom parts. For each part we solve our known calculus thing. I got WA. Someone plz tell what test fails this approach ? answered 17 Sep '13, 09:27
1
for my inputs, your solution returns
Can you describe your approach more precisely? As I understood, you split the points by line. In which part are point on that line?
(17 Sep '13, 09:39)
1
Yep , u r correct. I did all possible cases. Both to bottom(1 case). Both to top(1 case). one top one bottom(2 cases). So overall 4 cases for each pair of points.
(17 Sep '13, 09:45)
1
I have given best solution for @betlista 's inputs. Now you can check where you went wrong :)
(17 Sep '13, 10:46)
Thanks a lot. I found bug in my calculus formula. Plus dividing by only one line will not work. It is evident for me now. It is great relief to find out where I was wrong.
(18 Sep '13, 09:31)

I think approach to this problem is N^3logN. Two outer loops gives us n^2. And inside it we have sorting(respect to projections to line) which is NlogN. Am I correct ? answered 18 Sep '13, 13:21
Good spot ;) @utkarsh_lath is that sorting necessary ?
(18 Sep '13, 15:57)
Yep, sorting is to be used, however, it seems complexity of solving one case(using calculus) is large enough that it hides the complexity of sorting.
(19 Sep '13, 06:34)
Two consecutive projections only differ in two elements (a swap of two consecutive points), so maybe we can use only one sort and go clockwise, updating the sort order every time in O(1).
(14 Nov '13, 14:09)

Can anyone explain me why my solution finds better answer than the author's one? Here is test and I guess the optimal partition looks like this. Strange thing is when I try to solve 1line problem for these partition sets, my and author's solutions give the same total result, but it seems like author's solution doesn't find this partition when it solves the test mentioned above. My answer is 16555.8836279 and author's is 16559.9967756. Here is my code by the way. I hope someone will explain me this. answered 30 Sep '13, 01:08

Hi zenon! Thank you for pointing this out, The problem here is the precision of float number. You put to much digits after the decimal point so our solution will not accurate anymore. Let say the double type can represent exactly x digits after decimal point (x depends on the range of the integer part also). Then if you use multiplication ie to calculate the projection then the input should have only x/2 digits after decimal point, Similarly, if you have product of 3 numbers somewhere in your program, then the i put should have x/3 digits after the point. In our test cases there is about 34 digits after the decimal point only. Actually we should inform that in the problem statement, sorry about that. answered 04 Oct '13, 11:37
