Summarized notes from Introduction to Algorithms, Chapter 25
graph may include negative weights, no negative-weight cycles
considers intermediate vertices of path
intermediate is any vertex other than or :
if is not an intermediate vertex pf , then all intermediate vertices are in set
if is an intermediate vertex, then decompose p:
is weight of shortest path from vertex to for which all intermediate vertices are in the set , defined recursively by:
compute in order of increasing values of
input is matrix
output is matrix of shortest path weights
Implementation
123456789
FLOYD-WARSHALL(W)
n = W.rows
D^0 = W
for k = 1 to n
let D^k = (d_ij^k) be a new n x n matrix
for i = 1 to n
for j = 1 to n
d_ij^k = min(d_ij^(k-1), d_ik^(k-1) + d_kj^(k-1))
return D^n
Complexity
Running Time
Floyd-Warshall
Constructing shortest path
One strategy: compute then construct predecessor matrix from
Another strategy: compute during Floyd-Warshall while constructing
Transitive closure
Determines if path from to exists
Transitive closure of is where : there is a path from to in
Compute in time: assign weight 1 to each edge and run Floyd-Warshall → when path exists
Another strategy that can be faster and more space efficient:
substitute logical-OR and logical-AND for operations min and +
when
and for ,
Implementation
1 2 3 4 5 6 7 8 9101112131415
TRANSITIVE-CLOSURE(G)
n = |G.V|
let T^0 = t_ij^(0) be a new n x n matrix
for i = 1 to n
for j = 1 to n
if i == j or (i,j) in G.E
t_ij^(0) = 1
else
t_ij^(0) = 0
for k = 1 to n
let T^k = t_ij^(k) be a new n x n matrix
for i = 1 to n
for j = 1 to n
t_ij^(k) = t_ij^(k-1) or (t_ik^(k-1) and t_kj^(k-1))
return T^n