Floyd–Warshall
Floyd–Warshall finds the shortest paths between all pairs of vertices.
It supports negative edge weights, but not negative-weight cycles.
Approach
- Create an
n × ndistance matrix initialized to infinity. - Set
dist[i][i] = 0. - Add all direct edge weights to the matrix.
- For every vertex
k, check whether going throughkimproves the path fromitoj.
Pseudocode
FloydWarshall(dist, n):
for k from 0 to n - 1:
for i from 0 to n - 1:
for j from 0 to n - 1:
dist[i][j] = min(
dist[i][j],
dist[i][k] + dist[k][j]
)
return dist
Time: O(n³)
Space: O(n²)