Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Floyd-Warshall proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Compute all-pairs shortest paths by progressively allowing each vertex as an intermediate. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Floyd-Warshall when the prompt's constraints and required operations match this shape: Compute all-pairs shortest paths by progressively allowing each vertex as an intermediate.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Floyd-Warshall maintains a distance matrix for every source-target pair. Initialize zero diagonals, direct edge weights, and unreachable sentinels.
At layer k, each pair may either avoid vertex k or travel source to k plus k to target. Both subpaths use only earlier allowed intermediates.
def floyd_layers(matrix):
distance=[row.copy() for row in matrix];trace=[];n=len(matrix)
for middle in range(n):
previous=[row.copy() for row in distance]
for source in range(n):
for target in range(n):
if previous[source][middle] is not None and previous[middle][target] is not None:
candidate=previous[source][middle]+previous[middle][target]
if distance[source][target] is None or candidate<distance[source][target]:distance[source][target]=candidate
trace.append([row.copy() for row in distance])
return traceImplement floyd_layers(matrix). None means unreachable; return the distance matrix after each allowed intermediate vertex.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Only add the two subpaths when both are reachable. With explicit None sentinels this guard prevents invalid arithmetic from creating fake paths.
def all_pairs_distances(matrix):
distance=[row.copy() for row in matrix];n=len(matrix)
for middle in range(n):
for source in range(n):
for target in range(n):
if distance[source][middle] is not None and distance[middle][target] is not None:
candidate=distance[source][middle]+distance[middle][target]
if distance[source][target] is None or candidate<distance[source][target]:distance[source][target]=candidate
return distanceImplement all_pairs_distances(matrix). None means unreachable; return shortest distances after all intermediates.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
After all layers, a negative distance from a vertex to itself reveals a reachable negative cycle for that component. Path reconstruction needs a next-hop or predecessor matrix updated with distances.
Say: “After layer k, distance[i][j] is optimal using only intermediates through k. The recurrence either skips k or joins two earlier subpaths through it.” State O(V cubed) time and O(V squared) space.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Floyd-Warshall proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Floyd-Warshall complete workflow | O(V^3) | O(V^3) | Copy the matrix, convert absent edges to infinity, and apply the intermediate-vertex recurrence in k-first order. |
O(V^2) for the focused Compute All-Pairs Shortest Paths implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: Edge direction and weight constraints match the relaxation and finalization rules of the chosen method.
Avoid
def all_pairs_shortest_paths(matrix):
passUse instead
def all_pairs_shortest_paths(matrix):
size = len(matrix)
infinity = float('inf')
distances = [[infinity if value == -1 else value for value in row] for row in matrix]
for middle in range(size):
for start in range(size):
if distances[start][middle] == infinity:
continue
for end in range(size):
candidate = distances[start][middle] + distances[middle][end]
if candidate < distances[start][end]:
distances[start][end] = candidate
return [[-1 if value == infinity else value for value in row] for row in distances]Where you will hit this: Compute All-Pairs Shortest Paths(opens in a new tab)
Returns the input matrix without discovering a path through an intermediate vertex.
Prevent it: Preserve this proof obligation: Every recorded distance is a valid path cost, and the method eventually considers every path that could improve it.
Avoid
def all_pairs_shortest_paths(matrix):
return [row[:] for row in matrix]Use instead
def all_pairs_shortest_paths(matrix):
size = len(matrix)
infinity = float('inf')
distances = [[infinity if value == -1 else value for value in row] for row in matrix]
for middle in range(size):
for start in range(size):
if distances[start][middle] == infinity:
continue
for end in range(size):
candidate = distances[start][middle] + distances[middle][end]
if candidate < distances[start][end]:
distances[start][end] = candidate
return [[-1 if value == infinity else value for value in row] for row in distances]Where you will hit this: Compute All-Pairs Shortest Paths(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(V^3).
Avoid
def all_pairs_shortest_paths(matrix):
return [row[:] for row in matrix]Use instead
def all_pairs_shortest_paths(matrix):
size = len(matrix)
infinity = float('inf')
distances = [[infinity if value == -1 else value for value in row] for row in matrix]
for middle in range(size):
for start in range(size):
if distances[start][middle] == infinity:
continue
for end in range(size):
candidate = distances[start][middle] + distances[middle][end]
if candidate < distances[start][end]:
distances[start][end] = candidate
return [[-1 if value == infinity else value for value in row] for row in distances]Where you will hit this: Lowest Common Ancestor in a BST(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27