floydWarshall(
graph):FloydWarshallResult
Defined in: packages/graph/src/floyd-warshall.ts:50
Compute the shortest distance between every pair of vertices with the
Floyd-Warshall algorithm. O(n³ + R) time and 8n² bytes, where R is
the cost of enumerating every row once: O(n + m) on
compressed-sparse-row and adjacency-list, parallel copies included, and
O(n²) on adjacency-matrix.
Named for the algorithm because its cost is the caller’s to check. It runs
on any layout and allocates its own distance matrix, so an all-pairs
question does not decide the storage. Negative weights are allowed. On an
undirected graph a negative edge is itself a negative cycle, since it can be
walked back and forth. Parallel edges contribute their smallest weight, and
an unweighted edge weighs 1. When some cycle is negative, a Bellman-Ford
pass over the rows, O(n · R) more, finds a vertex on that cycle.
Distances are float64 sums. They are exact while the absolute weights along
every path sum to at most Number.MAX_VALUE; a partial sum past it rounds
to Infinity, which reads as unreachable.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Throws
Section titled “Throws”With code MAX_CAPACITY_EXCEEDED when 8n² bytes would pass
GRAPH_MAX_MATRIX_BYTES or the runtime refuses the allocation.
Example
Section titled “Example”const result = floydWarshall(graph)if (!result.negativeCycle) result.distance[from * graph.vertexCount + to]