Run Floyd-Warshall on 4 vertices, show distance matrices
A directed weighted graph has exactly four vertices labelled 0, 1, 2 and 3. The graph is given as a list of directed edges, where each edge is [u, v, w] and means an edge from u to v with integer weight w.
Run Floyd-Warshall and return every intermediate distance matrix, not only the final one. The output is an array of five matrices of shape 4 x 4. Matrix index k records the shortest distances when only vertices 0 through k - 1 may appear strictly inside a path. In other words 0 is the initial matrix, 1 allows vertex 0 as an intermediate, 2 allows 0 and 1, 3 allows 0, 1 and 2, and 4 allows all four vertices.
Rules for the numbers inside each matrix:
- The diagonal always holds
0for a vertex to itself. - A pair with no path yet uses the sentinel
1000000. - Weights may be negative, and there are no negative cycles, so every finite distance stays inside
[-3000, 3000]and is far below the sentinel. - If several edges join the same ordered pair, the smallest weight applies.
- A relaxation must ignore pairs whose current distance is still the sentinel, so an unreachable side can never be combined with a negative weight.
Each matrix is a snapshot. Matrix k is produced from matrix k - 1 by allowing vertex k - 1 as an intermediate, so later vertices are never used too early.
Run Floyd-Warshall on 4 vertices, show distance matrices
A directed weighted graph has exactly four vertices labelled `0`, `1`, `2` and `3`. The graph is given as a list of directed edges, where each edge is `[u, v, w]` and means an edge from `u` to `v` with integer weight `w`.