# Is there an algorithm to generate all possible shortest paths between two points?

**URL:** <https://boards.straightdope.com/t/is-there-an-algorithm-to-generate-all-possible-shortest-paths-between-two-points/680542>\
**Category:** Factual Questions\
**Created:** [February 5, 2014, 2:24am UTC](https://boards.straightdope.com/t/is-there-an-algorithm-to-generate-all-possible-shortest-paths-between-two-points/680542 "2014-02-05T02:24:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [February 5, 2014, 2:24am UTC](https://boards.straightdope.com/t/is-there-an-algorithm-to-generate-all-possible-shortest-paths-between-two-points/680542/1 "2014-02-05T02:24:17Z")

</div>

I’m writing a graph package, and for testing shortest path algorithms, I thought it may be useful to have an algorithm that generates all possible shortest paths between two vertices in a weighted, directed graph. That way I would only have to hand-solve toy cases to test this algorithm, then I could use that algorithm to generate sets of possible answers for all the other ones. Multiple optimal paths frequently exist, and most shortest path algorithms find only one and which one they find is based on the successor function ordering and priority queue sorting and phase of the moon and the current disposition of Fenris and all sorts of other hard to control stuff.

It looks like Matlab may have a function in a standard library that does this, which of course doesn’t have any open source code I can look at, but I haven’t found any standard algorithms or pseudocode. I could probably devise one (probably by modifying Uniform Cost Search), but there’s no point reinventing the wheel if I don’t have to.

I understand that the running time for such an algorithm is almost necessarily going to be bonkers, but it’s not a big deal since it probably won’t be used on very large graphs, and is unlikely to be used except in testing, which doesn’t have to be blazing fast. At worst, I can run it once, save the output to disk, and then compare against that instead of running it every time. (I mean, in cases that take a while but not “multiple months to find the solution” a while).

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [February 5, 2014, 3:09am UTC](https://boards.straightdope.com/t/is-there-an-algorithm-to-generate-all-possible-shortest-paths-between-two-points/680542/2 "2014-02-05T03:09:21Z")

</div>

Well, I suppose for starters you could make a step in the right direction by just leaving **Fenris** out of your algorithm? 😃

You’re writing this all yourself, from scratch? So you have, like, full, complete, total, unobstructed control over your own algorithm?

Typically, as I understand it, you would search all possible paths, _possibly with some pruning algorithm_, and each time you find your end point, you would:  
[ul]  
[li] Look at the distance of that path;[/li][li] Compare that distance with the best distance you found and saved so far;[/li][li] If the new path is better than the best saved so far, your new path replaces that as the best candidate.[/li][/ul]

One obvious pruning algorithm would be: Each time a particular path (so far) exceeds your best candidate so far, abandon any further exploration of that path.

Now, if you want to find ALL optimal paths (for whatever your definition of optimal path), this means you have to search ALL paths and have a mechanism for saving a list of paths. Each time you find a path that is equal to your best candidate, just add it to your list of optimal paths. (As opposed to some kinds of search algorithms, where you are only looking for any one solution, whereupon you can stop searching as soon as you find a solution. I wrote a program once for solving Skeleton Puzzzles (a.k.a. Kriss-Kross puzzles) this way.)

As I’m seeing your problem statement, the only thing you need to do is to be able to record a whole bunch of paths (namely, all the optimal ones you’ve found so far), rather than only recording one BEST-so-far candidate path.

Somehow, that advice sounds too simple. I don’t know a whole lot about these kinds of algorithms (although I’ve done some years ago), so there’s some serious possibility that I’m really not understanding the problem.

WWFD? (What would **Fenris** do?) 🆒

---

<div class="post-metadata">

**Author:** ![Senegoid](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/senegoid/32/6606_2.png) [@Senegoid](https://boards.straightdope.com/u/Senegoid)\
**Post date:** [February 5, 2014, 3:18am UTC](https://boards.straightdope.com/t/is-there-an-algorithm-to-generate-all-possible-shortest-paths-between-two-points/680542/3 "2014-02-05T03:18:24Z")

</div>

> [@Jragon](#):
>
> Multiple optimal paths frequently exist, and most shortest path algorithms find only one and which one they find is based on the successor function ordering and priority queue sorting and **phase of the moon and the current disposition of Fenris and all sorts of other hard to control stuff.**

(Bold added.) Of course, all those kinds of factors make the algorithm non-determinate. Those are a _bitch_ to test and debug, or to make a suite of test cases for them. GFL with that! 😉

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [February 5, 2014, 4:53am UTC](https://boards.straightdope.com/t/is-there-an-algorithm-to-generate-all-possible-shortest-paths-between-two-points/680542/4 "2014-02-05T04:53:59Z")

</div>

I managed to write something on top of Floyd-Warshall.

> **[Floyd–Warshall algorithm](https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm)**
>
> In computer science, the Floyd–Warshall algorithm (also known as Floyd's algorithm, the Roy–Warshall algorithm, the Roy–Floyd algorithm, or the WFI algorithm) is an algorithm for finding shortest paths in a directed weighted graph with positive or negative edge weights (but with no negative cycles). A single execution of the algorithm will find the lengths (summed weights) of shortest paths between all pairs of vertices. Although it does not return details of the paths themselves, it is pos The F...

It generates all costs between all nodes, and can be modified to keep track of the shortest path. I altered this further.

The algorithm to keep track of the shortest path seems to be: if some new successors is cheaper, supersede the current known successor with the new one. I altered this to: if some new successor is roughly (“roughly” because costs are floats) the same price as the known cost, add it to a list. If a new node is cheaper, supersede the whole list and start a new one containing this node.

Then it was a matter of altering the path reconstruction function they mention. The gist of the algorithm seemed to be: if we know two points i and j, and we know some intermediate point between them (from above). Reconstruct the path between i and the intermediate, and the intermediate and j, and join them. Do this recursively.

I altered this to say that for EACH intermediate, there’s a set of shortest predecessor paths to i, and a set of shortest successor paths to j. (Recursively) generate all of these paths. Now join each predecessor path via the junction at the intermediate with each successor path. Do this for each intermediate.

I think this falls under the umbrella of algorithms that “aren’t fast, but are about as fast as can be reasonably expected.”

---

<div class="post-metadata">

**Author:** ![septimus](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/septimus/32/410_2.png) [@septimus](https://boards.straightdope.com/u/septimus)\
**Post date:** [February 5, 2014, 7:27am UTC](https://boards.straightdope.com/t/is-there-an-algorithm-to-generate-all-possible-shortest-paths-between-two-points/680542/5 "2014-02-05T07:27:29Z")

</div>

It is straightforward to modify an algorithm like Floyd-Warshall to associate with every (i,j) the length l(i,j) of the minimum path between the pair. (You don’t even need every j, just your goal vertex).

Once you have that, from the start vertex it is clear which connections do and which do not lead to a tied-for-minimal path. You can print them all with a simple recursion.

---

<div class="post-metadata">

**Author:** ![Learjeff](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@Learjeff](https://boards.straightdope.com/u/Learjeff)\
**Post date:** [February 5, 2014, 5:39pm UTC](https://boards.straightdope.com/t/is-there-an-algorithm-to-generate-all-possible-shortest-paths-between-two-points/680542/6 "2014-02-05T17:39:45Z")

</div>

Djykstra’s [shortest path first](http://en.wikipedia.org/wiki/Dijkstra's_algorithm) algorithm, slightly extended to keep all equally shortest paths. This is what internet routers use (OSPF or IS-IS) for “introdomain” routing. Frequently, up to 8 equal shortest paths are kept, to allow “equal cost multi-path” forwarding. I used to be a bit of an expert on this, but it’s been a while.

It runs O(NlogN), but can be optimized to nearly O(N) if you constrain the total distance to a small enough number to keep a table of all possible distances. The IS-IS specification covers (and unfortunately, IIRC, requires) that optimization. Unfortunate because the limit on total path cost isn’t ideal. Look for something like “MAX\_PATH” in the IS-IS spec, if you can find a copy. The official standard is an ISO doc, which unfortunately isn’t free, but there was an IETF RFC version that was publicly available. The RFC has been retired but should still be available, and should have the optimization in it.

---

<div class="post-metadata">

**Author:** ![Learjeff](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@Learjeff](https://boards.straightdope.com/u/Learjeff)\
**Post date:** [February 5, 2014, 5:47pm UTC](https://boards.straightdope.com/t/is-there-an-algorithm-to-generate-all-possible-shortest-paths-between-two-points/680542/7 "2014-02-05T17:47:17Z")

</div>

Oops, that’s O(N^2), optimizable to O(NlogN).
