Compute all-pairs shortest distances in a weighted directed graph with Floyd-Warshall

Quick Overview

Implement the Floyd-Warshall algorithm to find the shortest distance between every pair of nodes in a weighted directed graph given as an adjacency matrix. It tests dynamic programming over intermediate nodes, careful handling of an infinity sentinel, and negative edge weights without negative cycles.

Compute all-pairs shortest distances in a weighted directed graph with Floyd-Warshall

Company: Wells Fargo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a weighted directed graph with `n` nodes labeled `0` to `n - 1`, represented as an `n x n` adjacency matrix `matrix`. `matrix[i][j]` is the weight of the edge from node `i` to node `j`, or `1000000000` (${10^9}$), which stands for infinity, when there is no direct edge. Implement the Floyd-Warshall algorithm to compute the shortest distance between every pair of nodes. Return an `n x n` matrix `result` where `result[i][j]` is the shortest distance from node `i` to node `j`. If no path exists from `i` to `j`, `result[i][j]` stays `1000000000`. The distance from any node to itself is `0`. ### Function Signature ```python def floyd_warshall(matrix: list[list[int]]) -> list[list[int]]: ... ``` ### Rules - `1000000000` is the only value that means "no edge" in the input and "no path" in the output. Every pair connected by some path gets its exact shortest distance, which can be negative. - Edge weights can be negative, but the graph has no negative weight cycles, so every shortest distance is well defined and `result[i][i]` is `0`. ### Constraints - `1 <= n <= 100` - `matrix.length == n` and `matrix[i].length == n` - `matrix[i][i] == 0` - `-10^4 <= matrix[i][j] <= 10^4`, or `matrix[i][j] == 10^9` (infinity) - The graph contains no negative weight cycles. ### Examples ```text Input: matrix = [[0, 3, 1000000000, 7], [8, 0, 2, 1000000000], [5, 1000000000, 0, 1], [2, 1000000000, 1000000000, 0]] Output: [[0, 3, 5, 6], [5, 0, 2, 3], [3, 6, 0, 1], [2, 5, 7, 0]] ``` The edges are 0->1 (3), 0->3 (7), 1->0 (8), 1->2 (2), 2->0 (5), 2->3 (1) and 3->0 (2). The shortest path from 0 to 2 goes through 1 at cost 3 + 2 = 5, and the shortest path from 2 to 1 is 2->3->0->1 at cost 1 + 2 + 3 = 6. ```text Input: matrix = [[0, 5, 1000000000], [1000000000, 0, 3], [1000000000, 1000000000, 0]] Output: [[0, 5, 8], [1000000000, 0, 3], [1000000000, 1000000000, 0]] ``` The graph is the chain 0->1->2, so node 0 reaches node 2 at cost 5 + 3 = 8. No edge leads back toward node 0 or from node 2 to node 1, so those entries stay at infinity. ```text Input: matrix = [[0, 4, 2, 1000000000], [1000000000, 0, -3, 1000000000], [1000000000, 1000000000, 0, 1000000000], [1000000000, 1000000000, 1000000000, 0]] Output: [[0, 4, 1, 1000000000], [1000000000, 0, -3, 1000000000], [1000000000, 1000000000, 0, 1000000000], [1000000000, 1000000000, 1000000000, 0]] ``` The path 0->1->2 costs 4 + (-3) = 1, which beats the direct edge of weight 2. Node 3 has no edges, so its row and column stay at `1000000000` everywhere except the diagonal, even though node 1 has a negative outgoing edge.

Overview: Implement the Floyd-Warshall algorithm to find the shortest distance between every pair of nodes in a weighted directed graph given as an adjacency matrix. It tests dynamic programming over intermediate nodes, careful handling of an infinity sentinel, and negative edge weights without negative cycles.

Wells Fargo logo
Wells Fargo
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a weighted directed graph with n nodes labeled 0 to n - 1, represented as an n x n adjacency matrix matrix. matrix[i][j] is the weight of the edge from node i to node j, or 1000000000 (109{10^9}), which stands for infinity, when there is no direct edge. Implement the Floyd-Warshall algorithm to compute the shortest distance between every pair of nodes.

Return an n x n matrix result where result[i][j] is the shortest distance from node i to node j. If no path exists from i to j, result[i][j] stays 1000000000. The distance from any node to itself is 0.

Function Signature

def floyd_warshall(matrix: list[list[int]]) -> list[list[int]]:
    ...

Rules

  • 1000000000 is the only value that means "no edge" in the input and "no path" in the output. Every pair connected by some path gets its exact shortest distance, which can be negative.
  • Edge weights can be negative, but the graph has no negative weight cycles, so every shortest distance is well defined and result[i][i] is 0 .

Constraints

  • 1 <= n <= 100
  • matrix.length == n and matrix[i].length == n
  • matrix[i][i] == 0
  • -10^4 <= matrix[i][j] <= 10^4 , or matrix[i][j] == 10^9 (infinity)
  • The graph contains no negative weight cycles.

Examples

Input: matrix = [[0, 3, 1000000000, 7], [8, 0, 2, 1000000000], [5, 1000000000, 0, 1], [2, 1000000000, 1000000000, 0]]
Output: [[0, 3, 5, 6], [5, 0, 2, 3], [3, 6, 0, 1], [2, 5, 7, 0]]

The edges are 0->1 (3), 0->3 (7), 1->0 (8), 1->2 (2), 2->0 (5), 2->3 (1) and 3->0 (2). The shortest path from 0 to 2 goes through 1 at cost 3 + 2 = 5, and the shortest path from 2 to 1 is 2->3->0->1 at cost 1 + 2 + 3 = 6.

Input: matrix = [[0, 5, 1000000000], [1000000000, 0, 3], [1000000000, 1000000000, 0]]
Output: [[0, 5, 8], [1000000000, 0, 3], [1000000000, 1000000000, 0]]

The graph is the chain 0->1->2, so node 0 reaches node 2 at cost 5 + 3 = 8. No edge leads back toward node 0 or from node 2 to node 1, so those entries stay at infinity.

Input: matrix = [[0, 4, 2, 1000000000], [1000000000, 0, -3, 1000000000], [1000000000, 1000000000, 0, 1000000000], [1000000000, 1000000000, 1000000000, 0]]
Output: [[0, 4, 1, 1000000000], [1000000000, 0, -3, 1000000000], [1000000000, 1000000000, 0, 1000000000], [1000000000, 1000000000, 1000000000, 0]]

The path 0->1->2 costs 4 + (-3) = 1, which beats the direct edge of weight 2. Node 3 has no edges, so its row and column stay at 1000000000 everywhere except the diagonal, even though node 1 has a negative outgoing edge.