0
0
Data Structures Theoryknowledge~10 mins

Floyd-Warshall algorithm in Data Structures Theory - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Floyd-Warshall algorithm
Start with distance matrix
For each intermediate vertex k
For each source vertex i
For each destination vertex j
Check if path i->k + k->j < i->j
Yes No
Update distance i->j
Repeat for all i,j,k
Final shortest paths matrix
The algorithm updates shortest paths by considering each vertex as an intermediate stop, improving distances step-by-step.
Execution Sample
Data Structures Theory
INF = float('inf')
dist = [[0, 3, INF, 7],
        [8, 0, 2, INF],
        [5, INF, 0, 1],
        [2, INF, INF, 0]]
for k in range(4):
  for i in range(4):
    for j in range(4):
      dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
This code updates the distance matrix to find shortest paths between all pairs of vertices.
Analysis Table
Stepk (intermediate)i (source)j (destination)Condition: dist[i][k] + dist[k][j] < dist[i][j]ActionUpdated dist[i][j]
1012dist[1][0] + dist[0][2] = 8 + INF = INF < 2? NoNo update2
2013dist[1][0] + dist[0][3] = 8 + 7 = 15 < INF? YesUpdate dist[1][3]15
3021dist[2][0] + dist[0][1] = 5 + 3 = 8 < INF? YesUpdate dist[2][1]8
4031dist[3][0] + dist[0][1] = 2 + 3 = 5 < INF? YesUpdate dist[3][1]5
5102dist[0][1] + dist[1][2] = 3 + 2 = 5 < INF? YesUpdate dist[0][2]5
6103dist[0][1] + dist[1][3] = 3 + 15 = 18 < 7? NoNo update7
7132dist[3][1] + dist[1][2] = 5 + 2 = 7 < INF? YesUpdate dist[3][2]7
8203dist[0][2] + dist[2][3] = 5 + 1 = 6 < 7? YesUpdate dist[0][3]6
9213dist[1][2] + dist[2][3] = 2 + 1 = 3 < 15? YesUpdate dist[1][3]3
10310dist[1][3] + dist[3][0] = 3 + 2 = 5 < 8? YesUpdate dist[1][0]5
11320dist[2][3] + dist[3][0] = 1 + 2 = 3 < 5? YesUpdate dist[2][0]3
12321dist[2][3] + dist[3][1] = 1 + 5 = 6 < 8? YesUpdate dist[2][1]6
13332dist[3][3] + dist[3][2] = 0 + 7 = 7 < 7? NoNo update7
14333dist[3][3] + dist[3][3] = 0 + 0 = 0 < 0? NoNo update0
💡 All pairs checked for all intermediate vertices k=0 to 3; no further updates possible.
State Tracker
VariableStartAfter k=0After k=1After k=2After k=3 (Final)
dist[0][3]77766
dist[1][3]INF151533
dist[2][1]INF8886
dist[3][1]INF5555
dist[3][2]INFINF777
dist[1][0]88885
dist[2][0]55553
Key Insights - 3 Insights
Why do we check if dist[i][k] + dist[k][j] < dist[i][j]?
Because we want to see if going through vertex k gives a shorter path from i to j than the current known path. This is shown in execution_table rows like 2 and 5 where updates happen only if the new path is shorter.
Why do we need three nested loops over k, i, and j?
The outer loop k tries each vertex as an intermediate point. The inner loops i and j check all pairs of start and end vertices to update distances. This ensures all possible paths through any vertex are considered, as seen in the stepwise updates in execution_table.
What does INF represent and why is it important?
INF means no direct path between two vertices initially. It helps identify unreachable paths. The algorithm updates INF to a real distance if a shorter path is found, as shown in rows where INF changes to a number.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the updated distance from vertex 0 to 2?
A2
B5
CINF
D3
💡 Hint
Check the 'Updated dist[i][j]' column at step 5 where i=0, j=2.
At which iteration does the distance from vertex 1 to 3 first get updated to a value less than INF?
AStep 14
BStep 9
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Updated dist[i][j]' columns for i=1, j=3 in execution_table.
If the initial distance from vertex 3 to 2 was not INF but 10, how would the update at step 7 change?
AUpdate would still occur, changing distance to 7
BNo update would occur because 5 + 2 = 7 is less than 10
CUpdate would not occur because 7 is not less than 10
DDistance would become INF
💡 Hint
Check step 7 where dist[3][1] + dist[1][2] = 5 + 2 = 7 and compare with initial distance.
Concept Snapshot
Floyd-Warshall algorithm finds shortest paths between all pairs of vertices.
Uses a distance matrix updated by considering each vertex as an intermediate point.
Three nested loops: k (intermediate), i (source), j (destination).
Update rule: dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]).
Handles negative edges but no negative cycles.
Final matrix shows shortest distances between all pairs.
Full Transcript
The Floyd-Warshall algorithm starts with a matrix of distances between vertices, where direct edges have their weights and no edges are marked as infinity. It uses three nested loops: the outer loop picks each vertex as an intermediate point, and the inner loops check all pairs of source and destination vertices. For each pair, it checks if going through the intermediate vertex offers a shorter path. If yes, it updates the distance. This process repeats for all vertices, gradually improving the shortest path estimates. The final matrix after all iterations contains the shortest distances between every pair of vertices. This method works even with negative edge weights but assumes no negative cycles exist. The execution table shows step-by-step updates, and the variable tracker follows key distance values as they change. Key moments clarify why the update condition is checked, why three loops are needed, and the role of infinity in representing unreachable paths.