1

I have a weighted Graph with all positive weights.I need to find the shortest path from vertex x to vertex y. Should I prefer in this case the Floyd–Warshall algorithm over Dijkstra's algorithm since I'm not interested in shortest path from a single source to all vertices but just between specific 2. I can restrict Floyd–Warshall to care only about the 2 vertices of interest, x and y. In that case it looks to me that Floyd–Warshall can find he shortest path from vertex x to vertex y in O(|V|).

Is that correct ?

Thanks!

Tomer
  • 1,159
  • 7
  • 15

1 Answers1

1

I would recommend a breadth-first search starting at both ends in parallel. With a hash of nodes visited so that you know when the searches met.

Assuming a random graph with decent connectivity, your average run-time will be on the order of sqrt(V).

This is a common trick to use to find, for example, the distance between two people in a social graph without having to look at most of it.

btilly
  • 43,296
  • 3
  • 59
  • 88