Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect points from set in the line segments

I have been given a task where I have to connects all the points in the 2D plane. There are four conditions to to be met:

  1. Length of the all segments joined together has to be minimal.
  2. One point can be a part of only one line segment.
  3. Line segments cannot intersect
  4. All points have to be used(one can't be left alone but only if it cannot be avoided)

Image to visualize the problem: enter image description here

The wrong image connected points correctly, although the total length is bigger that the the one in on the left.

At first I thought about sorting the points and doing it with a sweeping line and building a tree of all possibilities, although it does seem like a way to complicated solution with huge complexity. Therefore I search better approaches. I would appreciate some hints what to do, or how could I approach the problem.

like image 337
SirKometa Avatar asked Dec 04 '13 18:12

SirKometa


2 Answers

I would start with a Delaunay triangulation of the point set. This should already give you the nearest neighbor connections of each point without any intersections. In the next step I'd look at the triangles that result from the triangulation - the convenient property here is that based on your ruleset you can pick exactly one side from each triangle and remove the remaining two from the selection.

The problem that remains now is to pick those edges that give you the smallest total sum which of course will not always be the smallest side since that one might already have been blocked by a neighboring triangle. I'd start with a greedy approach, always picking the smallest remaining edge that has not been blocked by neighboring triangles yet.

Edit: In the next step you retrieve a list of all the edges in that triangulation and sort them by length. You also make another list in which you count the amount of connections each point has. Now you iterate through the edge list going from the longest edge to the shortest one and check the two points it connects in the connection count list: if each of the points has still more than 1 connection left, you can discard the edge and decrement the connection count for the two points involved. If at least one of the points has only one connection left, you have got yourself one of the edges you are looking for. You repeat the process until there are no edges left and this should hopefully give you the smallest possible edge sum.

If I am not mistaken this problem is loosely related to the knapsack problem which is NP-Hard so I am not sure if this solution really gives you the best possible one.

like image 106
Quasimondo Avatar answered Nov 05 '22 20:11

Quasimondo


I'd say this is an extension to the well-known travelling salesman problem.

A good technique (if a little old-fashioned) is to use a simulated annealing optimisation technique.

You'll need to make adjustments to the cost (a.k.a. objective) function to miss out sections of the path. But given a candidate continuous path, it's reasonably trivial to decide which sections to miss out to minimise its length. (You'd first remove the longer of any intersecting lines).

like image 39
Bathsheba Avatar answered Nov 05 '22 20:11

Bathsheba