Graph Theory37 sections · 1633 units
Open in Course

Space Improvement

(Overwriting in place)

The 3D3D DP state dp[k][i][j] uses O(n3)O(n^3) space. You can reduce this to O(n2)O(n^2). Notice that dp[k+1][i][j] only depends on dp[k]. You can update dist[i][j] in place without storing all kk layers.

This is why Floyd-Warshall uses a 2D2D array and updates it during iteration. The space drops from O(n3)O(n^3) to O(n2)O(n^2). This improvement makes the algorithm practical for graphs with thousands of vertices.