Graph Theory37 sections · 1633 units
Open in Course

Why k is Outermost

(Correctness depends on order)

The loop order in Floyd-Warshall is critical: kk must be the outer loop. Why?

Because paths using vertices {0,,k}\{0, \ldots, k\} depend on paths using {0,,k1}\{0, \ldots, k-1\}. If you loop ii or jj first, you break the dependency order. Always write: for k, for i, for j. Any other order gives wrong answers. This is not a performance improvement, it affects correctness. Get the order wrong and you get incorrect shortest paths.