Connect an even number of nodes without intersection

与世无争的帅哥 提交于 2019-12-05 08:04:45

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.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!