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 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 (3)
Use 999 as infinity for no direct edge.
Initial distance matrix:
[[0, 4, 11], [999, 0, 3], [999, 999, 0]]
Graph edges with weights:
0 -> 1 (4), 0 -> 2 (11), 1 -> 2 (3)
Use 999 as infinity for no direct edge.
Initial distance matrix:
[[0, 4, 11], [999, 0, 3], [999, 999, 0]]
DSA C++
int dist[3][3] = { {0, 4, 11}, {999, 0, 3}, {999, 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]; } } } } // Print dist matrix
Attempts:
2 left
💡 Hint
Check if going through node 1 reduces the distance from 0 to 2.
✗ Incorrect
The shortest path from 0 to 2 is via node 1: 0->1 (4) + 1->2 (3) = 7, which is less than direct 11.
🧠 Conceptual
intermediateUnderstanding Floyd Warshall Algorithm
Which statement correctly describes the Floyd Warshall algorithm?
Attempts:
2 left
💡 Hint
Think about how the algorithm updates distances using intermediate nodes.
✗ Incorrect
Floyd Warshall updates shortest paths by checking if going through each node k improves the path between i and j.
🔧 Debug
advancedIdentify the error in Floyd Warshall implementation
What error will this Floyd Warshall code cause when run on a graph with no negative cycles?
Code snippet:
for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) dist[i][j] = dist[i][k] + dist[k][j]; } } } // dist is a vector> initialized with large values for no edge
Code snippet:
for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) dist[i][j] = dist[i][k] + dist[k][j]; } } } // dist is a vector
DSA C++
for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) dist[i][j] = dist[i][k] + dist[k][j]; } } }
Attempts:
2 left
💡 Hint
Consider what happens when dist[i][k] or dist[k][j] is set to a large value representing infinity.
✗ Incorrect
Adding large infinity values can overflow int and produce incorrect results, so checks are needed before addition.
📝 Syntax
advancedCorrect syntax for Floyd Warshall update condition
Which option is the correct syntax for updating the distance matrix in Floyd Warshall in C++?
Attempts:
2 left
💡 Hint
Remember C++ uses parentheses for conditions and single equals for assignment.
✗ Incorrect
Option B uses correct C++ syntax: if(condition) statement; with assignment using =.
🚀 Application
expertDetecting negative cycles using Floyd Warshall
After running Floyd Warshall on a graph, how can you detect if the graph contains a negative weight cycle?
Attempts:
2 left
💡 Hint
Think about what a negative cycle means for the distance from a node to itself.
✗ Incorrect
A negative cycle causes the distance from a node to itself to become negative after Floyd Warshall.
