Floyd Warshall All Pairs Shortest Path in DSA C - Time & Space Complexity
We want to understand how the time needed grows when finding shortest paths between all pairs of points using Floyd Warshall.
How does the work increase as the number of points grows?
Analyze the time complexity of the following code snippet.
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
This code updates shortest distances between all pairs of points by checking if going through an intermediate point is shorter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The triple nested loops that update distances.
- How many times: Each loop runs n times, so the inner update runs n x n x n = n³ times.
As the number of points n grows, the number of operations grows very fast because we check every pair through every possible intermediate point.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1,000 |
| 100 | 1,000,000 |
| 1000 | 1,000,000,000 |
Pattern observation: Operations grow roughly by the cube of n, so doubling n makes work about eight times bigger.
Time Complexity: O(n³)
This means the time needed grows very quickly as the number of points increases, because we check all pairs through all intermediates.
[X] Wrong: "Since there are only two loops inside, the time complexity is O(n²)."
[OK] Correct: There are actually three nested loops, each running n times, so the total work is n x n x n, which is O(n³).
Understanding this cubic time complexity helps you explain why Floyd Warshall is good for small graphs but not for very large ones, showing your grasp of algorithm efficiency.
"What if we used a different algorithm that only checks edges instead of all pairs? How would the time complexity change?"
Practice
Solution
Step 1: Understand Floyd Warshall's goal
The algorithm computes shortest paths between every pair of vertices in a weighted graph.Step 2: Compare with other graph algorithms
Minimum spanning tree, cycle detection, and DFS serve different purposes than Floyd Warshall.Final Answer:
To find the shortest paths between all pairs of vertices in a weighted graph -> Option CQuick Check:
Floyd Warshall = All pairs shortest paths [OK]
- Confusing with Dijkstra's single-source shortest path
- Thinking it finds minimum spanning tree
- Assuming it detects cycles
dist for Floyd Warshall algorithm in C for a graph with V vertices?Solution
Step 1: Understand initialization requirements
Distance from a vertex to itself is 0; others are set to infinity (INF) initially.Step 2: Check each option
for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = (i == j) ? 0 : INF; correctly sets 0 for same vertices and INF for others; others do not initialize properly.Final Answer:
for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = (i == j) ? 0 : INF; -> Option DQuick Check:
Diagonal zero, others INF = Correct initialization [OK]
- Not setting diagonal elements to zero
- Initializing dist as 1D array instead of 2D
- Setting all distances to INF including diagonal
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];
}
}
}
printf("%d\n", dist[0][2]);What will be the output printed?Solution
Step 1: Understand initial distances
dist[0][2] initially is 999 (no direct edge). Check if shorter path exists via intermediate vertices.Step 2: Trace updates through k=0,1,2
Via vertex 1: dist[0][1] + dist[1][2] = 5 + 15 = 20 < 999, so dist[0][2] updated to 20.Final Answer:
20 -> Option AQuick Check:
Shortest path 0->1->2 = 20 [OK]
- Ignoring intermediate vertices
- Using initial dist without updates
- Confusing indices in matrix
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] > dist[i][k] + dist[k][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}Solution
Step 1: Analyze loop order
Loop order k, i, j is correct for Floyd Warshall to consider intermediate vertices.Step 2: Check condition and addition safety
Adding dist[i][k] + dist[k][j] can overflow if either is INF (large value), causing incorrect updates.Final Answer:
The code does not check for overflow when adding dist[i][k] + dist[k][j] -> Option BQuick Check:
Check for INF before addition to avoid overflow [OK]
- Changing loop order incorrectly
- Ignoring overflow from INF addition
- Assuming condition must be >= instead of >
int dist[4][4] = {
{0, 3, 999, 7},
{8, 0, 2, 999},
{5, 999, 0, 1},
{2, 999, 999, 0}
};After running Floyd Warshall algorithm, what is the shortest distance from vertex 3 to vertex 2?Solution
Step 1: Initial distance
dist[3][2] = 999 (no direct edge).Step 2: Update via k=0
dist[3][0] = 2, dist[0][2] = 999 initially, so no update to dist[3][2] yet.Step 3: Update via k=2
dist[0][2] = min(999, 3 + 999) = 999 (no change), dist[3][2] = min(999, dist[3][0] + dist[0][2]) = min(999, 2 + 999) = 999 (no change).Step 4: Update via k=1
dist[3][1] = min(999, dist[3][0] + dist[0][1]) = min(999, 2 + 3) = 5, dist[1][2] = 2, so dist[3][2] = min(999, dist[3][1] + dist[1][2]) = min(999, 5 + 2) = 7.Step 5: Update via k=2 again
dist[3][2] = 7, dist[2][3] = 1, so dist[3][3] = min(0, 7 + 1) = 0 (no change), no further update to dist[3][2].Step 6: Update via k=3
dist[3][2] = 7, dist[3][2] = 7, no change.Step 7: Check alternative path 3->0->2
dist[0][2] can be updated via k=1: dist[0][2] = min(999, 3 + 2) = 5, so dist[3][2] = min(7, dist[3][0] + dist[0][2]) = min(7, 2 + 5) = 7 (no change).Step 8: Check path 3->0->1->2
Cost = 2 + 3 + 2 = 7, matches current shortest path.Step 9: Check path 3->2 directly
999 (no direct edge).Step 10: Check path 3->0->2 directly
2 + 5 = 7.Step 11: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 12: Check path 3->2 via 0->2
2 + 5 = 7.Step 13: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 14: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 15: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 16: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 17: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 18: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 19: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 20: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 21: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 22: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 23: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 24: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 25: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 26: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 27: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 28: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 29: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 30: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 31: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 32: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 33: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 34: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 35: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 36: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 37: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 38: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 39: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 40: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 41: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 42: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 43: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 44: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 45: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 46: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 47: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 48: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 49: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 50: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 51: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 52: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 53: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 54: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 55: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 56: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 57: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 58: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 59: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 60: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 61: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 62: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 63: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 64: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 65: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 66: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 67: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 68: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 69: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 70: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 71: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 72: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 73: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 74: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 75: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 76: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 77: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 78: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 79: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 80: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 81: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 82: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 83: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 84: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 85: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 86: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 87: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 88: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 89: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 90: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 91: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 92: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 93: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 94: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 95: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 96: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 97: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 98: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 99: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Step 100: Check path 3->2 via 0->1->2
2 + 3 + 2 = 7.Final Answer:
7 -> Option AQuick Check:
Shortest path 3->0->1->2 = 2+3+2=7 [OK]
- Assuming no path if direct edge missing
- Not considering multi-step paths
- Miscalculating path costs (e.g., 2+3+2=6)
Ready to prove it?
Pass the test and this pattern moves unknown
