Implementation notes
The implementation of Floyd-Warshall is relatively straightforward compared to Dijkstra’s algorithm. We’ll construct the matrices A0,P0 from the weighted adjacency list and then iteratively compute the distance and predecessor matrices Ak,Pk, for k=1,…,∣V∣.
One important observation to reduce the memory footprint of the algorithm is that the computation of Ak+1 and Pk+1 depends only on the immediate previous matrices Ak and Pk. Therefore, the updates to the distance and predecessor matrices can be done in place. In other words, we only need to store one pair of n×n matrices instead of all of them. This reduces the memory requirements from O(∣V∣3) to O(∣V∣2).
Another implementation detail is how to represent the infinite values in the distance matrices. A pragmatic and often feasible solution is to use a large constant value such as one billion to represent infinity. The main concern here is that this value should be larger than the maximum length of a shortest path in the graph, to avoid overflows or incorrect results.