Graph Theory37 sections · 1633 units
Open in Course

Path Reconstruction

(Storing next hops)

Floyd-Warshall computes distances but not the actual paths. To reconstruct paths, maintain a 2D2D array next[i][j]. Initialize next[i][j] = j if there is a direct edge from ii to jj. When you improve dist[i][j] via vertex kk, set next[i][j] = next[i][k].

After the algorithm, follow next pointers to reconstruct the path from ii to jj. This adds O(n2)O(n^2) space but allows you to output the actual path, not its length.

Space complexity is O(n2)O(n^2) for the data structures used.