5

I have a series of graph coordinates and I need to find the shortest one-way path through them all. I have no predetermined start/end but each point must only be touched once and returning to the optimal origin is NOT required.

I've tried a couple TSP approaches, but they all seem to be based on returning to the origin at the end which gives terribly inefficient results in this case.

Example

1, 13
3, 0
3, 7
2, 21
2, 11
3, 12
1, 19
3, 6

would resolve to

3, 0
3, 6
3, 7
3, 12
2, 11
1, 13
1, 19
2, 21

Notes:

Yes I tried the search function, there is a basically identical question Algorithm: shortest path between all points however the only real answer is a TSP, which once again, closed circuit is inefficient for this.

It does not need to be 100% accurate, I already have a permutations method but its far too slow, I need to handle at least ~25-30 points, settling with a good approximation works for me

Thanks in advance.

Edit to clarify, TSP tends to solve as in img #1, my desired result is img #2
img 3 is the above sample solved via a TSP and img #4 is the desired (x coords shifted back -.5 for visibility)
enter image description here enter image description here enter image description here enter image description here
Couple more for good measure #1 = TSP, #2 = desired
enter image description here enter image description here
Basically i want the shortest chain connecting n points, using whichever start and end point is most efficient

Community
  • 1
  • 1
stumped_coder
  • 51
  • 1
  • 3
  • How is this related to PHP? Do you have code to show, e.g. structures representing nodes and graphs? – rik Jan 24 '11 at 08:59
  • Well I guess its not directly related to php other than I'd prefer a php solution as thats the language I'd like it to end in (assuming php can handle the processing in reasonable time), but an algorithm or some decent commented code in a major language (c++, java, ruby, etc) would suffice – stumped_coder Jan 24 '11 at 09:04
  • The pattern in your graphics say that you don't want the path _changing directions_. How about making larger the costs of links with (x2,y2) < (x1,y1)? It would bias standard TSP algorithms towards solutions that start near the upper left, and finish near the bottom-right. – Apalala Jan 25 '11 at 15:45
  • @Apalala could you clarify a tad? The images above do change direction occasionally though such as in img #2. Usually it tends to be top to bottom and 1 corner to the other but not always. The last image has it starting in the upper middle and ending in the lower left, its also possible that it starts in the upper left and ends in the bottom left. – stumped_coder Jan 26 '11 at 00:04
  • The suggestion of tweaking the link weights would not preclude bottom-to-left solutions, it would just make the top-to-right ones seem less costly to the search. It's an heuristic that can be applied on top of standard solutions. – Apalala Jan 28 '11 at 16:44

3 Answers3

3

This is an instance of the all-pairs shortest path problem with all edges having weight=1. You'll find common solutions like Dijkstra's or A-star algorithm on the linked page.
A naive approach is to iterate over the nodes and find the shortest path to every other node.

$paths = array();
foreach ($nodes as $start) {
    foreach ($nodes as $end) {
        if ($start !== $end) {
            $path[$start][$end] = findShortestPath($graph, $start, $end);
        }
    }
}

In a more sophisticated approach findShortestPath would remember subpaths of previous runs (or use $paths for that purpose) to gain better performance.

rik
  • 8,592
  • 1
  • 26
  • 21
  • 2
    I'm pretty sure that all-pairs shortest paths won't work; that gives you the distance to everything in the graph but won't tell you in what order to visit them to minimize the trip cost. – templatetypedef Jan 24 '11 at 09:11
  • @templatetypedef: "The all-pairs shortest path problem, in which we have to find shortest paths between every pair of vertices v, v' in the graph" (from Wikipedia) gives you the _paths_ not only their lengths. – rik Jan 24 '11 at 09:17
  • @rik- correct, but I don't think that's the question being asked. Even if you have the shortest paths between all points, in what order should you visit the points so that they're only visited once and the total path cost is minimized? I don't see how to get that information from the APSP answer. – templatetypedef Jan 24 '11 at 09:24
  • @rik: i appreciate the suggestion, however I've already briefly looked into those and I tend to agree with templatetypedef. They do find the shortest path from A to N but they don't ensure that it passes through each point. Is that the correct understanding or am I missing something? – stumped_coder Jan 24 '11 at 09:31
  • I'm sorry, didn't grasp you want to visit every node on every trip. I'll see if I can come up with a solution and update my answer. Gimme some time … – rik Jan 24 '11 at 09:37
  • no worries, I uploaded some quick images of what I'm looking for if that helps to clarify – stumped_coder Jan 24 '11 at 09:45
  • I had a closer look at your sample data and it turns out your graph is actually a set of 3 disjoint direct acyclic graphs and every possible path has length=1. If any of these limitations generally apply to all your graphs, it boils down to a simple sorting problem. However the sample data doesn't match with the images. Please clarify on that. – rik Jan 24 '11 at 10:11
  • sorry, its a small web app i made for comparing algorithms, it randomly generates some points and generates an image showing the path off those points, give me a sec and i'll configure it to use predefined coords so i can match the sample data – stumped_coder Jan 24 '11 at 10:14
3

Since you don't care about finding a closed loop - all you need is a single path - you can make a small modification to the points you have to avoid the cost of a closed loop. To do this, add a new point, call it v, that you define to be at distance 0 from all the other points. Now, find a TSP solution for this graph. At some point, you'll enter and then leave v. If you take the cycle and then remove the edges into and out of v from it, then you'll have a path that visits each node exactly once without any cycles.

This still requires you to solve or approximate TSP, but it eliminates the huge overhead of returning to your start point.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

there are many algorithms that search the shortest closed path in a graph. I think that it's not too hard to adapt one of the algorithms that solve that problem (a.k.a as travelling salesman problem) to your needs(the path should be a hamiltonian way not a hamiltonian cycle). Some of the well-known solutions for the salesman problem are Dijkstra's algorithm and Prim's algorithm.

gion_13
  • 41,171
  • 10
  • 96
  • 108