SPECIAL-MATRIX-MULTIPLYThe optimal solution can be computed by calling SPECIAL-MATRIX-MULTIPLY for . We only need to run to because that will give us giving us all the shortest path lengths of at most edges (you only need edges to connect vertices). Since SPECIAL-MATRIX-MULTIPLY is called times, the total running time is .
1
2 new matrix
3 for to
4 do for to
5 do
6 for to
7 do
. /* Here's where this algorithm */
. /* differs from MATRIX-MULTIPLY. */
8 return
Using repeated squaring, we only need to run SPECIAL-MATRIX-MULTIPLY times. Hence the running time of the improved matrix multiplication is .
ALL-PAIRS-SHORTEST-PATHS
1
2
3
4 while
5 do SPECIAL-MATRIX-MULTIPLY
6
7 return
FLOYD-WARSHALLThe time complexity of the algorithm above is .
1
2
3 for to
4 do for to
5 do for to
6 do MIN
7 return
TRANSITIVE-CLOSURE
1
2 for to
3 do for to
4 do if or
5 then
6 else
7 for to
8 do for to
9 do for to
10 do
11 return