From / To | 0 | 1 | 2 | 3 |
---|---|---|---|---|
0 | 0 | 5 | ∞ | 10 |
1 | ∞ | 0 | 3 | ∞ |
2 | ∞ | ∞ | 0 | 1 |
3 | ∞ | ∞ | ∞ | 0 |
1. Algorithm Description: The Floyd-Warshall algorithm computes shortest paths between all pairs of vertices, handling both positive and negative edge weights.
2. Steps of the Floyd-Warshall Algorithm:
3. Time Complexity: O(V³) due to three nested loops over the vertices.
4. Space Complexity: O(V²) as a 2D matrix is required to store distances between all pairs.
5. Usage: Floyd-Warshall is used for finding shortest paths in dense graphs and can handle negative weights but not negative weight cycles.