# Catalogue of Artificial Intelligence Techniques

## Floyd-Warshall Algorithm

**Aliases:**
Floyd's Algorithm, Roy-Floyd Algorithm, Warshall's Algorithm

**Keywords:**
all, directed, graph, pairs, path, shortest, theory

### Categories: Game Theory

Author(s): **Alex Price**

The Floyd-Warshall algorithm is an algorithm for solving the all pairs shortest paths problem on weighted, directed graphs. That is to say, it finds the shortest route between all possible pairs of vertices on a graph where all the edges have a specific weight and direction. Negative weight edges may be present, but we assume there are no negative weight cycles. The running time complexity is Θ(n³) (it runs in cubic time)

The algorithm works like this:

Assume the vertices of the directed graph G are V = {1,2,...,n}, consider a subset {1,2,...,k} for some k. For any pair of vertices i,j ∈ V, consider all paths from i to j whose intermediate vertices are all taken from {1,2,...,k}, and let p be a minimum weight path from among them. The algorithm exploits a relationship between path p and shortest paths from i to j with all intermediate vertices in the set {1,2,...,k−1}. The relationship depends on whether or not k is an intermediate vertex of path p.

* If k is not an intermediate vertex of path p, then all intermediate vertices of path p are in the set {1,2,...,k-1}

* If k is an intermediate vertex of p then the algorithm breaks down the path into two parts, i to k (p₁) and k to j (p₂), p₁ and p₂ are the shortest paths between their two vertices with intermediate vertices in the set {1,2,...,k-1}

### References:

- Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein,
*Introduction to algorithms*, MIT Press, 2001, pp.629.

### Comments:

No comments.