0
0
DSA Cprogramming~10 mins

Floyd Warshall All Pairs Shortest Path in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Floyd Warshall All Pairs Shortest Path
Start with distance matrix
Pick intermediate node k
For each pair (i, j)
Check if path i->k + k->j < i->j
Update dist[i
Repeat for all k
Final shortest paths matrix
The algorithm updates shortest paths by considering each node as an intermediate point, improving distances step-by-step until all pairs shortest paths are found.
Execution Sample
DSA C
int dist[V][V] = {
  {0, 3, INF, 7},
  {8, 0, 2, INF},
  {5, INF, 0, 1},
  {2, INF, INF, 0}
};
// Floyd Warshall updates dist
This code initializes a graph's distance matrix and runs Floyd Warshall to find shortest paths between all pairs.
Execution Table
StepIntermediate Node kPair (i,j)Old dist[i][j]New dist[i][j]Update?Distance Matrix State
10(0,0)00No[[0,3,INF,7],[8,0,2,INF],[5,INF,0,1],[2,INF,INF,0]]
20(0,1)33No[[0,3,INF,7],[8,0,2,INF],[5,INF,0,1],[2,INF,INF,0]]
30(0,2)INFINFNo[[0,3,INF,7],[8,0,2,INF],[5,INF,0,1],[2,INF,INF,0]]
40(0,3)77No[[0,3,INF,7],[8,0,2,INF],[5,INF,0,1],[2,INF,INF,0]]
50(1,0)88No[[0,3,INF,7],[8,0,2,INF],[5,INF,0,1],[2,INF,INF,0]]
60(1,1)00No[[0,3,INF,7],[8,0,2,INF],[5,INF,0,1],[2,INF,INF,0]]
70(1,2)22No[[0,3,INF,7],[8,0,2,INF],[5,INF,0,1],[2,INF,INF,0]]
80(1,3)INF15Yes[[0,3,INF,7],[8,0,2,15],[5,INF,0,1],[2,INF,INF,0]]
90(2,0)55No[[0,3,INF,7],[8,0,2,15],[5,INF,0,1],[2,INF,INF,0]]
100(2,1)INF8Yes[[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,INF,INF,0]]
110(2,2)00No[[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,INF,INF,0]]
120(2,3)11No[[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,INF,INF,0]]
130(3,0)22No[[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,INF,INF,0]]
140(3,1)INF5Yes[[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,5,INF,0]]
150(3,2)INF7Yes[[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
160(3,3)00No[[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
171(0,0)00No[[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
181(0,1)33No[[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
191(0,2)INF5Yes[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
201(0,3)77No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
211(1,0)88No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
221(1,1)00No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
231(1,2)22No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
241(1,3)1517No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
251(2,0)513No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
261(2,1)88No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
271(2,2)00No[[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]]
281(2,3)16Yes[[0,3,5,7],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
291(3,0)27No[[0,3,5,7],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
301(3,1)55No[[0,3,5,7],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
311(3,2)77No[[0,3,5,7],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
321(3,3)00No[[0,3,5,7],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
332(0,0)00No[[0,3,5,7],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
342(0,1)33No[[0,3,5,7],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
352(0,2)55No[[0,3,5,7],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
362(0,3)76Yes[[0,3,5,6],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
372(1,0)88No[[0,3,5,6],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
382(1,1)00No[[0,3,5,6],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
392(1,2)22No[[0,3,5,6],[8,0,2,15],[5,8,0,6],[2,5,7,0]]
402(1,3)153Yes[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
412(2,0)55No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
422(2,1)88No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
432(2,2)00No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
442(2,3)66No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
452(3,0)27No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
462(3,1)55No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
472(3,2)77No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
482(3,3)00No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
493(0,0)00No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
503(0,1)33No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
513(0,2)55No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
523(0,3)66No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
533(1,0)88No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
543(1,1)00No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
553(1,2)22No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
563(1,3)33No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
573(2,0)55No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
583(2,1)88No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
593(2,2)00No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
603(2,3)66No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
613(3,0)22No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
623(3,1)55No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
633(3,2)77No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
643(3,3)00No[[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
💡 All intermediate nodes k processed; final distance matrix contains shortest paths
Variable Tracker
VariableStartAfter k=0After k=1After k=2After k=3Final
dist[[0,3,INF,7],[8,0,2,INF],[5,INF,0,1],[2,INF,INF,0]][[0,3,INF,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]][[0,3,5,7],[8,0,2,15],[5,8,0,6],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[5,8,0,6],[2,5,7,0]]
Key Moments - 3 Insights
Why do we check if dist[i][k] + dist[k][j] < dist[i][j] instead of just updating dist[i][j]?
Because we only update dist[i][j] if going through node k gives a shorter path. This is shown in execution_table rows like 8 and 10 where updates happen only when the new path is shorter.
Why do we consider each node k as an intermediate node in order?
Considering nodes in order ensures that all shorter paths using nodes with smaller indices are found before using larger indices. This stepwise improvement is visible in variable_tracker where dist updates after each k.
What happens if there is no direct edge between nodes i and j initially?
We start with INF (infinity) for no direct edge. Floyd Warshall updates these to shorter paths if found via intermediate nodes, as seen in execution_table rows where INF changes to finite values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 10, what is the new dist[2][1] value?
AINF
B8
C5
D2
💡 Hint
Check row with Step 10 in execution_table under 'New dist[i][j]' column
At which intermediate node k does dist[0][2] first get updated from INF to a finite value?
Ak=0
Bk=2
Ck=1
Dk=3
💡 Hint
Look at execution_table rows where pair (0,2) is updated; see when INF changes to 5
If we change the edge weight from node 1 to 3 from INF to 4 initially, how would the distance matrix change after k=1?
Adist[1][3] would be 4 instead of 15
Bdist[0][3] would be 7 unchanged
Cdist[2][3] would become 1 unchanged
DNo changes in dist matrix
💡 Hint
Check how initial edge weights affect updates at k=1 in variable_tracker
Concept Snapshot
Floyd Warshall Algorithm:
- Uses dynamic programming on distance matrix
- Iterates over all nodes as intermediate k
- Updates dist[i][j] if dist[i][k] + dist[k][j] < dist[i][j]
- Finds shortest paths between all pairs
- Handles negative edges but no negative cycles
- Time complexity O(V^3)
Full Transcript
Floyd Warshall algorithm finds shortest paths between all pairs of nodes in a graph. It starts with a distance matrix where direct edges have weights and no edges are infinity. The algorithm picks each node as an intermediate point k and checks for every pair (i,j) if going through k gives a shorter path. If yes, it updates dist[i][j]. This process repeats for all nodes k. The execution table shows step-by-step updates of the distance matrix. The variable tracker shows how the matrix changes after each k iteration. Key moments clarify why updates happen only when shorter paths are found, why nodes are considered in order, and how infinite distances get updated. The visual quiz tests understanding of matrix updates and intermediate steps. The concept snapshot summarizes the algorithm's key points and complexity.