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
:
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.
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
.)
来源:https://stackoverflow.com/questions/38094704/connect-an-even-number-of-nodes-without-intersection