0
0
DSA Cprogramming~20 mins

Floyd Warshall All Pairs Shortest Path in DSA C - 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 3-node graph
What is the output distance matrix after running Floyd Warshall on the following graph?

Graph edges with weights:
0 -> 1: 4
0 -> 2: 11
1 -> 2: 2

Initial distance matrix (use 999 for infinity):
[[0, 4, 11], [999, 0, 2], [999, 999, 0]]

Show the final matrix after Floyd Warshall completes.
DSA C
int dist[3][3] = {
  {0, 4, 11},
  {999, 0, 2},
  {999, 999, 0}
};

// Floyd Warshall algorithm runs here
// Output final dist matrix
A[[0, 4, 6], [999, 0, 2], [999, 999, 0]]
B]]0 ,999 ,999[ ,]2 ,0 ,999[ ,]6 ,4 ,0[[
C[[0, 4, 11], [999, 0, 2], [999, 999, 0]]
D[0, 4, 6], [999, 0, 2], [999, 999, 0]]
Attempts:
2 left
💡 Hint
Check if going through node 1 reduces distance from 0 to 2.
🧠 Conceptual
intermediate
1:00remaining
Understanding Floyd Warshall Algorithm Complexity
What is the time complexity of the Floyd Warshall algorithm for a graph with n nodes?
AO(n^3)
BO(n^2)
CO(n log n)
DO(n!)
Attempts:
2 left
💡 Hint
Think about the three nested loops over nodes.
🔧 Debug
advanced
2:00remaining
Identify the error in Floyd Warshall implementation
What error will this Floyd Warshall code produce?

int dist[3][3] = { {0, 5, 999}, {50, 0, 15}, {30, 999, 0} }; for (int k = 0; k < 3; k++) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } } // Note: 999 represents infinity.
ANo error, code runs correctly
BInteger overflow when adding dist[i][k] + dist[k][j]
CArray index out of bounds error
DDivision by zero error
Attempts:
2 left
💡 Hint
Check what happens when adding large values representing infinity.
Predict Output
advanced
3:00remaining
Final distance matrix with negative edge weights but no negative cycles
Given the graph with edges:
0 -> 1: 3
0 -> 2: 8
1 -> 2: -4
2 -> 3: 2
3 -> 1: 1

Initial distance matrix (999 for infinity):
[[0, 3, 8, 999], [999, 0, -4, 999], [999, 999, 0, 2], [999, 1, 999, 0]]

What is the final distance matrix after Floyd Warshall?
A[[0, 3, 8, 999], [999, 0, -4, 999], [999, 999, 0, 2], [999, 1, 999, 0]]
B[[0, 3, 8, 10], [999, 0, -4, -2], [999, 999, 0, 2], [999, 1, 999, 0]]
C[[0, 1, -3, 1], [999, 0, -4, 0], [999, 3, 0, 2], [999, 1, -3, 0]]
D[[0, 1, -3, -1], [999, 0, -4, -2], [999, 3, 0, 2], [999, 1, -3, 0]]
Attempts:
2 left
💡 Hint
Check paths that use negative edges to reduce distances.
🧠 Conceptual
expert
1:30remaining
Detecting negative cycles using Floyd Warshall
How can Floyd Warshall algorithm detect if a graph contains a negative weight cycle?
AIf any dist[i][j] is greater than initial weight
BIf the sum of all distances is negative
CIf any dist[i][i] becomes negative after the algorithm finishes
DIf the number of edges is greater than nodes
Attempts:
2 left
💡 Hint
Think about shortest path from a node to itself.