Floyd Warshall All Pairs Shortest Path in DSA Java - Time & Space Complexity
We want to understand how the time needed to find shortest paths between all pairs of points grows as the number of points increases.
How does the work increase when we add more points to the map?
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 the shortest distances between all pairs of points by checking if going through an intermediate point k gives a shorter path.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The triple nested loops that check and update distances.
- How many times: Each loop runs n times, so the inner operation runs n x n x n = n³ times.
As the number of points n grows, the number of checks 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: When n increases by 10 times, operations increase by 1000 times (cube growth).
Time Complexity: O(n³)
This means the time needed grows very quickly as the number of points increases, roughly proportional to the cube of n.
[X] Wrong: "Since there are three loops, the time complexity is just O(n) because loops are simple."
[OK] Correct: Each loop runs n times, so the total work multiplies, not adds. The work is n x n x n, which is much bigger than just 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 replaced the triple nested loops with a recursive approach that splits the problem? How might that affect the time complexity?"
Practice
Solution
Step 1: Understand Floyd Warshall purpose
The Floyd Warshall algorithm is designed to find shortest paths between every pair of vertices in a weighted graph.Step 2: Compare with other graph algorithms
Minimum spanning tree, depth-first traversal, and maximum flow solve different problems, not all pairs shortest paths.Final Answer:
Shortest paths between all pairs of vertices -> Option AQuick Check:
Floyd Warshall = All pairs shortest paths [OK]
- Confusing with Dijkstra's single-source shortest path
- Thinking it finds minimum spanning tree
- Mixing with traversal algorithms
dist for Floyd Warshall in Java for a graph with V vertices?Solution
Step 1: Understand initialization needs
Distance from a vertex to itself should be 0, and to others should be infinity (represented by Integer.MAX_VALUE).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 : Integer.MAX_VALUE; correctly sets 0 for same vertices and max value for others. for (int i = 0; i < V; i++) dist[i] = new int[V]; only initializes array but not values. dist = new int[V][V]; for (int i = 0; i < V; i++) dist[i][i] = 1; sets diagonal to 1 incorrectly. for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = 0; sets all to 0, which is wrong.Final Answer:
for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) dist[i][j] = (i == j) ? 0 : Integer.MAX_VALUE; -> Option AQuick Check:
Initialize dist[i][i] = 0, others = infinity [OK]
- Not setting distance to self as zero
- Setting all distances to zero
- Forgetting to initialize the matrix fully
int V = 3;
int[][] dist = {
{0, 4, 10},
{Integer.MAX_VALUE, 0, 3},
{Integer.MAX_VALUE, Integer.MAX_VALUE, 0}
};
// Floyd Warshall algorithm applied here
Solution
Step 1: Initial distances
Start with dist matrix: 0->1=4, 1->2=3, 0->2=10, others infinity or 0 on diagonal.Step 2: Update using intermediate vertices
Check if path 0->1->2 is shorter than 0->2 directly: 4 + 3 = 7 < 10, so update dist[0][2] = 7.Final Answer:
[[0, 4, 7], [∞, 0, 3], [∞, ∞, 0]] -> Option DQuick Check:
0->2 updated to 7 via 1 [OK]
- Not updating indirect shorter paths
- Confusing infinity with zero
- Ignoring intermediate vertex updates
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}Assume
dist uses Integer.MAX_VALUE for infinity.Solution
Step 1: Understand addition with infinity
Adding Integer.MAX_VALUE + any positive number causes overflow and incorrect results.Step 2: Check code for safety
Code lacks checks to ensure dist[i][k] and dist[k][j] are not Integer.MAX_VALUE before adding.Final Answer:
No check for Integer.MAX_VALUE before addition causing overflow -> Option CQuick Check:
Check for infinity before addition to avoid overflow [OK]
- Ignoring overflow from Integer.MAX_VALUE + number
- Assuming loops order causes error
- Confusing comparison operator direction
0->1=5, 0->3=10, 1->2=3, 2->3=1
Use Floyd Warshall to find the shortest distance from vertex 0 to 3 after all updates.
What is the shortest distance from 0 to 3?
Solution
Step 1: Initial distances
Direct edges: 0->1=5, 0->3=10, 1->2=3, 2->3=1. Other distances are infinity or 0 on diagonal.Step 2: Check indirect paths
Path 0->1->2->3 distance = 5 + 3 + 1 = 9, which is less than direct 0->3=10, so update shortest distance to 9.Final Answer:
9 -> Option BQuick Check:
0->1->2->3 = 9 < 10 [OK]
- Ignoring multi-step paths
- Choosing direct edge without checking alternatives
- Adding edges incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
