Step 1: Initialize distance matrix with direct edges and 0 for same nodes
[[0, 5, ∞, 10],
[∞, 0, 3, ∞],
[∞, ∞, 0, 1],
[∞, ∞, ∞, 0]]
Why: We start with known direct distances or infinity if no direct edge
Step 2: Use node 0 as intermediate: update distances if going through 0 is shorter
[[0, 5, ∞, 10],
[∞, 0, 3, ∞],
[∞, ∞, 0, 1],
[∞, ∞, ∞, 0]]
Why: No shorter paths found through node 0 at this step
Step 3: Use node 1 as intermediate: update distance from 0 to 2 via 1
[[0, 5, 8, 10],
[∞, 0, 3, ∞],
[∞, ∞, 0, 1],
[∞, ∞, ∞, 0]]
Why: 0->1 + 1->2 = 5 + 3 = 8 is shorter than ∞, so update
Step 4: Use node 2 as intermediate: update distance from 0 to 3 via 2
[[0, 5, 8, 9],
[∞, 0, 3, 4],
[∞, ∞, 0, 1],
[∞, ∞, ∞, 0]]
Why: 0->2 + 2->3 = 8 + 1 = 9 is shorter than 10, update 0->3; also 1->2 + 2->3 = 3 + 1 = 4 update 1->3
Step 5: Use node 3 as intermediate: no shorter paths found
[[0, 5, 8, 9],
[∞, 0, 3, 4],
[∞, ∞, 0, 1],
[∞, ∞, ∞, 0]]
Why: No paths improve by going through node 3
Result: Final shortest path matrix:
[[0, 5, 8, 9],
[∞, 0, 3, 4],
[∞, ∞, 0, 1],
[∞, ∞, ∞, 0]]