Algorithms And Data Structures Codexery

Floyd–Warshall algorithm

Algorithm for all-pairs shortest paths in weighted graphs.

Floyd–Warshall algorithm

The Floyd–Warshall algorithm is an algorithm in computer science for finding shortest paths in a directed weighted graph with positive or negative edge weights, provided there are no negative cycles. A single execution finds the lengths of shortest paths between all pairs of vertices, and versions of the algorithm can also be used for finding the transitive closure of a relation or widest paths between all pairs of vertices in a weighted graph.

field
Computer science
known_for
All-pairs shortest path algorithm, transitive closure, widest paths

Lore & Background

The Floyd–Warshall algorithm is an example of dynamic programming. The algorithm compares many possible paths through the graph between each pair of vertices. It is guaranteed to find all shortest paths and does so with Θ(|V|³) comparisons, even though there may be Θ(|V|²) edges. It works by incrementally improving an estimate on the shortest path between two vertices until the estimate is optimal. The algorithm uses a recursive formula: shortestPath(i,j,k) = min(shortestPath(i,j,k-1), shortestPath(i,k,k-1) + shortestPath(k,j,k-1)), with the base case shortestPath(i,j,0) = w(i,j).

Reader's Guide

The Floyd–Warshall algorithm is significant as a foundational dynamic programming solution for the all-pairs shortest path problem. Its ability to handle positive or negative edge weights (without negative cycles) makes it versatile, though its Θ(|V|³) time complexity limits it to graphs of moderate size. Beyond shortest paths, the algorithm's variants serve other purposes: finding transitive closure of a relation and, in connection with the Schulze voting system, computing widest paths between all pairs of vertices. Its legacy lies in its simplicity, elegance, and broad applicability across graph theory, network analysis, and voting systems.

Did You Know?

More in Algorithms And Data Structures 1-24

Elsewhere in the Algorithms And Data Structures universe

Spotted an error? Know more?

This is a living reference — every entry is fact-audited, and reader corrections feed straight into our audit queue. Suggest an edit · See this site's audit record

Comments

Loading…
Open in the interactive codex →