Given a weighted graph, return the shortest distances between every pair of nodes within the graph, where the graph can contain negative weights. It solves the all-pair shortest path problem.

Restrictions

Abstract

Operates by repeatedly lowering the distance between two nodes by checking if the shortest path between and involving an intermediate node is shorter. A good explanation is contained in this YouTube video. The -th instance of the outermost loop represents the algorithm has found shortest distances out of all possible paths involving nodes or less.

Efficiency

Time complexity:

Implementation

Proof