Floyd-Warshall Algorithm: Difference between revisions
Jump to navigation
Jump to search
Line 11: | Line 11: | ||
=Overview= | =Overview= | ||
The Floyd-Warshall algorithm computes all-pairs shortest path in a directed graph with arbitrary edge length, include negative lengths. The only case that does not make sense is whether the graph has negative edge cycles. In this case there is no shortest path, because the cycle can be followed an infinite number of times, leading to negative infinity length paths. If this is the case, the Floyd-Warshall will report the situation and stop. | The Floyd-Warshall algorithm computes all-pairs shortest path in a directed graph with arbitrary edge length, include negative lengths. The only case that does not make sense is whether the graph has negative edge cycles. In this case there is no shortest path, because the cycle can be followed an infinite number of times, leading to negative infinity length paths. If this is the case, the Floyd-Warshall will report the situation and stop. | ||
The running time is O(n<sup>3</sub>). | |||
=Algorithm= | =Algorithm= | ||
We assume that the vertices are labeled from 1 to n. | We assume that the vertices are labeled from 1 to n. |
Revision as of 23:30, 24 November 2021
External
- https://www.coursera.org/learn/algorithms-npcomplete/lecture/VQStd/problem-definition
- https://www.coursera.org/learn/algorithms-npcomplete/lecture/3BBkw/optimal-substructure
- https://www.coursera.org/learn/algorithms-npcomplete/lecture/WKb60/the-floyd-warshall-algorithm
Internal
- All-Pairs Shortest Path Algorithms
- Dynamic Programming
- Bellman-Ford Shortest-Path Algorithm
- Johnson's Algorithm
Overview
The Floyd-Warshall algorithm computes all-pairs shortest path in a directed graph with arbitrary edge length, include negative lengths. The only case that does not make sense is whether the graph has negative edge cycles. In this case there is no shortest path, because the cycle can be followed an infinite number of times, leading to negative infinity length paths. If this is the case, the Floyd-Warshall will report the situation and stop.
The running time is O(n3).
Algorithm
We assume that the vertices are labeled from 1 to n.
Let A = 3D array, indexed by i, j, k # Initialize the tridimensional matrix with the trivial cases │ 0 if i == j for all i,j ∈ V, A[i,j,0] = │ cij if (i, j) ∈ E │ +∞ if i≠j and (i,j) ∉ E for k = 1 to n: for i = 1 to n: for j = 1 to n: A[i,j,k] = max(A[i,j,k-1], A[i,k,k-1] + A[k,j,k-1])