The Floyd–Warshall algorithm
Most shortest-path algorithms solve for distances from one vertex to the rest. Dijkstra and Bellman-Ford both start from a single source. For distances from every vertex to every other vertex, you could run Dijkstra V times, but that rebuilds the priority queue and adjacency list each time, and it fails entirely if any edge is negative. Floyd-Warshall does not fix a source. It puts down a single distance matrix and runs one triple loop, and when it finishes the shortest distance for every pair is in the matrix. The code is twelve lines, it handles negative edges, and it reports whether a negative cycle exists. ...