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 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 Infinity for no direct path.
Initial matrix (4x4):
[[0, 3, 8, Infinity],
[Infinity, 0, 2, Infinity],
[Infinity, Infinity, 0, 1],
[4, Infinity, Infinity, 0]]
Graph edges with weights:
0 -> 1 (3), 0 -> 2 (8), 1 -> 2 (2), 2 -> 3 (1), 3 -> 0 (4)
Use Infinity for no direct path.
Initial matrix (4x4):
[[0, 3, 8, Infinity],
[Infinity, 0, 2, Infinity],
[Infinity, Infinity, 0, 1],
[4, Infinity, Infinity, 0]]
DSA Typescript
const INF = Infinity; const dist = [ [0, 3, 8, INF], [INF, 0, 2, INF], [INF, INF, 0, 1], [4, INF, INF, 0] ]; const V = 4; 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]; } } } } console.log(dist);
Attempts:
2 left
💡 Hint
Trace the shortest paths through intermediate vertices step by step.
✗ Incorrect
Option C shows the correct shortest distances after considering all intermediate vertices. For example, distance from 0 to 2 is updated from 8 to 5 via vertex 1 (0->1->2).
🧠 Conceptual
intermediate1:00remaining
Understanding Floyd Warshall's time 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
advanced1:30remaining
Identify the error in Floyd Warshall implementation
What error will this code produce when running Floyd Warshall?
Code snippet:
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]; } } } }
Code snippet:
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]; } } } }
DSA Typescript
const V = 3; const dist = [ [0, 5, Infinity], [50, 0, 15], [30, Infinity, 0] ]; 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]; } } } } console.log(dist);
Attempts:
2 left
💡 Hint
Infinity plus a number is still Infinity in JavaScript.
✗ Incorrect
Adding Infinity to a number in JavaScript results in Infinity, so no error occurs. The code correctly updates shortest paths.
🚀 Application
advanced1:00remaining
Detecting negative cycles with Floyd Warshall
How can Floyd Warshall algorithm detect a negative weight cycle in a graph?
Attempts:
2 left
💡 Hint
Think about shortest path from a vertex to itself.
✗ Incorrect
A negative value on the diagonal dist[i][i] means a negative cycle reachable from vertex i.
❓ Predict Output
expert2:30remaining
Output of Floyd Warshall on a graph with unreachable nodes
Given this initial distance matrix for a graph with 3 vertices:
[[0, 7, Infinity], [Infinity, 0, Infinity], [2, Infinity, 0]]
What is the final distance matrix after running Floyd Warshall?
[[0, 7, Infinity], [Infinity, 0, Infinity], [2, Infinity, 0]]
What is the final distance matrix after running Floyd Warshall?
DSA Typescript
const INF = Infinity; const dist = [ [0, 7, INF], [INF, 0, INF], [2, INF, 0] ]; const V = 3; 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]; } } } } console.log(dist);
Attempts:
2 left
💡 Hint
Check if vertex 2 can reach vertex 1 through vertex 0.
✗ Incorrect
Vertex 2 can reach vertex 1 via vertex 0 with distance 2 + 7 = 9, updating dist[2][1]. Other unreachable paths remain Infinity.