The Floyd–Warshall algorithm compares many possible paths through the graph between each pair of vertices. It is guaranteed to find all shortest paths and is able to do this with $O(|V|^3)$ comparisons in a graph, even though there may be $O(|V|^2)$ edges in the graph. It does so by incrementally improving an estimate on the shortest path between two vertices, until the estimate is optimal.
1
2
3
4
5
6
7
8
9
10
11
letdistbea|V|×|V|arrayofminimumdistancesinitializedto∞(infinity)foreachedge(u,v)dodist[u][v]←w(u,v)// The weight of the edge (u, v)
foreachvertexvdodist[v][v]←0forkfrom1to|V|forifrom1to|V|forjfrom1to|V|ifdist[i][j]>dist[i][k]+dist[k][j]dist[i][j]←dist[i][k]+dist[k][j]endif