There are several descriptions of the Floyd-Warshall algorithm, including the one in Wikipedia where path tracking logic described like in the pseudo-code below (copy from Wikipedia https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm#Path_reconstruction)
procedure FloydWarshallWithPathReconstruction() is
for each edge (u, v) do
dist[u][v] = w(u, v) // The weight of the edge (u, v)
prev[u][v] = u
for each vertex v do
dist[v][v] = 0
prev[v][v] = v
for k from 1 to |V| do // standard Floyd-Warshall implementation
for i from 1 to |V|
for j from 1 to |V|
if dist[i][j] > dist[i][k] + dist[k][j] then // shouldn't it be dist[i][j] > graph[i][k] + dist[k][j]???
dist[i][j] = dist[i][k] + dist[k][j] // shouldn't it be dist[i][j] = graph[i][k] + dist[k][j]???
prev[i][j] = prev[k][j]
procedure Path(u, v) is
if prev[u][v] = null then
return []
path = [v]
while u ≠ v do
v = prev[u][v]
path.prepend(v)
return path
In Floyd-Warshall algorithm with path tracking shouldn't the update rule be 'dist[i][j] = graph[i][k] + dist[k][j]' (where graph[i][k] = w(i, k) is the weight of the edge (i, k) if any) instead of 'dist[i][j] = dist[i][k] + dist[k][j]'? I've marked with a comment the corresponding line in code above. I mean if the path from i to k is longer than one edge then path tracking logic won't work updating just one "hop". Or am I just missing something?
BTW corrected (if it's right) version would also work when we need to track the length of the path (in number of edges). Say, finding shortest path with no longer than N "hops" as we augmenting path by one edge at the iteration.
Thanks!