Note that for all m n-1.

Continue until
since
.

This requires only matrix products (calls to Extend).

This is called ``repeated squaring''.

Fast-APSP(W)

n = rows(W)

*D*^{(1)} = W

m = 1

while n-1 > m
;

*D*^{(2m)} = Extend-Shortest-Paths(*D*^{(m)}, *D*^{(m)})
;
*n*^{3}

m = 2m

return *D*^{(m)}

Running time: