Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest One-Way Path Through Multiple Nodes

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 hereenter image description hereenter image description hereenter image description here
Couple more for good measure #1 = TSP, #2 = desired
enter image description hereenter image description here
Basically i want the shortest chain connecting n points, using whichever start and end point is most efficient

like image 628
stumped_coder Avatar asked Jan 24 '11 08:01

stumped_coder


People also ask

Does Dijkstra visit all nodes?

Dijkstra's Algorithm finds the shortest path between a given node (which is called the "source node") and all other nodes in a graph.

Can A graph have more than one shortest path between two nodes?

In general, there can be multiple shortest paths in a graph. In particular, you can use Djikstra's algorithm to compute shortest paths and this algorithm can return multiple paths from any node A to any other node B.

What is the shortest path from node 1 to node 4?

Explanation: The number of shortest path from node 1 to node 4 is 2, having cost 5.

How do you visit all nodes in A graph the shortest path?

graph length will be N, and j is not same as i is in the list graph[i] exactly once, if and only if nodes i and j are connected. We have to find the length of the shortest path that visits every node. We can start and stop at any node, we can revisit nodes multiple times, and we can reuse edges.


2 Answers

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.

like image 110
templatetypedef Avatar answered Sep 19 '22 17:09

templatetypedef


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.

like image 42
rik Avatar answered Sep 19 '22 17:09

rik