The following code is for Floyd-Warshall algorithm
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
I propose to rewrite the code as follows
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
In the new code, I put the original outer loop into the most inner loop. I find that the new code is easy to understand solving all pair shortest path problem. My question is: is the new code equivalent to the original code for Floyd-Warshall algorithm?