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 9999 to represent infinity.
Initial distance matrix (4 vertices):
[[0, 3, 8, 9999], [9999, 0, 2, 9999], [9999, 9999, 0, 1], [4, 9999, 9999, 0]]
Graph edges with weights:
0 -> 1 (3), 0 -> 2 (8), 1 -> 2 (2), 2 -> 3 (1), 3 -> 0 (4)
Use 9999 to represent infinity.
Initial distance matrix (4 vertices):
[[0, 3, 8, 9999], [9999, 0, 2, 9999], [9999, 9999, 0, 1], [4, 9999, 9999, 0]]
DSA C#
int INF = 9999; int[,] dist = { {0, 3, 8, INF}, {INF, 0, 2, INF}, {INF, INF, 0, 1}, {4, INF, INF, 0} }; int V = 4; for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i,j] > dist[i,k] + dist[k,j]) { dist[i,j] = dist[i,k] + dist[k,j]; } } } } // Print dist matrix
Attempts:
2 left
💡 Hint
Trace the shortest paths step by step using intermediate vertices.
✗ Incorrect
Floyd Warshall updates distances by checking if going through an intermediate vertex k gives a shorter path. After all iterations, the matrix shows shortest distances between all pairs.
🧠 Conceptual
intermediateUnderstanding Floyd Warshall Algorithm Complexity
What is the time complexity of the Floyd Warshall algorithm for a graph with V vertices?
Attempts:
2 left
💡 Hint
Consider the three nested loops over vertices.
✗ Incorrect
Floyd Warshall uses three nested loops each running V times, resulting in O(V^3) time complexity.
🔧 Debug
advancedIdentify the bug in Floyd Warshall implementation
What error will this code cause when running Floyd Warshall?
Code snippet:
int INF = 9999;
int[,] dist = new int[V,V];
// dist initialized with INF except dist[i,i] = 0
for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i,j] > dist[i,k] + dist[k,j]) { dist[i,j] = dist[i,k] + dist[k,j]; } } } }
Code snippet:
int INF = 9999;
int[,] dist = new int[V,V];
// dist initialized with INF except dist[i,i] = 0
for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i,j] > dist[i,k] + dist[k,j]) { dist[i,j] = dist[i,k] + dist[k,j]; } } } }
Attempts:
2 left
💡 Hint
Check what happens when dist[i,k] or dist[k,j] is INF.
✗ Incorrect
Adding INF (9999) to another number can exceed int max or produce incorrect results, causing overflow or wrong comparisons.
📝 Syntax
advancedSyntax error in Floyd Warshall nested loops
Which option contains a syntax error in the Floyd Warshall nested loops?
DSA C#
for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i,j] > dist[i,k] + dist[k,j]) { dist[i,j] = dist[i,k] + dist[k,j]; } } } }
Attempts:
2 left
💡 Hint
Check the syntax of the for loop declarations.
✗ Incorrect
In C#, for loops require parentheses around initialization, condition, and increment. Option D misses parentheses causing syntax error.
🚀 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
Check the diagonal elements of the distance matrix after the algorithm.
✗ Incorrect
A negative cycle causes the distance from a vertex to itself to become negative after Floyd Warshall completes.
