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)
Initial distance matrix (use 999 for infinity):
[[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)
Initial distance matrix (use 999 for infinity):
[[0, 3, 8, 999], [999, 0, 2, 999], [999, 999, 0, 1], [4, 999, 999, 0]]
DSA Python
def floyd_warshall(dist): n = len(dist) for k in range(n): for i in range(n): for j in range(n): if dist[i][k] + dist[k][j] < dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j] return dist initial_dist = [ [0, 3, 8, 999], [999, 0, 2, 999], [999, 999, 0, 1], [4, 999, 999, 0] ] result = floyd_warshall(initial_dist) print(result)
Attempts:
2 left
💡 Hint
Check if going through intermediate nodes reduces the path cost.
✗ Incorrect
The Floyd Warshall algorithm updates the distance matrix by checking if going through an intermediate node k reduces the distance from i to j. After processing all nodes, the shortest paths are found. Option D shows the correct updated matrix.
🧠 Conceptual
intermediateUnderstanding Floyd Warshall's time complexity
What is the time complexity of the Floyd Warshall algorithm for a graph with n vertices?
Attempts:
2 left
💡 Hint
Think about 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 Floyd Warshall implementation
What error will this code produce when run?
Code snippet:
dist = [[0, 1], [999, 0]] for k in range(len(dist)): for i in range(len(dist)): for j in range(len(dist)): if dist[i][k] + dist[k][j] < dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j] print(dist)
Code snippet:
dist = [[0, 1], [999, 0]] for k in range(len(dist)): for i in range(len(dist)): for j in range(len(dist)): if dist[i][k] + dist[k][j] < dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j] print(dist)
DSA Python
dist = [[0, 1], [999, 0]] for k in range(len(dist)): for i in range(len(dist)): for j in range(len(dist)): if dist[i][k] + dist[k][j] < dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j] print(dist)
Attempts:
2 left
💡 Hint
Check the indices and data types carefully.
✗ Incorrect
The code runs correctly and updates no distances because no shorter path is found. The output remains [[0, 1], [999, 0]].
❓ Predict Output
advancedDetecting negative cycles with Floyd Warshall
After running Floyd Warshall on a graph, how can you detect if there is a negative weight cycle?
DSA Python
def has_negative_cycle(dist): n = len(dist) for i in range(n): if dist[i][i] < 0: return True return False
Attempts:
2 left
💡 Hint
Check the diagonal elements of the distance matrix.
✗ Incorrect
A negative cycle causes the distance from a node to itself to become negative after Floyd Warshall completes.
🚀 Application
expertUsing Floyd Warshall to find shortest path count
Given a graph with edges and weights, how can Floyd Warshall be adapted to also count the number of shortest paths between each pair of vertices?
Attempts:
2 left
💡 Hint
Think about updating path counts when distances improve or tie.
✗ Incorrect
To count shortest paths, maintain a count matrix. When a shorter path is found, reset count; when equal path found, add counts. This requires modifying Floyd Warshall.
