What problem does the Floyd Warshall algorithm solve?
It finds the shortest paths between all pairs of nodes in a weighted graph, including graphs with negative edge weights but no negative cycles.
Click to reveal answer
intermediate
What is the main idea behind the Floyd Warshall algorithm?
It uses dynamic programming to update shortest paths by considering each node as an intermediate point, improving paths step-by-step.
Click to reveal answer
beginner
In Floyd Warshall, what does the matrix entry dist[i][j] represent?
The shortest distance from node i to node j considering intermediate nodes processed so far.
Click to reveal answer
beginner
What is the time complexity of the Floyd Warshall algorithm?
O(n³), where n is the number of nodes in the graph.
Click to reveal answer
intermediate
Can Floyd Warshall detect negative weight cycles? If yes, how?
Yes, by checking if any dist[i][i] becomes negative after the algorithm finishes, indicating a negative cycle reachable from node i.
Click to reveal answer
What does Floyd Warshall algorithm use to find shortest paths?
ADivide and conquer
BGreedy approach
CBacktracking
DDynamic programming
✗ Incorrect
Floyd Warshall uses dynamic programming to iteratively improve shortest paths.
What is the initial value of dist[i][j] if there is no direct edge between i and j?
A0
B1
CInfinity (a very large number)
D-1
✗ Incorrect
Distances between nodes without direct edges are set to infinity to represent no known path initially.
Which of these graphs can Floyd Warshall handle correctly?
AGraphs with negative weight edges but no negative cycles
BGraphs with negative weight cycles
COnly unweighted graphs
DOnly directed acyclic graphs
✗ Incorrect
Floyd Warshall works with negative edges but fails if negative cycles exist.
What does a negative value in dist[i][i] after running Floyd Warshall indicate?
AShortest path found
BNegative weight cycle reachable from node i
CNo path exists
DGraph is disconnected
✗ Incorrect
A negative dist[i][i] means a negative cycle exists involving node i.
What is the space complexity of Floyd Warshall algorithm for a graph with n nodes?
AO(n²)
BO(n)
CO(n³)
DO(1)
✗ Incorrect
The algorithm stores a distance matrix of size n by n, so space complexity is O(n²).
Explain how Floyd Warshall algorithm updates the shortest path matrix step-by-step.
Think about how adding one intermediate node at a time can improve paths.
You got /4 concepts.
Describe how Floyd Warshall can detect negative weight cycles in a graph.
Focus on what the diagonal of the distance matrix represents after the algorithm finishes.
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 a depth-first search traversal
B. To find the minimum spanning tree of a graph
C. To detect cycles in a directed graph
D. To find the shortest paths between all pairs of vertices in a weighted graph
Solution
Step 1: Understand Floyd Warshall algorithm goal
The Floyd Warshall algorithm is designed to find shortest paths between every pair of vertices in a weighted graph, including negative weights but no negative cycles.
Step 2: Compare with other graph algorithms
Minimum spanning tree algorithms find a tree connecting all vertices with minimum total edge weight, cycle detection finds cycles, and DFS is a traversal method. These are different from Floyd Warshall's all pairs shortest path goal.
Final Answer:
To find the shortest paths between all pairs of vertices in a weighted graph -> Option D
Quick Check:
Floyd Warshall = All pairs shortest paths [OK]
Hint: Floyd Warshall finds shortest paths for all vertex pairs [OK]
Common Mistakes:
Confusing with minimum spanning tree algorithms
Thinking it only finds shortest path from one source
Mixing with cycle detection or traversal algorithms
2. Which of the following Rust code snippets correctly initializes the distance matrix for Floyd Warshall with 3 vertices and no edges (infinity represented by i32::MAX)?
easy
A. let mut dist = vec![vec![0; 3]; 3]; for i in 0..3 { dist[i][i] = i32::MAX; }
B. let mut dist = vec![vec![i32::MAX; 3]; 3]; for i in 0..3 { dist[i][i] = 0; }
C. let mut dist = vec![vec![i32::MAX; 3]; 3]; for i in 0..3 { dist[i][i] = i32::MAX; }
D. let mut dist = vec![vec![0; 3]; 3]; for i in 0..3 { dist[i][i] = 0; }
Solution
Step 1: Initialize matrix with infinity
We start with all distances as infinity (i32::MAX) because no edges exist initially.
Step 2: Set distance from vertex to itself as zero
Distance from a vertex to itself is zero, so dist[i][i] = 0 for all i.
Final Answer:
let mut dist = vec![vec![i32::MAX; 3]; 3]; for i in 0..3 { dist[i][i] = 0; } -> Option B
Quick Check:
Init with i32::MAX, diagonal zero [OK]
Hint: Set diagonal to zero, others to infinity for dist matrix [OK]
Common Mistakes:
Setting diagonal to infinity instead of zero
Initializing with zeros everywhere
Mixing up i32::MAX and 0 values
3. Given the following Rust code snippet implementing Floyd Warshall on a graph with 3 vertices and edges: 0->1 (4), 1->2 (3), 0->2 (10), what is the shortest distance from vertex 0 to vertex 2 after running the algorithm?
let n = 3;
let mut dist = vec![vec![i32::MAX; n]; n];
for i in 0..n { dist[i][i] = 0; }
dist[0][1] = 4;
dist[1][2] = 3;
dist[0][2] = 10;
for k in 0..n {
for i in 0..n {
for j in 0..n {
if dist[i][k] != i32::MAX && dist[k][j] != i32::MAX {
dist[i][j] = dist[i][j].min(dist[i][k] + dist[k][j]);
}
}
}
}
println!("{}", dist[0][2]);
medium
A. 7
B. 10
C. 4
D. 3
Solution
Step 1: Initial distances
dist[0][2] starts at 10, dist[0][1] = 4, dist[1][2] = 3.
Step 2: Update via intermediate vertex 1
Check if path 0->1->2 is shorter: 4 + 3 = 7, which is less than 10, so update dist[0][2] to 7.
Final Answer:
7 -> Option A
Quick Check:
0->1->2 = 7 < 10 [OK]
Hint: Check all intermediate vertices to update shortest paths [OK]
Common Mistakes:
Not updating dist[0][2] via vertex 1
Confusing initial edge weights with final shortest paths
Ignoring i32::MAX checks causing overflow
4. Identify the bug in this Rust Floyd Warshall implementation snippet that causes incorrect shortest path results:
for k in 0..n {
for i in 0..n {
for j in 0..n {
if dist[i][k] + dist[k][j] < dist[i][j] {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
medium
A. Using '<' instead of '<=' in comparison
B. Incorrect loop order; should be i, j, k
C. Missing check for i32::MAX before addition causing overflow
D. Not initializing diagonal elements to zero
Solution
Step 1: Analyze addition of distances
Adding dist[i][k] + dist[k][j] without checking if either is i32::MAX can cause integer overflow.
Step 2: Importance of checks before addition
Proper Floyd Warshall requires checking dist[i][k] != i32::MAX and dist[k][j] != i32::MAX before adding to avoid overflow and incorrect updates.
Final Answer:
Missing check for i32::MAX before addition causing overflow -> Option C
Quick Check:
Check for infinity before addition to avoid overflow [OK]
Hint: Always check for infinity before adding distances [OK]
Common Mistakes:
Ignoring overflow from adding i32::MAX
Changing loop order unnecessarily
Confusing comparison operators
Forgetting to initialize diagonal distances
5. You have a graph with 4 vertices and edges: 0->1 (5), 1->2 (2), 2->3 (1), and 0->3 (20). Using Floyd Warshall, what is the shortest distance from vertex 0 to vertex 3 after the algorithm completes?
hard
A. 8
B. 20
C. 5
D. 10
Solution
Step 1: Initial distances
Direct edge 0->3 is 20. Other edges: 0->1=5, 1->2=2, 2->3=1.
Step 2: Calculate path 0->1->2->3
Sum is 5 + 2 + 1 = 8, which is less than 20, so shortest path updates to 8.
Final Answer:
8 -> Option A
Quick Check:
0->1->2->3 = 8 < 20 [OK]
Hint: Sum edges along path to find shorter distance than direct edge [OK]
Common Mistakes:
Choosing direct edge weight without checking intermediate paths
Adding edges incorrectly
Ignoring path through multiple vertices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.