0
0
DSA Typescriptprogramming~10 mins

Floyd Warshall All Pairs Shortest Path in DSA Typescript - 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
Yes/No
Update distance[i
Repeat for all i, j
Repeat for all k
Final shortest paths matrix
The algorithm updates shortest paths by checking if going through an intermediate node k improves the path between every pair (i, j).
Execution Sample
DSA Typescript
const dist = [
  [0, 3, Infinity, 7],
  [8, 0, 2, Infinity],
  [5, Infinity, 0, 1],
  [2, Infinity, Infinity, 0]
];
// Floyd Warshall updates dist
This code runs Floyd Warshall on a 4-node graph to find shortest paths between all pairs.
Execution Table
StepOperationIndices (k,i,j)Distance BeforeDistance AfterVisual State
1Initialize distance matrix--[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]]┌─────┬─────┬───────┬───────┐ │ 0 │ 3 │ ∞ │ 7 │ ├─────┼─────┼───────┼───────┤ │ 8 │ 0 │ 2 │ ∞ │ ├─────┼─────┼───────┼───────┤ │ 5 │ ∞ │ 0 │ 1 │ ├─────┼─────┼───────┼───────┤ │ 2 │ ∞ │ ∞ │ 0 │ └─────┴─────┴───────┴───────┘
2k=0, i=1, j=3: Check if dist[1][0] + dist[0][3] < dist[1][3](0,1,3)dist[1][3] = ∞dist[1][3] = dist[1][0] + dist[0][3] = 8 + 7 = 15Updated dist[1][3] from ∞ to 15
3k=0, i=2, j=1: Check if dist[2][0] + dist[0][1] < dist[2][1](0,2,1)dist[2][1] = ∞dist[2][1] = dist[2][0] + dist[0][1] = 5 + 3 = 8Updated dist[2][1] from ∞ to 8
4k=0, i=3, j=1: Check if dist[3][0] + dist[0][1] < dist[3][1](0,3,1)dist[3][1] = ∞dist[3][1] = dist[3][0] + dist[0][1] = 2 + 3 = 5Updated dist[3][1] from ∞ to 5
5k=1, i=0, j=2: Check if dist[0][1] + dist[1][2] < dist[0][2](1,0,2)dist[0][2] = ∞dist[0][2] = dist[0][1] + dist[1][2] = 3 + 2 = 5Updated dist[0][2] from ∞ to 5
6k=1, i=3, j=2: Check if dist[3][1] + dist[1][2] < dist[3][2](1,3,2)dist[3][2] = ∞dist[3][2] = dist[3][1] + dist[1][2] = 5 + 2 = 7Updated dist[3][2] from ∞ to 7
7k=2, i=0, j=3: Check if dist[0][2] + dist[2][3] < dist[0][3](2,0,3)dist[0][3] = 7dist[0][3] = dist[0][2] + dist[2][3] = 5 + 1 = 6Updated dist[0][3] from 7 to 6
8k=2, i=1, j=3: Check if dist[1][2] + dist[2][3] < dist[1][3](2,1,3)dist[1][3] = 15dist[1][3] = dist[1][2] + dist[2][3] = 2 + 1 = 3Updated dist[1][3] from 15 to 3
9k=3, i=2, j=0: Check if dist[2][3] + dist[3][0] < dist[2][0](3,2,0)dist[2][0] = 5dist[2][0] = dist[2][3] + dist[3][0] = 1 + 2 = 3Updated dist[2][0] from 5 to 3
10k=3, i=2, j=1: Check if dist[2][3] + dist[3][1] < dist[2][1](3,2,1)dist[2][1] = 8dist[2][1] = dist[2][3] + dist[3][1] = 1 + 5 = 6Updated dist[2][1] from 8 to 6
11Final distance matrix--[[0,3,5,6],[8,0,2,3],[3,6,0,1],[2,5,7,0]]┌───┬───┬───┬───┐ │ 0 │ 3 │ 5 │ 6 │ ├───┼───┼───┼───┤ │ 8 │ 0 │ 2 │ 3 │ ├───┼───┼───┼───┤ │ 3 │ 6 │ 0 │ 1 │ ├───┼───┼───┼───┤ │ 2 │ 5 │ 7 │ 0 │ └───┴───┴───┴───┘
💡 All intermediate nodes k processed; shortest paths finalized.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10Final
dist[[0,3,∞,7],[8,0,2,∞],[5,∞,0,1],[2,∞,∞,0]][[0,3,∞,7],[8,0,2,15],[5,∞,0,1],[2,∞,∞,0]][[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,∞,∞,0]][[0,3,∞,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]][[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,∞,0]][[0,3,5,7],[8,0,2,15],[5,8,0,1],[2,5,7,0]][[0,3,5,6],[8,0,2,15],[5,8,0,1],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[5,8,0,1],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[3,8,0,1],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[3,6,0,1],[2,5,7,0]][[0,3,5,6],[8,0,2,3],[3,6,0,1],[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 2-10 where updates happen only when the new path is shorter.
What does 'Infinity' mean in the distance matrix at the start?
'Infinity' means no direct path exists between those nodes initially. The algorithm tries to find shorter paths through intermediate nodes, replacing Infinity with actual distances if found, as seen in steps 2, 3, 4, 5, and 6.
Why do we repeat the process for all k from 0 to n-1?
Because each k represents an intermediate node that might improve paths between all pairs (i, j). The algorithm ensures all possible intermediate nodes are considered, as shown by the repeated steps for k=0,1,2,3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the updated distance from node 0 to node 2?
A5
B
C3
D2
💡 Hint
Check the 'Distance After' column at step 5 in execution_table.
At which step does the distance from node 1 to node 3 first get updated to a finite value?
AStep 8
BStep 5
CStep 2
DStep 10
💡 Hint
Look for the first update of dist[1][3] in execution_table rows.
If the initial distance from node 3 to node 1 was 4 instead of Infinity, how would the distance from node 3 to node 2 change after step 6?
AIt would become 6
BIt would remain 7
CIt would become 4
DIt would become 9
💡 Hint
Check how dist[3][2] is updated at step 6 using dist[3][1] + dist[1][2].
Concept Snapshot
Floyd Warshall Algorithm:
- Uses a distance matrix dist[n][n]
- For each intermediate node k:
  For each pair (i,j):
    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
- Finds shortest paths between all pairs
- Handles negative edges but no negative cycles
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 their weights and no edges are marked as Infinity. It then iteratively considers each node as an intermediate point k. For every pair of nodes (i, j), it checks if going through k gives a shorter path than the current known path. If yes, it updates the distance. This process repeats for all k, ensuring all possible intermediate nodes are considered. The final matrix shows the shortest distances between every pair of nodes. The algorithm handles graphs with negative edges but assumes no negative cycles. The execution table shows step-by-step updates of the distance matrix, and the variable tracker records the matrix state after each update.