The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of vertices in a weighted graph, including graphs with negative edge weights but no negative cycles.
Click to reveal answer
beginner
How does the Floyd-Warshall algorithm update distances between nodes?
It updates the shortest distance between two nodes by checking if going through an intermediate node provides a shorter path than the current known distance.
Click to reveal answer
intermediate
What is the time complexity of the Floyd-Warshall algorithm?
The time complexity is O(n³), where n is the number of vertices in the graph, because it uses three nested loops to check all pairs of nodes through all possible intermediate nodes.
Click to reveal answer
intermediate
Can the Floyd-Warshall algorithm handle graphs with negative edge weights?
Yes, it can handle negative edge weights as long as there are no negative weight cycles in the graph, because such cycles would make shortest paths undefined.
Click to reveal answer
intermediate
What is the main difference between Floyd-Warshall and Dijkstra's algorithm?
Floyd-Warshall finds shortest paths between all pairs of nodes, while Dijkstra finds the shortest path from one source node to all others. Floyd-Warshall works with negative edges; Dijkstra does not.
Click to reveal answer
What type of graph does the Floyd-Warshall algorithm work on?
AWeighted graphs with no negative cycles
BOnly unweighted graphs
CGraphs with negative cycles
DOnly directed acyclic graphs
✗ Incorrect
Floyd-Warshall works on weighted graphs and can handle negative edges but not negative cycles.
What is the main idea behind Floyd-Warshall's approach?
AGreedy selection of the next closest node
BUsing intermediate nodes to update shortest paths
CDepth-first search traversal
DSorting edges by weight
✗ Incorrect
The algorithm checks if paths through intermediate nodes are shorter than current known paths.
What is the time complexity of Floyd-Warshall algorithm?
AO(n²)
BO(n log n)
CO(n³)
DO(2^n)
✗ Incorrect
It uses three nested loops over all vertices, resulting in O(n³) time complexity.
Which algorithm is better for finding shortest paths from a single source node?
ADijkstra
BFloyd-Warshall
CBellman-Ford
DPrim's algorithm
✗ Incorrect
Dijkstra is optimized for single-source shortest path problems.
What happens if the graph contains a negative weight cycle?
AFloyd-Warshall detects it and stops
BIt converts negative weights to positive
CAlgorithm runs faster
DShortest paths are undefined
✗ Incorrect
Negative cycles cause shortest paths to be undefined because you can keep reducing path length indefinitely.
Explain how the Floyd-Warshall algorithm finds shortest paths between all pairs of nodes.
Think about checking every possible path through each node.
You got /3 concepts.
Describe the limitations of the Floyd-Warshall algorithm.
Consider what kinds of graphs cause problems and performance issues.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the Floyd-Warshall algorithm in graph theory?
easy
A. To perform depth-first search
B. To find the minimum spanning tree
C. To detect cycles in a graph
D. To find shortest paths between all pairs of vertices
Solution
Step 1: Understand the algorithm's goal
The Floyd-Warshall algorithm is designed to find the shortest paths between every pair of vertices in a graph.
Step 2: Compare with other graph algorithms
Minimum spanning tree algorithms find a tree connecting all vertices, cycle detection finds cycles, and DFS explores nodes, none of which match Floyd-Warshall's purpose.
Final Answer:
To find shortest paths between all pairs of vertices -> Option D
Quick Check:
Floyd-Warshall = All pairs shortest paths [OK]
Hint: Remember: Floyd-Warshall finds shortest paths between all pairs [OK]
Common Mistakes:
Confusing it with minimum spanning tree algorithms
Thinking it finds shortest path from one source only
Mixing it up with cycle detection methods
2. Which of the following correctly represents the core update step in the Floyd-Warshall algorithm for vertices i, j, and k?
easy
A. dist[i][j] = dist[i][k] * dist[k][j]
B. dist[i][j] = max(dist[i][j], dist[i][k] + dist[k][j])
C. dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
D. dist[i][j] = dist[i][j] + dist[k][k]
Solution
Step 1: Recall the update formula
The Floyd-Warshall algorithm updates the shortest distance between i and j by checking if going through k is shorter.
Step 2: Identify the correct mathematical operation
The update uses the minimum of the current distance and the sum of distances through k, which matches dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]).
Final Answer:
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) -> Option C
Quick Check:
Update step uses min sum of paths [OK]
Hint: Update distances using minimum sum through intermediate vertex [OK]
Common Mistakes:
Using max instead of min
Multiplying distances instead of adding
Adding distances incorrectly without min
3. Given the following distance matrix for a graph with vertices 0, 1, 2:
dist = [[0, 3, 999],
[2, 0, 5],
[999, 1, 0]]
After running one iteration of Floyd-Warshall with intermediate vertex k=1, what is the updated distance from vertex 0 to vertex 2?
medium
A. 8
B. 5
C. 999
D. 3
Solution
Step 1: Identify current distances
dist[0][2] = 999 (no direct path), dist[0][1] = 3, dist[1][2] = 5.
Step 2: Calculate new distance via vertex 1
New distance = dist[0][1] + dist[1][2] = 3 + 5 = 8, which is less than 999, so update dist[0][2] to 8.
Final Answer:
8 -> Option A
Quick Check:
Updated dist[0][2] = 3 + 5 = 8 [OK]
Hint: Add distances through k and compare with current [OK]
Common Mistakes:
Not updating because original was 999
Using dist[0][2] directly without checking intermediate
Mixing vertex indices
4. Consider this incorrect Floyd-Warshall update step:
for k in range(n):
for i in range(n):
for j in range(n):
if dist[i][j] > dist[i][k] + dist[j][k]:
dist[i][j] = dist[i][k] + dist[j][k]
What is the main error in this code?
medium
A. The condition uses dist[j][k] instead of dist[k][j]
B. The loops should be reversed in order
C. The update should use max instead of min
D. The code misses initialization of dist matrix
Solution
Step 1: Analyze the update condition
The code compares dist[i][j] with dist[i][k] + dist[j][k], but the second term should be dist[k][j] to represent path i->k->j.
Step 2: Identify the correct indexing
Using dist[j][k] reverses the direction and gives wrong path length, causing incorrect updates.
Final Answer:
The condition uses dist[j][k] instead of dist[k][j] -> Option A
Quick Check:
Correct indexing is dist[k][j] in update [OK]
Hint: Check order of indices in sum: dist[i][k] + dist[k][j] [OK]
Common Mistakes:
Swapping indices in distance matrix
Changing loop order unnecessarily
Confusing min with max in update
5. You have a graph with 4 vertices and the following edges with weights: