Floyd-Warshall All-Pairs Shortest Paths

Compute shortest distances between every pair of nodes and reconstruct routes with a next-hop matrix in O(n^3) time.

PythonData Integrity

Code

pythonsnippet
59 lines
INF = float("inf")


def floyd_warshall(
    graph: dict[str, dict[str, float]]
) -> tuple[dict[str, dict[str, float]], dict[str, dict[str, str | None]]]:
    nodes = sorted(graph)
    dist = {source: {target: INF for target in nodes} for source in nodes}
    nxt: dict[str, dict[str, str | None]] = {
        source: {target: None for target in nodes} for source in nodes
    }
    for node in nodes:
        dist[node][node] = 0.0
    for source, edges in graph.items():
        for target, weight in edges.items():
            dist[source][target] = weight
            nxt[source][target] = target

    for middle in nodes:
        for source in nodes:
            if dist[source][middle] == INF:
                continue
            for target in nodes:
                candidate = dist[source][middle] + dist[middle][target]
                if candidate < dist[source][target]:
                    dist[source][target] = candidate
                    nxt[source][target] = nxt[source][middle]
    return dist, nxt


def build_path(
    nxt: dict[str, dict[str, str | None]], source: str, target: str
) -> list[str] | None:
    if source == target:
        return [source]
    if nxt[source][target] is None:
        return None
    route = [source]
    while source != target:
        step = nxt[source][target]
        if step is None:
            return None
        source = step
        route.append(source)
    return route


if __name__ == "__main__":
    graph = {
        "A": {"B": 3.0, "D": 7.0},
        "B": {"A": 8.0, "C": 2.0},
        "C": {"A": 5.0, "D": 1.0},
        "D": {"A": 2.0},
    }
    dist, nxt = floyd_warshall(graph)
    print("A to C:", dist["A"]["C"], build_path(nxt, "A", "C"))
    print("D to C:", dist["D"]["C"], build_path(nxt, "D", "C"))
    print("C to C:", dist["C"]["C"], build_path(nxt, "C", "C"))

Output

Captured from a sandboxed run of this snippet in the build pipeline.

A to C: 5.0 ['A', 'B', 'C']
D to C: 7.0 ['D', 'A', 'B', 'C']
C to C: 0.0 ['C']

How the snippet works

The working surface is the functions floyd_warshall and build_path — everything else is wiring.

Iteration happens inside for node in nodes — everything outside it runs once.

The return value is newly constructed — inputs are never handed back.

Applying it

Entry signature: def floyd_warshall( graph: dict[str, dict[str, float]] ) -> tuple[dict[str, dict[str, float]], dict[str, di…

The demo output is labeled (A to C:, D to C:, C to C:), so every printed line maps back to its originating operation.

Statements that matter

source: {target: None for target in nodes} for source in nodes
}
if nxt[source][target] is None:
    return None
def floyd_warshall(
graph: dict[str, dict[str, float]]
) -> tuple[dict[str, dict[str, float]], dict[str, dict[str, str | None]]]:

Interactive Tools for This Topic

FAQ

Does `floyd_warshall` modify its input?
No. Nothing passed in is written to, which makes the call safe on shared data.
What does Floyd-Warshall All-Pairs Shortest Paths print when run?
Running the file directly is enough — expect labeled output lines such as `A to C:`, `D to C:`, `C to C:`.