Step 1: Initialize distance matrix with direct edges and 0 for self loops
[[0, 5, 3, 10],
[∞, 0, 1, ∞],
[∞, ∞, 0, 2],
[∞, ∞, ∞, 0]]
Why: Start with known direct distances and zero for same node
Step 2: Use node 0 as intermediate: update distances if going through 0 is shorter
[[0, 5, 3, 10],
[∞, 0, 1, ∞],
[∞, ∞, 0, 2],
[∞, ∞, ∞, 0]]
Why: No shorter paths found through node 0 at this step
Step 3: Use node 1 as intermediate: update distances if going through 1 is shorter
[[0, 5, 3, 10],
[∞, 0, 1, ∞],
[∞, ∞, 0, 2],
[∞, ∞, ∞, 0]]
Why: No shorter paths found through node 1. For example, 0->1->2 = 5 + 1 = 6 > current 3
Step 4: Use node 2 as intermediate: update distances if going through 2 is shorter
[[0, 5, 3, 5],
[∞, 0, 1, 3],
[∞, ∞, 0, 2],
[∞, ∞, ∞, 0]]
Why: 0->3 updated from 10 to 5 via 0->2->3 (3+2=5), 1->3 updated from ∞ to 3 via 1->2->3 (1+2=3)
Step 5: Use node 3 as intermediate: update distances if going through 3 is shorter
[[0, 5, 3, 5],
[∞, 0, 1, 3],
[∞, ∞, 0, 2],
[∞, ∞, ∞, 0]]
Why: No shorter paths found through node 3 at this step
Result: Final shortest path matrix:
[[0, 5, 3, 5],
[∞, 0, 1, 3],
[∞, ∞, 0, 2],
[∞, ∞, ∞, 0]]