0

I just started learning about algorithm for graphs, and more specifically - the Floyd-Warshall algorithm. Looking in wikipedia at the algorithm modified to allow path reconstructed, I noticed it keeps the intermediate node, instead of the more logical (in my opinion) way - to save the next hop. Further more, in the course book the way is saved by the one to last node. Why to save the path this way?

phs
  • 10,687
  • 4
  • 58
  • 84
elyashiv
  • 3,623
  • 2
  • 29
  • 52

1 Answers1

0

There is no one "next hop". The problem is storing, in a compact way, enough information to reconstruct the shortest path from i to j, for any combination of nodes i and j. Suppose the last edge in the shortest path from i to j1 is (k,j1) and the last edge in the shortest path from i to j2 is (k,j2). What would you store as the "next hop" for node k?

On the other hand, suppose k is the highest index vertex on both the path from i to j1 and from i to j2. Both shortest paths can be reconstructed recursively given the information the algorithm stores. One will be the shortest path from i to k followed by the shortest path from k to j1. The other will be the shortest path from i to k followed by the shortest path from k to j2. Storing one node on the path, for each i,j combination, is enough to reconstruct any of the shortest paths.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • I know that storing one node is enough. Consider the shortest path from 2 to 3 is the following: `2 -> 4 -> 6 -> 9 -> 5 -> 3` according to the algorithm in wikipedia you will store 9, according to the text book - 5. My question is: why not 4? – elyashiv Nov 09 '13 at 15:58