Challenge - 5 Problems
Floyd Warshall Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Floyd Warshall on a 3-node graph
What is the output distance matrix after running Floyd Warshall on the following graph?
Graph edges with weights:
0 -> 1: 4
0 -> 2: 11
1 -> 2: 2
Initial distance matrix (use 999 for infinity):
[[0, 4, 11], [999, 0, 2], [999, 999, 0]]
Show the final matrix after Floyd Warshall completes.
Graph edges with weights:
0 -> 1: 4
0 -> 2: 11
1 -> 2: 2
Initial distance matrix (use 999 for infinity):
[[0, 4, 11], [999, 0, 2], [999, 999, 0]]
Show the final matrix after Floyd Warshall completes.
DSA C
int dist[3][3] = { {0, 4, 11}, {999, 0, 2}, {999, 999, 0} }; // Floyd Warshall algorithm runs here // Output final dist matrix
Attempts:
2 left
💡 Hint
Check if going through node 1 reduces distance from 0 to 2.
✗ Incorrect
The shortest path from 0 to 2 is via node 1: 0->1 (4) + 1->2 (2) = 6, which is less than direct 11.
🧠 Conceptual
intermediate1:00remaining
Understanding Floyd Warshall Algorithm Complexity
What is the time complexity of the Floyd Warshall algorithm for a graph with n nodes?
Attempts:
2 left
💡 Hint
Think about the three nested loops over nodes.
✗ Incorrect
Floyd Warshall uses three nested loops each running n times, resulting in O(n^3) complexity.
🔧 Debug
advanced2:00remaining
Identify the error in Floyd Warshall implementation
What error will this Floyd Warshall code produce?
int dist[3][3] = { {0, 5, 999}, {50, 0, 15}, {30, 999, 0} }; for (int k = 0; k < 3; k++) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } // Note: 999 represents infinity.
int dist[3][3] = { {0, 5, 999}, {50, 0, 15}, {30, 999, 0} }; for (int k = 0; k < 3; k++) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } // Note: 999 represents infinity.
Attempts:
2 left
💡 Hint
Check what happens when adding large values representing infinity.
✗ Incorrect
Adding 999 + 999 can overflow int or produce incorrect results, causing wrong comparisons.
❓ Predict Output
advanced3:00remaining
Final distance matrix with negative edge weights but no negative cycles
Given the graph with edges:
0 -> 1: 3
0 -> 2: 8
1 -> 2: -4
2 -> 3: 2
3 -> 1: 1
Initial distance matrix (999 for infinity):
[[0, 3, 8, 999], [999, 0, -4, 999], [999, 999, 0, 2], [999, 1, 999, 0]]
What is the final distance matrix after Floyd Warshall?
0 -> 1: 3
0 -> 2: 8
1 -> 2: -4
2 -> 3: 2
3 -> 1: 1
Initial distance matrix (999 for infinity):
[[0, 3, 8, 999], [999, 0, -4, 999], [999, 999, 0, 2], [999, 1, 999, 0]]
What is the final distance matrix after Floyd Warshall?
Attempts:
2 left
💡 Hint
Check paths that use negative edges to reduce distances.
✗ Incorrect
Floyd Warshall updates distances using intermediate nodes, reducing some paths below initial weights.
🧠 Conceptual
expert1:30remaining
Detecting negative cycles using Floyd Warshall
How can Floyd Warshall algorithm detect if a graph contains a negative weight cycle?
Attempts:
2 left
💡 Hint
Think about shortest path from a node to itself.
✗ Incorrect
A negative value on the diagonal means a negative cycle reachable from that node.