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?
DSA Javascript
const INF = 99999; const graph = [ [0, 3, INF, 7], [8, 0, 2, INF], [5, INF, 0, 1], [2, INF, INF, 0] ]; function floydWarshall(graph) { const dist = graph.map(row => row.slice()); const V = graph.length; for (let k = 0; k < V; k++) { for (let i = 0; i < V; i++) { for (let j = 0; j < V; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } return dist; } console.log(floydWarshall(graph));
Attempts:
2 left
💡 Hint
Check if going through intermediate nodes reduces the path cost.
✗ Incorrect
Option B shows the shortest distances between all pairs after considering all intermediate nodes. Other options have incorrect distances due to missing shorter paths.
🧠 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 error in this Floyd Warshall implementation
What error will this code produce when run?
DSA Javascript
const INF = 99999; const graph = [ [0, 1, INF], [INF, 0, 2], [3, INF, 0] ]; function floydWarshall(graph) { const dist = graph.map(row => row.slice()); const V = graph.length; for (let k = 0; k < V; k++) { for (let i = 0; i < V; i++) { for (let j = 0; j < V; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } return dist; } console.log(floydWarshall(graph));
Attempts:
2 left
💡 Hint
Check if the code uses valid indices and initialization.
✗ Incorrect
The code correctly initializes and updates the distance matrix without errors.
❓ Predict Output
advancedDetecting negative cycles with Floyd Warshall
After running Floyd Warshall, how can you detect if the graph contains a negative weight cycle?
DSA Javascript
const INF = 99999; const graph = [ [0, 1, INF], [INF, 0, -1], [-1, INF, 0] ]; function floydWarshall(graph) { const dist = graph.map(row => row.slice()); const V = graph.length; for (let k = 0; k < V; k++) { for (let i = 0; i < V; i++) { for (let j = 0; j < V; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } return dist; } const dist = floydWarshall(graph); const hasNegativeCycle = dist.some((row, i) => row[i] < 0); console.log(hasNegativeCycle);
Attempts:
2 left
💡 Hint
Check the diagonal elements of the distance matrix after running the algorithm.
✗ Incorrect
If any diagonal element dist[i][i] is negative, it means a negative cycle exists reachable from vertex i.
🧠 Conceptual
expertWhy Floyd Warshall is preferred over repeated Dijkstra for dense graphs?
Which reason best explains why Floyd Warshall is often preferred over running Dijkstra from each vertex in dense graphs?
Attempts:
2 left
💡 Hint
Consider edge weights and algorithm capabilities.
✗ Incorrect
Floyd Warshall can handle graphs with negative edge weights (but no negative cycles), while Dijkstra cannot handle negative weights correctly.
