What is the main purpose of the Floyd-Warshall algorithm in graph theory?
Think about what kind of paths the algorithm calculates for every pair of points.
The Floyd-Warshall algorithm calculates the shortest distances between every pair of vertices in a weighted graph, even if some edges have negative weights (but no negative cycles).
What is the time complexity of the Floyd-Warshall algorithm for a graph with n vertices?
Consider the three nested loops used in the algorithm.
The Floyd-Warshall algorithm uses three nested loops each running up to n, resulting in O(n^3) time complexity.
After running the Floyd-Warshall algorithm, how can you detect if the graph contains a negative weight cycle?
Think about what the diagonal elements represent in the distance matrix.
If any diagonal element (distance from a vertex to itself) becomes negative, it means there is a negative cycle reachable from that vertex.
Given a graph with 3 vertices and edges: 0→1 (weight 4), 1→2 (weight -2), 2→0 (weight 3), what is the shortest distance from vertex 0 to vertex 2 after running Floyd-Warshall?
Consider the paths from 0 to 2 and their total weights.
Path 0→1→2 has weight 4 + (-2) = 2, which is shorter than the direct path 0→2 (which does not exist). So the shortest distance is 2.
Why can the Floyd-Warshall algorithm handle graphs with negative edge weights but fails if there is a negative weight cycle?
Think about what happens if you keep going around a negative cycle.
Negative cycles allow infinite reductions in path length by looping, so shortest paths are not well-defined. Floyd-Warshall detects this but cannot produce valid shortest paths in such cases.