HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Is there an algorithm to find all the shortest paths between two nodes?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
nodestheshortestallpathsalgorithmtwobetweenfindthere

Problem

Given a directed graph, Dijkstra or Bellman-Ford can tell you the shortest path between two nodes.

What if there are two (or n) paths that are shortest, is there an algorithm that will tell you all such paths?

Edit:

I have just thought up a possible solution. Could you just do a Bellman-Ford but when relaxing edges, if (distance[u] + w == distance[v]) then add u to the set of predecessors of v? Does this work?

Solution

For the $k$ shortest paths, there are algorithms; see for instance here.

Enumerating all shortest paths is, however, an inherently costly thing; there may be superexponentially many such paths.

Context

StackExchange Computer Science Q#38000, answer score: 4

Revisions (0)

No revisions yet.