-1

Graph

Lets assume i have a weighted undirected graph with edges and wanted to find the shortest path as well as all possible paths that i could follow from the startpoint to the endpoint with distances, what would be the best way to implement this? Breadth depth search and k paths algorithm seem to offer reasonable solutions, although im not sure which is best

john cenator
  • 33
  • 2
  • 7
  • 2
    Dijkstra and reconstruct paths by checking whether the distance is equal to the differences of distances between two points. – Willem Van Onsem Mar 31 '17 at 13:08
  • Try http://cs.stackexchange.com – pvg Mar 31 '17 at 13:11
  • 1
    Since you ask for "all possible paths" you may want to specify your expectations in case of a cycle. But I think the question is off-topic as it stands right now. – grek40 Mar 31 '17 at 13:12
  • Since you aks for "all possible paths", note that the number of these may grow exponentially in the size of the input, so possibly usual measures of efficiency (i.e. polynomially bounded running time) might not apply. – Codor Mar 31 '17 at 13:38
  • Yes the possible number of paths is not that large, i've calculated that there should be 8 possible paths, depth first traversal might help me find a solution, ill post the code when its done – john cenator Mar 31 '17 at 14:06
  • 1
    Note that as soon as there are cycles in the graph, the number of "all paths" quickly increases to, well, infinity, unless you explicitly exclude paths that contain cycles. – tobias_k Mar 31 '17 at 14:26
  • Possible duplicate of http://stackoverflow.com/questions/43142841/find-all-paths-from-one-node-to-another-in-an-undirected-graph-adjacency-list – Codor Mar 31 '17 at 14:33

1 Answers1

1

Sorry, can't post this as comments...

If you need all possible paths, you can't do really better than "tree" traversal (BFS or DFS for instance). Note that you'll need to consider each node as many times as it can be reached from the start (the "tree" is much bigger than the original graph - even infinite if you have cycles in your graph, but let's assume you don't).

To get the smallest path, you could look for it in your list in the end; or preferably, you could use a Dijkstra-like order for your tree traversal, so the shortest path will be the first to come up.

gdelab
  • 6,124
  • 2
  • 26
  • 59