Challenge - 5 Problems
Floyd Warshall Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Floyd Warshall on a simple graph
What is the output distance matrix after running Floyd Warshall on this graph?
Graph edges with weights:
0 -> 1 (3), 0 -> 2 (8), 1 -> 2 (2), 2 -> 3 (1), 3 -> 0 (4)
Use 999 as infinity for no direct edge.
Initial distance matrix (4x4):
[[0, 3, 8, 999], [999, 0, 2, 999], [999, 999, 0, 1], [4, 999, 999, 0]]
Graph edges with weights:
0 -> 1 (3), 0 -> 2 (8), 1 -> 2 (2), 2 -> 3 (1), 3 -> 0 (4)
Use 999 as infinity for no direct edge.
Initial distance matrix (4x4):
[[0, 3, 8, 999], [999, 0, 2, 999], [999, 999, 0, 1], [4, 999, 999, 0]]
DSA Rust
fn floyd_warshall(mut dist: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = dist.len();
for k in 0..n {
for i in 0..n {
for j in 0..n {
if dist[i][k] != 999 && dist[k][j] != 999 {
let new_dist = dist[i][k] + dist[k][j];
if new_dist < dist[i][j] {
dist[i][j] = new_dist;
}
}
}
}
}
dist
}
fn main() {
let dist = vec![
vec![0, 3, 8, 999],
vec![999, 0, 2, 999],
vec![999, 999, 0, 1],
vec![4, 999, 999, 0],
];
let result = floyd_warshall(dist);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Trace the shortest paths through intermediate nodes step by step.
✗ Incorrect
Option C correctly updates distances using intermediate nodes. For example, distance from 0 to 2 is updated from 8 to 5 via node 1 (0->1->2). Other options have incorrect distances or miss updates.
🧠 Conceptual
intermediateUnderstanding Floyd Warshall Algorithm Complexity
What is the time complexity of the Floyd Warshall algorithm for a graph with n vertices?
Attempts:
2 left
💡 Hint
Consider the three nested loops over vertices.
✗ Incorrect
Floyd Warshall uses three nested loops each running n times, resulting in O(n^3) time complexity.
🔧 Debug
advancedIdentify the error in this Floyd Warshall implementation
What error will this Rust code produce when running Floyd Warshall?
Code snippet:
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]; } } } }
Code snippet:
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]; } } } }
DSA Rust
fn floyd_warshall(mut dist: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n = dist.len();
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];
}
}
}
}
dist
}Attempts:
2 left
💡 Hint
Check what happens when dist[i][k] or dist[k][j] is 999 (infinity).
✗ Incorrect
Adding distances without checking if either is infinity (999) can cause incorrect updates because 999 + 999 is not a valid path length.
❓ Predict Output
advancedDetecting negative cycles with Floyd Warshall
After running Floyd Warshall on a graph, how can you detect if there is a negative cycle?
DSA Rust
fn has_negative_cycle(dist: &Vec<Vec<i32>>) -> bool { for i in 0..dist.len() { if dist[i][i] < 0 { return true; } } false }
Attempts:
2 left
💡 Hint
Think about what a negative cycle means for distance from a node to itself.
✗ Incorrect
A negative cycle causes the distance from a node to itself to become negative after Floyd Warshall completes.
🚀 Application
expertFloyd Warshall with path reconstruction
Which code snippet correctly updates the path matrix to reconstruct shortest paths during Floyd Warshall?
DSA Rust
let mut dist = vec![vec![999; n]; n]; let mut next = vec![vec![None; n]; n]; // Initialization omitted for k in 0..n { for i in 0..n { for j in 0..n { if dist[i][k] != 999 && dist[k][j] != 999 { let new_dist = dist[i][k] + dist[k][j]; if new_dist < dist[i][j] { dist[i][j] = new_dist;
Attempts:
2 left
💡 Hint
When updating dist[i][j], the next node on path from i to j is the next node on path from i to k.
✗ Incorrect
To reconstruct the path, next[i][j] should point to the next node after i on the shortest path to j. When updating via k, this is next[i][k].
