What if you could instantly know the shortest path between any two places without checking every route?
Why Floyd Warshall All Pairs Shortest Path in DSA Typescript?
Imagine you have a map of cities connected by roads, and you want to find the shortest way to travel between every pair of cities. Doing this by checking each path manually would be like trying to remember every possible route and distance between cities on a huge map.
Manually calculating shortest paths between all pairs of cities is slow and confusing. You might miss shorter routes or get lost in too many possibilities. It's easy to make mistakes and waste time trying every possible path one by one.
The Floyd Warshall algorithm solves this by smartly checking all possible routes step-by-step. It updates the shortest distances between every pair of cities using a simple rule, so you get the shortest paths for all pairs quickly and without confusion.
for each city A: for each city B: find all paths from A to B choose the shortest path
for k in cities: for i in cities: for j in cities: dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j])
This algorithm lets you quickly find the shortest travel distances between every pair of points in a network, enabling smart route planning and analysis.
GPS apps use similar ideas to find the fastest routes between all locations on a map, helping drivers avoid traffic and reach destinations faster.
Manual path checking is slow and error-prone.
Floyd Warshall updates shortest paths using a simple triple loop.
It finds shortest distances between all pairs efficiently.
Practice
Solution
Step 1: Single-source vs all-pairs
Dijkstra/Bellman-Ford compute shortest paths from ONE source to all others. To get all-pairs with Dijkstra, run it V times = O(V(E + V log V)).Step 2: Floyd-Warshall
Floyd-Warshall computes shortest paths between every pair (i,j) in a single O(V³) pass. Simpler to implement for dense graphs.Final Answer:
All-pairs shortest paths in one pass. -> Option BQuick Check:
Floyd-Warshall = all-pairs. Dijkstra = single-source. [OK]
- Confusing with Dijkstra (single-source)
- Thinking it finds MST
- Confusing with topological sort
Solution
Step 1: Core idea
For each intermediate vertex k: is the path i→k→j shorter than the current best i→j?Step 2: Recurrence
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]). Keep the minimum of: current best OR going through k.Final Answer:
min(dist[i][j], dist[i][k] + dist[k][j]) -> Option BQuick Check:
Always take the minimum - relaxation. [OK]
- Using + without min (overwrites current best)
- Using max instead of min
- Multiplying instead of adding edge weights
Solution
Step 1: Self-distance
dist[i][i] = 0 - zero cost to stay at the same vertex.Step 2: No edge
dist[i][j] = Infinity when no direct edge exists - represents unreachable initially. In TypeScript: Infinity works correctly with Math.min().Final Answer:
dist[i][i]=0, no edge=Infinity -> Option BQuick Check:
Self=0, no edge=Infinity. Infinity + x = Infinity in JS - works with min(). [OK]
- Using -1 for no edge (breaks min comparison)
- Setting self-distance to Infinity
- Using 0 for no edge (treats as zero-weight edge)
Solution
Step 1: Why k is outermost
k is the intermediate vertex being 'unlocked' at each outer iteration. When we process intermediate k, all pairs i→j must be relaxed using k.Step 2: Correctness
If k is innermost, we might use a later-unlocked vertex when relaxing - violating the DP ordering. k must be outermost so all (i,j) pairs are fully relaxed before moving to k+1.Final Answer:
k outermost, then i, then j. -> Option BQuick Check:
Floyd-Warshall loop order: k→i→j. k must be outermost. [OK]
- Putting i outermost (most common mistake)
- Putting j outermost
- Any order other than k→i→j gives wrong results
Solution
Step 1: Negative cycle effect
If a negative cycle exists reachable from i back to i, the shortest path dist[i][i] will become negative (going around the cycle reduces cost below 0).Step 2: Detection
After the algorithm completes, check: for any i, if dist[i][i] < 0, a negative cycle exists involving i.Final Answer:
Check dist[i][i] < 0 for any i. -> Option BQuick Check:
Negative cycle: dist[i][i] < 0 after Floyd-Warshall. [OK]
- Checking dist[i][j] for non-self pairs
- Summing all distances
- Running algorithm twice
Ready to prove it?
Pass the test and this pattern moves unknown
