c++ - why does floyd warshall just use one distance matrix? -
i read pseudocode of floyd warshall algorithm 1 let dist |v| × |v| array of minimum distances initialized ∞ (infinity) 2 each vertex v 3 dist[v][v] ← 0 4 each edge (u,v) 5 dist[u][v] ← w(u,v) // weight of edge (u,v) 6 k 1 |v| 7 1 |v| 8 j 1 |v| 9 if dist[i][j] > dist[i][k] + dist[k][j] 10 dist[i][j] ← dist[i][k] + dist[k][j] 11 end if
uses 1 dist matrix save distances. think there should n dist matrixes, n number of vertexes, or @ least need 2 dist matrixes. 1 stores current shortest path within vertexes k-1, other stores shortest path within vertexes k, first 1 stores shortest path within k+1, .... how can store new shortest path distances within vertexes k in original matrix distances within vertexes k-1?
this picture shows need d0, d1, d2....d(n)
you partially correct here.
the output of floyd warshall algorithm(i.e nxn matrix) doesn't reconstruct actual shortest path between 2 given vertices.
these paths can recovered if retain parent matrix p, such stores last intermediate vertex used each vertex pair (x, y). value k.
the shortest path x y concatenation of shortest path x k shortest path k y, can reconstructed recursively given matrix p.
note,however, all-pairs applications need resulting distance matrix.these jobs floyd’s algorithm designed for.
Comments
Post a Comment