2

I have to write an algorithm that given n blue and n red points in a 2D space will connect red and blue points with lines without any of them intersecting. In other words, I want to have n disjoint pairs (each consisting of a blue and a red point). When I connect the points in each pair the connecting line segments cannot intersect. The algorithm has to run in O(n^2*logn) time.

After a lot of reading I decided to go with the following idea: recursively divide the plane in halfs so that there is always the same number of blue and red points on each side until there is only one red and one blue point left - these can be connected with a line. Such divide and conquer approach will give that logarithmic complexity that I was looking for.

The question was how to divide a plane with a line so that there is an equal number of blue and red points on each side. Again, after some reading, I found the ham sandwich algorithm which is supposed to do exactly what I'm looking for in linear time. I am, however, having hard time understanding how it works exactly and translating it into code. This and this link were the most helpful to me but they only describe in general the steps of this algorithm and lack some implementation details.

The major question is: is my approach correct? If so, can you explain to me the algorithm of ham sandwich or maybe provide some additional materials/pseudocode?

tomomomo
  • 157
  • 2
  • 13
  • so you want to have n disjoint pairs, each pair consisting of a blue and a red point, and when you connect the points of pair, the connecting lines shall not intersect? – coproc Nov 14 '15 at 21:47
  • just a side remark: the problem can be rewritten as a LSAP (linear sum assignment problem), which can be solved in O(n^3) time. For certain configurations (e.g. if the union of the blue and red points form a convex polygon) there are faster algorithms (for the mentioned example in. O(n^2) time). – coproc Nov 15 '15 at 11:29
  • I know, I've read about it [here](http://stackoverflow.com/a/19069408/2908420) but I'm looking for a O(n^2*logn) solution. – tomomomo Nov 15 '15 at 11:34
  • Wouldn't the application of a ham-sandwich-algorithm with O(n) time complexity lead to an O(n^2) time algorithm? There are only O(n) "ham-sandwich-cuts" necessary, right? – coproc Nov 15 '15 at 13:44
  • I think that there are logn cuts necessary, so in total I will obtain O(n*logn) and not O(n^2*logn) which is of course better but I'm not sure if it's correct. The teacher wanted me to do it in O(n^2*logn) time since he probably believed it's the fastest we can do... – tomomomo Nov 15 '15 at 14:38
  • 1
    for isolating one pair of (blue,red) ld(n) cuts suffice. But for finally separating all n pairs you will need (n-1) cuts. – coproc Nov 15 '15 at 15:22

0 Answers0