8

I have two sets of n nodes. Now I want to connect each node from one set with another node from the other set. The resulting graph should have no intersections.

I know of several sweep line algorithms (Bentley-Ottmann-Algorithm to check where intersections occur, but I couldn't find an algorithm to solve those intersections, except for a brute-force approach.

Each node from one set can be connected to any other node within the other set.

Any pointers to (an efficient) algorithm that solves this problem? No implementation needed.

EDIT1:

Here is one solution to the problem for n=7:

Intersection problem

The black dots are a set of nodes and the red dotes are a set. Each black node has to be connected to one red node so that the lines connecting them are not crossing.

EDIT2:

To further clarify: The positions of all the nodes are fixed and the resulting graph will have n edges. I also don't have any proof that a solution exists, but I couldn't create an example where there was no solution. I'm sure there is a proof out there somewhere that creating such a planar graph is always possible. Also, only one solution is needed, not all possible solutions.

Soift
  • 157
  • 5
  • 3
    Here's a problem instance with no solution: Draw any line segment of length 4. Put a red dot at the left (or bottom) end, and another red dot 1 unit rightwards (or upwards) along the line from here. Put a black dot at the right (or top) end, and another black dot 1 unit leftwards (or downwards) from there. I.e. the diagram looks like `R R B B`. All possible pairs of black-red lines intersect in an entire line segment of length 1. – j_random_hacker Jun 29 '16 at 13:07
  • 1
    Did you think of it as an Optimization Problem? by that I mean that you want to find the optimized set of edges, and recursively you check for each possible edge the correctness of the set with or without the edge. I can guide you more if you think this is a good direction. – Gal Dreiman Jun 29 '16 at 13:13
  • If the points are fixed it is not true that a solution always exists. The easiest counter example is if you imagine all nodes on the same line, "black" nodes in the middle and the red ones on either side – Ivaylo Strandjev Jun 29 '16 at 13:35
  • @j_random_hacker Yes, there are cases where it doesn't work. I forgot to mention that those edge cases are never occurring. Is there a solution, if we assume that there will never be a black-red lines that are parallel to each other? – Soift Jun 29 '16 at 13:51
  • 1
    I say draw those lines at random, or by means of "for each dot of one set, draw line to closest unoccupied dot from other set", then if any two lines intersect, swap points on either side. This should end eventually, but I think it should be optimized that if one line crosses more than one of the others, swap that line's end with either first. – Vesper Jun 29 '16 at 14:36
  • @Vesper That is what I meant with the brute-force-approach, however, it seems extremely inefficient. – Soift Jun 29 '16 at 14:50

1 Answers1

7

When a solution exists (see my comment giving an example instance where it does not), it can be found by finding a minimum weight matching in a complete bipartite graph that contains a (red or black) vertex for every point, and for every red vertex u and black vertex v, an edge (u, v) of weight equal to the Euclidean distance between their corresponding points. This can be solved optimally in O(V^4) time.

Why should this work? The main idea, which I took from David Eisenstat's answer to a similar question, is that whenever we have a pair of line segments AB and CD that intersect at some point X, the Triangle Inequality can be used to show that picking either endpoint of each and swapping them gives a pair of line segments with lower or equal total length:

A
|\
| \
|  \ X
C---+-----D
     \   /
      \ /
       B

AX + XC >= AC (tri. ineq.)
BX + XD >= BD (tri. ineq.)
AX + XC + BX + XD >= AC + BD (sum both sides)
(AX + BX) + (XC + XD) >= AC + BD (rearrange LHS)
   AB     +    CD     >= AC + BD (combine pairs of segments on LHS)

Assuming further that the triangles AXC and BXC are non-degenerate, the >= becomes a >. (A sufficient condition for this is that no set of 3 points containing at least 1 red and 1 black point are collinear.) So, for any given solution (assignment of red nodes to black nodes), if that solution contains a crossing, then its total sum of line segment lengths can be reduced by a nonzero amount by swapping the red (or black) endpoints of the two crossing line segments.

In other words,

Solution contains a crossing => sum of segment lengths is not minimal.

Taking the contrapositive, we immediately get

Sum of segment lengths is minimal => solution contains no crossing.

Since the minimum weight matching algorithm returns a solution of minimum possible weight, this establishes its correctness.

(Notice that it's not necessary to worry about whether or not the endpoint-swapping actually guarantees that the new pair of line segments AC and BD don't intersect -- while it seems obvious that they won't, all we actually need for the proof of correctness is to show that crossing exists => sum not minimal.)

j_random_hacker
  • 47,823
  • 9
  • 95
  • 154
  • 3
    " it's not necessary to worry about whether or not the endpoint-swapping actually guarantees that the new pair of line segments AC and BC don't intersect" - "AD" I assume. But, if they would intersect after changing their ends, changing them AGAIN will lead to AB and CD, which sum of lengths is greater than sum of AD and BC, which cannot be. Therefor they don't intersect. – Vesper Jun 29 '16 at 14:55