Prove Floyd-Warshall optimal substructure

This task has two parts: prove the optimal-substructure property that justifies Floyd-Warshall, and implement the corresponding all-pairs shortest-path (APSP) computation for a directed weighted graph.

General proof (theorem)

The proof applies to an arbitrary finite directed weighted graph with no negative cycles anywhere (negative edge weights are allowed when no cycle of negative total weight exists). Ties in shortest-path weight are allowed: any path of minimum total weight is optimal. The proof must establish the intermediate-vertex optimal-substructure recurrence for Floyd-Warshall, including the base case, the induction step, and correctness with negative edges and ties.

Executable companion

Given n vertices labeled 0 to n - 1 and a list of directed edges edges, where each edge is [u, v, w], compute the shortest-path distance from every vertex to every other vertex.

  • If multiple directed edges connect the same ordered pair (u, v), keep the edge with the minimum weight w.
  • The distance from every vertex to itself is 0: dist[i][i] = 0.
  • If j is not reachable from i, set dist[i][j] = 1000000000 (one billion).

Unreachable representation: the value 1000000000 is the explicit unreachable sentinel in the output matrix (it is not -1, because a valid finite shortest distance in this companion may be negative, including -1). Every finite shortest simple path in this executable companion has magnitude at most 99 * 10000 = 990000, so the sentinel cannot collide with any finite answer. Implementations must never add the sentinel to any value; relaxations must be guarded so unreachable sides are skipped. Guarded finite intermediate additions remain within safe 32-bit integer range.

The bounds below apply only to this pedagogical executable companion and do not limit the general proof theorem.

Examples
Input: [1,[]]
Output: [[0]]
Hints

Prove Floyd-Warshall optimal substructure

This task has two parts: prove the optimal-substructure property that justifies Floyd-Warshall, and implement the corresponding all-pairs shortest-path (APSP) computation for a directed weighted graph.