0
0
DSA Typescriptprogramming~20 mins

Floyd Warshall All Pairs Shortest Path in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Floyd Warshall Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]]
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);
A[[0, 3, 5, 7],[8, 0, 2, 3],[5, 8, 0, 1],[4, 7, 9, 0]]
B[[0, 3, 8, 9],[Infinity, 0, 2, 3],[Infinity, Infinity, 0, 1],[4, 7, 12, 0]]
C[[0, 3, 5, 6],[7, 0, 2, 3],[5, 8, 0, 1],[4, 7, 9, 0]]
D]]0 ,21 ,7 ,4[,]1 ,0 ,ytinifnI ,ytinifnI[,]3 ,2 ,0 ,ytinifnI[,]9 ,8 ,3 ,0[[
Attempts:
2 left
💡 Hint
Trace the shortest paths through intermediate vertices step by step.
🧠 Conceptual
intermediate
1:00remaining
Understanding Floyd Warshall's time complexity
What is the time complexity of the Floyd Warshall algorithm for a graph with V vertices?
AO(V^2)
BO(V log V)
CO(V^4)
DO(V^3)
Attempts:
2 left
💡 Hint
Consider the three nested loops over vertices.
🔧 Debug
advanced
1: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]; } } } }
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);
ANo error, outputs correct shortest paths
BTypeError: Cannot add Infinity to a number
CNaN values appear in the matrix
DRuntime error due to index out of bounds
Attempts:
2 left
💡 Hint
Infinity plus a number is still Infinity in JavaScript.
🚀 Application
advanced
1:00remaining
Detecting negative cycles with Floyd Warshall
How can Floyd Warshall algorithm detect a negative weight cycle in a graph?
AIf any element dist[i][j] is Infinity after the algorithm finishes
BIf any diagonal element dist[i][i] becomes negative after the algorithm finishes
CIf the sum of all distances in dist matrix is negative
DIf the number of edges is greater than vertices
Attempts:
2 left
💡 Hint
Think about shortest path from a vertex to itself.
Predict Output
expert
2: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?
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);
A[[0, 7, Infinity],[Infinity, 0, Infinity],[2, 9, 0]]
B[[0, 7, Infinity],[Infinity, 0, Infinity],[2, Infinity, 0]]
C[[0, 7, Infinity],[Infinity, 0, Infinity],[Infinity, Infinity, 0]]
D[[0, 7, Infinity],[Infinity, 0, Infinity],[2, 7, 0]]
Attempts:
2 left
💡 Hint
Check if vertex 2 can reach vertex 1 through vertex 0.